#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) {//a square is made up of three vertical bars for (j = 0; J < 4; ++j)//First row vertical bars account for 5 squares between each vertical bar printf ("|"); printf ("|\n"); for (j = 0; J < 4; ++j) {//second row vertical bar with number if (a[i][j] = = 0) printf ("|"), Else printf ("|%4d", A[i][j]);} printf ("|\n") ; for (j = 0; J < 4; ++j)//third-line vertical line plus bottom-row 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 & ; lt;= 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 + +) {///= As long as the current is 0 put the following one to it if (a[i][j] = = 0) {A I [j] = A[i + 1][j]; The following one is 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 + +) {//* * * If current is 0 give the previous to it if (a[i][j] = = 0) {a[i][j ] = A[i-1][j]; Previous for 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 + +) {//= as long as the current is 0 put the previous one to it if (a[i][j] = = 0) {a[ I][J] = a[i][j-1]; The previous one is 0 a[i][j-1] = 0; }}} Myrand ();} void Left () {for (int i = 0; I <= 3, i++) {for (int j = 1; J <=3; J + +) {//As long as one is not 0 in. if (A[i][j]!=0) { Merge the same 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 + +) {//* * * * * * * if (a[i][j] = 0) {a[i][j If current is 0) ] = a[i][j + 1]; The latter one is 0 a[i][j + 1] = 0; }}} Myrand ();} void To_up () {int x, y, I; for (y = 0; y < 4; ++y) {//merge the same squares from top to bottom (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)//Move the box up 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)//down merge the same squares 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)//Move the grid 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)//left merge identical squares 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)//move square 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)//right merges the same square 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)//Right-move the square 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, for the next key around the direction!!! "<< 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 <& Amp;lt; "You're awesome!!! "<< Endl Bo = false; } } } }}
Bug version
ConsoleApplication6.cpp: Defines the entry point of the console application. There are Bug#include "stdafx.h" #include <windows.h> #include <time.h> #include <vector>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 (); cloc k_t time1_end;clock_t Time2_start = Clock () clock_t time2_end;int map[4][4];bool wirtechar (int high, int Wide, char * PSZC har, int warr) {Console_cursor_info CCI; Cci.dwsize = 1; cci.bvisible = FALSE; Do not display cursor 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); It is important to note that some special characters take up two bytes to return true when printing.} BOOL Setwindowsize (wchar_t * ptitle, int nX, int nY) {//SetConsole title Setconsoletitle (ptitle); HANDLE hStdIn, hStdOut; hStdIn = GetStdHandle (Std_input_handle); hStdOut = GetStdHandle (Std_output_handle); Get maximum console window size COORD pos = getlargestconsolewindowsize (hstdout); COORD buffersize = {pos. X + 1, pos. Y + 1}; Set the console buffer size if (! Setconsolescreenbuffersize (hStdOut, buffersize)) {//Setting console window buffer failed printf ("Buffer err (%d,%d)%d\n", buffersize.x, Buffersize.x, GetLastError ()); return false; } small_rect Srctwindow = {0,0,nx,ny}; Console coordinate position if (! Setconsolewindowinfo (hStdOut, True, &srctwindow)) {//Setting console window size printf ("Size err%d\n", GetLastError ()); return false; } COORD Buffer = {NX + 1, NY + 1}; Buffer coordinate location//setting console buffer size if (! Setconsolescreenbuffersize (hstdout, buffer)) {//Setting console window buffer failed printf ("Buffer err (%d,%d)%d\n", Buffersize.x, Buff Ersize.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<point>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, "up", 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 >) {time1_start = Time1_end; BOOL ismove= Kib (); if (ismove) {printmap (); Addnum (); Printmap (); }}} return 0;}
C + + write 2048