First talk about the rules of the 2048 game:
At the beginning of the space will appear two digits (only 2 or 4), the user can choose to move around the next key, the numbers are moving along the direction of the whole, there is no space in the middle, if the adjacent two numbers are equal, then merge to the next one along the direction, update the maximum value, the total number plus the new number. When the 2048,win appears. Or there's no space, lose.
Basically is a follow the idea of the simulation problem, no difficulty. But I want to write an automatic solution to the program, but BFS search, the solution space is too large, did not think of a good way, there are passing cattle people also please pointing.
2048 by tach#include<iostream> #include <string> #include <ctime> #include <cstring> #include <conio.h> #include <iomanip> #include <algorithm>using namespace Std;int g[4][4];int vis[4][4];int Maxnum=0,score=0;void print ()//plot pattern {for (int. i=0;i<4;i++) {for (int j=0;j<4;j++) COUT<<SETW (5) <<g[i ][j];cout<<endl<<endl;} cout<< "Max:" <<maxNum<< "<<" score: "<<SCORE<<ENDL;} void Init () {for (Int. i=0;i<4;i++) for (int j=0;j<4;j++) g[i][j]=0;memset (vis,0,sizeof (VIS));p rint ();} BOOL Gameover ()//Determines if there is a space {bool flag=false;for (int i=0;i<4;i++) for (int j=0;j<4;j++) {if (vis[i][j]==0) {flag= True goto Loop;}} Loop:return Flag;} BOOL GetResult ()//determine if 2048 {for (int i=0;i<4;i++) for (int j=0;j<4;j++) {if (g[i][j]==2048) return true;}} void Randnum (int f)//randomly generated 2 or 4,f represents the number required to generate {int Num=0;while (1) {int X=rand ()%4;int Y=rand ()%4;if (vis[x][y]==0) {Vis[x][y ]=1;int Z=rand ()%4+2;if (z%2!=0) z-=1;g[x][y]=z;num++;if (num==f) break;}} System ("CLS");p rint ();} void work ()//main function, handling keystrokes {while (1) {int Ch=getch (), if (ch==72)//on {for (Int. j=0;j<4;j++) for (int i=0;i<4;i++) {if (vis[ I][j]==1) {for (int k=i-1;k>=0;k--) {if (vis[k][j]==1) {if (G[k][j]==g[k+1][j]) {g[k][j]+=g[k][j]; g[k+1][j]=0; vis[k+1][j]=0; Maxnum=max (Maxnum,g[k][j]); SCORE+=G[K][J]; Break } else break; } else {int temp=g[k][j];//similar to bubbling g[k][j]=g[k+1][j]; G[k+1][j]=temp; vis[k+1][j]=0; Vis[k][j]=1; }}}} randnum (1); System ("CLS"); Print (); if (Gameover () ==false) {cout<< "Game over!! 1 "<<endl; Break } if (GetResult () ==true) {cout<< "You win!!" <<endl; Break }} if (ch==80)//under {for (int j=0;j<4;j++) for (int. i=3;i>=0;i--) {if (vis[i][j]==1) {for (int k=i+1;k<4;k++) {if (vi S[k][j]==1) {if (G[k][j]==g[k-1][j]) {g[k][j]+=g[k-1][j]; g[k-1][j]=0; vis[k-1][j]=0; Maxnum=max (Maxnum,g[k][j]); SCORE+=G[K][J]; Break } else break; } else {int temp=g[k][j]; G[K][J]=G[K-1][J]; G[k-1][j]=temp; vis[k-1][j]=0; Vis[k][j]=1; }}}} randnum (1); System ("CLS"); Print (); if (Gameover () ==false) {cout<< "Game over!! 1 "<<endl; Break } if (GetResult () ==true) {cout<< "You win!!" <<endl; Break }} if (ch==75)//left {for (int i=0;i<4;i++) for (int j=0;j<4;j++) {if (vis[i][j]==1) {= (int k=j-1;k>=0;k--) {if (vis[i][k]==1) {if (g[i][k]==g[i][k+1]) {g[i][k]+=g[i][k+1]; g[i][k+1]=0; vis[i][k+1]=0; Maxnum=max (Maxnum,g[i][k]); SCORE+=G[I][K]; Break } else break; } else {int temp=g[i][k]; G[I][K]=G[I][K+1]; G[i][k+1]=temp; vis[i][k+1]=0; Vis[i][k]=1; }}}} randnum (1); System ("CLS"); Print (); if (Gameover () ==false) {cout<< "Game over!!!" <<endl Break } if (GetResult () ==true) {cout<< "You win!!" <<endl; Break }} if (ch==77)//Right {for (int i=0;i<4;i++) for (int j=3;j>=0;j--) {if (vis[i][j]==1) {= (int k=j+1;k<4; k++) {if (vis[i][k]==1) {if (g[i][k]==g[i][k-1]) {g[i][k]+=g[i][k-1]; g[i][k-1]=0; vis[i][k-1]=0; Maxnum=max (Maxnum,g[i][k]); SCORE+=G[I][K]; Break } else break; } else {int temp=g[i][k]; G[I][K]=G[I][K-1]; G[i][k-1]=temp; vis[i][k-1]=0; Vis[i][k]=1; }}}} randnum (1); System ("CLS"); Print (); if (Gameover () ==false) {cout<< "Game over!!!" <<endl; Break } if (GetResult () ==true) {cout<< "You win!!" <<endl; Break }}}}int Main () {init (); Srand ((unsigned) time (NULL)); Randnum (2); Work (); return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C + + Console version 2048 mini-game