- # include <stdio.h>
- # include <windows.h>
- # include <conio.h>
- # include <stdlib.h>
- # include <time.h>
- const int n = 4;//map size
- int dt[4][4],r,w,i,j,ch = 'd';//data group
- void HideCursor()//hide _
- {
- CONSOLE_CURSOR_INFO cursor_info = {1, 0};
- SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
- }
- void gotoxy(int y, int x)//goto xy
- {
- COORD pos = {x,y};
- HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
- SetConsoleCursorPosition(hOut, pos);
- }
- void init()//init
- {
- srand((unsigned)time(NULL));
- SetConsoleTitle("2^n by Á÷ÐDz»ËÀ");
- HideCursor();
- for(i=0;i<n;i++)
- for(j=0;j<n;j++)
- dt[i][j] = 0;
- dt[0][0] = 2;
- }
- void move(char ch)
- {
- r = 0;
- if(ch=='w' || ch=='W')
- for(j=0;j<n;j++)
- for(i=1;i<n;i++)
- while(dt[i][j] != 0 && dt[i-1][j] == 0)
- {
- dt[i-1][j] = dt[i][j],dt[i][j] = 0,r = 1;
- if(i != 1)
- i--;
- }
- else if(ch=='s' || ch=='S')
- for(j=0;j<n;j++)
- for(i=n-2;i>=0;i--)
- while(dt[i][j] != 0 && dt[i+1][j] == 0)
- {
- dt[i+1][j] = dt[i][j],dt[i][j] = 0,r = 1;
- if(i != n-2)
- i++;
- }
- else if(ch=='a' || ch=='A')
- for(i=0;i<n;i++)
- for(j=1;j<n;j++)
- while(dt[i][j] != 0 && dt[i][j-1] == 0)
- {
- dt[i][j-1] = dt[i][j],dt[i][j] = 0,r = 1;
- if(j != 1)
- j--;
- }
- else if(ch=='d' || ch=='D')
- for(i=0;i<n;i++)
- for(j=n-2;j>=0;j--)
- while(dt[i][j] != 0 && dt[i][j+1] == 0)
- {
- dt[i][j+1] = dt[i][j],dt[i][j] = 0,r = 1;
- if(j != n-2)
- j++;
- }
- }
- void gath(char ch)
- {
- int f;
- k1: f = 0;
- if(ch=='w' || ch=='W')
- for(j=0;j<n;j++)
- for(i=1;i<n;i++)
- if(dt[i][j] == dt [i-1][j] && dt[i][j]!=0)
- dt[i-1][j]*=2,dt[i][j] = 0,f = 1;
- if(ch=='s' || ch=='S')
- for(j=0;j<n;j++)
- for(i=n-2;i>=0;i--)
- if(dt[i][j] == dt [i+1][j] && dt[i][j]!=0)
- dt[i+1][j]*=2,dt[i][j] = 0,f = 1;
- if(ch=='a' || ch=='A')
- for(i=0;i<n;i++)
- for(j=1;j<n;j++)
- if(dt[i][j] == dt [i][j-1] && dt[i][j]!=0)
- dt[i][j-1]*=2,dt[i][j] = 0,f = 1;
- if(ch=='d' || ch=='D')
- for(i=0;i<n;i++)
- for(j=n-2;j>=0;j--)
- if(dt[i][j] == dt [i][j+1] && dt[i][j]!=0)
- dt[i][j+1]*=2,dt[i][j] = 0,f = 1;
- if(f == 1)
- {
- move(ch);
- goto k1;
- }
- }
- void food()
- {
- int f = 1,a,b;
- do{
- a = rand()%n;
- b = rand()%n;
- if(dt[a][b] == 0)
- dt[a][b] = 2,f = 0;
- }while(f);
- }
- void show()
- {
- for(i=0;i<n;i++)
- for(j=0;j<n;j++)
- if(dt[i][j] != 0)
- gotoxy(i*3,j*7),printf("%7d",dt[i][j]);
- else
- gotoxy(i*3,j*7),printf(" -");
- }
- void lost()
- {
- int f = 1;
- for(i=0;i<n;i++)
- for(j=1;j<n;j++)
- if(dt[i][j] == dt[i][j-1])
- f = 0;
- for(j=0;j<n;j++)
- for(i=1;i<n;i++)
- if(dt[i][j] == dt[i-1][j])
- f = 0;
- if(f)
- printf("\\n\\n game over!Created by Lxbs."),Sleep(5000),exit(0);
- }
- int main()
- {
- init();
- show();
- while(1)
- {
- if(kbhit())
- {
- ch = getch();
- move(ch);
- gath(ch);
- w = 0;
- for(i=0;i<n;i++)
- for(j=0;j<n;j++)
- if(dt[i][j] == 0)
- w = 1;
- if(r && w)
- food();
- show();
- if(!w)
- lost();
- }
- }
- return 0;
- }
- //该片段来自于http://www.codesnippet.cn/detail/1510201513821.html
来源: http://www.codesnippet.cn/detail/1510201513821.html