C++五子棋代码(控制台应用)

按WSAD移动,按J键放棋子。(连成五子或六子可获胜)(控制台)

#include<iostream>
#include<string>
#include<csetjmp>
#include<stdlib.h>
#include<queue>
#include<Windows.h>
#include<conio.h>
using namespace std;
int main()
{
 while(true) {
 int player=1;
 printf("按WSAD移动\n");
 int qiPan[19][19];
 for(int h=0;h<19;h++)
 {
 for(int l=0;l<19;l++)
 {
  qiPan[h][l]=0;
 }
 }
 int qiPanTwo[19][19];
 for(int h=0;h<19;h++)
 {
 for(int l=0;l<19;l++)
 {
  qiPanTwo[h][l]=0;
 }
 }
 int X=9;
 int Y=9;   
 qiPan[9][9]=8;
 for(int h=0;h<19;h++)
 {
  for(int l=0;l<19;l++)
  {
  if(qiPan[h][l]==8)
  {
   cout<<"╋";
  }
  else if(qiPanTwo[h][l]==0)
  {
   cout<<"·";
  }
  else if(qiPanTwo[h][l]==1)
  {
   cout<<"○";
  }
  else if(qiPanTwo[h][l]==2)
  {
   cout<<"●";
  }
  }
  cout<<endl;
 }
 while(true)
 {
 char xx=getch(); //如果是VS,就要改成_getch()
 switch(xx)
 {
  case 'w':
  Y=Y-1;
  qiPan[Y][X]=8;
  qiPan[Y+1][X]=0;
  break;
  case 's':
  Y=Y+1;
  qiPan[Y][X]=8;
  qiPan[Y-1][X]=0;
  break;
  case 'a':
  X=X-1;
  qiPan[Y][X]=8;
  qiPan[Y][X+1]=0;
  break;
  case 'd':
  X=X+1;
  qiPan[Y][X]=8;
  qiPan[Y][X-1]=0;
  break;
  case 'j':
  if(player==1)
  {
   qiPanTwo[Y][X]=1;
   player=2; 
  }
  else if(player==2)
  {
   qiPanTwo[Y][X]=2;
   player=1;   
  }
  break;    
 }
 system("cls");
 printf("按WSAD移动\n");
 for(int h=0;h<19;h++)
 {
  for(int l=0;l<19;l++)
  {
 
  if(qiPan[h][l]==8)
  {
   cout<<"╋";
  }
  else if(qiPanTwo[h][l]==0)
  {
   cout<<"·";
  }
  else if(qiPanTwo[h][l]==1)
  {
   cout<<"○";
  }
  else if(qiPanTwo[h][l]==2)
  {
   cout<<"●";
  }
  }
  cout<<endl;
 }
 int shu=1,heng=1,pie=1,na=1;
 for(int i=1;i<=4;i++)
 {
  if(qiPanTwo[Y+i][X]==qiPanTwo[Y][X]&&qiPanTwo[Y][X]!=0)
  shu++;
  else
  break;
 }
 for(int i=1;i<=4;i++)
 {
  if(qiPanTwo[Y-i][X]==qiPanTwo[Y][X]&&qiPanTwo[Y][X]!=0)
  shu++;
  else
  break;
 }
 for(int i=1;i<=4;i++)
 {
  if(qiPanTwo[Y][X-i]==qiPanTwo[Y][X]&&qiPanTwo[Y][X]!=0)
  heng++;
  else
  break;
 }
 for(int i=1;i<=4;i++)
 {
  if(qiPanTwo[Y][X+i]==qiPanTwo[Y][X]&&qiPanTwo[Y][X]!=0)
  heng++;
  else
  break;
 }
 for(int i=1;i<=4;i++)
 {
  if(qiPanTwo[Y+i][X-i]==qiPanTwo[Y][X]&&qiPanTwo[Y][X]!=0)
  pie++;
  else
  break;
 }
 for(int i=1;i<=4;i++)
 {
  if(qiPanTwo[Y-i][X+i]==qiPanTwo[Y][X]&&qiPanTwo[Y][X]!=0)
  pie++;
  else
  break;
 }
 for(int i=1;i<=4;i++)
 {
  if(qiPanTwo[Y+i][X+i]==qiPanTwo[Y][X]&&qiPanTwo[Y][X]!=0)
  na++;
  else
  break;
 }
 for(int i=1;i<=4;i++)
 {
  if(qiPanTwo[Y-i][X-i]==qiPanTwo[Y][X]&&qiPanTwo[Y][X]!=0)
  na++;
  else
  break;
 }
 if(shu==5 or heng==5 or pie==5 or na==5 or shu==6 or heng==6 or pie==6 or na==6)
 {
  if(qiPanTwo[Y][X]==1)
  {
   cout<<"Black win!"<<'\n';
   printf("是否继续?(Y/N) ");
   char ss=getch();
   if(ss=='N' or ss=='n')
    printf("N"),exit(0);
   system("cls");
   break;
  }
  else
  {
   cout<<"White win!"<<'\n';
   printf("是否继续?(Y/N) ");
   char ss=getch();
   if(ss=='N' or ss=='n')
    printf("N"),exit(0);
   system("cls");
   break;
  }
 }
 }
 }
 exit(0);
}
01-30 09:17