资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

怎么在c++中使用ege图形库实现五子棋游戏

怎么在c++中使用ege图形库实现五子棋游戏?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

公司主营业务:做网站、网站制作、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联推出东乃免费做网站回馈大家。

棋盘对象名: chessboard  

属性:

1:所有棋子-allchessman  二维数组,用来存放整个棋盘上棋子的分布和选手信息  
数组元素值为0 表示该位置无子   值为1表示该位置为白方落子  值为-1表示该位置为黑方落子
二维数组元素以结构体来表示 , 存X, Y坐标和身份标识.要注意的是 ,标识值为2标识是棋盘边界.不能落子          

方法:

 1:添加棋子 - bool  addchessman(int , int , int message)   //message指示落子黑白方身份识别
 2:画棋盘 - void  drawchessboard()
 3:判胜  - int bunko(int , int , int message)
 4:void  playchess()  运行代码的总程序         

黑方对象:

属性:
 1: 棋子横向位置  int chessman_X 
 2: 棋子纵向位置  int chessman_Y
 3: 落子总个数    black_chessman_count
 4: 身份标识      int black_chessplayer
方法:
 1: 提交棋子  submit_chessman(int , int)                   

白方对象:

属性:
1:棋子横向位置  int chessman_X
2:棋子纵向位置  int chessman_Y
3:落子总个数    white_chessman_count
4:身份标识      int white_chessplayer
方法:
1: 提交棋子     submit_chessman(int ,int )

三个头文件对应三个对象:

黑棋选手:

#include
using namespace std;
 
class black 
{
 int chessman_X; //横向位置
 int chessman_Y; //纵向位置 
 int black_chessman_count ; //落子总数
 int black_chessmanplayer ;
 
 public:
 
 
 black()
 {
 black_chessman_count=0;
 black_chessmanplayer=-1;
 }
 
 
 bool submit_chessman(int chessman_X , int chessman_Y )
 {
 if(chessman_X>15 || chessman_X<1 || chessman_Y>15 ||chessman_Y<1)
 {
 return false;
 }
 else
 {
 this->chessman_X = chessman_X;
 this->chessman_Y = chessman_Y;
 black_chessman_count++;
 return true;
 }
 } 
 
 int getIdentity()
 {
 return black_chessmanplayer; 
 }
 int getChessman_X()
 {
 return chessman_X-1; //这里设置减一是因为画图从0位置开始 
 }
 
 int getChessman_Y()
 {
 return chessman_Y-1;
 }
 
 int getChessmanCount()
 {
 return black_chessman_count ;
 }
};

白棋选手:

#include
using namespace std;
 
class white
{
 int chessman_X; //横向位置
 int chessman_Y; //纵向位置 
 int white_chessman_count; //落子总数
 int white_chessmanplayer;
 
 public:
 
 white()
 {
 white_chessman_count=0;
 white_chessmanplayer=1;
 }
 
 bool submit_chessman(int chessman_X , int chessman_Y )
 {
 if(chessman_X>15 || chessman_X<1 || chessman_Y>15 || chessman_Y<1)
 {
 return false;
 }
 else
  {
  this->chessman_X = chessman_X;
 this->chessman_Y = chessman_Y;
 white_chessman_count++;
 return true;
 }
 } 
 
 int getIdentity()
 {
 return white_chessmanplayer; 
 }
 
 int getChessman_X()
 {
 return chessman_X-1;
 }
 
 int getChessman_Y()
 {
 return chessman_Y-1;
 }
 
 int getChessmanCount()
 {
 return white_chessman_count ;
 }
};

棋盘对象:

#include
#include "graphics.h"
#include"black.h"
#include"white.h"
#include 
#define singleGirdSize 40
#define girdLength 15
 
using namespace std;
class chessboard
{
 //int allchessman[girdLength][girdLength] = {{0 ,0}};
 struct allchessman
 {
 int point_X; //记录棋子X轴位置 
 int point_Y; //记录棋子Y轴位置
 int message; //识别棋子身份 (黑方? 白方 ? 空子? ) 
 }allchessman[girdLength][girdLength];
 
 
public : bool win =false; //玩家输赢的标记 
 black b; //定义黑方对象 
 white w; //定义白方对象 
 //这里b , w 是全局的 ,局部的话会对black_chessman_count这种属性的变化有影响 
 
 public:
 
