2048 of the command line2048 quite a fire game, what is the implementation in the command line? Small try, here thanks to the CSS great God to debug, there is the game space with special characters, so the program in Linux run may appear garbled, can manually adjust
Game:
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvvgnfvg9fvg9w/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">
Code:
#include <iostream> #include <ctime> #include <cstdlib> #include <cstring> #include <conio.h > #include <cstdlib> #include <windows.h> #define coord_x 15#define coord_y 15#define Height 20#define Width 16using namespace Std;class game{public:int score; int num[5][5]; Public:game (int s = 0); BOOL Control (char ch); void Init (); void make ();} G;class console{public:void gotoxy (HANDLE hOut, int x, int y); void Enter_game (); void window (); void Show (); void Start_game (); void End_game (); Friend class Game;} C Game::game (int s) {score = s; memset (num,0,sizeof (num));} void Console::gotoxy (HANDLE hOut, int x, int y) {COORD pos; Pos. x = x; Pos. y = y; SetConsoleCursorPosition (HOut, POS);} void Console::enter_game () {HANDLE hOut = GetStdHandle (Std_output_handle); HANDLE HANDLE = GetStdHandle (Std_output_handle); System ("title 2048 by Tc"); Gotoxy (HOUT,COORD_X+WIDTH-1,COORD_Y-12); Setconsoletextattribute (handle,foreground_intensity | foreground_red | Foreground_green); cout<< "Welcome to 2 0 4 8"; Setconsoletextattribute (handle,foreground_intensity | Foreground_green); Gotoxy (hout,coord_x+width-25,coord_y-8); cout << "* * * * * * * * * *" << Endl; Gotoxy (hout,coord_x+width-25,coord_y-7); cout << "* * * * * * * * * *" << Endl; Gotoxy (hout,coord_x+width-25,coord_y-6); cout << "* * * * * * * * * *" << Endl; Gotoxy (hout,coord_x+width-25,coord_y-5); cout << "* * * * * * *" << Endl; Gotoxy (hout,coord_x+width-25,coord_y-4); cout << "* * * * * * * * * * * * * *" <&Lt Endl Gotoxy (hout,coord_x+width-25,coord_y-3); cout << "* * * * * * *" << Endl; Gotoxy (hout,coord_x+width-25,coord_y-2); cout << "* * * * * * *" << Endl; Gotoxy (hout,coord_x+width-25,coord_y-1); cout << "* * * * * * * * * * * *" << Endl; Gotoxy (hout,coord_x+width+1,coord_y+3); Setconsoletextattribute (handle,foreground_intensity | foreground_red); cout << "Press ENTER to enter the game \ n" << Endl; while (1) {char C; if (Kbhit ()) {c = Getch (); if (c = =) {System ("CLS"); C.start_game (); }}} if (Getch () = =) {C.end_game (); }}void Console::end_game () {HANDLE HANDLE = GetStdHandle (Std_output_handle); HANDLE HOut = getstDhandle (Std_output_handle); System ("CLS"); Gotoxy (HOUT,COORD_X+WIDTH+5,COORD_Y-10); Setconsoletextattribute (handle,foreground_intensity | foreground_red); cout << "Game Over"; Gotoxy (hout,coord_x+width+5,coord_y-7); cout << "finally scored:" << G.score << Endl; Setconsoletextattribute (handle,foreground_intensity | foreground_red | Foreground_green); Exit (0);} void Console::window () {HANDLE hOut = GetStdHandle (Std_output_handle); HANDLE HANDLE = GetStdHandle (Std_output_handle); System ("Title 2 0 4 8 by Tc"); System ("Color 0C"); Gotoxy (HOUT,COORD_X+WIDTH-2,COORD_Y-13); cout << "*******"; Gotoxy (HOUT,COORD_X+WIDTH-2,COORD_Y-12); cout << "2 0 4 8"; Gotoxy (HOUT,COORD_X+WIDTH-2,COORD_Y-11); cout << "*******"; Gotoxy (HOUT,COORD_X+2*WIDTH+3,COORD_Y+6); Setconsoletextattribute (handle,foreground_intensity | foreground_red | Foreground_green); Gotoxy (hout,coord_x+2*width+3 + 2,coord_y-8); cout <&Lt "W Key: Up" << "s key: Down"; Gotoxy (hout,coord_x+2*width+3 + 2,coord_y-6); cout << "A key: Left" << "D Key: Right"; Gotoxy (hout,coord_x+2*width+3 + 2,coord_y-4); cout << "ESC: Exit";} void Console:: Show () {HANDLE hOut = GetStdHandle (Std_output_handle); HANDLE HANDLE = GetStdHandle (Std_output_handle); Setconsoletextattribute (handle,foreground_intensity | Foreground_green | foreground_red); Gotoxy (hout,coord_x+2*width+3 + 2,coord_y-10); cout << ""; Gotoxy (hout,coord_x+2*width+3 + 2,coord_y-10); cout << "score:" << G.score; for (int i = 0, i < 4; i++) {for (int j = 0; J < 4; J + +) {if (g.num[i][j]! = 0) {cout << ""; Setconsoletextattribute (handle,foreground_intensity | foreground_red); Gotoxy (HOut, coord_x + 6 + J * 8,coord_x-8 + i * 4); cout << G.num[i][j]; } else {cout << ""; Setconsoletextattribute (handle,foreground_intensity | Foreground_green); Gotoxy (HOut, coord_x + 6 + J * 8,coord_x-8 + i * 4); cout << ""; }} cout << Endl; }}bool Game::control (char dir) {int num2[4][4]; for (int n = 0, n < 4; n++) for (int m = 0; m < 4; m++) num2[n][m] = num[n][m]; BOOL Ismove = false; if (dir = = ' W ' | | dir = = ' W ') {for (int j = 0; J < 4; + j) {int II = 0, temp = 5; for (int i = 0; i < 4; i++) {if (num[i][j] = = 0) continue; else {temp = i; Break }} if (temp = = 5) continue; NUM[II][J] = Num[temp][j]; for (int i = temp + 1; i < 4; i++) { if (num[i][j] = = 0) continue; else if (num[i][j]! = 0 && Num[i][j] = = Num[ii][j]) {num[ii][j] *= 2; G.score + = Num[ii][j]; NUM[I][J] = 0; } else if (num[i][j]! = 0 && num[i][j]! = Num[ii][j]) num[++ii][j] = Num[i][j]; } while (ii < 4) num[++ii][j] = 0; for (int n = 0, n < 4; n++) {for (int m = 0; m < 4; m++) { if (num2[n][m]! = Num[n][m]) {ismove = true; Break }} if (Ismove) break; }} return ismove; } else if (dir = = ' s ' | | dir = = ' s ') {for (int j = 0; J < 4; J + +) {int II = 3, temp = 5; for (int i = 3;I >= 0; i--) {if (num[i][j] = = 0) continue; else {temp = i; Break }} if (temp = = 5) continue; NUM[II][J] = Num[temp][j]; for (int i = temp-1; I >= 0; i--) {if (num[i][j] = = 0) continue; else if (num[i][j]! = 0 && Num[i][j] = = Num[ii][j]) {num[ii][j] *= 2; G.score + = Num[ii][j]; NUM[I][J] = 0; } else if (num[i][j]! = 0 && num[i][j]! = Num[ii][j]) num[--ii][j] = Num[i][j]; } while (ii >= 0) Num[--ii][j] = 0; for (int n = 0, n < 4; n++) {for (int m = 0; m < 4; m++) { if (num2[n][m]! = num[n][M]) {Ismove = true; Break }} if (Ismove) break; }} return ismove; } else if (dir = = ' A ' | | dir = = ' a ') {for (int i = 0; i < 4; i++) {int JJ = 0, temp = 5; for (int j = 0; J < 4; J + +) {if (num[i][j] = = 0) continue; else {temp = j; Break }} if (temp = = 5) continue; NUM[I][JJ] = num[i][temp]; for (int j = temp + 1; j < 4; J + +) {if (num[i][j] = = 0) continue; else if (num[i][j]! = 0 && Num[i][j] = = Num[i][jj]) {NUM[I][JJ] *= 2; G.score + = Num[i][jj]; Num[i][j] = 0; } else if (num[i][j]! = 0 && num[i][j]! = Num[i][jj]) {num[i][++j J] = Num[i][j]; }} while (JJ < 4) {NUM[I][++JJ] = 0; } for (int n = 0, n < 4; n++) {for (int m = 0; m < 4; m++) { if (num2[n][m]! = Num[n][m]) {ismove = true; Break }} if (Ismove) break; }} return ismove; } else if (dir = = ' d ' | | dir = = ' d ') {for (int i = 0; i < 4; i++) {int JJ = 3, temp = 5; for (int j = 3; J >= 0; j--) {if (num[i][j] = = 0) continue; else {temp = j; Break }} if (temp = = 5) continue; NUM[I][JJ] = num[i][temp]; for (int j = temp-1; J >= 0 && JJ > 0; j--) {if (num[i][j] = = 0) Continue else if (num[i][j]! = 0 && Num[i][j] = = Num[i][jj]) {NUM[I][JJ] *= 2; G.score + = Num[i][jj]; NUM[I][J] = 0; } else if (num[i][j]! = 0 && num[i][j]! = NUM[I][JJ]) NUM[I][--JJ] = Num[i][j]; } while (JJ > 0) NUM[I][--JJ] = 0; for (int n = 0, n < 4; n++) {for (int m = 0; m < 4; m++) { if (num2[n][m]! = Num[n][m]) {ismove = true; Break }} if (Ismove) Break }} return ismove; } return false;} void Game:: Init () {Srand ((unsigned int) time (NULL)); int x = rand ()% 4; int y = rand ()% 4; G.num[x][y] = 2; int xx = rand ()% 4; int yy = rand ()% 4; while (1) {if (xx! = x && yy! = y) {G.num[xx][yy] = 2; Break } xx = rand ()% 4; yy = rand ()% 4; }}void Game:: Make () {int count = 0; for (int i = 0, i < 4; i++) {for (int j = 0; J < 4; J + +) {if (num[i][j]) c Ount + +; }} if (count = =) C.end_game (); int xx = rand ()% 4; int yy = rand ()% 4; while (1) {if (num[xx][yy] = = 0) {Num[xx][yy] = 2; Break } xx = rand ()% 4; yy = rand ()% 4; }}void Console:: Start_game () {g.init (); Window (); Show (); Char dir; while (true) {bool flag = false;dir = Getch (); if (dir = =) End_game (); if (dir = = ' W ' | | dir = = ' W ' | | | dir = = ' A ' | | dir = = ' A ' | | dir = = ' s ' | | Flag = G.control (dir); Window (); if (flag) g.make (); Show (); }}}int Main () {c.enter_game ();}
Command line 2048