当前位置:编程学习 > C/C++ >>

简化我的C语言五子棋代码

#include <conio.h>
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>


int ChessData[15][15] ={0};
int GuangbiaoData[2]={8,8};
int ChessStepData[255][2]={0};


void gotoxy(int x, int y) //指定y行,x列
{
 COORD c;
 c.X=x-1;
 c.Y=y-1;
 SetConsoleCursorPosition (GetStdHandle(STD_OUTPUT_HANDLE), c);
}

void GotoChess(int x,int y)
{
 x=3*x-2;y=2*y-1;
 gotoxy(x,y);
}

void Move(int MoveData) //输入参数为用户输入的方向(1表示上,2表示下,3表示左,4表示右)
{
 switch (MoveData)
 {
 case 1:GuangbiaoData[1]-=1;break;
 case 2:GuangbiaoData[1]+=1;break;
 case 3:GuangbiaoData[0]-=1;break;
 case 4:GuangbiaoData[0]+=1;break;
 default:printf("Move函数出错");
 }
 GotoChess(GuangbiaoData[0],GuangbiaoData[1]);
}

int Get(int *data) //该函数的功能是用户的按键,并转化为01234567(0表示输入错误,1表示上,2表示下,3表示左,4表示右,5表示落子,6表示悔棋,7表示退出。)
{                   //并返回输入的用户号码(共同键返回3),错误则返回0
 int temp;

B: temp=getch();
   if (temp==224)
   {
    temp=getch();
    switch (temp)
    {
    case 72:*data=1;break;
    case 80:*data=2;break;
       case 75:*data=3;break;
       case 77:*data=4;break;
       default:goto B;
    }
       return 2;
}
   else
   {
    switch (temp)
    {
    case 'w':
    case 'W':*data=1;return 1;break;
    case 's':
       case 'S':*data=2;return 1;break;
       case 'a':
       case 'A':*data=3;return 1;break;
       case 'd':
       case 'D':*data=4;return 1;break;
       case 13 :*data=5;return 2;break;
       case 32 :*data=5;return 1;break;
       case 8 :*data=6;break;
       case 27 :*data=7;break;
       default:*data=0 ;return 0;break;
    }
    return 3;
   }
}
void MoveToEnd()
{
 gotoxy(1,30);
}
int LogicBeOut(int a,int b)
{
 if (a==-1||a==15||b==-1||b==15) return 1;
 else return 0;
}
int win(int v)
{
 int i=1,j=1,a=0,b=0;
 while (ChessData[a=GuangbiaoData[0]-i-1][b=GuangbiaoData[1]-i-1]==v*2&&!LogicBeOut(a,b)) i++;
 while (ChessData[a=GuangbiaoData[0]+j-1][b=GuangbiaoData[1]+j-1]==v*2&&!LogicBeOut(a,b)) j++;
 if (i+j-1>=5) return 1;
 i=1,j=1,a=0,b=0;
 while (ChessData[a=GuangbiaoData[0]+i-1][b=GuangbiaoData[1]-i-1]==v*2&&!LogicBeOut(a,b)) i++;
 while (ChessData[a=GuangbiaoData[0]-j-1][b=GuangbiaoData[1]+j-1]==v*2&&!LogicBeOut(a,b)) j++;
 if (i+j-1>=5) return 1;
 i=1,j=1,a=0,b=0;
 while (ChessData[a=GuangbiaoData[0]-i-1][b=GuangbiaoData[1]-1]==v*2&&!LogicBeOut(a,b)) i++;
 while (ChessData[a=GuangbiaoData[0]+j-1][b=GuangbiaoData[1]-1]==v*2&&!LogicBeOut(a,b)) j++;
 if (i+j-1>=5) return 1;
 i=1,j=1,a=0,b=0;
 while (ChessData[a=GuangbiaoData[0]-1][b=GuangbiaoData[1]-i-1]==v*2&&!LogicBeOut(a,b)) i++;
 while (ChessData[a=GuangbiaoData[0]-1][b=GuangbiaoData[1]+j-1]==v*2&&!LogicBeOut(a,b)) j++;
 if (i+j-1>=5) return 1;
 return 0;
}

void NewShow() //新棋局的开始
{
 int i,j;
 for (i=0;i<15;i++)
  for (j=0;j<15;j++)
   ChessData[i][j]=0;
  system("cls");
  for (i=1;i<=29;i++)
  {
   for (j=1;j<=43;j++)
    if(i%2==1) printf("-");
    else if (j%3==1) printf("|");
    else printf(" ");
    printf("\n");
  }
  GuangbiaoData[0]=8;GuangbiaoData[1]=8;
  MoveToEnd();
  printf("现在请用户1下棋         \n");
  printf("用户1使用 w,s,a,d移动光标,空格键落子\n");
  printf("用户2使用各方向键移动光标,回车键落子\n");
  printf("按下Backspace键悔棋,按下esc返回主菜单\n");
  GotoChess(8,8);
}

