#include<bits/stdc++.h>
#include<conio.h>
#include"./game_into.h"
#define Height 4
#define Width 4
#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77
#define SPACE 32
#define ESC 27
using namespace std;
struct pos{
int x,y;
friend bool operator<(const pos&_X,const pos&_Y){
return _X.x*Width+_X.y<_Y.x*Width+_Y.y;
}
};
map<pos,int>Map;
int remaining,grade,max_1,max_2;
int Color[14]={1,18,36,70,108,197,90,174,237,210,173,209,23,192};
void ReadGrade(){
FILE *pf=fopen("2048最高得分记录.txt","r");
if(pf==NULL){
pf=fopen("2048最高得分记录.txt", "w");
fwrite(&max_1,sizeof(int),1,pf);
}fseek(pf,0,SEEK_SET);
fread(&max_1,sizeof(int),1,pf);
fclose(pf);
pf=NULL;
return ;
}void WriteGrade(){
FILE* pf=fopen("2048最高得分记录.txt","w");
if(pf==NULL){
printf("保存最高得分记录失败\n");
exit(0);
}fwrite(&grade,sizeof(int),1,pf);
fclose(pf);
pf=NULL;
return ;
}
void DrawMap(){
for(int i=1;i<=Width;i++)for(int j=1;j<=Height;j++)if(Map[{i,j}]!=-1){
int x=Map[{i,j}];
int Mapij=pow(2,x);
color(Color[x]);
CursorJump(j*5-4,i*3-2);
cout<<" ";
CursorJump(j*5-4,i*3-1);
if(Mapij<10)cout<<" "<<Mapij<<" ";
else if(Mapij<100)cout<<" "<<Mapij<<' ';
else if(Mapij<1000)cout<<' '<<Mapij<<' ';
else cout<<' '<<Mapij;
CursorJump(j*5-4,i*3);
cout<<" ";
}else{
color(0);
CursorJump(j*5-4,i*3-2);
cout<<" ";
CursorJump(j*5-4,i*3-1);
cout<<" ";
CursorJump(j*5-4,i*3);
cout<<" ";
}return ;
}void AddMap(){
if(remaining!=0){
int number=max(0,rand()%4+max_2-6);
pos new_pos;
do{
new_pos.x=rand()%Height+1;
new_pos.y=rand()%Width+1;
}while(Map[new_pos]!=-1);
Map[new_pos]=number;
}DrawMap();
if(remaining!=0){
remaining--;
Sleep(300);
}return ;
}
void MoveNumber(int dir){
if(dir==1){
int T=Height-1;
while(T--){
bool flag=true;
for(int x=2;x<=Height;x++)for(int y=1;y<=Width;y++){
if(Map[{x-1,y}]==-1&&Map[{x,y}]!=-1)Map[{x-1,y}]=Map[{x,y}],Map[{x,y}]=-1,flag=false;
if(Map[{x-1,y}]==Map[{x,y}]&&Map[{x,y}]!=-1)Map[{x-1,y}]++,max_2=max(max_2,Map[{x-1,y}]),Map[{x,y}]=-1,remaining++,grade+=Map[{x-1,y}],flag=false;
}DrawMap();
Sleep(20);
if(flag)break;
}AddMap();
T=Height-1;
while(T--){
bool flag=true;
for(int x=2;x<=Height;x++)for(int y=1;y<=Width;y++){
if(Map[{x-1,y}]==-1&&Map[{x,y}]!=-1)Map[{x-1,y}]=Map[{x,y}],Map[{x,y}]=-1,flag=false;
if(Map[{x-1,y}]==Map[{x,y}]&&Map[{x,y}]!=-1)Map[{x-1,y}]++,max_2=max(max_2,Map[{x-1,y}]),Map[{x,y}]=-1,remaining++,grade+=Map[{x-1,y}],flag=false;
}DrawMap();
Sleep(20);
if(flag)break;
}
}else if(dir==2){
int T=Height-1;
while(T--){
bool flag=true;
for(int x=Height-1;x>=1;x--)for(int y=1;y<=Width;y++){
if(Map[{x+1,y}]==-1&&Map[{x,y}]!=-1)Map[{x+1,y}]=Map[{x,y}],Map[{x,y}]=-1,flag=false;
if(Map[{x+1,y}]==Map[{x,y}]&&Map[{x,y}]!=-1)Map[{x+1,y}]++,max_2=max(max_2,Map[{x+1,y}]),Map[{x,y}]=-1,remaining++,grade+=Map[{x+1,y}],flag=false;
}DrawMap();
Sleep(20);
if(flag)break;
}AddMap();
T=Height-1;
while(T--){
bool flag=true;
for(int x=Height-1;x>=1;x--)for(int y=1;y<=Width;y++){
if(Map[{x+1,y}]==-1&&Map[{x,y}]!=-1)Map[{x+1,y}]=Map[{x,y}],Map[{x,y}]=-1,flag=false;
if(Map[{x+1,y}]==Map[{x,y}]&&Map[{x,y}]!=-1)Map[{x+1,y}]++,max_2=max(max_2,Map[{x+1,y}]),Map[{x,y}]=-1,remaining++,grade+=Map[{x+1,y}],flag=false;
}DrawMap();
Sleep(20);
if(flag)break;
}
}else if(dir==3){
int T=Width-1;
while(T--){
bool flag=true;
for(int x=1;x<=Height;x++)for(int y=2;y<=Width;y++){
if(Map[{x,y-1}]==-1&&Map[{x,y}]!=-1)Map[{x,y-1}]=Map[{x,y}],Map[{x,y}]=-1,flag=false;
if(Map[{x,y-1}]==Map[{x,y}]&&Map[{x,y}]!=-1)Map[{x,y-1}]++,max_2=max(max_2,Map[{x,y-1}]),Map[{x,y}]=-1,remaining++,grade+=Map[{x,y-1}],flag=false;
}DrawMap();
Sleep(20);
if(flag)break;
}AddMap();
T=Width-1;
while(T--){
bool flag=true;
for(int x=1;x<=Height;x++)for(int y=2;y<=Width;y++){
if(Map[{x,y-1}]==-1&&Map[{x,y}]!=-1)Map[{x,y-1}]=Map[{x,y}],Map[{x,y}]=-1,flag=false;
if(Map[{x,y-1}]==Map[{x,y}]&&Map[{x,y}]!=-1)Map[{x,y-1}]++,max_2=max(max_2,Map[{x,y-1}]),Map[{x,y}]=-1,remaining++,grade+=Map[{x,y-1}],flag=false;
}DrawMap();
Sleep(20);
if(flag)break;
}
}else if(dir==4){
int T=Width-1;
while(T--){
bool flag=true;
for(int x=1;x<=Height;x++)for(int y=Width-1;y>=1;y--){
if(Map[{x,y+1}]==-1&&Map[{x,y}]!=-1)Map[{x,y+1}]=Map[{x,y}],Map[{x,y}]=-1,flag=false;
if(Map[{x,y+1}]==Map[{x,y}]&&Map[{x,y}]!=-1)Map[{x,y+1}]++,max_2=max(max_2,Map[{x,y+1}]),Map[{x,y}]=-1,remaining++,grade+=Map[{x,y+1}],flag=false;
}DrawMap();
Sleep(20);
if(flag)break;
}AddMap();
T=Width-1;
while(T--){
bool flag=true;
for(int x=1;x<=Height;x++)for(int y=Width-1;y>=1;y--){
if(Map[{x,y+1}]==-1&&Map[{x,y}]!=-1)Map[{x,y+1}]=Map[{x,y}],Map[{x,y}]=-1,flag=false;
if(Map[{x,y+1}]==Map[{x,y}]&&Map[{x,y}]!=-1)Map[{x,y+1}]++,max_2=max(max_2,Map[{x,y+1}]),Map[{x,y}]=-1,remaining++,grade+=Map[{x,y+1}],flag=false;
}DrawMap();
Sleep(20);
if(flag)break;
}
}CursorJump(Width*5*1.5,Height);
return ;
}
bool judge_Over(){
if(remaining>0)return false;
for(int x=2;x<=Height;x++)for(int y=1;y<=Width;y++)if(Map[{x-1,y}]==Map[{x,y}]&&Map[{x,y}]!=-1)return false;
for(int x=1;x<=Height;x++)for(int y=2;y<=Width;y++)if(Map[{x,y-1}]==Map[{x,y}]&&Map[{x,y}]!=-1)return false;
return true;
}
int main();
void game(){
char n;
while(true){
color(7);
CursorJump(2,17);
cout<<"本次得分:"<<grade<<" 历史最高分:"<<max_1;
if(judge_Over()){
color(7);
system("cls");
if(grade>max_1){
CursorJump(2,0);
cout<<"恭喜打破最高分记录!";
CursorJump(2,1);
cout<<"最高分已更新为"<<grade;
WriteGrade();
}CursorJump(10,2);
cout<<"游戏";
CursorJump(10,3);
cout<<"失败";
CursorJump(2,5);
cout<<"您是否再玩一局?";
CursorJump(2,6);
cout<<"(y是n否)";
char ch;
cin>>ch;
while(ch!='y'&&ch!='n'){
CursorJump(2,7);
cout<<"请输入正确";
CursorJump(10,6);
cin>>ch;
}if(ch=='y')main();
else exit(0);
}n=getch();
switch(n){
case 'W':
case 'w':
case UP:
MoveNumber(1);
break;
case 'S':
case 's':
case DOWN:
MoveNumber(2);
break;
case 'A':
case 'a':
case LEFT:
MoveNumber(3);
break;
case 'D':
case 'd':
case RIGHT:
MoveNumber(4);
break;
case ESC:
color(7);
system("cls");
CursorJump(10,2);
cout<<"游戏";
CursorJump(10,3);
cout<<"结束";
CursorJump(2,5);
cout<<"您是否再玩一局?";
CursorJump(2,6);
cout<<"(y是n否)";
char ch;
cin>>ch;
while(ch!='y'&&ch!='n'){
CursorJump(2,7);
cout<<"请输入正确";
CursorJump(10,6);
cin>>ch;
}if(ch=='y')main();
else exit(0);
break;
case 'r':
case 'R':
main();
break;
}
}
return ;
}
int main(){
system("title 2048小游戏");
system("mode con cols=22 lines=20");
WriteWall(Height,Width,5,3);
srand((unsigned int)time(NULL));
ReadGrade();
remaining=Height*Width,max_2=grade=0;
for(int i=1;i<=Height;i++)for(int j=1;j<=Width;j++)Map[{i,j}]=-1;
AddMap();
game();
return 0;
}
这头文件game_into.h请查看文章游戏程序头文件。
更新日志:
2025/8/8 9:40 更新更新日志
2025/8/8 9:43 加入分数系统
8万+

被折叠的 条评论
为什么被折叠?



