c/c++语言开发共享C语言实现五子棋对战系统

本文实例为大家分享了c语言实现五子棋对战的具体代码,供大家参考,具体内容如下一直以来,有不少热爱并希望学习五子棋的人,或者仅为了娱乐来下五子棋的人,他们一般通过下棋对战来增加自己的对战经验,而在现实生

c/c++开发分享C语言实现五子棋对战系统实例为大家分享了c语言实现五子棋对战的具体代码,供大家参考,具体内容如下

一直以来,有不少热爱并希望学习五子棋的人,或者仅为了娱乐来下五子棋的人,他们一般通过下棋对战来增加自己的对战经验,而在现实生活由于五子棋布板麻烦,经常缺少能下棋的环境,并且下棋时效率较低,记录步数也较为麻烦。利用计算机来模拟下五子棋环境,只要有计算机,就可以很方便的随时随地进行下棋,并且对战过程中对步数和下子过程进行记录,方便了喜欢下五子棋的人,让他们的五子棋学习更加高效或者娱乐起来更加方便。

#include <stdio.h>  #include <stdlib.h>  #include <string.h>  #include <windows.h>  #include <conio.h>     char draw[32][60] = {{" "},      {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"},      {" "},      {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"},      {" "},      {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"},      {" "},      {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"},      {" "},      {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"},      {" "},      {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"},      {" "},      {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"},      {" "},      {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"},      {" "},      {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"},      {" "},      {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"},      {" "},      {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"},      {" "},      {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"},      {" "},      {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"},      {" "},      {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"},      {" "},      {"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]"},  };     typedef struct//坐标结构体  {      int x;      int y;  } node;     typedef struct//栈结构体  {      node data[120];      int top;  } stack;     int win[3], player, step1, step2;  stack player1,player2;     char ch;  node pos;     int convy(int y);  int convx(int x);  void refreshdraw();  void blue();  void red();  void green();  void white();  void printmap();  void printmenu();  void refreshmap();  void playeroperation(char cheese);  void jundge(int p, char cheese);  void in(char cheese);  void playervsplayer();  void playervscomputer();  void gotoxy(int x, int y);  void printstep(int player, int step);  void askexit();  void printpos(int player, int x, int y);  void printroad();     int main()  {      char num[99];      printmenu();      while(1)      {          white();          refreshdraw();          pos.x = 0;          pos.y = 0;          win[1] = 0;          win[2] = 0;          gets(num);          if(strcmp(num, "1") == 0)          {              playervsplayer();              system("cls");              printmenu();          }          else if(strcmp(num, "2") == 0)          {              askexit();              continue;          }          else          {              printf("请输入正确数字!n");          }      }         return 0;  }     void blue()//字体变蓝  {      setconsoletextattribute(getstdhandle(std_output_handle),foreground_blue|foreground_intensity);  }     void red()//字体变红  {      setconsoletextattribute(getstdhandle(std_output_handle),foreground_red|foreground_intensity);  }     void green()//字体变绿  {      setconsoletextattribute(getstdhandle(std_output_handle),foreground_green|foreground_intensity);  }     void white()//字体变白  {      setconsoletextattribute(getstdhandle(std_output_handle),foreground_green|foreground_red|foreground_blue);  }     void printmap()//打印棋盘和棋子  {      for(int i = 0; i <= 29; i++)      {          for(int j = 0; j <= 59; j++)          {              if(i==convx(pos.x) && (j==convy(pos.y)-1 || j==convy(pos.y)+1) )              {                  if(player == 1)                  {                      green();                  }                  else if(player == 2)                  {                      red();                  }                 }              else if(draw[i][j] == '*')              {                  red();              }              else if(draw[i][j] == 'o')              {                  green();              }              printf("%c", draw[i][j]);              white();          }          printf("n");      }  }     void refreshdraw()//清空棋盘  {      int i;      for(i = 0; i <= 30; i=i+2)      {          strcpy(draw[i]," ");          strcpy(draw[i+1],"[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]");      }      strcpy(draw[31]," ");  }     void printmenu()//打印菜单  {      white();      printf("nn     ****************************n");      printf("     *                          *n");      printf("     *         欢迎使用         *n");      printf("     *    五子棋模拟对战系统    *n");      printf("     *                          *n");      printf("     ****************************nnn");      printf("n【设计制作:by 软件18-8邵蔚】nn");      printf("游戏规则为:n二人参与游玩,双方分别使用两种棋子,n");      printf("下在棋盘的 [ ] 上,n");      printf("先形成5子连线(斜线、竖线或横线)的人获胜。nn");      printf("操作说明:n玩家用键盘“w”“s”“a”“d”来选择下棋位置,选择好后,n");      printf("按“j”键来下棋,然后本回合结束,下一名玩家操作。nn");      printf("输入 1 并按回车开始游戏n");      printf("输入 2 并按回车退出游戏n");  }     int convy(int y)//将棋盘坐标转换为字符串数组下标  {      return 4*(y+8)-3;  }     int convx(int x)//将棋盘坐标转换为字符串数组下标  {      return 2*(8-x)-1;  }     void refreshmap()//刷新棋盘画面  {      gotoxy(0,0);      printmap();      gotoxy(0,0);  }     void playeroperation(char cheese)//扫描玩家输入的 "w""a""s""d""j""k" , 并且在棋盘显示或删除棋子;  {      while(1)      {          refreshmap();          ch = getch();          if(ch == 'a' && pos.y > -7)          {              pos.y--;          }          else if(ch == 'd' && pos.y < 7)          {              pos.y++;          }          else if(ch == 'w' && pos.x < 7)          {              pos.x++;          }          else if(ch == 's' && pos.x > -7)          {              pos.x--;          }          else if(ch == 'j' && draw[convx(pos.x)][convy(pos.y)] == ' ')          {              draw[convx(pos.x)][convy(pos.y)] = cheese;              in(cheese);              refreshmap();              break;          }          else if(ch == 'k' && player1.top > 0)          {              int tx, ty;              if(cheese == '*')              {                  tx = player1.data[player1.top].x;                  ty = player1.data[player1.top].y;                  player1.top--;                     draw[convx(tx)][convy(ty)] = ' ';                  refreshmap();                  break;              }              else if(cheese == 'o')              {                  tx = player2.data[player2.top].x;                  ty = player2.data[player2.top].y;                  player2.top--;                 }              draw[convx(tx)][convy(ty)] = ' ';              refreshmap();              break;          }          else if(ch == 'k' && player1.top <= 0)          {              system("cls");              printf("提示:没有棋子了!n");              system("pause");              refreshmap();              printstep(1, step1);              printstep(2, step2);              gotoxy(65,16);              printf(" 按 k 来悔棋。");              gotoxy(65, 23);              green();              printf("(玩家1 : o)");              gotoxy(65, 24);              red();              printf("(玩家1 : *)");          }      }  }     void jundge(int p, char cheese)//判断是否有赢家  {      int tx, ty, sum = 0;         tx = pos.x;      ty = pos.y;      sum = 0;      while(draw[convx(tx)][convy(ty)] == cheese && sum <= 5)      {          if(draw[convx(tx)][convy(ty)] == cheese)              sum++;          tx++;      }      tx = pos.x - 1;      ty = pos.y;      while(draw[convx(tx)][convy(ty)] == cheese && sum <= 5)      {          if(draw[convx(tx)][convy(ty)] == cheese)              sum++;          tx--;      }      if(sum >= 5)      {          win[p] = 1;          return;      }  //--------------------------------------------------------------------      tx = pos.x;      ty = pos.y;      sum = 0;      while(draw[convx(tx)][convy(ty)] == cheese && sum <= 5)      {          if(draw[convx(tx)][convy(ty)] == cheese)              sum++;          ty++;      }      tx = pos.x;      ty = pos.y - 1;      while(draw[convx(tx)][convy(ty)] == cheese && sum <= 5)      {          if(draw[convx(tx)][convy(ty)] == cheese)              sum++;          ty--;      }      if(sum >= 5)      {          win[p] = 1;          return;      }  //----------------------------------------------------------------------      tx = pos.x;      ty = pos.y;      sum = 0;      while(draw[convx(tx)][convy(ty)] == cheese && sum <= 5)      {          if(draw[convx(tx)][convy(ty)] == cheese)              sum++;          ty++;          tx++;      }      tx = pos.x - 1;      ty = pos.y - 1;      while(draw[convx(tx)][convy(ty)] == cheese && sum <= 5)      {          if(draw[convx(tx)][convy(ty)] == cheese)              sum++;          ty--;          tx--;      }      if(sum >= 5)      {          win[p] = 1;          return;      }  //----------------------------------------------------------------------      tx = pos.x;      ty = pos.y;      sum = 0;      while(draw[convx(tx)][convy(ty)] == cheese && sum <= 5)      {          if(draw[convx(tx)][convy(ty)] == cheese)              sum++;          tx--;          ty++;      }      tx = pos.x + 1;      ty = pos.y - 1;      while(draw[convx(tx)][convy(ty)] == cheese && sum <= 5)      {          if(draw[convx(tx)][convy(ty)] == cheese)              sum++;          tx++;          ty--;      }      if(sum >= 5)      {          win[p] = 1;          return;      }         return;  }     void in(char cheese)//将棋子坐标入栈  {      if(cheese == '*')      {          player2.top++;          player2.data[player2.top].x = pos.x;          player2.data[player2.top].y = pos.y;      }      else if(cheese == 'o')      {          player1.top++;          player1.data[player1.top].x = pos.x;          player1.data[player1.top].y = pos.y;      }  }     void playervsplayer()  {      step1 = 0;      step2 = 0;      player1.top = 0;      player2.top = 0;      player1.data[0].x = -1;      player1.data[0].y = -1;      player2.data[0].x = -1;      player2.data[0].y = -1;      player = 2;      printstep(1, step1);      printstep(2, step2);      gotoxy(65, 16);      printf(" 按 k 来悔棋。");      gotoxy(65, 17);      printf(" 按 j 来下子。");      gotoxy(65, 18);      printf(" 按 w a s d 控制方向。");      gotoxy(65, 23);      green();      printf("(玩家1 : o)");      gotoxy(65, 24);      red();      printf("(玩家2 : *)");      white();      while(win[1] == 0 && win[2] == 0)      {          int tx, ty;          if(win[1] == 0 && win[2] == 0)          {              player = 1;              playeroperation('o');              if(ch == 'k' || ch == 'k')                  step2--;              else                  step1++;              tx = player1.data[player1.top].x;              ty = player1.data[player1.top].y;              printstep(1, step1);              printstep(2, step2);              printpos(1, tx, ty);          }          jundge(1,'o');             if(win[1] == 0 && win[2] == 0)          {              player = 2;              playeroperation('*');              if(ch == 'k' || ch == 'k')                  step1--;              else                  step2++;              tx = player2.data[player2.top].x;              ty = player2.data[player2.top].y;              printstep(1, step1);              printstep(2, step2);              printpos(2, tx, ty);          }          jundge(2,'*');      }      if(win[1] == 1)      {          gotoxy(0,31);          printf("【");          green();          printf("玩家1");          white();          printf("】 赢了 !n");      }      else if(win[2] == 1)      {          gotoxy(0,31);          printf("【");          red();          printf("玩家2");          white();          printf("】 赢了 !n");      }      printf("按回车查看双方下棋路线!n");      getchar();      system("cls");      if(win[1] == 1)      {          green();          printf("【玩家1】 赢了 !n");      }      else if(win[2] == 1)      {          red();          printf("【玩家2】 赢了 !n");      }      printroad();      getchar();      return;  }     void askexit()//询问退出  {      char dis[20];      system("cls");      printf("确定退出?n y/n?n");      gets(dis);      if(strcmp(dis, "y") == 0 || strcmp(dis, "y") == 0)      {          system("cls");          printf("感谢你的使用!n");          exit(0);      }      else      {          system("cls");          printmenu();      }      return;  }     void gotoxy(int x, int y)//移动光标函数  {      coord pos = {x,y};      handle hout = getstdhandle(std_output_handle);      setconsolecursorposition(hout, pos);  }     void printstep(int player, int step)//打印已走步数  {         if(player == 1)      {          gotoxy(65,4);          green();          printf("【玩家1】 :%02d", step);          white();      }      else if(player == 2)      {          gotoxy(65,6);          red();          printf("【玩家2】 :%02d", step);          white();      }      return;  }     void printpos(int player, int x, int y)//打印下子坐标  {      if(player == 1)      {          gotoxy(65, 10);          green();          if(x == -1 && y == -1)          {              printf("                                  ");          }          else          {              printf("玩家1下棋位置:第%d行,第%d列",8 + x, 8 + y);          }          white();      }      else if(player == 2)      {          gotoxy(65, 10);          red();          if(x == -1 && y == -1)          {              printf("                                   ");          }          else          {              printf("玩家2下棋位置:第%d行,第%d列",8 + x, 8 + y);          }          white();      }         return;  }     void printroad()//打印所有下子坐标  {      int i;      green();      printf("玩家1的下棋路线为:n");      for(i = 1; i <= player1.top; i++)      {          printf("[ 第%d步 : (%d, %d) ]   ", i, 8 + player1.data[i].x, 8 + player1.data[i].y);          if(i % 5 == 0)              printf("n");      }      printf("n");         red();      printf("玩家2的下棋路线为:n");      for(i = 1; i <= player2.top; i++)      {          printf("[ 第%d步 : (%d, %d) ]   ", i, 8 + player2.data[i].x, 8 + player2.data[i].y);          if(i % 5 == 0)              printf("n");      }      printf("n");      white();      printf("输入 e 结束查看并返回菜单!");         while(1)      {          scanf("%c", &ch);          if(ch == 'e' || ch == 'e')              break;      }      return;  }

以上就是c/c++开发分享C语言实现五子棋对战系统的全部内容,希望对大家的学习有所帮助,也希望大家多多支持<计算机技术网(www.ctvol.com)!!>。

需要了解更多c/c++开发分享C语言实现五子棋对战系统,都可以关注C/C++技术分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/1247952.html

(0)
上一篇 2022年9月27日
下一篇 2022年9月27日

精彩推荐