int BeOut(int data)
{
 int Xiuzheng[2],New[2];
 switch (data)
 {
 case 1:Xiuzheng[0]=0;Xiuzheng[1]=-1 ;break;
 case 2:Xiuzheng[0]=0;Xiuzheng[1]=1;break;
 case 3:Xiuzheng[0]=-1;Xiuzheng[1]=0;break;
 case 4:Xiuzheng[0]=1;Xiuzheng[1]=0 ;break;
 case 5:Xiuzheng[0]=0;Xiuzheng[1]=0 ;break;
 default:printf("BeOut函数出错");
 }
 New[0]=GuangbiaoData[0]+Xiuzheng[0];
 New[1]=GuangbiaoData[1]+Xiuzheng[1];
 if (New[0]>15||New[0]<1||New[1]>15||New[1]<1) return 1;
 else return 0;
}

void UserChoose(int * choice)
{
 system("cls");
 printf("________________________________________________________________________________");
    printf("________________________________________________________________________________");
    printf("                                 $1.单人游戏                                    ");
    printf("                                  2.双人游戏                                    ");
    printf("                                  3.退出游戏                                    ");
    printf("                                  4.游戏帮助                                    ");
    printf("________________________________________________________________________________");
    printf("________________________________________________________________________________");
    printf("                                                                                ");
    printf("                                  开心五子棋                                    ");
    printf("                                                                                ");
    printf("                                 出品人:赵威                                 ");
    printf("                                 学号:04091120                                ");
    printf("                                                                    ");
    printf("                                                                                ");
    printf("                                                                                ");
    printf("                  温馨提示,游戏时请把窗口最大化,以达到最佳效果                ");
    printf("________________________________________________________________________________");
 int temp=0,i=0;
 do
 {
  if ((temp=getch())==224)
  {
   temp=getch();
   if (temp==72&&i!=0)
   {
    gotoxy(34,3+i);
    printf(" ");
    i--;
    gotoxy(34,3+i);
    printf("$");
    gotoxy(0,0);
   }
   else if(temp==80&&i!=3)
   {
    gotoxy(34,3+i);
    printf(" ");
    i++;
    gotoxy(34,3+i);
    printf("$");
    gotoxy(0,0);
   }  
  }
  else if (temp==13) {*choice=i+1;return;}
  else if (temp==27) {*choice=3;return;}
  else if (temp=='1'||temp=='2'||temp=='3'||temp=='4') {*choice=temp-48;return;}
 }while(1);
}
int CannotDo(int v1,int v2,int MoveData,int choice) //第一个输入值为按键的用户号,第二个是本应该按键的用户号,第三个为按下键的对应值,第四个键代表游戏模式。
{
 if (v1==3) return 0; //如果用户输入的为共用按键,则CannotDo为假

 else if (v1==0) return 1;//如果用户输入错误,则CannotDo为真
 else if (v1!=v2&&choice==2) return 1; //如果不该此用户输入,而用户进行了输入,则CannotDo为真

 if (BeOut(MoveData)) return 1; //如果移动出边界则CannotDo为真
 return 0;
}

int CannotLuozi() //判断是否可以落子。
{
 if (ChessData[GuangbiaoData[0]-1][GuangbiaoData[1]-1])
  return 1;
 else return 0;
}


int luozi(int v)    //玩家v落子。
{
 ChessData[GuangbiaoData[0]-1][GuangbiaoData[1]-1]+=v*2;
 if (v==1) printf("O");
 else if (v==2) printf("X");
 else printf("luozi函数出错");
 if (win(v)) {MoveToEnd();printf("玩家%d获得了胜利!         \n",v);for (int i=1;i<=240;i++) printf(" ");GotoChess(GuangbiaoData[0],GuangbiaoData[1]);getch();return 1;}
 MoveToEnd();
 printf("现在请用户%d下棋             ",v%2+1);
 GotoChess(GuangbiaoData[0],GuangbiaoData[1]);
 return 0;
}

