基于c++ ege图形库实现五子棋游戏

 更新时间:2020年4月25日 17:27  点击:1583

本文分享的五子棋实例,制作基于ege图像库, 首先需要安装配置ege环境 就可以编写小游戏了. 用到的ege库函数不多 , 主要是基于c++的.

先看界面效果:

输入界面:(就是控制台)

游戏胜利界面:

文档如下:

关于五子棋的构思:

实现人人对战的五子棋游戏.使用面向对象的c++ 和 ege库实现.
ege的安装过程不在说明 , 在添加编译链接时去掉 -mwindows 选项.
dev c++ 的运行环境设置为 TDM-GCC 4.8.1.32-bit Debug
为保险起见,编译时选择菜单栏里的  运行-全部重新编译(F12)

需要3个对象 :

1:棋盘对象
2:黑方棋手对象
3:白方棋手对象       

需要说明,对五子棋的实现来说,棋子的数据结构和游戏使用界面相互分离.对棋子的操作基于二维数组,棋盘和棋子的显示用单独的方法实现.       

棋盘对象名: 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<iostream>
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<iostream>
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<iostream>
#include "graphics.h"
#include"black.h"
#include"white.h"
#include <process.h>
#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<<"       *************先输入竖列 再输入横列*************** "<<endl;
 black_entry(x ,y);
 if(is_run()) delay_fps(10);
 if(!win)
 {
  white_entry(x ,y);
 }
 system("cls");
 } while(!win);
 getch();
 }
 
 void black_entry(int &x , int &y)
 {
 //bool addchessman(int chessman_X , int chessman_Y , int message);
 cout<<"       请黑方落子(您的棋子颜色是红色):"<<endl;
 cout<<"       请输入横向位置:"<<endl;
 cout<<"       ";
 cin>>x;
 cout<<"       请输入纵向位置:"<<endl;
 cout<<"       ";
 cin>>y; 
 
 if( ! b.submit_chessman(x ,y) )
 {
 cout<<"      输入位置超出棋盘大小或不合法,请重新输入"<<endl;
 black_entry(x ,y);
 }
 
 if( !addchessman(b.getChessman_X(), b.getChessman_Y() , b.getIdentity()) )
 {
 cout<<"      落子失败! 该位置已有棋子或棋盘边界不能落子! 请重新输入~~~"<<endl; 
 black_entry( x ,y);
 }
 else
 {
 if(bunko( b.getChessman_X(), b.getChessman_Y() , b.getIdentity()) ) 
 {
  setfont(50 , 0 ,"宋体");
  setfontbkcolor(GREEN);
  outtextxy(300 ,300 ,"黑方胜!");
  setfont(20 ,0 ,"宋体");
 outtextxy(300 ,750 ,"按任意键退出!"); 
  win = true;
 }
 }
 cout<<"       当前黑方落子总数:"<<b.getChessmanCount()<<endl;
 cout<<"       当前白方落子总数:"<<w.getChessmanCount()<<endl; 
 }
 
 
 void white_entry(int &x , int &y)
 {
 // bool addchessman(int chessman_X , int chessman_Y , int message);
 cout<<endl<<endl<<endl;
 cout<<"       请白方落子(您的棋子颜色是白色):"<<endl;
 cout<<"       请输入横向位置:"<<endl;
 cout<<"       ";
 cin>>x;
 cout<<"       请输入纵向位置:"<<endl;
 cout<<"       ";
 cin>>y; 
 
 if( ! w.submit_chessman(x ,y) )
 {
 cout<<"      输入位置超出棋盘大小或不合法,请重新输入"<<endl;
 black_entry(x ,y);
 }
 
 if( !addchessman(w.getChessman_X() ,w.getChessman_Y() ,w.getIdentity()) )
 {
 cout<<"      落子失败! 该位置已有棋子或棋盘边界不能落子! 请重新输入~~~"<<endl;
 white_entry(x ,y);
 }
 else
 {
 if(bunko( w.getChessman_X(), w.getChessman_Y() , w.getIdentity()) ) 
 {
  setfont(50 , 0 ,"宋体");
  setfontbkcolor(LIGHTGRAY);
  outtextxy(300 ,300 ,"白方胜!");
  setfont(20 ,0 ,"宋体");
 outtextxy(300 ,720 ,"按任意键退出!"); 
  win = true;
 }
 } 
 }
 
 bool bunko(int x, int y , int message) //判胜 
 {
  int xReturnZero =x;
  int yReturnZero =y; 
  int accumulative=0; //用来记录黑方或白方累计连在一起的 棋子个数 
  
  //先以该子位置为基点,向上(X轴不动 ,Y轴反方向) 逐一判断
  while(allchessman[--x][y].message == message)
  {
   accumulative++; 
   //cout<<"累计的:"<<accumulative<<endl;
  } 
  /* 
  if(accumulative == 5) 
  {
   accumulative=0; //重置计数为0 
   return true;
  }
  else
  {
   return false;
  }
  */ 
  x = xReturnZero;
  y = yReturnZero;
  //先以该子位置为基点 , 向下( X轴不动 , Y轴正方向) 逐一判断 
  while(allchessman[++x][y].message == message)
  {
   accumulative++; 
   //cout<<"累计的:"<<accumulative<<endl;
  } 
  
  if(accumulative == 5) 
  {
   accumulative=0; //重置计数为0 
   return true;
  }
  else
  {
  // return false;
  }
  
  x = xReturnZero;
  y = yReturnZero;
  //先以该子位置为基点 , 向左(Y轴不动 , X轴反方向) 逐一判断
  while(allchessman[x][--y].message == message)
  {
   accumulative++; 
   //cout<<"累计的:"<<accumulative<<endl;
  } 
  
  /*
  if(accumulative == 5) 
  {
   accumulative=0; //重置计数为0 
   return true;
  }
  else
  {
   return false;
  }
  */
  x = xReturnZero;
  y = yReturnZero;
  //先以该子位置为基点, 向右(Y轴不动 , X轴正方向) 逐一判断
  while(allchessman[x][++y].message == message)
  {
   accumulative++; 
   //cout<<"累计的:"<<accumulative<<endl;
  } 
  
  if(accumulative == 5) 
  {
   accumulative=0; //重置计数为0 
   return true;
  }
  else
  {
  // return false;
  }
  
  x = xReturnZero;
  y = yReturnZero;
  //右下方
  while(allchessman[++x][++y].message == message)
  {
   accumulative++; 
   //cout<<"累计的:"<<accumulative<<endl;
  } 
  /* 
  if(accumulative == 5) 
  {
   accumulative=0; //重置计数为0 
   return true;
  }
  else
  {
   return false;
  } */ 
  
  x = xReturnZero;
  y = yReturnZero;
  //左上方
  while(allchessman[--x][--y].message == message)
  {
   accumulative++; 
   //cout<<"累计的:"<<accumulative<<endl;
  } 
  
  if(accumulative == 5) 
  {
   accumulative=0; //重置计数为0 
   return true;
  }
  else
  {
   //return false;
  } 
  
  x = xReturnZero;
  y = yReturnZero;
  //右上方
  while(allchessman[--x][++y].message == message)
  {
   accumulative++; 
   //cout<<"累计的:"<<accumulative<<endl;
  } 
  /* 
  if(accumulative == 5) 
  {
   accumulative=0; //重置计数为0 
   return true;
  }
  else
  {
   return false;
  } */ 
  x = xReturnZero;
  y = yReturnZero;
  //左下方
  while(allchessman[++x][--y].message == message)
  {
   accumulative++; 
   //cout<<"累计的:"<<accumulative<<endl;
  } 
  
  if(accumulative == 5) 
  {
   accumulative=0; //重置计数为0 
   return true;
  }
  else
  {
   return false;
  } 
 } 
 
 
 
 //要在界面上显示黑方已下棋子个数 
 //这个方法目前没有实现 , 实际运行有bug , 棋子数一直为初始值没有改变 , 所以没有用这个方法 
 char* showBlackChessmanCount(black b)
 {
  char str[50];
  sprintf(str , "black role chessman count:%d" , b.getChessmanCount());
  return str;
 }
 
 
};

