c/c++语言开发共享C++实现图形界面双人五子棋游戏

本文实例为大家分享了c++实现图形界面双人五子棋游戏的具体代码,供大家参考,具体内容如下设计思路定义一个chess类,用16*16的二维数组的来表示一方棋手在棋局上的落子状态。通过控制键盘←

c/c++开发分享C++实现图形界面双人五子棋游戏实例为大家分享了c++实现图形界面双人五子棋游戏的具体代码,供大家参考,具体内容如下

设计思路

定义一个chess类,用16*16的二维数组的来表示一方棋手在棋局上的落子状态。
通过控制键盘←↑→↓来选择落点,getch()函数可获得用户的键盘输入,用coord控制台屏幕上的坐标。
胜利条件:chess类的成员函数win(),每落一个棋子进行win()的判断,当横向、竖向或斜向存在同一颜色的连续五个棋子时,win()返回值1。

代码如下

#include <windows.h>    #include <iostream>    #include<conio.h>  #include<cstring>  using namespace std;    coord c;  class chess  {/*一方棋局*/  private:      int p[15][15];    /*落子情况*/  public:      void newchess()      {/*空棋盘*/          for(int i=0;i<16;i++)              for(int j=0;j<16;j++)                  p[i][j]=0;      }      int win()      {/*判断胜利*/          int i,j,f;          for(i=0;i<16;i++)          {/*横向*/              j=f=0;              while(f<5&&j<16)              {                  if(p[i][j]) f++;                  else f=0;                  j++;              }              if(f==5) return 1;          }          for(j=0;j<16;j++)          {/*竖向*/              i=f=0;              while(f<5&&i<16)              {                  if(p[i][j]) f++;                  else f=0;                  i++;              }              if(f==5) return 1;          }          for(i=0;i<12;i++)          {/*下斜*/              j=f=0;              while(f<5&&j<12)              {                  if(p[i+f][j+f]) f++;                  else {f=0;j++;}              }              if(f==5) return 1;          }          for(i=4;i<16;i++)          {/*上斜*/              j=f=0;              while(f<5&&j<12)              {                  if(p[i-f][j+f]) f++;                  else {f=0;j++;}              }              if(f==5) return 1;          }          return 0;      }      void go(int x,int y)      {/*落子*/          p[y][x]=1;      }      friend int gochess(int);  }a,b;/**/  void gotoxy(int x, int y)   {/*指定移动光标*/      coord coord;       coord.x=x;       coord.y=y;       setconsolecursorposition (getstdhandle(std_output_handle), coord);   }  void chessboard()  {/*画棋盘格*/      gotoxy(0,0);      cout<<"┏━━━┯━━━┯━━━┯━━━┯━━━┯━━━┯━━━┯━━━┯━━━┯━━━┯━━━┯━━━┯━━━┯━━━┓n";      for(int i=0;i<13;i++)      {          cout<<"┃   │   │   │   │   │   │   │   │   │   │   │   │   │   ┃n";          cout<<"┠───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┼───┨n";      }      cout<<"┃   │   │   │   │   │   │   │   │   │   │   │   │   │   ┃n";      cout<<"┗━━━┷━━━┷━━━┷━━━┷━━━┷━━━┷━━━┷━━━┷━━━┷━━━┷━━━┷━━━┷━━━┷━━━┛n";  }    void put(int n)  {      if(n==1)      {          setconsoletextattribute(getstdhandle(std_output_handle),foreground_intensity|foreground_red);          cout<<"●b";      }      else       {          setconsoletextattribute(getstdhandle(std_output_handle),foreground_intensity|foreground_blue);          cout<<"◆b";      }  }  int gochess(int n)  {/*下棋(移动+确认)*/      int i,k,w;      while(k=_getch())      {          switch(k)          {          case 13:/*按回车键下子*/              if(n==1)               {                  put(n);                  a.p[c.x/4][c.y/2]=1;                  return 1;              }              else               {                  put(n);                  b.p[c.x/4][c.y/2]=1;                  return 1;              }          case 224:              w=_getch();              switch(w)              {/*上下左右移动*/              case 72:                   for(i=1;c.y-2*i>=0;i++)                      if(!a.p[c.x/4][(c.y-2*i)/2]&&!b.p[c.x/4][(c.y-2*i)/2])                          {c.y-=2*i;break;}                  break;              case 80:                  for(i=1;c.y+2*i<=28;i++)                      if(!a.p[c.x/4][(c.y+2*i)/2]&&!b.p[c.x/4][(c.y+2*i)/2])                          {c.y+=2*i;break;}                  break;              case 75:                  for(i=1;c.x-4*i>=0;i++)                      if(!a.p[(c.x-4*i)/4][c.y/2]&&!b.p[(c.x-4*i)/4][c.y/2])                          {c.x-=4*i;break;}                  break;              case 77:                  for(i=1;c.x+4*i<=56;i++)                      if(!a.p[(c.x+4*i)/4][c.y/2]&&!b.p[(c.x+4*i)/4][c.y/2])                          {c.x+=4*i;break;}                  break;              }              gotoxy(c.x,c.y);          }      }  }  int game()  {/*游戏开始*/      chessboard();                 //画棋盘        a.newchess();b.newchess();    //定义chess对象a、b      c.x=28;c.y=14;      gotoxy(c.x,c.y);              //光标居中      while(1)      {          gochess(1);          if(a.win())               return 1;          gochess(2);          if(b.win())               return 2;      }  }  int main()    {        gotoxy(70,5);      setconsoletextattribute(getstdhandle(std_output_handle),foreground_intensity|foreground_blue|foreground_green);      cout<<"五子棋游戏";      gotoxy(60,9);      setconsoletextattribute(getstdhandle(std_output_handle),foreground_intensity|foreground_green|foreground_red);      cout<<"操作说明:";      gotoxy(60,11);      cout<<"选择落点:←↑→↓";      gotoxy(60,12);      cout<<"落子:回车键(←┘)";      setconsoletextattribute(getstdhandle(std_output_handle),foreground_intensity|foreground_blue|foreground_green|foreground_red);      int f;      while(f=game())      {          gotoxy(60,16);          setconsoletextattribute(getstdhandle(std_output_handle),foreground_intensity|foreground_green|foreground_red);          if(f==1)          {              setconsoletextattribute(getstdhandle(std_output_handle),foreground_intensity|foreground_red);              cout<<"红方胜!!";          }          else           {              setconsoletextattribute(getstdhandle(std_output_handle),foreground_intensity|foreground_blue);              cout<<"蓝方胜!!";          }            gotoxy(60,18);          setconsoletextattribute(getstdhandle(std_output_handle),foreground_intensity|foreground_green|foreground_blue|foreground_red);          cout<<"按任意键开始新游戏";          f=_getch();          cout<<"bbbbbbbbbbbbbbbbbb                     ";          gotoxy(60,16);          cout<<"bbbbbbbbb                     ";      }  }  

运行结果

C++实现图形界面双人五子棋游戏

C++实现图形界面双人五子棋游戏

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

需要了解更多c/c++开发分享C++实现图形界面双人五子棋游戏,都可以关注C/C++技术分享栏目—计算机技术网(www.ctvol.com)!

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

ctvol管理联系方式QQ:251552304

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

(0)
上一篇 2022年5月6日
下一篇 2022年5月6日

精彩推荐