void HuiQi(int step) //输入的是当前的要悔的棋是第几步
{
 GuangbiaoData[0]=ChessStepData[step-1][0];
 GuangbiaoData[1]=ChessStepData[step-1][1];
 ChessData[GuangbiaoData[0]-1][GuangbiaoData[1]-1]=0;

 GotoChess(GuangbiaoData[0],GuangbiaoData[1]);
 printf("-");
 MoveToEnd();
 printf("现在请用户%d下棋         ",(step+1)%2+1);
 GotoChess(GuangbiaoData[0],GuangbiaoData[1]);
}

int DataGetAndChoose(int choice)
{
 int MoveData=0,i=0,temp; //MoveData 0表示不可移动,1表示上,2表示下,3表示左,4表示右,5表示落子,6表示悔棋,7表示退出。
 while(1)
{
loop: while (temp=Get(&MoveData),CannotDo(temp,i%2+1,MoveData,choice));
   switch (MoveData)
   {
   case 1:
      case 2:
      case 3:
      case 4:Move(MoveData);break;
      case 7:return 0;
      case 6:
    if (i==0) {MoveToEnd();printf("现在无法悔棋                         ");GotoChess(GuangbiaoData[0],GuangbiaoData[1]);}
    else HuiQi(i--);
    break;
   case 5:
    if (CannotLuozi()) goto loop;
    if(luozi(i%2+1)) return 0;
    ChessStepData[i][0]=GuangbiaoData[0];
    ChessStepData[i][1]=GuangbiaoData[1];
    i++;
    break;
   default:printf("DataGetAndChoose函数出错");break;
   }
 }
 return 1; 
}

void ShowHelp()
{
 system("cls");
 printf("********************************************************************************");
    printf("********************************************************************************");
    printf("******          单人游戏供用户一个人自己与自己下棋研究棋局之用            ******");
    printf("******          双人游戏中,用户1使用wsad控制方向,按空格落子             ******");
    printf("******               用户2按方向键控制方向,回车键落子                    ******");
    printf("******                   游戏过程中按esc返回主菜单                        ******");
    printf("******                    游戏过程中退格键悔棋                            ******");
    printf("******        双人模式中某人下棋时,另一个用户无法控制光标与落子          ******");
    printf("******                                                                    ******");
    printf("******                             帮助                                   ******");
    printf("******                          按任意键返回                              ******");
    printf("********************************************************************************");
    printf("********************************************************************************");
    getch();
}


int main()
{
 int choice=0;
    system("color 2b");
choose: UserChoose(&choice);
  if (choice<1||choice>4) goto choose;
  if (choice==3) {gotoxy(1,18);
  printf("\n谢谢您的使用,再见        "); getch();return 0;}
  if (choice==4) {ShowHelp(); goto choose;}
  NewShow();
  DataGetAndChoose(choice);
  main();
  return 0;
}
这个是在VC6.0下运行的五子棋游戏,有没有高手帮我简化代码,实际上我用不到那么多功能,只要其中一个“单人游戏”的功能就好了,不需要“双人游戏”、“退出游戏”、“游戏帮助”等功能。谢谢!

补充:怎么没人来帮我回答问题呢……55555!
		
追问:我主要是只想要一个双人对弈的,不要跟计算机下的,能直接运行的源代码……你的是那一种吗?
答案:                                                               简化好了,请验收,,!

#include <conio.h>
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
int ChessData[15][15] ={0};
int GuangbiaoData[2]={8,8};
int ChessStepData[255][2]={0};
void gotoxy(int x, int y) //指定y行,x列
{
 COORD c;
 c.X=x-1;
 c.Y=y-1;
 SetConsoleCursorPosition (GetStdHandle(STD_OUTPUT_HANDLE), c);
}

void GotoChess(int x,int y)
{
 x=3*x-2;y=2*y-1;
 gotoxy(x,y);
}

void Move(int MoveData) //输入参数为用户输入的方向(1表示上,2表示下,3表示左,4表示右)
{
 switch (MoveData)
 {
 case 1:GuangbiaoData[1]-=1;break;
 case 2:GuangbiaoData[1]+=1;break;
 case 3:GuangbiaoData[0]-=1;break;
 case 4:GuangbiaoData[0]+=1;break;
 default:printf("Move函数出错");
 }
 GotoChess(GuangbiaoData[0],GuangbiaoData[1]);//指定GuangbiaoData[1]行,GuangbiaoData[0]列
}