主函数运行:

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

用时两天,希望大家喜欢!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。

[!--infotagslink--]

相关文章

  • C++ STL标准库std::vector的使用详解

    vector是表示可以改变大小的数组的序列容器,本文主要介绍了C++STL标准库std::vector的使用详解,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2022-03-06
  • C++中取余运算的实现

    这篇文章主要介绍了C++中取余运算的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
  • 详解C++ string常用截取字符串方法

    这篇文章主要介绍了C++ string常用截取字符串方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
  • C++调用C#的DLL程序实现方法

    本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
  • C++中四种加密算法之AES源代码

    本篇文章主要介绍了C++中四种加密算法之AES源代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。...2020-04-25
  • C++ 整数拆分方法详解

    整数拆分,指把一个整数分解成若干个整数的和。本文重点给大家介绍C++ 整数拆分方法详解,非常不错,感兴趣的朋友一起学习吧...2020-04-25
  • C++中 Sort函数详细解析

    这篇文章主要介绍了C++中Sort函数详细解析,sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变...2022-08-18
  • C++万能库头文件在vs中的安装步骤(图文)

    这篇文章主要介绍了C++万能库头文件在vs中的安装步骤(图文),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
  • 详解C++ bitset用法

    这篇文章主要介绍了C++ bitset用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
  • 浅谈C++中的string 类型占几个字节

    本篇文章小编并不是为大家讲解string类型的用法,而是讲解我个人比较好奇的问题,就是string 类型占几个字节...2020-04-25
  • C++ Eigen库计算矩阵特征值及特征向量

    这篇文章主要为大家详细介绍了C++ Eigen库计算矩阵特征值及特征向量,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-04-25
  • C++ pair的用法实例详解

    这篇文章主要介绍了C++ pair的用法实例详解的相关资料,需要的朋友可以参考下...2020-04-25
  • VSCode C++多文件编译的简单使用方法

    这篇文章主要介绍了VSCode C++多文件编译的简单使用方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-03-29
  • C++中的循环引用

    虽然C++11引入了智能指针的,但是开发人员在与内存的斗争问题上并没有解放,如果我门实用不当仍然有内存泄漏问题,其中智能指针的循环引用缺陷是最大的问题。下面通过实例代码给大家介绍c++中的循环引用,一起看看吧...2020-04-25
  • C++随机点名生成器实例代码(老师们的福音!)

    这篇文章主要给大家介绍了关于C++随机点名生成器的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
  • C++如何删除map容器中指定值的元素详解

    map容器是C++ STL中的重要一员,删除map容器中value为指定元素的问题是我们经常与遇到的一个问题,下面这篇文章主要给大家介绍了关于利用C++如何删除map容器中指定值的元素的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。...2020-04-25
  • C++ 约瑟夫环问题案例详解

    这篇文章主要介绍了C++ 约瑟夫环问题案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下...2021-08-15
  • C++中cin的用法详细

    这篇文章主要介绍了C++中cin的用法详细,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
  • 基于C++中常见编译错误的总结详解

    本篇文章是对C++中的常见编译错误进行了详细的分析介绍,需要的朋友参考下...2020-04-25
  • C++实现递归函数的方法

    在本篇内容里小编给大家分享了关于C++实现递归函数的教学步骤,需要的朋友跟着参考下。...2020-04-25