用C语言编写2048游戏源代码
C语言课程设计报告-游戏2048
![C语言课程设计报告-游戏2048](https://img.taocdn.com/s3/m/d115ec66011ca300a6c39033.png)
东华理工大学C语言课程设计报告学院:国际教育学院学院专业:电子信息工程班级:1420606学号:************姓名:***一、课程设计题目:游戏2048二、课程设计要求:a)使用C语言编写2048这款游戏b)能够正常运行,拥有游戏界面。
c)能正常进行游戏从开始到结束。
d)用户操作方便三、设计思路:a)游戏介绍:i.2048是一款简单的数字类游戏,界面是一个4*4的方形格子。
每个格子里可以为空或者有一个2^n的数值。
ii.用户可以输入4种指令,分别是:上下左右,游戏会根据用户的指定的方向,将格子中的数值向对应方向进行移动,直至移动到最边上的格子或者有其他数值占用,如果碰到等大数值,将会进行合并。
此外,成功移动后,会在一个空格子随机生成一个2或者4 iii.游戏目标是合成2048这个数值或者更大的数值。
b)实现思路:i.可以使用二维数组来保存4*4格子中的数值ii.指令,可以通过输入字符函数,读取用户在键盘上的方向键,进行判断执行对应的代码。
iii.游戏界面,可以使用简单的特殊制表符,来实现,并通过清屏函数来进行反复同位置打印界面。
iv.需要判断游戏结束的函数,以及记录游戏分数和步骤的变量v.当游戏结束时,能够询问用户是否重新开始。
vi.随机生成一个新数,可以调用随机函数,使用时间做种子。
c)实现难点:i.打印游戏界面,要实现灵活能根据棋盘数组里面的数据灵活打印。
ii.执行操作时,数值的移动和合并。
四、流程图五、C语言源代码// 游戏2048.c#include "windows.h"#include "time.h"#include "stdio.h"#include "conio.h"#include "string.h"//宏定义常量方向键值//const int LEFT = 75, UP = 72, RIGHT = 77, DOWN = 80;#define LEFT 75#define UP 72#define RIGHT 77#define DOWN 80const char error_str[] = "您上次输入的指令无法识别,请重新输入。
2048小游戏代码解析C语言版
![2048小游戏代码解析C语言版](https://img.taocdn.com/s3/m/68f51f2ca4e9856a561252d380eb6294dd882284.png)
2048⼩游戏代码解析C 语⾔版2048⼩游戏,也算是风靡⼀时的益智游戏。
其背后实现的逻辑⽐较简单,代码量不算多,⽽且趣味性强,适合作为有语⾔基础的童鞋来加强编程训练。
本篇分析2048⼩游戏的C 语⾔实现代码。
前⾔游戏截图:游戏实现原理:使⽤终端图形库⽂件curses 绘制终端⾥的图形。
使⽤⼀个⼆维数组保存4 x 4 空格中的变量。
键盘输⼊控制移动,经过逻辑判断,⼆维数组数据变化。
⼆维数组数据变化后交给图形函数显⽰出来。
库⽂件curses 介绍:curses 是⼀种终端图形绘制库,利⽤curses 可以在终端中绘制多种图形。
简单demo深⼊学习请查询相关资料。
#include <stdio.h>#include <curses.h>int main(){initscr();border(0,0,0,0,0,0,0,0);move(5,15);printw("%s","hello world");refresh();char ch=getch();endwin();return 0;}编译:gcc curses_demo.c -lcurses2048实现代码分析根据2048实现原理,代码要实现的主要有三件事:图形绘制游戏逻辑操作图形加载逻辑结果主程序代码如下:2048 C语⾔版代码分析//-------------头⽂件--------------------//#include <stdio.h>#include <stdlib.h>#include <curses.h>#include <time.h>#include <unistd.h>#include <signal.h>//--------------------------------------////------------------全局变量-------------------------------// 游戏主界⾯是⼀个 4*4 的 16 宫格,使⽤⼆维数组进⾏表⽰,⽤ 0 表⽰空格int a[4][4] = {0};// 16 宫格中空格的个数int empty;// 涉及到新产⽣的数字的位置的两个变量int old_y, old_x;//所有的C语⾔代码就是在这三个函数中int main(){//初始化函数init();//游戏运⾏时函数play();//结束函数,清屏//endwin()来关闭 curses 模式.endwin();return0;}main()函数代码分析头⽂件+全局变量头⽂件中包含的库⽂件如下:<stdio.h> 标准输⼊输出<stdlib.h> 设计到内存操作函数<curses.h> 绘制图形库⽂件<time.h> 时间函数<unistd.h> 睡眠函数库⽂件<signal.h> 信号相关操作库⽂件主函数代码主函数中共有三个⼦函数,其中复杂的为前两个,第三个为curses关闭的函数,没有任何逻辑。
2048小游戏C语言编程设计
![2048小游戏C语言编程设计](https://img.taocdn.com/s3/m/5be258fc19e8b8f67c1cb979.png)
if (*(p + b) != 0) if (*(p + i) == *(p + b)) { score = score + (*(p + i)) + (*(p + b)); *(p + i) = *(p + i) + *(p + b); if (*(p + i) == 2048) gamew = 1; *(p + b) = 0; i = b + i; ++ifappear; break; } else { i = b; break; }
++ifappear; e++; } } } if (ifappear != 0) ++move; break; case 'd':
case 'D': case 77:
ifappear = 0; for (j = 0; j < 4; j++) {
for (i = 0; i < 4; i++) {
b[i] = num[j][i]; num[j][i] = 0; } add(b); e = 3; for (g = 3; g >=0; g--) { if (b[g] != 0) {
void menu(); system("cls"); printf("\t\t*****************************************\t\t\n"); printf("\t\t*****************************************\n"); printf("\t\t******************游戏规则***************\n"); printf("\t\t*****************************************\n"); printf("\t\t*****************************************\t\t\n"); printf("玩家可以选择上、下、左、右或 W、A、S、D 去移动滑块\n"); printf("玩家选择的方向上若有相同的数字则合并\n"); printf("合并所得的所有新生成数字相加即为该步的有效得分\n"); printf("玩家选择的方向行或列前方有空格则出现位移\n"); printf("每移动一步,空位随机出现一个 2 或 4\n"); printf("棋盘被数字填满,无法进行有效移动,判负,游戏结束\n"); printf("棋盘上出现 2048,获胜,游戏结束\n"); printf("按上下左右去移动滑块\n"); printf("请按任意键返回主菜单...\n"); getch(); system("cls"); main(); } void gamefaile() { int i, j; system("cls"); printf("\t\t*****************************************\t\t\n"); printf("\t\t*****************************************\n"); printf("\t\t******************you fail***************\n"); printf("\t\t*****************************************\n"); printf("\t\t*****************************************\t\t\n"); printf("\t\t\t---------------------\n\t\t\t"); for (j = 0; j<4; j++) {
2048C#版游戏源代码详细注释
![2048C#版游戏源代码详细注释](https://img.taocdn.com/s3/m/09a1470f16fc700abb68fcac.png)
}
//类成员方法:绘制线条 public void Draw2048Board(Graphics g)
{ Pen mypen = new Pen(Color.Gray, 22); //绘制横线 for (int row = 1; row <= 5; row++) { g.DrawLine(mypen, new Point(_leftTop.X - 11, _leftTop.Y + _height * (row -
for (int col = 1; col <= 4; col++) _2048ImageValue[row, col] = "0";
} //装载所有的图片位图文件 _bmpPiece[0] = new Bitmap("Images\\0.png"); _bmpPiece[1] = new Bitmap("Images\\2.png"); _bmpPiece[2] = new Bitmap("Images\\4.png"); _bmpPiece[3] = new Bitmap("Images\\8.png"); _bmpPiece[4] = new Bitmap("Images\\16.png"); _bmpPiece[5] = new Bitmap("Images\\32.png"); _bmpPiece[6] = new Bitmap("Images\\64.png"); _bmpPiece[7] = new Bitmap("Images\\128.png"); _bmpPiece[8] = new Bitmap("Images\\256.png"); _bmpPiece[9] = new Bitmap("Images\\512.png"); _bmpPiece[10] = new Bitmap("Images\\1024.png"); _bmpPiece[11] = new Bitmap("Images\\2048.png"); _bmpPiece[12] = new Bitmap("Images\\4096.png"); _bmpPiece[13] = new Bitmap("Images\\8192.png"); _bmpPiece[14] = new Bitmap("Images\\16384.png"); _bmpPiece[15] = new Bitmap("Images\\4-boom.png"); _bmpPiece[16] = new Bitmap("Images\\8-boom.png"); _bmpPiece[17] = new Bitmap("Images\\16-boom.png"); _bmpPiece[18] = new Bitmap("Images\\32-boom.png"); _bmpPiece[19] = new Bitmap("Images\\64-boom.png"); _bmpPiece[20] = new Bitmap("Images\\128-boom.png"); _bmpPiece[21] = new Bitmap("Images\\256-boom.png"); _bmpPiece[22] = new Bitmap("Images\\512-boom.png"); _bmpPiece[23] = new Bitmap("Images\\1024-boom.png"); _bmpPiece[24] = new Bitmap("Images\\2048-boom.png"); _bmpPiece[25] = new Bitmap("Images\\4096-boom.png"); _bmpPiece[26] = new Bitmap("Images\\8192-boom.png"); //设置所有图片的透明色 for (int i = 0; i <= 26; i++)
C++实现2048小游戏(控制台版的)
![C++实现2048小游戏(控制台版的)](https://img.taocdn.com/s3/m/ad48942128ea81c759f578c5.png)
C++实现2048小游戏(控制台版的)#include <iostream>#include <windows.h>#include <ctime>using namespace std;int const ROW = 4;int const COL = 4;int game[ROW][COL] = {0};//上下左右int const UP = 1;int const DOWN = 2;int const LEFT = 3;int const RIGHT = 4;//游戏所处的状态int const GAME_OVER = 1;int const GAME_WIN = 2;int const GAME_CONTINUE = 3;enum GameNum{Game_2 = 2,Game_4 = 4,Game_8 = 8,Game_16 = 16,Game_32 = 32,Game_64 = 64,Game_128 = 128,Game_256 = 256,Game_512 = 512,Game_1024 = 1024,Game_2048 = 2048,};//打印所得的数组void Print(){system("cls");cout << "***************** 2048 控制台版******************" << endl;cout << "***************** By Tanzf (Intern) ******************" << endl << endl;for (int i = 0; i < ROW; ++i){cout << "---------------------------------"<< endl;for (int j = 0; j < COL; ++j){if (game[i][j] == 0){cout <<"| \t";}else{cout <<"| " << game[i][j] << "\t";}}cout << "|" << endl;}cout << "---------------------------------"<< endl;}bool CreateNumber(){int x = -1;int y = -1;int times = 0;int maxTimes = ROW * COL;//三分之二的概率生成2,三分之一的概率生成4 int whitch = rand() % 3;do{x = rand() % ROW;y = rand() % COL;++times;} while (game[x][y] != 0 && times <= maxTimes);//说明格子已经满了if(times >= maxTimes){return false;}else{GameNum num;if(whitch == 0){num = Game_4;}else if(whitch){num = Game_2;}game[x][y] = num;}return true;}void Process(int direction){switch (direction){case UP://最上面一行不动for(int row = 1; row < ROW; ++row){for(int crow = row; crow >= 1; --crow){for(int col = 0; col < COL; ++col){//上一个格子为空if(game[crow-1][col] == 0){game[crow-1][col] = game[crow][col];game[crow][col] = 0;}else{//合并if(game[crow-1][col] == game[crow][col]){game[crow - 1][col] *= 2;game[crow][col] = 0;}}}}}break;case DOWN://最下面一行不动for(int row = ROW - 2; row >= 0; --row){for(int crow = row; crow < ROW - 1; ++crow){for(int col = 0; col < COL; ++col){//上一个格子为空if(game[crow + 1][col] == 0){game[crow + 1][col] = game[crow][col];game[crow][col] = 0;}else{//合并if(game[crow + 1][col] == game[crow][col]){game[crow + 1][col] *= 2;game[crow][col] = 0;}}}}}break;case LEFT://最左边一列不动for(int col = 1; col < COL; ++col){for(int ccol = col; ccol >= 1; --ccol){for(int row = 0; row < ROW; ++row){//上一个格子为空if(game[row][ccol-1] == 0){game[row][ccol - 1] = game[row][ccol];game[row][ccol] = 0;}else{//合并if(game[row][ccol - 1] == game[row][ccol]){game[row][ccol - 1] *= 2;game[row][ccol] = 0;}}}}}break;case RIGHT://最右边一列不动for(int col = COL - 2; col >= 0; --col){for(int ccol = col; ccol <= COL - 2; ++ccol){for(int row = 0; row < ROW; ++row){//上一个格子为空if(game[row][ccol + 1] == 0){game[row][ccol + 1] = game[row][ccol];game[row][ccol] = 0;}else{//合并if(game[row][ccol + 1] == game[row][ccol]){game[row][ccol + 1] *= 2;game[row][ccol] = 0;}}}}}break;}}//处理输入输出,返回上下左右int Input(){//读取上下左右四个方向键int upArrow = 0;int downArrow = 0;int leftArrow = 0;int rightArrow = 0;int direction = 0;while (true){upArrow = GetAsyncKeyState(VK_UP);downArrow = GetAsyncKeyState(VK_DOWN);leftArrow = GetAsyncKeyState(VK_LEFT);rightArrow = GetAsyncKeyState(VK_RIGHT);if(upArrow){direction = UP;break;}else if(downArrow){direction = DOWN;break;}else if(leftArrow){direction = LEFT;break;}else if(rightArrow){direction = RIGHT;break;}Sleep(100);}return direction;}//判断游戏状态int Judge(){//赢得游戏for(int i = 0; i < ROW; ++i){for(int j = 0; j < COL; ++j){if(game[i][j] == 2048){return GAME_WIN;break;}}}//横向检查for(int i = 0 ; i < ROW; ++i){for(int j = 0; j < COL - 1; ++j){if(!game[i][j] || (game[i][j] == game[i][j+1])){return GAME_CONTINUE;break;}}}//纵向检查for(int j = 0; j< COL; ++j){for(int i = 0; i < ROW -1; ++i){if(!game[i][j] || (game[i][j] == game[i+1][j])){return GAME_CONTINUE;break;}}}//不符合上述两种状况,游戏结束return GAME_OVER;}int main(){//设置一个随机数种子srand((unsigned int)time(0));CreateNumber();CreateNumber();Print();int direction = 0;int gameState = -1;while(true){direction = Input();gameState = Judge();if(direction && gameState == GAME_CONTINUE){Process(direction);CreateNumber();Print();Sleep(100);}else if(gameState == GAME_WIN){Print();cout << "You Win!" << endl;break;}else if(gameState == GAME_OVER){Print();cout <<"You lose!" << endl;break;}}return 0;}。
2048代码
![2048代码](https://img.taocdn.com/s3/m/a968bf6d27284b73f2425057.png)
for (int line = 3; line >= 1; line--)
{
if (t[line][row] == t[line - 1][row])
{
iscleared = 1;
t[line][row] *= 2;
score += t[line][row];
*/
int moveLeft()
{
int ismove = 0;
for (int count = 0; count<4; count++)
{
for (int line = 0; line<4; line++)
{
for (int row = 0; row<4; row++)
void welcome(); //显示欢迎界面
int _tmain(int argc, _TCHAR* argv[])
{
welcome();
while (1)
{
char input = _getch();
if (input == ' ') break;
}
}
}
}
return ismove;
}
/*
函数名:moveDown
作用:向下移动方块
返回值:是否有移动方块(1-成功,0-失败)
*/
int moveDown()
{
int ismove = 0;
for (int count = 0; count<4; count++)
2048 c语言源代码
![2048 c语言源代码](https://img.taocdn.com/s3/m/b729c374d5bbfd0a785673a3.png)
#include <stdlib.h>#include<time.h>#include <stdio.h>#include <conio.h>#include<windows.h>#define N 4int grid[N][N]={0};int D=0;int M=2048;//显示void showdata(){int i,j;system("CLS");for(i=0;i<N+2;i++)printf("%5c",'+');printf("\n\n");for(i=0;i<N;i++){printf("%5c",'+');for(j=0;j<N;j++)if(grid[i][j]!=0)printf("%5d",grid[i][j]);elseprintf("%5c",' ');printf("%5c",'+');printf("\n\n");}for(i=0;i<N+2;i++)printf("%5c",'+');printf("\n"); }//判断是否有空位int isNotFull(){int i,j,k=0;for(i=0;i<N;i++)for(j=0;j<N;j++)if(grid[i][j]==0){k=1; break;}return k;}//随机数字void randomdata(){int r,c,x;x = rand()%2*2+2;do{r = rand()%N;c = rand()%N;}while(grid[r][c]!=0);grid[r][c]=x;}//获取最大值int getMax(){int i,j,max=0;for(i=0;i<N;i++)for(j=0;j<N;j++)if(max<grid[i][j]) max=grid[i][j];return max;}//移动相加,返回1表示有移动,返回0表示无移动int add(){int i,j,cr,w,F=0;if(D==1)//top{for(i=1;i<N;i++)for(j=0;j<N;j++){cr=i;w=0;//0:未合并1:合并过while(cr>=1 && grid[cr][j]!=0 ){if(grid[cr-1][j]==0)//上方有空位,上移{grid[cr-1][j]=grid[cr][j];grid[cr][j]=0;F=1;}else//上方无空位{if(grid[cr-1][j]==grid[cr][j]&& w==0)//相等,相加{grid[cr-1][j]=grid[cr-1][j]*2;grid[cr][j]=0; w=1; F=1;}else//不等{break;}}cr--;}}}if(D==2)//down{for(i=N-2;i>=0;i--)for(j=0;j<N;j++){cr=i;w=0;while(cr<=N-2 && grid[cr][j]!=0 ){if(grid[cr+1][j]==0)//下方有空位,下移{grid[cr+1][j]=grid[cr][j];grid[cr][j]=0;F=1;}else//下方无空位{if(grid[cr+1][j]==grid[cr][j] && w==0)//相等,相加{grid[cr+1][j]=grid[cr+1][j]*2;grid[cr][j]=0; w=1;F=1;}else//不等{break;}}cr++;}}}if(D==3)//left{for(i=0;i<N;i++)for(j=1;j<N;j++){cr=j;w=0;while(cr>=1 && grid[i][cr]!=0 ){if(grid[i][cr-1]==0)//左方有空位,左移grid[i][cr-1]=grid[i][cr];grid[i][cr]=0;F=1;}else//左方无空位{if(grid[i][cr-1]==grid[i][cr] && w==0)//相等,相加{grid[i][cr-1]=grid[i][cr-1]*2;grid[i][cr]=0;w=1;F=1;}else//不等{break;}}cr--;}}}if(D==4)//right{for(i=0;i<N;i++)for(j=N-2;j>=0;j--){cr=j;w=0;while(cr<=N-2 && grid[i][cr]!=0 ){if(grid[i][cr+1]==0)//右方有空位,右移{grid[i][cr+1]=grid[i][cr];grid[i][cr]=0;F=1;}else//右方无空位{if(grid[i][cr+1]==grid[i][cr] && w==0)//相等,相加{grid[i][cr+1]=grid[i][cr+1]*2;grid[i][cr]=0; w=1; F=1;}else//不等{break;}cr++;}}}return F;}int getKey(){int k=0;char c=getch();if(c<0) //c<0为特殊键,还要再读下一个字节判断为何键{c=getch();if(c==72) {D=1; k=1;}//topif(c==80) {D=2; k=1;}//downif(c==75) {D=3; k=1;}//leftif(c==77) {D=4; k=1;}//right}return k;}//在数字全满下,检查是否还有合并的可能,有则返回1;int canAdd(){int i,j,k,F=0;for(i=0;i<N;i++)for(j=0;j<N-1;j++)if(grid[i][j]==grid[i][j+1]) F=1;for(j=0;j<N;j++)for(i=0;i<N-1;i++)if(grid[i][j]==grid[i+1][j]) F=1;return F;}main(){char c;int mov,key,isf;printf("请输入游戏要拼凑的最大数字,例如32,64,128,...,2048:");scanf("%d",&M);//初次状态srand(time(NULL));randomdata();//随机第一个数randomdata();//随机第二个数showdata();do{key=getKey();//读取操作键if(key==0) continue; //不是上下左右键,重新读取键盘mov=add();//根据方向键合并相加,返回1表示有移动if(mov==1) showdata();//显示if(getMax()==M)//判断是否胜利{printf("你赢了!\n");break;}isf=isNotFull();//返回1表示还有空位if(isf==1 && mov==1) //有空位且有移动再随机{randomdata();//再随机showdata();//显示}if(isf==0)//没有空间则游戏结束{if(canAdd()==1)printf("请选择另一个方向滑动!\n");else break;}}while(1);}。
2048C语言
![2048C语言](https://img.taocdn.com/s3/m/d632e7928762caaedd33d4ca.png)
{
for (int j = 1; j <= 2 * W * N; j++){
putchar('*');
}
}
else if (i%W == (W + 1) / 2) //在中间行输出数字或者空白
putchar('*');
else
putchar(' ');
}
}
putchar('\n');
}
printf("******************************************************************************");
return 0;
}
//判断是否可以上下左右移动的函数,判断成败的条件
int Up(int number[][N]){
int i, j, k, t; //k为本次所求单个数的列下标,t为移动的,计算要和k做变化的下标
int b = 0; //b为标志变量,标志此次移动是否发生,无论是移动还是相加都代表着发生了移动
int n;//获得的随机数存放在这里
int x, y;//获得的随机坐标存放在这里
long a = rand();//a为与时间相关的随机种子数
if (a % 2 == 0)
n = 2;
else
n = 4;
/* while (1){
x = (time(0)%100) % 4;
system("color 1F");
基于C语言实现2048游戏
![基于C语言实现2048游戏](https://img.taocdn.com/s3/m/a9f8655f2f3f5727a5e9856a561252d380eb20b0.png)
基于C语⾔实现2048游戏本⽂实例为⼤家分享了C语⾔实现2048游戏的具体代码,供⼤家参考,具体内容如下#include <stdio.h>#include <stdlib.h>#include <time.h>#include <conio.h>#include <windows.h>#define ROW 4#define COL ROW#define KEY1 224#define KEY_LEFT 75#define KEY_UP 72#define KEY_RIGHT 77#define KEY_DOWN 80int g_sgap = 0;/*应⽤市场下载2048如果需要图形界⾯,需要加界⾯库*///在数组arr产⽣⼀个新的数字void GetNewVal(int arr[ROW][COL]){srand( (unsigned)time( NULL ) + g_sgap++);int x = rand()%ROW;//⾏下标,保证不越界int y = rand()%COL;//列下标,保证不越界int newval = 2;if(x == 0)//75%的概率为2,25%的概率为4{newval = 4;}//找到空闲的格⼦while(arr[x][y] != 0)//该格⼦已经有值,todo有可能死循环{y++;if(y == COL)//{y = 0;x = (x+1)%ROW;//下⼀⾏}}arr[x][y] = newval;}//打印void Show(int arr[ROW][COL]){system("cls");for(int i=0; i<ROW;i++){for(int j=0;j<COL;j++){printf("%4d",arr[i][j]);}printf("\n");}}//显⽰开始界⾯void Start(int arr[ROW][COL]){//获取两个数字,然后显⽰界⾯GetNewVal(arr);GetNewVal(arr);Show(arr);}//获取键值,左:1,上:2,右:3,下:4,其它:0int GetButton(){int key1 = 0;//第⼀个键值int key2 = 0;//第⼆个键值while(1){if(_kbhit()){key1 = _getch();//获得第⼀个键值if(key1 == KEY1)//0xE0{key2 = _getch();//获取第⼆个键值if(key2 == KEY_LEFT){return 1;}else if(key2 == KEY_UP){return 2;}else if(key2 == KEY_RIGHT){return 3;}else if(key2 == KEY_DOWN){return 4;}}}Sleep(100);//睡眠,让出CPU,避免忙等待}}//向左合并bool MergeLeft(int arr[ROW][COL]){int x1 = -1;//第⼀个需要合并的数字下标bool flg = false;//当前没有有效合并(没有数据合并,也没有数据移动) for(int i=0;i<ROW;i++){x1 = -1;//第⼀步,合并相同的数字for(int j=0;j<COL;j++){if(arr[i][j]!=0){if(x1 == -1)//该⾏第⼀个⾮0的值{x1 = j;}else//当前第⼆个需要处理的值{if(arr[i][j] == arr[i][x1])//合并,将x1下标的值*2,j下标的值置为0{arr[i][x1] *= 2;arr[i][j] = 0;x1 = -1;flg = true;}else//第⼀个值和第⼆个值不等,{x1 = j;}}}}//第⼆步,移动数字int index = 0;//当前可以放数据的下标for(int j=0;j<COL;j++){if(arr[i][j]!=0)//需要移动数据{if(index != j){arr[i][index] = arr[i][j];arr[i][j] = 0;index++;flg = true;}else{index++;}}}}return flg;}//游戏是否结束//1.没有空闲单元格//2.相邻没有相同的数字bool IsGameOver(int arr[ROW][COL]){//判断有没有空闲单元格int activeCell = 0;//统计空闲单元格数量for(int i=0;i<ROW;i++){for(int j=0;j<COL;j++){if(arr[i][j] == 0){activeCell++;}}}if(activeCell != 0){return false;}//相邻是否有相同的数字,只需要判断右边和下边for(int i=0;i<ROW;i++){for(int j=0;j<COL;j++){//if(arr[i][j]==arr[i][j+1] || arr[i][j] == arr[i+1][j])if(j+1<COL&&arr[i][j]==arr[i][j+1] || i+1<ROW&&arr[i][j]==arr[i+1][j]) {return false;}}}return true;}void Run(int arr[ROW][COL]){int bt;bool rt = false;while(1){bt = GetButton();if(bt == 1)//⽅向键左{rt = MergeLeft(arr);if(rt){GetNewVal(arr);Show(arr);if(IsGameOver(arr)){return ;}}}}}int main(){int arr[ROW][COL] = {0};Start(arr);Run(arr);return 0;}int main1(){int a = 0;while(1){if(_kbhit()){a = _getch();//getchar();printf("键值是:%d\n",a);}}return 0;}/*int main(){srand( (unsigned)time( NULL ) );for(int i=0;i<10;i++){printf("%d ",rand());}printf("\n");return 0;}*/运⾏画⾯以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
2048游戏源代码
![2048游戏源代码](https://img.taocdn.com/s3/m/dd749638a32d7375a4178066.png)
#include <iostream>#include <iomanip>#include <cstdlib>#include <ctime>#include <string>#include <memory>// 2的概率大于4#define random_2_4 (rand()%5==4 ? 4:2)#define random_x(x) (rand()%x)using namespace std;// 矩阵大小const int MAX = 4;const int MAXMAX = MAX*MAX;// 初始数据个数const int INIT_SIZE = 2;// 数据矩阵int _matrix[MAX][MAX];// 合并临时数组int current[MAX];// 数据计数int _count;// 按键操作 => 不使用char...防止不必要的失败... => ch[0]即可... string ch;// 得分...int score;// 打印数组void print();// 操作说明void help();// 初始操作void init();// 随机位置 => 随机数据2/4bool random_data_one();// 上左下右bool b_up();bool b_left();bool b_down();bool b_right();void _up();void _left();void _down();void _right();int main(){init();print();while (b_up() || b_left() || b_down() || b_right()){help();switch (ch[0]){case 'w':_up();break;case 'a':_left();break;case 's':_down();break;case 'd':_right();break;default:cout <<"无效输入..."<< endl << endl; break;}print();}cout << " Game Over !! " << endl;cout << "最终得分Socre => " << score << endl;system("pause");system("pause");system("pause");return 0;}void print(){int j;cout << "-------------------------------------" << endl; cout << "得分Socre => " << score << endl;cout << "-------------------------------------" << endl;for (int i = 0; i < MAX; i++){for ( j = 0; j < MAX; j++){if (_matrix[i][j])cout << setw(5) << _matrix[i][j] << " |";elsecout << setw(7) << " |";}cout << endl;}cout << "-------------------------------------" << endl << endl;; }void help(){cout << "wasd => 上左下右" << endl;cout << "请输入: ";cin >> ch;}void init(){_count = 0;score = 0;srand((int)time(0));for (int i = 0; i < INIT_SIZE; i++)random_data_one();}bool random_data_one(){if (_count == MAXMAX)return false;int left = MAXMAX - _count;int tmp = random_x(left);int num = random_2_4;int k = 0;for (int i = 0; i < MAX; i++){for (int j = 0; j < MAX; j++){if (_matrix[i][j] == 0){if (k++ == tmp){_matrix[i][j] = num;break;}}}}++_count;return true;}bool b_up(){int j;for (int k = 0; k < MAX; k++){// 特殊情况...这一列有空元素...bool flag = false;for (int i = 0; i < MAX - 1; i++){if (_matrix[i][k] == 0)flag = true;else{int j = i + 1;while (j < MAX){if (_matrix[j][k]){if (_matrix[i][k] == _matrix[j][k]) return true;elsebreak;}else{++j;}}}}if (flag){// 空元素上方有元素则为True...// 使用左右夹击法...int i = 0, j = MAX - 1;while (i < MAX){if (_matrix[i][k])++i;elsebreak;}while (j >= 0){if (_matrix[j][k] == 0)--j;elsebreak;}if (i < j)return true;}}return false;}bool b_left(){for (int k = 0; k < MAX; k++){// 特殊情况...这一行有空元素...bool flag = false;for (int i = 0; i < MAX - 1; i++){if (_matrix[k][i] == 0)flag = true;else{int j = i + 1;while (j < MAX){if (_matrix[k][j]){if (_matrix[k][i] == _matrix[k][j]) return true;elsebreak;}else{++j;}}}}if (flag){// 空元素右边有元素则为True...// 使用左右夹击法...int i = 0, j = MAX - 1;// i 是空元素位置,j是非空元素位置 while (i < MAX){if (_matrix[k][i])++i;elsebreak;}while (j >= 0){if (_matrix[k][j] == 0)--j;elsebreak;}if (i < j)return true;}}return false;}bool b_down(){for (int k = 0; k < MAX; k++){// 特殊情况...这一列有空元素...bool flag = false;for (int i = MAX - 1; i > 0; i--){if (_matrix[i][k] == 0)flag = true;else{int j = i - 1;while (j >= 0){if (_matrix[j][k]){if (_matrix[i][k] == _matrix[j][k])return true;elsebreak;}else{--j;}}}}if (flag){// 空元素上方有元素则为True... => 下边第一个空元素在上边第一个非空元素下边即可...// 使用左右夹击法...int i = 0, j = MAX - 1;// i 是非空位置,j是空元素位置while (i < MAX){if (_matrix[i][k] == 0)++i;elsebreak;}while (j >= 0){if (_matrix[j][k])--j;elsebreak;}if (i < j)return true;}}return false;}bool b_right(){for (int k = 0; k < MAX; k++){// 特殊情况...这一行这一行有空元素...bool flag = false;for (int i = MAX - 1; i > 0; i--){if (_matrix[k][i] == 0)flag = true;else{int j = i - 1;while (j >= 0){if (_matrix[k][j]){if (_matrix[k][i] == _matrix[k][j])return true;elsebreak;}else{--j;}}}}if (flag){// 空元素左边有元素则为True... => 右边第一个空元素在左边第一个非空元素右边即可...// 使用左右夹击法...int i = 0, j = MAX - 1;// i 是非空位置,j是空元素位置while (i < MAX){if (_matrix[k][i] == 0)++i;elsebreak;}while (j >= 0){if (_matrix[k][j])--j;elsebreak;}if (i < j)return true;}}return false;}void _up(){cout << "按下了上键" << endl << endl;if (b_up()){cout << "可以向上合并" << endl;for (int i = 0; i < MAX; i++){memset(current, 0, sizeof(int)*MAX); int ii = 0;for (int j = 0; j < MAX; j++){if (_matrix[j][i])current[ii++] = _matrix[j][i]; }for (int k = 0; k < ii - 1; k++){if (current[k] == current[k + 1]) {current[k] *= 2;score += current[k];current[k + 1] = 0;++k;--_count;}}ii = 0;for ( j = 0; j < MAX; j++){if (current[j])_matrix[ii++][i] = current[j]; }for (; ii < MAX; ii++)_matrix[ii][i] = 0;}random_data_one();}else{cout << "向上无效" << endl << endl; }}void _left(){cout << "按下了左键" << endl << endl;if (b_left()){cout << "可以向左合并" << endl << endl;for (int i = 0; i < MAX; i++){memset(current, 0, sizeof(int)*MAX);int ii = 0;for (int j = 0; j < MAX; j++){if (_matrix[i][j])current[ii++] = _matrix[i][j];}for (int k = 0; k < ii - 1; k++){if (current[k] == current[k + 1]){current[k] *= 2;score += current[k];current[k + 1] = 0;++k;--_count;}}ii = 0;for ( j = 0; j < MAX; j++){if (current[j])_matrix[i][ii++] = current[j];}for (; ii < MAX; ii++)_matrix[i][ii] = 0;}random_data_one();}else{cout << "向左无效" << endl << endl; }}void _down(){cout << "按下了下键" << endl << endl;if (b_down()){cout << "可以向下合并" << endl;for (int i = 0; i < MAX; i++){memset(current, 0, sizeof(int)*MAX);int ii = 0;for (int j = MAX - 1; j >= 0; j--){if (_matrix[j][i])current[ii++] = _matrix[j][i];}for (int k = 0; k < ii - 1; k++){if (current[k] == current[k + 1]){current[k] *= 2;score += current[k];current[k + 1] = 0;++k;--_count;}}ii = MAX - 1;for ( j = 0; j < MAX; j++){if (current[j])_matrix[ii--][i] = current[j];}for (; ii >= 0; ii--)_matrix[ii][i] = 0;}random_data_one();}else{cout << "向下无效" << endl << endl; }}void _right(){cout << "按下了右键" << endl << endl;if (b_right()){cout << "可以向右合并" << endl;for (int i = 0; i < MAX; i++){memset(current, 0, sizeof(int)*MAX);int ii = 0;for (int j = MAX - 1; j >= 0; j--){if (_matrix[i][j])current[ii++] = _matrix[i][j];}for (int k = 0; k < ii - 1; k++){if (current[k] == current[k + 1]){current[k] *= 2;score += current[k];current[k + 1] = 0;++k;--_count;}}ii = MAX - 1;for ( j = 0; j < MAX; j++){if (current[j])_matrix[i][ii--] = current[j];}for (; ii >= 0; ii--)_matrix[i][ii] = 0;}random_data_one();}else{cout << "向右无效" << endl << endl;}}//</memory></string></ctime></cstdlib></iomanip></iostream>。
C语言实现2048游戏代码
![C语言实现2048游戏代码](https://img.taocdn.com/s3/m/e11e1c21effdc8d376eeaeaad1f34693daef101e.png)
C语⾔实现2048游戏代码本⽂实例为⼤家分享了C语⾔实现2048游戏具体代码,供⼤家参考,具体内容如下效果图:使⽤⽂本界⾯的屏幕绘图库 ncurses.设计思路:在满⾜条件情况下消除⽅块允许在游戏主界⾯(16 宫格)中任意⼀格输出数据实现代码:#include <stdio.h>#include <stdlib.h>#include <curses.h>#include <unistd.h>#include <signal.h>#include <time.h>void draw(); // ⽤于绘制游戏界⾯void play(); // 游戏运⾏的逻辑主体void init(); // 初始化函数,⽤于完成⼀些必要的初始化操作void draw_one(int y, int x); // 绘制单个数字void cnt_value(int *new_y, int *new_x); //统计(y, x)对应的格⼦周围⼀圈的空格的个数int game_over(); // 结束游戏int cnt_one(int y, int x); //统计(y, x)对应的格⼦周围⼀圈的空格的个数// 游戏主界⾯是⼀个 4*4 的 16 宫格,使⽤⼆维数组进⾏表⽰,⽤ 0 表⽰空格int a[4][4] = { 0 };// 16 宫格中空格的个数int empty;int old_y, old_x;int main(){init();play();endwin();return 0;}void init(){int x, y;initscr(); //开启curses模式cbreak(); //开启cbreak模式,除 DELETE 或 CTRL 等仍被视为特殊控制字元外⼀切输⼊的字元将⽴刻被⼀⼀读取 noecho(); //echo() and noecho(): 此函式⽤来控制从键盘输⼊字元时是否将字元显⽰在终端机上curs_set(0); //设置光标模式empty = 15;srand(time(0));x = rand() % 4;y = rand() % 4;a[y][x] = 2;draw();}void draw(){int n, m, x, y;char c[4] = {'0', '0', '0', '0'};clear(); //清除终端屏幕{move(n, m);//将游标移动⾄ x,y 的位置 addch('-');//在当前位置画字符'-'refresh();//将做清除萤幕的⼯作}}for(m = 0; m < 22; m += 5){for(n = 1; n < 8; n++){move(n, m);addch('|');refresh();}}for(y = 0; y < 4; y++){for(x = 0; x < 4; x++){draw_one(y, x);}}}void draw_one(int y, int x){int i, m, k, j;char c[5] = {0x00};i = a[y][x];m = 0;while(i > 0){j = i % 10;c[m++] = j + '0';i = i / 10;}m = 0;k = (x + 1) * 5 - 1;while(c[m] != 0x00){move(2 * y + 1, k);addch(c[m++]);k--;}}void play(){int x, y, i, new_x, new_y, temp;int old_empty, move;char ch;while(1){move = 0;old_empty = empty;ch = getch();switch(ch){case 97: //左移 acase 104: // hcase 68: // 左移⽅向键for(y = 0; y < 4; y++)for(x = 0; x < 4; ){if(a[y][x] == 0){x++;for(i = x + 1; i < 4; i++){if(a[y][i] == 0){continue;}else{if(a[y][x] == a[y][i]){a[y][x] += a[y][i];a[y][i] = 0;empty++;break;}else{break;}}}x = i;}}for(y = 0; y < 4; y++)for(x = 0; x < 4; x++){if(a[y][x] == 0){continue;}else{for(i = x; (i > 0) && (a[y][i - 1] == 0); i--) {a[y][i - 1] = a[y][i];a[y][i] = 0;move = 1;}}}break;case 100: //右移 dcase 108: // lcase 67: //右移⽅向键for(y = 0; y < 4; y++)for(x = 3; x >= 0; ){if(a[y][x] == 0){x--;continue;}else{for(i = x - 1; i >= 0; i--){if(a[y][i] == 0){continue;}else if(a[y][x] == a[y][i]){a[y][x] += a[y][i];a[y][i] = 0;empty++;break;}else{break;}for(y = 0; y < 4; y++)for(x = 3; x >= 0; x--){if(a[y][x] == 0){continue;}else{for(i = x; (i < 3) && (a[y][i + 1] == 0); i++) {a[y][i + 1] = a[y][i];a[y][i] = 0;move = 1;}}}break;case 119: //上移 wcase 107: //kcase 65: //上移⽅向键for(x = 0; x < 4; x++)for(y = 0; y < 4; ){if(a[y][x] == 0){y++;continue;}else{for(i = y + 1; i < 4; i++){if(a[i][x] == 0){continue;}else if(a[y][x] == a[i][x]){a[y][x] += a[i][x];a[i][x] = 0;empty++;break;}else{break;}}y = i;}}for(x = 0; x < 4; x++)for(y = 0; y < 4; y++){if(a[y][x] == 0){continue;}else{for(i = y; (i > 0) && (a[i - 1][x] == 0); i--) {a[i - 1][x] = a[i][x];a[i][x] = 0;move = 1;}}}case 66: //下移⽅向键for(x = 0; x < 4; x++)for(y = 3; y >= 0; ){if(a[y][x] == 0){y--;continue;}else{for(i = y - 1; i >= 0; i--){if(a[i][x] == 0){continue;}else if(a[y][x] == a[i][x]){a[y][x] += a[i][x];a[i][x] = 0;empty++;break;}else{break;}}y = i;}}for(x = 0; x < 4; x++)for(y = 3; y >= 0; y--){if(a[y][x] == 0){continue;}else{for(i = y; (i < 3) && (a[i + 1][x] == 0); i++) {a[i + 1][x] = a[i][x];a[i][x] = 0;move = 1;}}}break;case 'Q':case 'q':game_over();break;default:continue;break;}if(empty <= 0)game_over();if((empty != old_empty) || (move == 1)){do{new_x = rand() % 4;new_y = rand() % 4;}while(a[new_y][new_x] != 0);cnt_value(&new_y, &new_x);{temp = rand() % 4;}while(temp == 0 || temp == 2);a[new_y][new_x] = temp + 1;empty--;}draw();}}int cnt_one(int y, int x){int value = 1;if(y - 1 > 0)a[y - 1][x] ? 0 : value++;if(y + 1 < 4)a[y + 1][x] ? 0 : value++;if(x - 1 >= 0)a[y][x - 1] ? 0 : value++;if(x + 1 < 4)a[y][x + 1] ? 0 : value++;if(y - 1 >= 0 && x - 1 >= 0)a[y - 1][x - 1] ? 0 : value++;if(y - 1 >= 0 && x + 1 < 4)a[y - 1][x + 1] ? 0 : value++;if(y + 1 < 4 && x - 1 >= 0)a[y + 1][x - 1] ? 0 : value++;if(y + 1 < 4 && x + 1 < 4)a[y + 1][x + 1] ? 0 : value++;return value;}void cnt_value(int *new_y, int *new_x){int max_x, max_y, x, y, value;int max = 0;max = cnt_one(*new_y, *new_x);for(y = 0; y < 4; y++)for(x = 0; x < 4; x++){if(!a[y][x]){value = cnt_one(y, x);if(value > max && old_y != y && old_x != x){*new_y = y;*new_x = x;old_x = x;old_y = y;break;}}}}int game_over(){sleep(1); //暂停1秒endwin(); //关闭curses并重置tty(结束curses编程时,最后调⽤的⼀个函数)exit(0);}以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
C语言2048源代码
![C语言2048源代码](https://img.taocdn.com/s3/m/c8cc37fa81c758f5f61f672c.png)
#include <stdio.h>#include <stdlib.h>#include <time.h>#include <conio.h>#define N 4void Generat_picture(int d[N][N],char c[N][N][N],int s); void Control_synthesis(int a[N][N]);void add_num(int a[N][N]);void swap(int *a,int *b);int score(int a[N][N]);int moveup(int a[N][N]);int movedown(int a[N][N]);int moveleft(int a[N][N]);int moveright(int a[N][N]);int main(){int d[N][N];char c[N][N][N];int i,j,s=2;system("COLOR 5F");for(i=0; i<N; i++)for(j=0; j<N; j++)d[i][j]=0;while(1){add_num(d);Generat_picture(d,c,s);Control_synthesis(d);s=score(d);}return 0;}void Generat_picture(int d[N][N],char c[N][N][N],int s) {int i,j,k;for(i=0; i<N; i++)for(j=0; j<N; j++){if(d[i][j]==0){c[i][j][0]=' ';c[i][j][1]=' ';c[i][j][2]=' ';c[i][j][3]=' ';}else{c[i][j][0]=(int)(d[i][j]/1000)%10+48; //4358c[i][j][1]=(int)(d[i][j]/100)%10+48;c[i][j][2]=(int)(d[i][j]/10)%10+48;c[i][j][3]=d[i][j]%10+48;for(k=0; k<N; k++)if(c[i][j][k]==48)c[i][j][k]=' ';}}system("cls");printf("得分:%d\n",s);printf("┏━━┳━━┳━━┳━━┓\n");printf("┃%c%c%c%c┃%c%c%c%c┃%c%c%c%c┃%c%c%c%c┃\n",c[0][0][0],c[0][0][1],c[0][0][2],c[0][0][3],c[0][1][0],c[0][1][1],c[0][1][2],c[0][1][3],c[0][2][0],c[0][2] [1],c[0][2][2],c[0][2][3],c[0][3][0],c[0][3][1],c[0][3][2],c[0][3][3]);printf("┣━━╋━━╋━━╋━━┫\n");printf("┃%c%c%c%c┃%c%c%c%c┃%c%c%c%c┃%c%c%c%c┃\n",c[1][0][0],c[1][0][1],c[1][0][2],c[1][0][3],c[1][1][0],c[1][1][1],c[1][1][2],c[1][1][3],c[1][2][0],c[1][2] [1],c[1][2][2],c[1][2][3],c[1][3][0],c[1][3][1],c[1][3][2],c[1][3][3]);printf("┣━━╋━━╋━━╋━━┫\n");printf("┃%c%c%c%c┃%c%c%c%c┃%c%c%c%c┃%c%c%c%c┃\n",c[2][0][0],c[2][0][1],c[2][0][2],c[2][0][3],c[2][1][0],c[2][1][1],c[2][1][2],c[2][1][3],c[2][2][0],c[2][2] [1],c[2][2][2],c[2][2][3],c[2][3][0],c[2][3][1],c[2][3][2],c[2][3][3]);printf("┣━━╋━━╋━━╋━━┫\n");printf("┃%c%c%c%c┃%c%c%c%c┃%c%c%c%c┃%c%c%c%c┃\n",c[3][0][0],c[3][0][1],c[3][0][2],c[3][0][3],c[3][1][0],c[3][1][1],c[3][1][2],c[3][1][3],c[3][2][0],c[3][2] [1],c[3][2][2],c[3][2][3],c[3][3][0],c[3][3][1],c[3][3][2],c[3][3][3]);printf("┗━━┻━━┻━━┻━━┛\n");printf("wsad控制方向\n");}void Control_synthesis(int a[N][N]){int c,flag;label:c=getch();if(c=='w'||c=='W')flag=moveup(a);else if(c=='s'||c=='S')flag=movedown(a);else if(c=='a'||c=='A')flag=moveleft(a);else if(c=='d'||c=='D')flag=moveright(a);else{printf("重新输入:\n");goto label;}if(flag==0)goto label;}void add_num(int a[N][N]){int i,j,k,t=0;int *p[16];srand((unsigned int)time(0));for(i=0; i<N; i++)for(j=0; j<N; j++)if(a[i][j]==0){p[t]=&a[i][j];t++;}k=rand()%t;*p[k]=2;}int moveup(int a[N][N]){int t,p=0,q=0;for(t=0; t<N; t++){if(a[0][t]==a[1][t]&&a[2][t]==a[3][t]&&a[0][t]!=0&&a[2][t]!=0) {a[1][t]*=2;a[3][t]*=2;a[0][t]=a[t][2]=0;}else if(a[0][t]==a[1][t]&&a[0][t]!=0){a[0][t]=0;a[1][t]*=2;}else if(a[0][t]==a[2][t]&&a[1][t]==0&&a[0][t]!=0){a[0][t]=0;a[2][t]*=2;}else if(a[1][t]==a[2][t]&&a[1][t]!=0){a[1][t]=0;a[2][t]*=2;}else if(a[0][t]==a[3][t]&&a[1][t]==0&&a[2][t]==0&&a[0][t]!=0) {a[0][t]=0;a[3][t]*=2;}else if(a[1][t]==a[3][t]&&a[2][t]==0&&a[1][t]!=0){a[1][t]=0;a[3][t]*=2;}else if(a[2][t]==a[3][t]&&a[2][t]!=0){a[2][t]=0;a[3][t]*=2;}elsep++;int n=3;while(n--){if(a[2][t]==0&&a[3][t]!=0){swap(&a[2][t],&a[3][t]);q++;}if(a[1][t]==0&&a[2][t]!=0){swap(&a[1][t],&a[2][t]);q++;}if(a[0][t]==0&&a[1][t]!=0){swap(&a[0][t],&a[1][t]);q++;}}}if(4==p&&q==0)return 0;elsereturn 1;}int movedown(int a[N][N]){int t,p=0,q=0;for(t=0; t<N; t++){if(a[0][t]==a[1][t]&&a[2][t]==a[3][t]&&a[0][t]!=0&&a[2][t]!=0) {a[1][t]*=2;a[3][t]*=2;a[0][t]=a[t][2]=0;}else if(a[2][t]==a[3][t]&&a[2][t]!=0){a[2][t]=0;a[3][t]*=2;}else if(a[1][t]==a[3][t]&&a[2][t]==0&&a[1][t]!=0){a[1][t]=0;a[3][t]*=2;}else if(a[0][t]==a[3][t]&&a[1][t]==0&&a[2][t]==0&&a[0][t]!=0) {a[0][t]=0;a[3][t]*=2;}else if(a[1][t]==a[2][t]&&a[1][t]!=0){a[1][t]=0;a[2][t]*=2;}else if(a[0][t]==a[2][t]&&a[1][t]==0&&a[0][t]!=0){a[0][t]=0;a[2][t]*=2;}else if(a[0][t]==a[1][t]&&a[0][t]!=0){a[0][t]=0;a[1][t]*=2;}elsep++;int n=3;while(n--){if(a[1][t]==0&&a[0][t]!=0){swap(&a[0][t],&a[1][t]);q++;}if(a[2][t]==0&&a[1][t]!=0){swap(&a[1][t],&a[2][t]);q++;}if(a[3][t]==0&&a[2][t]!=0){swap(&a[2][t],&a[3][t]);q++;}}}if(4==p&&q==0)return 0;elsereturn 1;}int moveleft(int a[N][N]){int t,p=0,q=0;for(t=0; t<N; t++){if(a[t][0]==a[t][1]&&a[t][2]==a[t][3]&&a[t][0]!=0&&a[t][1]!=0) {a[t][1]*=2;a[t][3]*=2;a[t][0]=a[t][2]=0;}else if(a[t][0]==a[t][1]&&a[t][0]!=0){a[t][0]=0;a[t][1]*=2;}else if(a[t][0]==a[t][2]&&a[t][1]==0&&a[t][0]!=0){a[t][0]=0;a[t][2]*=2;}else if(a[t][1]==a[t][2]&&a[t][1]!=0){a[t][1]=0;a[t][2]*=2;}else if(a[t][0]==a[t][3]&&a[t][1]==0&&a[t][2]==0&&a[t][0]!=0) {a[t][0]=0;a[t][3]*=2;}else if(a[t][1]==a[t][3]&&a[t][2]==0&&a[t][1]!=0){a[t][1]=0;a[t][3]*=2;}else if(a[t][2]==a[t][3]&&a[t][2]!=0){a[t][2]=0;a[t][3]*=2;}elsep++;int n=3;while(n--){if(a[t][2]==0&&a[t][3]!=0){swap(&a[t][2],&a[t][3]);q++;}if(a[t][1]==0&&a[t][2]!=0){swap(&a[t][1],&a[t][2]);q++;}if(a[t][0]==0&&a[t][1]!=0){swap(&a[t][0],&a[t][1]);q++;}}}if(4==p&&q==0)return 0;elsereturn 1;}int moveright(int a[N][N]){int t,p=0,q=0;for(t=0; t<N; t++){if(a[t][0]==a[t][1]&&a[t][2]==a[t][3]&&a[t][0]!=0&&a[t][1]!=0) {a[t][1]*=2;a[t][3]*=2;a[t][0]=a[t][2]=0;}else if(a[t][2]==a[t][3]&&a[t][2]!=0){a[t][2]=0;a[t][3]*=2;}else if(a[t][1]==a[t][3]&&a[t][2]==0&&a[t][1]!=0){a[t][1]=0;a[t][3]*=2;}else if(a[t][0]==a[t][3]&&a[t][1]==0&&a[t][2]==0&&a[t][0]!=0) {a[t][0]=0;a[t][3]*=2;}else if(a[t][1]==a[t][2]&&a[t][1]!=0){a[t][1]=0;a[t][2]*=2;}else if(a[t][0]==a[t][2]&&a[t][1]==0&&a[t][0]!=0){a[t][0]=0;a[t][2]*=2;}else if(a[t][0]==a[t][1]&&a[t][0]!=0) {a[t][0]=0;a[t][1]*=2;}elsep++;int n=3;while(n--){if(a[t][1]==0&&a[t][0]!=0){swap(&a[t][0],&a[t][1]);q++;}if(a[t][2]==0&&a[t][1]!=0){swap(&a[t][1],&a[t][2]);q++;}if(a[t][3]==0&&a[t][2]!=0){swap(&a[t][2],&a[t][3]);q++;}}}if(4==p&&q==0)return 0;elsereturn 1;}void swap(int *a,int *b){int t;t=*a;*a=*b;*b=t;}int score(int a[N][N]){int i,j,s=0;for(i=0; i<N; i++)for(j=0; j<N; j++) if(s<a[i][j]) s=a[i][j]; return s;}。
C语言实现简易2048小游戏
![C语言实现简易2048小游戏](https://img.taocdn.com/s3/m/5c0b7f1b0a4e767f5acfa1c7aa00b52acfc79c05.png)
C语⾔实现简易2048⼩游戏⼀直很喜欢玩这个⼩游戏,简单的游戏中包含运⽓与思考与策略,喜欢这种简约⼜不失内涵的游戏风格。
于是萌⽣了⽤C语⾔实现⼀下的想法。
具体代码是模仿这个:博主分析的都很到位,很多算法技巧都值得借鉴,C语⾔实现2048的主要思想已经在那个博客中详细的分析了,但是我觉得在博主的代码中还是有很多很好的思想是值得我借鉴学习的。
⽐如这个⽣成随机数,顺便规定随机数的概率:/* ⽣成随机数函数定义 */void add_rand_num(){srand(time(0));int n = rand() % get_null_count();/* 确定在何处空位置⽣成随机数 */for (int i = 0; i < 4; i++){for (int j = 0; j < 4; j++){if (board[i][j] == 0 && n-- == 0) /* 定位待⽣成的位置 */{board[i][j] = (rand() % 3 ? 2 : 4);/* 确定⽣成何值,设定⽣成2的概率是4的概率的两倍 */return;}}}}⾸先是 srand() 函数,他是⼀个随机数发⽣器的初始化函数。
原型为:void srand(unsigned seed)⽤法是:程序员需要为这个函数提供⼀个随机数的种⼦:srand(随机数),如果使⽤相同的种⼦,那么后⾯的rand()函数就会每次运⾏都是⽣成⼀样的随机数,即伪随机数。
如:srand(1),直接⽤1来初始化种⼦,后⾯都是⼀样的随机数。
为了⽣成真正的随机数,我们⼀般采⽤系统时间来作为随机数初始化函数的种⼦。
使⽤time()函数来获取系统时间:它的返回值为从 00:00:00 GMT, January 1, 1970 到现在所持续的秒数,然后将time_t型数据转化为(unsigned)型再传给srand函数,即:srand((unsigned) time(&t));还有⼀个经常⽤法,不需要定义 time_t 型 t 变量,即: srand((unsigned) time(NULL)); 直接传⼊⼀个空指针,因为你的程序中往往并不需要经过参数获得的 t 数据。
C语言代码实现简单2048游戏
![C语言代码实现简单2048游戏](https://img.taocdn.com/s3/m/51d257978662caaedd3383c4bb4cf7ec4afeb63d.png)
C语⾔代码实现简单2048游戏最近玩2048上瘾,于是尝试⽤C++写了⼀个2048⼩游戏操作⽅法很简单,通过wasd控制⽅块的⽅向,数据的上限为65536代码如下#include<bits/stdc++.h>#include<conio.h>#include <windows.h>void color(short x){if(x>=0 && x<=15)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);elseSetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);}using namespace std;int qp[4][4]={0};long long int gread=0;int pd(){int i,j;for(i=0;i<4;i++){for(j=0;j<4;j++){if(qp[i][j]==0){return 0;}if(i==0&&j==0){if(qp[i][j]==qp[i+1][j]||qp[i][j]==qp[i][j+1]){return 0;}}else if(i==0&&j==3){if(qp[i][j]==qp[i+1][j]||qp[i][j]==qp[i][j-1]){return 0;}}else if(i==0){if(qp[i][j]==qp[i+1][j]||qp[i][j]==qp[i][j+1]||qp[i][j]==qp[i][j-1]){return 0;}}else if(i==3&&j==0){if(qp[i][j]==qp[i][j+1]||qp[i][j]==qp[i-1][j]){return 0;}}else if(j==0){if(qp[i][j]==qp[i+1][j]||qp[i][j]==qp[i-1][j]||qp[i][j]==qp[i][j+1]){return 0;}}else if(i==3&&j==3){if(qp[i][j]==qp[i-1][j]||qp[i][j]==qp[i][j-1]){if(qp[i][j]==qp[i-1][j]||qp[i][j]==qp[i][j-1]||qp[i][j]==qp[i][j+1]) {return 0;}}else if(j==3){if(qp[i][j]==qp[i-1][j]||qp[i][j]==qp[i][j-1]||qp[i][j]==qp[i+1][j]) {return 0;}}}}return 1;}int sjs(){int num = rand() % 100 + 1;if(num<=5){return 4;}else{return 2;}}int sc(){for(;;){int n=rand()%4;int m=rand()%4;if(qp[n][m]==0){qp[n][m]=sjs();return 0;}}}void dy(int n){if(n==0){cout<<" ";}else if(n==2){color(7);cout<<" "<<n<<" ";color(7);}else if(n==4){color(14);cout<<" "<<n<<" ";color(7);}else if(n==8){color(6);cout<<" "<<n<<" ";color(7);}else if(n==16){color(12);cout<<" "<<n<<" ";cout<<" "<<n<<" ";color(7);}else if(n==64){color(13);cout<<" "<<n<<" ";color(7);}else if(n==128){color(5);cout<<" "<<n<<" ";color(7);}else if(n==256){color(9);cout<<" "<<n<<" ";color(7);}else if(n==512){color(3);cout<<" "<<n<<" ";color(7);}else if(n==1024){color(11);cout<<n<<" ";color(7);}else if(n==2048){color(10);cout<<n<<" ";color(7);}else if(n==4096){color(2);cout<<n<<" ";color(7);}else{color(15);cout<<n;color(7);}}int main(){srand(time(NULL));int i,j;cout<<"Game start!(输⼊w a s d进⾏控制)"<<endl; sc();sc();cout<<"-------------------------"<<endl;cout<<"|";dy(qp[0][0]);cout<<"|";dy(qp[0][1]);cout<<"|";dy(qp[0][2]);cout<<"|";dy(qp[0][3]);cout<<"|"<<endl;cout<<"-------------------------"<<endl;cout<<"|";dy(qp[1][3]);cout<<"|"<<endl;cout<<"-------------------------"<<endl; cout<<"|";dy(qp[2][0]);cout<<"|";dy(qp[2][1]);cout<<"|";dy(qp[2][2]);cout<<"|";dy(qp[2][3]);cout<<"|"<<endl;cout<<"-------------------------"<<endl; cout<<"|";dy(qp[3][0]);cout<<"|";dy(qp[3][1]);cout<<"|";dy(qp[3][2]);cout<<"|";dy(qp[3][3]);cout<<"|"<<endl;cout<<"-------------------------"<<endl; for(;;){char n;n=getch();if(n=='w'){int g=0;for(i=0;i<4;i++){for(j=1;j<4;j++){if(qp[j][i]!=0){int k=j;while(qp[k-1][i]==0&&k!=0){k--;}qp[k][i]=qp[j][i];if(k!=j){qp[j][i]=0;g=1;}}}if(qp[0][i]==qp[1][i]&&qp[0][i]!=0) {qp[0][i]=qp[0][i]*2;gread+=qp[0][i];qp[1][i]=qp[2][i];qp[2][i]=qp[3][i];qp[3][i]=0;g=1;}if(qp[1][i]==qp[2][i]&&qp[1][i]!=0) {qp[1][i]=qp[1][i]*2;gread+=qp[1][i];qp[2][i]=qp[3][i];qp[3][i]=0;g=1;}if(qp[2][i]==qp[3][i]&&qp[2][i]!=0) {qp[2][i]=qp[2][i]*2;{cout<<"换个⽅向试试~"<<endl; continue;}else{system("cls");}}else if(n=='d'){int g=0;for(i=0;i<4;i++){for(j=2;j>=0;j--){if(qp[i][j]!=0){int k=j;while(qp[i][k+1]==0&&k!=3){k++;}qp[i][k]=qp[i][j];if(k!=j){qp[i][j]=0;g=1;}}}if(qp[i][3]==qp[i][2]&&qp[i][3]!=0) {qp[i][3]=qp[i][3]*2;gread+=qp[i][3];qp[i][2]=qp[i][1];qp[i][1]=qp[i][0];qp[i][0]=0;g=1;}if(qp[i][2]==qp[i][1]&&qp[i][2]!=0) {qp[i][2]=qp[i][2]*2;gread+=qp[i][2];qp[i][1]=qp[i][0];qp[i][0]=0;g=1;}if(qp[i][1]==qp[i][0]&&qp[i][1]!=0) {qp[i][1]=qp[i][1]*2;gread+=qp[i][1];qp[i][0]=0;g=1;}}if(g==0){cout<<"换个⽅向试试~"<<endl; continue;}else{system("cls");}}else if(n=='s'){int g=0;for(i=0;i<4;i++)while(qp[k+1][i]==0&&k!=3){k++;}qp[k][i]=qp[j][i];if(k!=j){qp[j][i]=0;g=1;}}}if(qp[3][i]==qp[2][i]&&qp[3][i]!=0) {qp[3][i]=qp[3][i]*2;gread+=qp[3][i];qp[2][i]=qp[1][i];qp[1][i]=qp[0][i];qp[0][i]=0;g=1;}if(qp[2][i]==qp[1][i]&&qp[2][i]!=0) {qp[2][i]=qp[2][i]*2;gread+=qp[2][i];qp[1][i]=qp[0][i];qp[0][i]=0;g=1;}if(qp[1][i]==qp[0][i]&&qp[1][i]!=0) {qp[1][i]=qp[1][i]*2;gread+=qp[1][i];qp[0][i]=0;g=1;}}if(g==0){cout<<"换个⽅向试试~"<<endl; continue;}else{system("cls");}}else if(n=='a'){int g=0;for(i=0;i<4;i++){for(j=1;j<4;j++){if(qp[i][j]!=0){int k=j;while(qp[i][k-1]==0&&k!=0){k--;}qp[i][k]=qp[i][j];if(k!=j){qp[i][j]=0;g=1;}}}if(qp[i][0]==qp[i][1]&&qp[i][0]!=0)qp[i][2]=qp[i][3];qp[i][3]=0;g=1;}if(qp[i][1]==qp[i][2]&&qp[i][1]!=0){qp[i][1]=qp[i][1]*2;gread+=qp[i][1];qp[i][2]=qp[i][3];qp[i][3]=0;g=1;}if(qp[i][2]==qp[i][3]&&qp[i][2]!=0){qp[i][2]=qp[i][2]*2;gread+=qp[i][2];qp[i][3]=0;g=1;}}if(g==0){cout<<"换个⽅向试试~"<<endl;continue;}else{system("cls");}}else{cout<<"请输⼊w、a、s、d"<<endl; continue;}sc();cout<<"分数:"<<gread<<endl;cout<<"-------------------------"<<endl; cout<<"|";dy(qp[0][0]);cout<<"|";dy(qp[0][1]);cout<<"|";dy(qp[0][2]);cout<<"|";dy(qp[0][3]);cout<<"|"<<endl;cout<<"-------------------------"<<endl; cout<<"|";dy(qp[1][0]);cout<<"|";dy(qp[1][1]);cout<<"|";dy(qp[1][2]);cout<<"|";dy(qp[1][3]);cout<<"|"<<endl;cout<<"-------------------------"<<endl; cout<<"|";dy(qp[2][0]);cout<<"|";dy(qp[2][1]);cout<<"|";dy(qp[2][2]);cout<<"|";dy(qp[2][3]);cout<<"|"<<endl;cout<<"-------------------------"<<endl; cout<<"|";dy(qp[3][0]);cout<<"|";dy(qp[3][1]);cout<<"|"<<endl;cout<<"-------------------------"<<endl;if(pd()==1){break;}}cout<<"Game over~"<<endl;cout<<"请输⼊“quit”并回车退出游戏"<<endl;for(;;){char s[10000];cin>>s;if(strcmp(s,"quit")==0){break;}}return 0;}以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
2048游戏C语言源代码(后缀txt改成cpp可直接运行)
![2048游戏C语言源代码(后缀txt改成cpp可直接运行)](https://img.taocdn.com/s3/m/69894f3358fb770bf78a55da.png)
{
temp[j]=code[i][j];/*把一行数移到中间变量*/
}
temp[4]=0;
{
code[i][j]=2;/*随机选一个空格填上2或4*/
}
move++;/*增加次数*/
}
print();/*显示*/
input=getch();/*输入方向*/
{
for(j=0;j<=3;j++)
{
if(code[i][j]==0)
{
printf("| ");/*0显示空格*/
}
else
printf("Score:%d Move:%d\n",score,move);
printf("Made by Yanjisheng\n");
printf("|-------------------|\n");/*显示横向分隔线*/
for(i=0;i<=3;i++)
}
}
break;
case 'S':
case 's':/*下*/
for(j=0;j<=3;j++)
{
}while(code[i][j]!=0);
if(((unsigned)rand())%4==0)
{
code[i][j]=4;
}
else
{
int i,j;
// clrscr();/*清屏*/
游戏2048的c语言源程序
![游戏2048的c语言源程序](https://img.taocdn.com/s3/m/6fa08616ff00bed5b9f31d82.png)
//2048的源程序#include <stdio.h>#include <stdlib.h>#include <time.h>#include <conio.h>#include <windows.h>//预处理方块的个数#define height 15 //Height#define width 40 //Widthint board[4][4],temp[4],f,t = 0,r = 0;long long score = 0;char key;void xy(int x,int y); //坐标信息void draw(); //绘画棋格,信息栏void menu(); //主菜单void qaz_draw(); //绘画QAZvoid info(); //动态信息void keyboard(char key); //键盘动作void getchkey(); //获取键盘操作void random(int j); //随机数产生void print(); //打印结果void move(int k); //移动数字方块(算法核心)//void replay(int r); //失败与成功后的操作int lose(); //寻找失败信息int main(){system("mode con: cols=121 lines=33");qaz_draw();return 0;}void xy(int x,int y){COORD coord;coord.X = x;coord.Y = y;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord); }void draw(){short i;//画左右,中间竖五列方块for(i = 1;i <= height;i++){xy(0,i);printf("■");xy(width,i);printf("■");xy((width / 4),i);printf("■");xy((width / 2),i);printf("■");xy((width / 4 * 3),i);printf("■");}//画上下,中间横五行方块for(i = 0;i <= width + 1;i+= 2){xy(i,0);printf("■");xy(i,height + 1);printf("■");xy(i,((height + 1) / 4));printf("■");xy(i,((height + 1) / 2));printf("■");xy(i,((height + 1) / 4 * 3));printf("■");}//画信息栏两行方块for(i = 0;i <= 78;i+= 2){xy(i,19);printf("■");xy(i,30);printf("■");}//画信息栏两列方块for(i = 19;i < 30;i++){xy(0,i);printf("■");xy(78,i);printf("■");}}void qaz_draw(){system("color 2F");int i;xy(23,9); printf(" ■■■■■■■■■■■■■■■■■■\n"); xy(23,10);printf(" ■■■■■\n");xy(23,11);printf(" ■■■■■\n");xy(23,12);printf("■■■■■\n");xy(23,13);printf("■■■■■■\n");xy(23,14);printf("■■■■■■\n");xy(23,15);printf("■■■■■■\n");xy(23,16);printf("■■■■■■\n");xy(23,17);printf(" ■■■■■\n");xy(23,18);printf(" ■■■■■\n");xy(23,19);printf(" ■■■■■■■■■■\n");xy(23,20);printf(" ■■■■■■■■■■■■■\n");xy(54,25);printf("qaz与你同在");for(i = 24;i < 93;i+= 3){xy(i,23);printf("→_→");Sleep(55);if(i == 57){xy(57,26);printf("2048");}}xy(48,28);printf("Press Any Key To Continue");getch();menu();}void info(){xy(44,1); printf("2048");xy(44,3); printf("使用帮助:");xy(44,4); printf("WASD滑动数字");xy(44,5); printf("所有的数字方块都会往滑动的方向靠拢");xy(44,6); printf("系统会在随机空白地区出现2或4数字方块");xy(44,7); printf("相同数字的会相加");xy(44,8); printf("凑出2048即算成功");xy(44,9); printf("F1重新开始F2保存最高纪录与战局");xy(44,10); printf("ESC关闭");xy(44,12); printf("made by qaz");xy(44,13); printf("helper: zxy,sun");xy(5,22); printf("Score:");}void getchkey(){key = getch();keyboard(key);}void random(int j){int i,f1,f2,k;switch(j){case 1: srand((unsigned) time(NULL)); //初始状态下设置初始两个数for(i = 0; i <= 1;i++){f1 = rand() % 4; f2 = rand() % 4; k = rand() % 2 + 1;//#if board[f1][f2] = 2;//#endif xy(5,27);printf("行= %d 列= %d",f1 + 1,f2 + 1);if(board[f1][f2] == 0){if(k == 1){board[f1][f2] = 2; xy(5,26);printf("2");xy(5,27);printf("行= %d 列= %d",f1 + 1,f2 + 1);}if(k == 2){board[f1][f2] = 4; xy(5,26);printf("4");xy(5,27);printf("行= %d 列= %d",f1 + 1,f2 + 1);}}else i-= 1;}print();break;case 2: srand((unsigned) time(NULL)); //经过移动后产生数for(i = 0; i < 1; i++){f1 = rand() % 4; f2 = rand() % 4; k = rand() % 2 + 1;if(board[f1][f2] == 0){if(k == 1){board[f1][f2] = 2; xy(5,26);printf("2");xy(5,27);printf("行= %d 列= %d",f1 + 1,f2 + 1);}if(k == 2){board[f1][f2] = 4; xy(5,26);printf("4");xy(5,27);printf("行= %d 列= %d",f1 + 1,f2 + 1);}}else i-= 1;}print();break;}}void keyboard(char key){switch(key){case 59: for(int i = 0; i < 4; i++) for(int j = 0; j < 4; j++) board[i][j] = 0; menu();break; case 27: exit(0);case 119: t = 0;move(1);random(2); xy(5,24);printf("↑");getchkey();break; //UP case 115: t = 0;move(2);random(2); xy(5,24);printf("↓");getchkey();break; //DOWN case 97: t = 0;move(3);random(2); xy(5,24);printf("←");getchkey();break; //LEFT case 100: t = 0;move(4);random(2); xy(5,24);printf("→");getchkey();break; //RIGHT default: /*cls(1);*/xy(5,24);printf("?\7");getchkey();break;}}void print(){int i,j,f1,f2;for(i = 4,f1 = 0;i <= 34 && f1 < 4;i+= 10,f1++){ //f1为递增列for(j = 2,f2 = 0; j <= 18 && f2 < 4; j+= 4,f2++){ //f2为递增行if(board[f2][f1] != 0){xy(i,j); printf("%d ",board[f2][f1]);}else if(board[f2][f1] == 0){xy(i,j); printf(" ");}if(board[f2][f1] == 2048){xy(50,22); printf("You Win!");}}}xy(12,22);printf("%lld",score);}int lose(){int i,j;for(j = 0;j < 4;j++) //Ufor(i = 3;i > 0;i--){if(((board[i][j] != 0 && board[i - 1][j] == 0) || (board[i][j] == board[i - 1][j]))) r++; }for(j = 0;j < 4;j++) //Dfor(i = 0;i < 3;i++){if(((board[i][j] != 0 && board[i + 1][j] == 0) || (board[i][j] == board[i + 1][j]))) r++; }for(i = 0;i < 4;i++) //Lfor(j = 3;j > 0;j--){if(((board[i][j] != 0 && board[i][j - 1] == 0) || (board[i][j] == board[i][j - 1]))) r++; }for(i = 0;i < 4;i++) //Rfor(j = 0;j < 3;j++){if(((board[i][j] != 0 && board[i][j + 1] == 0) || (board[i][j] == board[i][j + 1]))) r++; }if(r == 0) return 1;r = 0;}void move(int k){int i,j;switch(k){case 1: //UPif(lose() == 1){xy(50,22); printf("You Lose!");}for(i = 0;i < 4;i++){for(j = 3;j > 0;j--){if((board[j][i] != 0 && board[j - 1][i] == 0) || (board[j][i] == board[j - 1][i]) && (board[j][i] != 0)){t++;break;}}}if(t == 0){getchkey();break;}//判断输入是否有效,否则返回for(f = 0;f < 4;f++) temp[f] = 0; //临时数组赋值为0for(i = 0;i < 4;i++){ //赋值for(j = 0,f = 0;j < 4;j++){if(board[j][i]){temp[f] = board[j][i];f++;}}for(j = 0,f = 0;j < 4;j++,f++) board[j][i] = temp[f]; //赋值回去for(f = 0;f < 4;f++)temp[f] = 0; //清空数组}for(j = 0;j < 4;j++){ //相加for(i = 0;i < 3;i++){if(board[i][j] == board[i + 1][j]){board[i][j] += board[i + 1][j];board[i + 1][j] = 0;score += board[i][j] * 2;}}}for(f = 0;f < 4;f++) temp[f] = 0; //临时数组赋值为0for(i = 0;i < 4;i++){ //赋值for(j = 0,f = 0;j < 4;j++){if(board[j][i]){temp[f] = board[j][i];f++;}}for(j = 0,f = 0;j < 4;j++,f++) board[j][i] = temp[f]; //赋值回去for(f = 0;f < 4;f++)temp[f] = 0; //清空数组if(lose() == 1){xy(50,22); printf("You Lose!");}}break;case 2://DOWNif(lose() == 1){xy(50,22); printf("You Lose!");}for(i = 0;i < 4;i++){for(j = 0;j < 3;j++){if((board[j][i] != 0 && board[j + 1][i] == 0) || (board[j][i] == board[j + 1][i]) && (board[j][i] != 0)){t++;break;}}}if(t == 0){ getchkey();break;} //判断是否有用for(f = 0;f < 4;f++) temp[f] = 0; //清空for(i = 0;i < 4;i++){for(j = 3,f = 3;j >= 0;j--){ //赋值if(board[j][i]){temp[f]=board[j][i];f--;}}for(j = 0,f = 0;j < 4;j++,f++) board[j][i] = temp[f]; //赋值回去for(f = 0;f < 4;f++) temp[f] = 0; //清空}for(i = 0;i < 4;i++){ //相加for(j = 3;j > 0;j--){if(board[j][i] == board[j - 1][i]){board[j][i] += board[j - 1][i];board[j - 1][i] = 0;score += board[j][i] * 2;}}}for(f = 0;f < 4;f++) temp[f] = 0; //清空for(i = 0;i < 4;i++){ //赋值for(j = 3,f = 3;j >= 0;j--){if(board[j][i]){temp[f]=board[j][i];f--;}}for(j = 0,f = 0;j < 4;j++,f++) board[j][i] = temp[f];for(f = 0;f < 4;f++) temp[f] = 0;if(lose() == 1){xy(50,22); printf("You Lose!");}}break;case 3://LEFTif(lose() == 1){xy(50,22); printf("You Lose!");}for(i = 0;i < 4;i++)for(j = 3;j > 0;j--){if((board[i][j] != 0 && board[i][j - 1] == 0) || (board[i][j] == board[i][j - 1]) && (board[i][j] != 0)){t++;break;}}if(t == 0) {getchkey();break;}for(f = 0;f < 4;f++)temp[f] = 0;for(i = 0;i < 4;i++){for(j = 0,f = 0;j < 4;j++){if(board[i][j]){temp[f] = board[i][j];f++;}}for(j = 0,f = 0;j < 4;j++,f++) board[i][j] = temp[f];for(f = 0;f < 4;f++)temp[f] = 0;}for(i = 0;i < 4;i++)for(j = 0;j < 3;j++){if(board[i][j] == board[i][j + 1]){board[i][j] += board[i][j + 1];score += board[i][j];board[i][j + 1] = 0;}}for(f = 0;f < 4;f++)temp[f] = 0;for(i = 0;i < 4;i++){for(j = 0,f = 0;j < 4;j++){if(board[i][j]){temp[f] = board[i][j];f++;}}for(j = 0,f = 0;j < 4;j++,f++)board[i][j] = temp[f];for(f = 0;f < 4;f++)temp[f] = 0;if(lose() == 1){xy(50,22); printf("You Lose!");}}break;case 4://RIGHTif(lose() == 1){xy(50,22); printf("You Lose!");}for(i = 0;i < 4;i++)for(j = 0;j < 3;j++){if((board[i][j] != 0 && board[i][j + 1] == 0) || (board[i][j] == board[i][j + 1]) && (board[i][j] != 0)){t++;break;}}if(t == 0){getchkey(); break;}for(f = 0;f < 4;f++)temp[f] = 0;for(i = 0;i < 4;i++){for(j = 3,f = 3;j >= 0;j--){if(board[i][j]){temp[f] = board[i][j];f--;}}for(j = 0,f = 0;j < 4;j++,f++)board[i][j] = temp[f]; for(f = 0;f < 4;f++)temp[f] = 0;}for(i = 0;i < 4;i++)for(j = 3;j > 0;j--){if(board[i][j] == board[i][j - 1]){board[i][j] += board[i][j - 1];score += board[i][j];board[i][j - 1] = 0;}}for(f = 0;f < 4;f++)temp[f] = 0;for(i = 0;i < 4;i++){for(j = 3,f = 3;j >= 0;j--){if(board[i][j]){temp[f] = board[i][j];f--;}}for(j = 0,f = 0;j < 4;j++,f++)board[i][j] = temp[f]; for(f = 0;f < 4;f++)temp[f] = 0;if(lose() == 1){xy(50,22); printf("You Lose!");}}break;}}void menu(){system("mode con: cols=81 lines=31");system("cls");info();draw();random(1); getchkey(); }。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
i=((unsigned)rand())%4;
j=((unsigned)rand())%4;
}while(code[i][j]!=0);
if(((unsigned)rand())%4==0)
}
temp[4]=0;
change=change+add();
for(i=0;i<=3;i++)
{
code[i][3-j]=temp[j];/*把处理好的中间变量移回来*/
}
}
break;
}
gameover=1;
char input;
srand((unsigned)time(NULL));/*设置随机数的起点*/
while(gameover==0)
{
if(change>=1)/*仅当数发生改变时添加新数*/
{
do
for(i=0;i<=3;i++)
for(j=0;j<=3;j++)
if(code[i][j]==0)
gameover=0;/*所有格子都填满则游戏结束*/
}
printf("Game over!\n");
}
print();/*显示*/
input=getch();/*输入方向*/
change=0;
switch(input)
{
case '0':/*退出*/
printf("Are you sure to exit?(y/n)");
}
temp[i-1]=temp[i-1]*2;
temp[i]=0;
}
}/*把两个相邻的相同的数加起来*/
do
{
for(i=0;i<=3;i++)
{
temp[i]=temp[i+1];
temp[i+1]=0;
}
}/*去掉中间的0*/
t++;
}while(t<=3);/*重复多次*/
for(i=1;i<=3;i++)
{
if(temp[i]==temp[i-1])
{
if(temp[i]!=0)
{
change=1;/*当两个非零相同的数相加时数组改变*/
score=score+temp[i];/*加分*/
printf("| ");/*0显示空格*/
}
else
{
printf("|%4d",code[i][j]);/*显示数字和分隔线*/
}
}
temp[4]=0;
change=change+add();
for(i=0;i<=3;i++)
{
code[i][j]=temp[i];/*把处理好的中间变量移回来*/
}
}
break;
case 'A':
case 'a':/*左*/
printf("|-------------------|\n");/*显示横向分隔线*/
for(i=0;i<=3;i++)
{
for(j=0;j<=3;j++)
{
if(code[i][j]==0)
{
0,0,0,0};/*游戏中的16个格子*/
int temp[5];/*中间变量*/
int move=0;/*移动次数*/
int score=0;/*分数*/
void print(void)/*显示游戏界面*/
{
int i,j;
clrscr();/*清屏*/
if(temp[i]==0)
{
temp[i]=temp[i+1];
temp[i+1]=0;
}
}/*去掉中间的0*/
t++;
input=getchar();
if(input=='y'||input=='Y')
exit(0);
break;
case 'W':
}while(t<=3);/*重复多次*/
return change;
}
int main(void)
{
int gameover=0;/*判断游戏是否结束,1结束,0继续*/
int i,j;
int change=1;/*判断格子中的数是否改变,0不变*/
code[3-i][j]=temp[i];/*把处理好的中间变量移回来*/
}
}
break;
case 'D':
case 'd':/*右*/
printf("2048\n");
printf("W--UP A--LEFT S--DOWN D--RIGHT 0--EXIT\n");
printf("Score:%d Move:%d\n",score,mov Yanjisheng\n");
for(i=0;i<=3;i++)
{
for(j=0;j<=3;j++)
{
temp[j]=code[i][j];/*把一行数移到中间变量*/
do
{
for(i=0;i<=3;i++)
{
if(temp[i]==0)
{
if(temp[i]!=temp[i+1])
change=1;/*当一个0后面不是0时数组改变*/
}
printf("|\n|-------------------|\n");/*显示横向分隔线*/
}
}
int add(void)/*对中间变量数组进行处理*/
{
int i;
int t=0;
int change=0;/*判断数组是否有改变,0不变,1变化*/
for(i=0;i<=3;i++)
{
for(j=0;j<=3;j++)
{
temp[j]=code[i][3-j];/*把一行数移到中间变量*/
{
code[i][j]=4;
}
else
{
code[i][j]=2;/*随机选一个空格填上2或4*/
}
move++;/*增加次数*/
for(j=0;j<=3;j++)
{
for(i=0;i<=3;i++)
{
temp[i]=code[3-i][j];/*把一列数移到中间变量*/
}
temp[4]=0;
change=change+add();
for(j=0;j<=3;j++)
{
case 'w':/*上*/
for(j=0;j<=3;j++)
{
for(i=0;i<=3;i++)
{
temp[i]=code[i][j];/*把一列数移到中间变量*/
code[i][j]=temp[j];/*把处理好的中间变量移回来*/
}
}
break;
case 'S':
case 's':/*下*/
/*2048*/
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
int code[4][4]={0,0,0,0,
0,0,0,0,
0,0,0,0,
getch();
return 0;
}
}
temp[4]=0;
change=change+add();
for(j=0;j<=3;j++)
{