int Get(int *data) //该函数的功能是定义键位,并转化为01234567(0表示输入错误,1表示上,2表示下,3表示左,4表示右,5表示落子)
{                   //并返回输入的用户号码(共同键返回3),错误则返回0
 int temp;

B: temp=getch();
    temp=getch();//getch()函数是接受键盘输入一个字符,但是不显示在屏幕上。
    switch (temp)
    {
    case 72:*data=1;break;
    case 80:*data=2;break;
       case 75:*data=3;break;
       case 77:*data=4;break;
    case 32:*data=5;break;
       default:goto B;
    }
       return 2;
}
void MoveToEnd()
{
 gotoxy(1,30);
}
int LogicBeOut(int a,int b)
{
 if (a==-1||a==15||b==-1||b==15) return 1;
 else return 0;
}
int win(int v)
{
 int i=1,j=1,a=0,b=0;
 while (ChessData[a=GuangbiaoData[0]-i-1][b=GuangbiaoData[1]-i-1]==v*2&&!LogicBeOut(a,b)) i++;
 while (ChessData[a=GuangbiaoData[0]+j-1][b=GuangbiaoData[1]+j-1]==v*2&&!LogicBeOut(a,b)) j++;
 if (i+j-1>=5) return 1;
 i=1,j=1,a=0,b=0;
 while (ChessData[a=GuangbiaoData[0]+i-1][b=GuangbiaoData[1]-i-1]==v*2&&!LogicBeOut(a,b)) i++;
 while (ChessData[a=GuangbiaoData[0]-j-1][b=GuangbiaoData[1]+j-1]==v*2&&!LogicBeOut(a,b)) j++;
 if (i+j-1>=5) return 1;
 i=1,j=1,a=0,b=0;
 while (ChessData[a=GuangbiaoData[0]-i-1][b=GuangbiaoData[1]-1]==v*2&&!LogicBeOut(a,b)) i++;
 while (ChessData[a=GuangbiaoData[0]+j-1][b=GuangbiaoData[1]-1]==v*2&&!LogicBeOut(a,b)) j++;
 if (i+j-1>=5) return 1;
 i=1,j=1,a=0,b=0;
 while (ChessData[a=GuangbiaoData[0]-1][b=GuangbiaoData[1]-i-1]==v*2&&!LogicBeOut(a,b)) i++;
 while (ChessData[a=GuangbiaoData[0]-1][b=GuangbiaoData[1]+j-1]==v*2&&!LogicBeOut(a,b)) j++;
 if (i+j-1>=5) return 1;
 return 0;
}

void NewShow() //新棋局的开始
{
 int i,j;
 for (i=0;i<15;i++)
  for (j=0;j<15;j++)
   ChessData[i][j]=0;
  system("cls");
  for (i=1;i<=29;i++)
  {
   for (j=1;j<=43;j++)
    if(i%2==1) printf("-");
    else if (j%3==1) printf("|");
    else printf(" ");
    printf("\n");
  }
  GuangbiaoData[0]=8;GuangbiaoData[1]=8;
  MoveToEnd();
  printf("现在请用户1下棋         \n");
  printf("用户1、2均使用 方向键 移动光标,按两次空格键落子\n");
  printf("请不要在游戏过程中使用规定以外的按键!\n");
  printf("游戏结束后按下任意键返回主菜单\n");
  GotoChess(8,8);
}

int BeOut(int data)
{
 int Xiuzheng[2],New[2];
 switch (data)
 {
 case 1:Xiuzheng[0]=0;Xiuzheng[1]=-1 ;break;
 case 2:Xiuzheng[0]=0;Xiuzheng[1]=1;break;
 case 3:Xiuzheng[0]=-1;Xiuzheng[1]=0;break;
 case 4:Xiuzheng[0]=1;Xiuzheng[1]=0 ;break;
 case 5:Xiuzheng[0]=0;Xiuzheng[1]=0 ;break;
 default:printf("BeOut函数出错");
 }
 New[0]=GuangbiaoData[0]+Xiuzheng[0];
 New[1]=GuangbiaoData[1]+Xiuzheng[1];
 if (New[0]>15||New[0]<1||New[1]>15||New[1]<1) return 1;
 else return 0;
}

void UserChoose(int * choice)
{
 system("cls");//执行DOS下的清屏命令
 printf("■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■");
    printf("·....·˙˙·...·˙·★·....·˙˙·...·˙·★·....·....·˙˙");
    printf("                                 $1.开始游戏                                    ");
    printf("                                  2.棋盘预览                                    ");
    printf("                                  3.退出游戏                                    ");
    printf("                                  4.游戏帮助                                    ");
    printf("________________________________________________________________________________");
    printf("______|__________|_________⑤___________孒____________棋__________|_____________");
    printf("                                                                                ");
    printf("&nb

上一个:谁能提供C语言详细内容
下一个:解三元一次方程的c语言程序

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,