使用C語言編寫一個2048游戲-創(chuàng)新互聯(lián)

這篇文章給大家介紹使用C語言編寫一個2048游戲,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

成都創(chuàng)新互聯(lián)公司堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站設(shè)計、成都網(wǎng)站設(shè)計、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的單縣網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

代碼如下


#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進行控制)"<<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<<"換個方向試試~"<<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++)
 {
 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<<"換個方向試試~"<<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<<"換個方向試試~"<<endl;
 continue;
 }
 else
 {
 system("cls");
 }
 }
 else
 {
 cout<<"請輸入w、a、s、d"<<endl; 
 continue;
 }
 sc();
 cout<<"分數(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<<"請輸入“quit”并回車退出游戲"<<endl;
 for(;;)
 {
 char s[10000];
 cin>>s;
 if(strcmp(s,"quit")==0)
 {
 break;
 }
 }
 return 0;
}

關(guān)于使用C語言編寫一個2048游戲就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

當(dāng)前標(biāo)題:使用C語言編寫一個2048游戲-創(chuàng)新互聯(lián)
轉(zhuǎn)載注明:http://muchs.cn/article12/cspgdc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、做網(wǎng)站微信公眾號、App設(shè)計、網(wǎng)頁設(shè)計公司企業(yè)網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作