C語(yǔ)言代碼實(shí)現(xiàn)簡(jiǎn)單2048游戲
更新時(shí)間:2020年12月30日 18:11:55 作者:ylszzz
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)2048游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
最近玩2048上癮,于是嘗試用C++寫(xiě)了一個(gè)2048小游戲
操作方法很簡(jiǎn)單,通過(guò)wasd控制方塊的方向,數(shù)據(jù)的上限為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);
else
SetConsoleTextAttribute(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])
{
return 0;
}
}
else if(i==3)
{
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<<" ";
color(7);
}
else if(n==32)
{
color(4);
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進(jìn)行控制)"<<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][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<<"|";
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;
gread+=qp[2][i];
qp[3][i]=0;
g=1;
}
}
if(g==0)
{
cout<<"換個(gè)方向試試~"<<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<<"換個(gè)方向試試~"<<endl;
continue;
}
else
{
system("cls");
}
}
else if(n=='s')
{
int g=0;
for(i=0;i<4;i++)
{
for(j=3;j>=0;j--)
{
if(qp[j][i]!=0)
{
int k=j;
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<<"換個(gè)方向試試~"<<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][0]=qp[i][0]*2;
gread+=qp[i][0];
qp[i][1]=qp[i][2];
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<<"換個(gè)方向試試~"<<endl;
continue;
}
else
{
system("cls");
}
}
else
{
cout<<"請(qǐng)輸入w、a、s、d"<<endl;
continue;
}
sc();
cout<<"分?jǐn)?shù):"<<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<<"|";
dy(qp[3][2]);
cout<<"|";
dy(qp[3][3]);
cout<<"|"<<endl;
cout<<"-------------------------"<<endl;
if(pd()==1)
{
break;
}
}
cout<<"Game over~"<<endl;
cout<<"請(qǐng)輸入“quit”并回車(chē)退出游戲"<<endl;
for(;;)
{
char s[10000];
cin>>s;
if(strcmp(s,"quit")==0)
{
break;
}
}
return 0;
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
json格式解析和libjson的用法介紹(關(guān)于cjson的使用方法)
下面小編就為大家?guī)?lái)一篇json格式解析和libjson的用法介紹(關(guān)于cjson的使用方法)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12
C++與namespace有關(guān)的兩個(gè)編譯錯(cuò)誤的講解
今天小編就為大家分享一篇關(guān)于C++與namespace有關(guān)的兩個(gè)編譯錯(cuò)誤的講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-04-04
C++實(shí)現(xiàn)圖書(shū)管理系統(tǒng)源碼
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)圖書(shū)管理系統(tǒng)源碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
vscode搭建STM32開(kāi)發(fā)環(huán)境的詳細(xì)過(guò)程
這篇文章主要介紹了vscode搭建STM32開(kāi)發(fā)環(huán)境的詳細(xì)過(guò)程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-05-05

