2048 console version, 2048 Console
This is
C ++ is actually written using c ++ input and output, and the rest is C content.
The code is explained in detail, which is very simple. You are welcome to learn from and exchange ideas.
# Include <iostream> # include <stdlib. h> # include <string. h> # include <time. h> # include <conio. h> // to read the direction key # include <iomanip> // set using namespace std such as console fill characters; int score = 0; int map [4] [4]; // checker void showMap () // display checker {cout <setw (46) <"2048 by DoubleCake" <endl; cout <setw (50) <"| --------------------- |" <endl; for (int I = 0; I <= 3; I ++) {cout <setw (24) <""; for (int j = 0; j <= 3; j ++) {if (map [I] [J] = 0) cout <setw (2) <"|" <setw (4) <"; elsecout <setw (2) <"|" <setw (4) <map [I] [j]; if (j = 3) {cout <setw (2) <"|" <endl; cout <setw (50) <"| --------------------- |" <endl ;}}} void randNum () // number of records generated from random locations {int m = rand () % 4; int n = rand () % 4; while (map [m] [n]) {m = rand () % 4; n = rand () % 4;} map [m] [n] = 2;} void startGame () // start the game {memset (map, 0, sizeof (map); randNum (); showMap ();} Int moveUp () // move up {int I, j; int res = 0; // used to record whether moving for (j = 0; j <4; j ++) {// first fill in the blank for (I = 1; I <4; I ++) {if (map [I] [j] &! Map [I-1] [j]) {map [I-1] [j] = map [I] [j]; map [I] [j] = 0; // fill in if (I> 1) I-= 2; res = 1 ;}/// from the root, the same merging for (I = 1; I <4; I ++) {if (map [I] [j] & map [I] [j] = map [I-1] [j]) {map [I-1] [j] * = 2; score + = map [I] [j]; map [I] [j] = 0; res = 1 ;}// fill in the blank space for (I = 1; I <4; I ++) {if (map [I] [j] &! Map [I-1] [j]) {map [I-1] [j] = map [I] [j]; map [I] [j] = 0; // fill in if (I> 1) I-= 2 ;}}return res;} int moveDown () // move down {int I, j; int res = 0; for (j = 0; j <4; j ++) {for (I = 2; I> = 0; I --) {if (map [I] [j] &! Map [I + 1] [j]) {map [I + 1] [j] = map [I] [j]; map [I] [j] = 0; if (I <2) I + = 2; res = 1 ;}} for (I = 2; I> = 0; I --) {if (map [I] [j] & map [I] [j] = map [I + 1] [j]) {map [I + 1] [j] * = 2; score + = map [I] [j]; map [I] [j] = 0; res = 1 ;}}for (I = 2; I >= 0; I --) {if (map [I] [j] &! Map [I + 1] [j]) {map [I + 1] [j] = map [I] [j]; map [I] [j] = 0; if (I <2) I + = 2 ;}} return res ;}int moveLeft () {int I, j; int res = 0; for (I = 0; I <4; I ++) {for (j = 1; j <4; j ++) {if (map [I] [j] &! Map [I] [j-1]) {map [I] [j-1] = map [I] [j]; map [I] [j] = 0; if (j> 1) j-= 2; res = 1 ;}} for (j = 1; j <4; j ++) {if (map [I] [j] & map [I] [j] = map [I] [j-1]) {map [I] [j-1] * = 2; score + = map [I] [j]; map [I] [j] = 0; res = 1 ;}}for (j = 1; j <4; j ++) {if (map [I] [j] &! Map [I] [j-1]) {map [I] [j-1] = map [I] [j]; map [I] [j] = 0; if (j> 1) j-= 2 ;}} return res ;}int moveRight () {int I, j; int res = 0; for (I = 0; I <4; I ++) {for (j = 2; j> = 0; j --) {if (map [I] [j] &! Map [I] [j + 1]) {map [I] [j + 1] = map [I] [j]; map [I] [j] = 0; if (j <2) j + = 2; res = 1 ;}} for (j = 2; j> = 0; j --) {if (map [I] [j] & map [I] [j] = map [I] [j + 1]) {map [I] [j + 1] * = 2; score + = map [I] [j]; map [I] [j] = 0; res = 1 ;}}for (j = 2; j> = 0; j --) {if (map [I] [j] &! Map [I] [j + 1]) {map [I] [j + 1] = map [I] [j]; map [I] [j] = 0; if (j <2) j + = 2 ;}} return res;} int maxNum () // maximum number of checkerboard {int max = map [0] [0]; for (int I = 0; I <= 3; I ++) for (int j = 0; j <= 3; j ++) if (map [I] [j]> max) max = map [I] [j]; return max;} int Win () // determine whether to Win {int flag = 0; if (maxNum () = 2048) {cout <setw (45) <"You Win! "<Endl; flag = 1;} return flag;} int GameOver () // determines whether the game is over {int flag = 1; int I, j; // continue for (I = 0; I <4; I ++) for (j = 0; j <4; j ++) if (! Map [I] [j]) flag = 0; // you can continue if (flag = 1) {for (I = 0; I <4; I ++) {for (j = 0; j <4; j ++) {if (I! = 0 & map [I] [j] = map [I-1] [j]) flag = 0; if (I! = 3 & map [I] [j] = map [I + 1] [j]) flag = 0; if (j! = 0 & map [I] [j] = map [I] [j-1]) flag = 0; if (j! = 3 & map [I] [j] = map [I] [j + 1]) flag = 0 ;}} if (flag = 1) cout <setw (43) <"Game Over! "<Endl; return flag;} void keydown () // read direction {int ch = _ getch (); srand (unsigned) time (NULL )); switch (ch) {case 72: // topif (moveUp () {randNum (); system ("cls"); showMap () ;}break; case 75: // leftif (moveLeft () {randNum (); system ("cls"); showMap () ;}break; case 77: // rightif (moveRight ()) {randNum (); system ("cls"); showMap () ;}break; case 80: // downif (moveDown () {randNum (); system ("cls"); showMap ();} break; defaul T: break ;}} int main () // main function {system ("color f9"); int makesure = 1; // whether to continue the game while (makesure) after the game ends) {system ("cls"); startGame (); while (Win () + GameOver () = 0) {keydown () ;}cout <setw (43) <"your final score is:" <score <endl; cout <setw (60) <"Enter 1 to start the game again, to end, enter 0. "<Endl; cin> makesure; while (makesure! = 1 & makesure! = 0) {cout <"the input is incorrect. Please enter it again! "<Endl; cin> makesure ;}} cout <" goodbye! "<Endl; system (" pause "); return 0 ;}