本文共 14903 字,大约阅读时间需要 49 分钟。
#include <stdlib.h> #include <conio.h> #include <windows.h> #include<iostream>#include<ctime>using namespace std;int a[4][4];void myrand();void init() { for (int i = 0; i <= 3; i++) { for (int j = 0; j <= 3; j++) { a[i][j] = 0; } } myrand();}void draw() { int i, j; for (i = 0; i < 4; ++i) { // 一个方格由三根竖线组成 for (j = 0; j < 4; ++j) // 第一排竖线 每个竖线之间占5个格 printf("| "); printf("|\n"); for (j = 0; j < 4; ++j) { // 第二排竖线与数字 if (a[i][j] == 0) printf("| "); else printf("|%4d", a[i][j]); } printf("|\n"); for (j = 0; j < 4; ++j) // 第三排竖线加底线 printf("|____"); printf("|\n"); }}void myrand() { srand(time(0)); bool bo = true; while (bo) { int i = rand() % 4; int j = rand() % 4; int n = rand() % 2; if (a[i][j] == 0) { if (n == 1) { a[i][j] = 2; } else if (n == 0) { a[i][j] = 4; } break; } else { for (int i = 1; i <= 3; i++) { for (int j = 0; j <= 3; j++) { if (a[i][j] != 0) { bo = false; } } } continue; } }}///void up(){ for (int i = 1; i <= 3; i++) { for (int j = 0; j <= 3; j++) { if (a[i][j] != 0) { if (a[i -1][j] == a[i][j]) { a[i - 1][j] += a[i][j]; a[i][j] = 0; } } } } for (int i = 0; i <= 2; i++) { for (int j = 0; j <= 3; j++) { //只要当前为0就把下面一个给它 if (a[i][j] == 0) { a[i][j] = a[i + 1][j]; //下面一个为0 a[i + 1][j] = 0; } } } myrand();}void down(){ for (int i = 0; i <= 2; i++) { for (int j = 0; j <= 3; j++) { if (a[i][j] != 0) { if (a[i+1][j] == a[i][j]) { a[i + 1][j] += a[i][j]; a[i][j] = 0; } } } } for (int i = 1; i <= 3; i++) { for (int j = 0; j <= 3; j++) { //只要当前为0就把上一个给它 if (a[i][j] == 0) { a[i][j] = a[i-1][j]; //上一个为0 a[i-1][j] = 0; } } } myrand();}void righ(){ for (int i = 0; i <= 3; i++) { for (int j = 0; j <= 2; j++) { if (a[i][j] != 0) { if (a[i][j+1] == a[i][j]) { a[i][j + 1] += a[i][j]; a[i][j] = 0; } } } } for (int i = 0; i <= 3; i++) { for (int j = 1; j <= 3; j++) { //只要当前为0就把前一个给它 if (a[i][j] == 0) { a[i][j] = a[i][j -1]; //前一个为0 a[i][j - 1] = 0; } } } myrand();}void left(){ for (int i = 0; i <= 3; i++) { for (int j = 1; j <=3; j++) { //只要有一个不为0就进来 if (a[i][j] !=0 ) { //合并相同的 if (a[i][j-1] == a[i][j]) { a[i][j-1] += a[i][j]; a[i][j] = 0; } } } } for (int i = 0; i <= 3; i++) { for (int j = 0; j <= 2; j++) { //只要当前为0就把后一个给它 if (a[i][j] == 0) { a[i][j] = a[i][j + 1]; //后一个为0 a[i][j + 1] = 0; } } } myrand();}///void to_up() { int x, y, i; for (y = 0; y < 4; ++y) { // 从上向下合并相同的方块 for (x = 0; x < 4; ++x) { if (a[x][y] == 0) ; else { for (i = x + 1; i < 4; ++i) { if (a[i][y] == 0) ; else if (a[x][y] == a[i][y]) { a[x][y] += a[i][y]; a[i][y] = 0; x = i; break; } else { //x = i - 1; break; } } } } } for (y = 0; y < 4; ++y) // 向上移动箱子 for (x = 0; x < 4; ++x) { if (a[x][y] == 0) ; else { for (i = x; (i > 0) && (a[i - 1][y] == 0); --i) { a[i - 1][y] = a[i][y]; a[i][y] = 0; } } } myrand();}void to_down() { int x, y, i; for (y = 0; y < 4; ++y) // 向下合并相同的方格 for (x = 3; x >= 0; --x) { if (a[x][y] == 0) ; else { for (i = x - 1; i >= 0; --i) { if (a[i][y] == 0) ; else if (a[x][y] == a[i][y]) { a[x][y] += a[i][y]; a[i][y] = 0; x = i; break; } else break; } } } for (y = 0; y < 4; ++y) // 向下移动方格 for (x = 3; x >= 0; --x) { if (a[x][y] == 0) ; else { for (i = x; (i < 3) && (a[i + 1][y] == 0); ++i) { a[i + 1][y] = a[i][y]; a[i][y] = 0; } } } myrand();}void to_left() { int x, y, i; for (x = 0; x < 4; ++x) // 向左合并相同的方格 for (y = 0; y < 4; ++y) { if (a[x][y] == 0) ; else { for (i = y + 1; i < 4; ++i) { if (a[x][i] == 0) ; else if (a[x][y] == a[x][i]) { a[x][y] += a[x][i]; a[x][i] = 0; y = i; break; } else break; } } } for (x = 0; x < 4; ++x) // 向左移动方格 for (y = 0; y < 4; ++y) { if (a[x][y] == 0) ; else { for (i = y; (i > 0) && (a[x][i - 1] == 0); --i) { a[x][i - 1] = a[x][i]; a[x][i] = 0; } } } myrand();}void to_right() { int x, y, i; for (x = 0; x < 4; ++x) // 向右合并相同的方格 for (y = 3; y >= 0; --y) { if (a[x][y] == 0) ; else { for (i = y - 1; i >= 0; --i) { if (a[x][i] == 0) ; else if (a[x][y] == a[x][i]) { a[x][y] += a[x][i]; a[x][i] = 0; y = i; break; } else break; } } } for (x = 0; x < 4; ++x) // 向右移动方格 for (y = 3; y >= 0; --y) { if (a[x][y] == 0) ; else { for (i = y; (i < 3) && (a[x][i + 1] == 0); ++i) { a[x][i + 1] = a[x][i]; a[x][i] = 0; } } } myrand();}int main(){ cout << "wasd,为上下左右方向键!!!" << endl; init(); draw(); //char ch; bool bo = true; while (bo) { switch (getch()) { case 'w': case 'W': up(); //to_up(); system("cls"); draw(); break; case 's': case 'S': down(); //to_down(); system("cls"); draw(); break; case 'a': case 'A': left(); //to_left(); system("cls"); draw(); break; case 'd': case 'D': righ(); //to_right(); system("cls"); draw(); break; } for (int i = 1; i <= 3; i++) { for (int j = 0; j <= 3; j++) { if (a[i][j] >= 2048) { cout << "你牛逼!!!" << endl; bo = false; } } } }}
bug 版
// ConsoleApplication6.cpp : 定义控制台应用程序的入口点。///有bug#include "stdafx.h"#include#include #include using namespace std;#define KEYDOWN(vk_code)((GetAsyncKeyState(vk_code)&0x8000)?1:0)#define KEYUP(vk_code)((GetAsyncKeyState(vk_code)&0x8000)?0:1)#define UP 0#define DOWN 1#define LEFT 2#define RIGHT 3//clock_t time1_start = clock();clock_t time1_end;clock_t time2_start = clock();clock_t time2_end;int map[4][4];bool WirteChar(int High, int Wide, char * pszChar, int wArr){ CONSOLE_CURSOR_INFO cci; cci.dwSize = 1; cci.bVisible = FALSE; //不显示光标 SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cci); COORD loc; loc.X = Wide * 2*3; loc.Y = High*3; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wArr); SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), loc); printf(pszChar); //打印时需要注意有些特殊字符是占两个字节 return true;}bool SetWindowSize(wchar_t * pTitle, int nX, int nY){ // 设置控制台标题 SetConsoleTitle(pTitle); HANDLE hStdIn, hStdOut; hStdIn = GetStdHandle(STD_INPUT_HANDLE); hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); //获取最大控制台窗口大小 COORD pos = GetLargestConsoleWindowSize(hStdOut); COORD BufferSize = { pos.X + 1, pos.Y + 1 }; //设置控制台缓冲区大小 if (!SetConsoleScreenBufferSize(hStdOut, BufferSize)) { //设置控制台窗口缓冲区失败 printf("buffer err(%d,%d)%d\n", BufferSize.X, BufferSize.X, GetLastError()); return false; } SMALL_RECT srctWindow = { 0,0,nX,nY }; //控制台坐标位置 if (!SetConsoleWindowInfo(hStdOut, true, &srctWindow)) { //设置控制台窗口大小 printf("size err %d\n", GetLastError()); return false; } COORD Buffer = { nX + 1, nY + 1 }; //缓冲区坐标位置 //设置控制台缓冲区大小 if (!SetConsoleScreenBufferSize(hStdOut, Buffer)) { //设置控制台窗口缓冲区失败 printf("buffer err(%d,%d)%d\n", BufferSize.X, BufferSize.Y, GetLastError()); return false; } return true;}void initMap(){ srand((unsigned int)time(NULL)); int arr[4] = { 0,2,4,8 }; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { int num = rand() % 4; map[i][j] = arr[num]; } }}int AddNum(){ srand((unsigned int)time(NULL)); int arr[4] = { 0,2,4,8 }; int num = rand() % 4; vector Vec; POINT point = {}; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (map[i][j] == 0) { point.x = i; point.y = j; Vec.push_back(point); } } } if (Vec.size() >= 3) { map[Vec[0].x][Vec[0].y] = arr[num]; } return 0;}void PrintMap(){ for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { switch (map[i][j]) { case 0: WirteChar(i+1 , j+1 , " 0 ", 48|14); break; case 2: WirteChar(i+1 , j+1 , " 2 ", 80|11); break; case 4: WirteChar(i+1 , j+1 , " 4 ", 192|1); break; case 8: WirteChar(i+1 , j+1 , " 8 ", 96|0); break; case 16: WirteChar(i+1 , j+1 , " 16 ", 176|0); break; case 32: WirteChar(i+1 , j+1 , " 32 ", 192|5); break; case 64: WirteChar(i+1 , j+1 , " 64 ", FOREGROUND_INTENSITY); break; case 128:WirteChar(i+1 , j+1 , " 128 ", 112|9); break; } } }}bool MoveUp(){ bool IsA = false; for (int i = 0; i < 4; i++)//0 1 2 3 { for (int j = 0; j < 4; j++)//0 1 2 3 { if (i == 0) break; if (map[i][j] == map[i - 1][j]) { map[i - 1][j] = map[i][j] + map[i][j]; map[i][j] = 0; IsA = true; continue; } if (map[i - 1][j] == 0) { map[i - 1][j] = map[i][j]; map[i][j] = 0; IsA = true; continue; } } } if (IsA) return true; return false;}bool MoveDown(){ bool IsA = false; for (int i = 3; i >= 0; i--)//3 2 1 0 { for (int j = 0; j < 4; j++) //1 2 3 4 { if (i == 3) break; if (map[i][j] == map[i + 1][j]) { map[i + 1][j] = map[i][j] + map[i][j]; map[i][j] = 0; IsA = true; continue; } if (map[i + 1][j] == 0) { map[i + 1][j] = map[i][j]; map[i][j] = 0; IsA = true; continue; } } } if (IsA) return true; return false;}bool MoveLeft(){ bool IsA = false; for (int i = 0; i < 4; i++)//0 1 2 3 { for (int j = 0; j < 4; j++)//0 1 2 3 { if (j == 0)continue; if (map[i][j] == map[i][j - 1]) { map[i][j - 1] = map[i][j] + map[i][j]; map[i][j] = 0; IsA = true; continue; } if (map[i][j - 1] == 0) { map[i][j - 1] = map[i][j]; map[i][j] = 0; IsA = true; continue; } } } if (IsA) return true; return false;}bool MoveRight(){ bool IsA = false; for (int i = 0; i < 4; i++)//0 1 2 3 { for (int j = 3; j >= 0; j--)//3 2 1 0 { if (j == 3)continue; if (map[i][j] == map[i][j + 1]) { map[i][j + 1] = map[i][j] + map[i][j]; map[i][j] = 0; IsA = true; continue; } if (map[i][j + 1] == 0) { map[i][j + 1] = map[i][j]; map[i][j] = 0; IsA = true; continue; } } } if (IsA) return true; return false;}bool Kib(){ if (KEYDOWN('W') || KEYDOWN('w')) { return MoveUp(); } if (KEYDOWN('S') || KEYDOWN('s')) { return MoveDown(); } if (KEYDOWN('A') || KEYDOWN('a')) { return MoveLeft(); } if (KEYDOWN('D') || KEYDOWN('d')) { return MoveRight(); } return 0;}int main(){ SetWindowSize(L"hi", 40, 20); system("color 7f"); initMap(); PrintMap(); while (1) { time1_end = clock(); if (time1_end - time1_start > 30) { time1_start = time1_end; bool IsMove= Kib(); if (IsMove) { PrintMap(); AddNum(); PrintMap(); } } } return 0;}
转载于:https://blog.51cto.com/haidragon/2067888