 //在构造方法中初始化所有棋子
 chessboard()
 {
 int x=100 , y=100; //棋盘左上端点100 ,100 
 for(int i=0 ; i<15 ; i++ , y+=singleGirdSize)
 {
 for(int u=0; u<15 ; u++ , x+=singleGirdSize)
 {
  allchessman[i][u].point_X = x;
  allchessman[i][u].point_Y = y;
  if(allchessman[i][u].point_X == 100 || allchessman[i][u].point_X == 660 || allchessman[i][u].point_Y == 100 || allchessman[i][u].point_Y == 660)
  {
  allchessman[i][u].message =2; //棋盘边界标识记为2 , 不能落子 
  }
  else
  {
  allchessman[i][u].message =0; //初始化为空子 
  }
 }
 x=100; //让X重新回到端点位置 
 }
 } 
 
 
 
 //添加棋子 
 bool addchessman(int chessman_X , int chessman_Y , int message)
 {
 if(message == -1) //黑方落子 
 {
 if(allchessman[chessman_X][chessman_Y].message== 0) //预落子位置无子
 {
 allchessman[chessman_X][chessman_Y].message = -1; //落子 
 setfillcolor(RED);
 fillellipse(allchessman[chessman_X][chessman_Y].point_X , allchessman[chessman_X][chessman_Y].point_Y , 20 , 20); //界面上显示落子 .半径20 
 if(is_run()) delay_fps(10);
 return true; //落子成功 
 } 
 else return false; //添加棋子失败 重复落子的处理 
 }
 else 
 {
 
 if (message == 1)
 {
 if(allchessman[chessman_X][chessman_Y].message == 0 ) 
 {
  allchessman[chessman_X][chessman_Y].message =1;
  setfillcolor(WHITE);
  fillellipse(allchessman[chessman_X][chessman_Y].point_X , allchessman[chessman_X][chessman_Y].point_Y , 20 , 20); //界面上显示落子 .半径20 
  if(is_run()) delay_fps(10);
  return true; //落子成功 
 }
 else return false;
 }
 else
 {
 return false; //应对意外情况 --message身份出错时 
 }
 }
 } //addchessman
 
 
 
 
 void drawchessboard()
 {
 setinitmode(INIT_WITHLOGO, CW_USEDEFAULT, CW_USEDEFAULT);
 //画布大小暂定800 ,800
 initgraph(800 , 800);
 
 setfont(50 ,0 ,"宋体");
 outtextxy(250 , 0 , "简易五子棋");
 setfont(20 , 0 , "宋体");
 //画出棋盘 
 //预定棋盘左上端点是100 ,100 像素点
 int startpoint_X =100 , startpoint_Y =100 ;
 char str[10];
 for(int i=0; i<15 ; i++)
 {
 sprintf(str , "%d" , i+1);
 outtextxy(startpoint_X-20 , startpoint_Y-7, str);
 line(startpoint_X , startpoint_Y , startpoint_X+( girdLength*singleGirdSize-singleGirdSize) , startpoint_Y); //线段画出屏幕会出错:什么也画不出来 
 startpoint_Y+=singleGirdSize;
 } 
 
 startpoint_Y = 100; //重置起始点Y
 
 for(int i=0 ; i<15 ; i++)
 {
 sprintf(str , "%d" , i+1);
 outtextxy(startpoint_X-7, startpoint_Y-20 , str);
 line(startpoint_X , startpoint_Y , startpoint_X , startpoint_Y+(girdLength*singleGirdSize-singleGirdSize) );
 startpoint_X+=singleGirdSize;
 } 
 
 
 /* 
 for(int i=0 ; i<15 ; i++)
 {
 for(int u=0 ; u<15 ; u++)
 {
 if(allchessman[i][u].message == 2) {}
 else
 {
  circle(allchessman[i][u].point_X , allchessman[i][u].point_Y , 20);
 }
 }
 }
 */ 
 
 }//drawchessboard 
 
 
 
 
 void playchess()
 {
 if(is_run()) delay_fps(10);
 int x ,y ; //接收落子的位置 
 int identity=1; // 标识黑方 白方身份 identity取余不为0 则是黑方
 
 do
 {
 cout<<"       *************先输入竖列 再输入横列*************** "<>x;
 cout<<"       请输入纵向位置:"<>y; 
 
 if( ! b.submit_chessman(x ,y) )
 {
 cout<<"      输入位置超出棋盘大小或不合法,请重新输入"<>x;
 cout<<"       请输入纵向位置:"<>y; 
 
 if( ! w.submit_chessman(x ,y) )
 {
 cout<<"      输入位置超出棋盘大小或不合法,请重新输入"<

主函数运行:

#include
#include"chessboard.h"
using namespace std;
int main()
{
 chessboard chman;
 chman.drawchessboard();
 chman.playchess();
}

关于怎么在c++中使用ege图形库实现五子棋游戏问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


标题名称:怎么在c++中使用ege图形库实现五子棋游戏
地址分享:http://cdkjz.cn/article/jipjid.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220