欢迎来到天天文库
浏览记录
ID:49765733
大小:41.50 KB
页数:8页
时间:2020-03-04
《C++实现2048小游戏(控制台版的).doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、C++实现2048小游戏(控制台版的)#include#include#includeusingnamespacestd;intconstROW=4;intconstCOL=4;intgame[ROW][COL]={0};//上下左右intconstUP=1;intconstDOWN=2;intconstLEFT=3;intconstRIGHT=4;//游戏所处的状态intconstGAME_OVER=1;intconstGAME_WIN=2;intconstGAME_CONTI
2、NUE=3;enumGameNum{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,};//打印所得的数组voidPrint(){system("cls");cout<<"*****************2048控制台版******************"<3、nzf(Intern)******************"<4、t";}else{cout<<"5、"<6、"<7、lCreateNumber(){intx=-1;inty=-1;inttimes=0;intmaxTimes=ROW*COL;//三分之二的概率生成2,三分之一的概率生成4intwhitch=rand()%3;do{x=rand()%ROW;y=rand()%COL;++times;}while(game[x][y]!=0&×<=maxTimes);//说明格子已经满了if(times>=maxTimes){returnfalse;}else{GameNumnum;if(whitch==0){num=Game_4;}else8、if(whitch){num=Game_2;}game[x][y]=num;}returntrue;}voidProcess(intdirection){switch(direction){caseUP://最上面一行不动for(introw=1;row=1;--crow){for(intcol=0;col9、e[crow][col]=0;}else{//合并if(game[crow-1][col]==game[crow][col]){game[crow-1][col]*=2;game[crow][col]=0;}}}}}break;caseDOWN://最下面一行不动for(introw=ROW-2;row>=0;--row){for(intcrow=row;crow10、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;caseLEFT://最左边一列不动for(intcol=1;col=1;--ccol){for(introw=0;row11、col-1]==0){game[row][ccol-1]=game[row][ccol];game[row][ccol]=0;}else{//合并if(game[row][ccol-1]==game[row][ccol]
3、nzf(Intern)******************"<4、t";}else{cout<<"5、"<6、"<7、lCreateNumber(){intx=-1;inty=-1;inttimes=0;intmaxTimes=ROW*COL;//三分之二的概率生成2,三分之一的概率生成4intwhitch=rand()%3;do{x=rand()%ROW;y=rand()%COL;++times;}while(game[x][y]!=0&×<=maxTimes);//说明格子已经满了if(times>=maxTimes){returnfalse;}else{GameNumnum;if(whitch==0){num=Game_4;}else8、if(whitch){num=Game_2;}game[x][y]=num;}returntrue;}voidProcess(intdirection){switch(direction){caseUP://最上面一行不动for(introw=1;row=1;--crow){for(intcol=0;col9、e[crow][col]=0;}else{//合并if(game[crow-1][col]==game[crow][col]){game[crow-1][col]*=2;game[crow][col]=0;}}}}}break;caseDOWN://最下面一行不动for(introw=ROW-2;row>=0;--row){for(intcrow=row;crow10、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;caseLEFT://最左边一列不动for(intcol=1;col=1;--ccol){for(introw=0;row11、col-1]==0){game[row][ccol-1]=game[row][ccol];game[row][ccol]=0;}else{//合并if(game[row][ccol-1]==game[row][ccol]
4、t";}else{cout<<"
5、"<6、"<7、lCreateNumber(){intx=-1;inty=-1;inttimes=0;intmaxTimes=ROW*COL;//三分之二的概率生成2,三分之一的概率生成4intwhitch=rand()%3;do{x=rand()%ROW;y=rand()%COL;++times;}while(game[x][y]!=0&×<=maxTimes);//说明格子已经满了if(times>=maxTimes){returnfalse;}else{GameNumnum;if(whitch==0){num=Game_4;}else8、if(whitch){num=Game_2;}game[x][y]=num;}returntrue;}voidProcess(intdirection){switch(direction){caseUP://最上面一行不动for(introw=1;row=1;--crow){for(intcol=0;col9、e[crow][col]=0;}else{//合并if(game[crow-1][col]==game[crow][col]){game[crow-1][col]*=2;game[crow][col]=0;}}}}}break;caseDOWN://最下面一行不动for(introw=ROW-2;row>=0;--row){for(intcrow=row;crow10、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;caseLEFT://最左边一列不动for(intcol=1;col=1;--ccol){for(introw=0;row11、col-1]==0){game[row][ccol-1]=game[row][ccol];game[row][ccol]=0;}else{//合并if(game[row][ccol-1]==game[row][ccol]
6、"<7、lCreateNumber(){intx=-1;inty=-1;inttimes=0;intmaxTimes=ROW*COL;//三分之二的概率生成2,三分之一的概率生成4intwhitch=rand()%3;do{x=rand()%ROW;y=rand()%COL;++times;}while(game[x][y]!=0&×<=maxTimes);//说明格子已经满了if(times>=maxTimes){returnfalse;}else{GameNumnum;if(whitch==0){num=Game_4;}else8、if(whitch){num=Game_2;}game[x][y]=num;}returntrue;}voidProcess(intdirection){switch(direction){caseUP://最上面一行不动for(introw=1;row=1;--crow){for(intcol=0;col9、e[crow][col]=0;}else{//合并if(game[crow-1][col]==game[crow][col]){game[crow-1][col]*=2;game[crow][col]=0;}}}}}break;caseDOWN://最下面一行不动for(introw=ROW-2;row>=0;--row){for(intcrow=row;crow10、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;caseLEFT://最左边一列不动for(intcol=1;col=1;--ccol){for(introw=0;row11、col-1]==0){game[row][ccol-1]=game[row][ccol];game[row][ccol]=0;}else{//合并if(game[row][ccol-1]==game[row][ccol]
7、lCreateNumber(){intx=-1;inty=-1;inttimes=0;intmaxTimes=ROW*COL;//三分之二的概率生成2,三分之一的概率生成4intwhitch=rand()%3;do{x=rand()%ROW;y=rand()%COL;++times;}while(game[x][y]!=0&×<=maxTimes);//说明格子已经满了if(times>=maxTimes){returnfalse;}else{GameNumnum;if(whitch==0){num=Game_4;}else
8、if(whitch){num=Game_2;}game[x][y]=num;}returntrue;}voidProcess(intdirection){switch(direction){caseUP://最上面一行不动for(introw=1;row=1;--crow){for(intcol=0;col9、e[crow][col]=0;}else{//合并if(game[crow-1][col]==game[crow][col]){game[crow-1][col]*=2;game[crow][col]=0;}}}}}break;caseDOWN://最下面一行不动for(introw=ROW-2;row>=0;--row){for(intcrow=row;crow10、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;caseLEFT://最左边一列不动for(intcol=1;col=1;--ccol){for(introw=0;row11、col-1]==0){game[row][ccol-1]=game[row][ccol];game[row][ccol]=0;}else{//合并if(game[row][ccol-1]==game[row][ccol]
9、e[crow][col]=0;}else{//合并if(game[crow-1][col]==game[crow][col]){game[crow-1][col]*=2;game[crow][col]=0;}}}}}break;caseDOWN://最下面一行不动for(introw=ROW-2;row>=0;--row){for(intcrow=row;crow10、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;caseLEFT://最左边一列不动for(intcol=1;col=1;--ccol){for(introw=0;row11、col-1]==0){game[row][ccol-1]=game[row][ccol];game[row][ccol]=0;}else{//合并if(game[row][ccol-1]==game[row][ccol]
10、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;caseLEFT://最左边一列不动for(intcol=1;col
11、col-1]==0){game[row][ccol-1]=game[row][ccol];game[row][ccol]=0;}else{//合并if(game[row][ccol-1]==game[row][ccol]
此文档下载收益归作者所有