最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于c++ ege圖形庫(kù)實(shí)現(xiàn)五子棋游戲

 更新時(shí)間:2018年12月21日 16:42:32   作者:libingbojava  
這篇文章主要為大家詳細(xì)介紹了基于c++ ege圖形庫(kù)實(shí)現(xiàn)五子棋游戲,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文分享的五子棋實(shí)例,制作基于ege圖像庫(kù), 首先需要安裝配置ege環(huán)境 就可以編寫小游戲了. 用到的ege庫(kù)函數(shù)不多 , 主要是基于c++的.

先看界面效果:

輸入界面:(就是控制臺(tái))

游戲勝利界面:

文檔如下:

關(guān)于五子棋的構(gòu)思:

實(shí)現(xiàn)人人對(duì)戰(zhàn)的五子棋游戲.使用面向?qū)ο蟮腸++ 和 ege庫(kù)實(shí)現(xiàn).
ege的安裝過程不在說明 , 在添加編譯鏈接時(shí)去掉 -mwindows 選項(xiàng).
dev c++ 的運(yùn)行環(huán)境設(shè)置為 TDM-GCC 4.8.1.32-bit Debug
為保險(xiǎn)起見,編譯時(shí)選擇菜單欄里的  運(yùn)行-全部重新編譯(F12)

需要3個(gè)對(duì)象 :

1:棋盤對(duì)象
2:黑方棋手對(duì)象
3:白方棋手對(duì)象       

需要說明,對(duì)五子棋的實(shí)現(xiàn)來說,棋子的數(shù)據(jù)結(jié)構(gòu)和游戲使用界面相互分離.對(duì)棋子的操作基于二維數(shù)組,棋盤和棋子的顯示用單獨(dú)的方法實(shí)現(xiàn).       

棋盤對(duì)象名: chessboard 

屬性:

1:所有棋子-allchessman  二維數(shù)組,用來存放整個(gè)棋盤上棋子的分布和選手信息  
數(shù)組元素值為0 表示該位置無子   值為1表示該位置為白方落子  值為-1表示該位置為黑方落子
二維數(shù)組元素以結(jié)構(gòu)體來表示 , 存X, Y坐標(biāo)和身份標(biāo)識(shí).要注意的是 ,標(biāo)識(shí)值為2標(biāo)識(shí)是棋盤邊界.不能落子          

方法:

 1:添加棋子 - bool  addchessman(int , int , int message)   //message指示落子黑白方身份識(shí)別
 2:畫棋盤 - void  drawchessboard()
 3:判勝  - int bunko(int , int , int message)
 4:void  playchess()  運(yùn)行代碼的總程序         

黑方對(duì)象:

屬性:
 1: 棋子橫向位置  int chessman_X 
 2: 棋子縱向位置  int chessman_Y
 3: 落子總個(gè)數(shù)    black_chessman_count
 4: 身份標(biāo)識(shí)      int black_chessplayer
方法:
 1: 提交棋子  submit_chessman(int , int)                   

白方對(duì)象:

屬性:
1:棋子橫向位置  int chessman_X
2:棋子縱向位置  int chessman_Y
3:落子總個(gè)數(shù)    white_chessman_count
4:身份標(biāo)識(shí)      int white_chessplayer
方法:
1: 提交棋子     submit_chessman(int ,int )

三個(gè)頭文件對(duì)應(yīng)三個(gè)對(duì)象:

黑棋選手:

#include<iostream>
using namespace std;
 
class black 
{
 int chessman_X; //橫向位置
 int chessman_Y; //縱向位置 
 int black_chessman_count ; //落子總數(shù)
 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; //這里設(shè)置減一是因?yàn)楫媹D從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; //落子總數(shù)
 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 ;
 }
};

棋盤對(duì)象:

#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; //識(shí)別棋子身份 (黑方? 白方 ? 空子? ) 
 }allchessman[girdLength][girdLength];
 
 
public : bool win =false; //玩家輸贏的標(biāo)記 
 black b; //定義黑方對(duì)象 
 white w; //定義白方對(duì)象 
 //這里b , w 是全局的 ,局部的話會(huì)對(duì)black_chessman_count這種屬性的變化有影響 
 
 public:
 
 //在構(gòu)造方法中初始化所有棋子
 chessboard()
 {
 int x=100 , y=100; //棋盤左上端點(diǎn)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; //棋盤邊界標(biāo)識(shí)記為2 , 不能落子 
  }
  else
  {
  allchessman[i][u].message =0; //初始化為空子 
  }
 }
 x=100; //讓X重新回到端點(diǎn)位置 
 }
 } 
 
 
 
 //添加棋子 
 bool addchessman(int chessman_X , int chessman_Y , int message)
 {
 if(message == -1) //黑方落子 
 {
 if(allchessman[chessman_X][chessman_Y].message== 0) //預(yù)落子位置無子
 {
 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; //添加棋子失敗 重復(fù)落子的處理 
 }
 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; //應(yīng)對(duì)意外情況 --message身份出錯(cuò)時(shí) 
 }
 }
 } //addchessman
 
 
 
 
 void drawchessboard()
 {
 setinitmode(INIT_WITHLOGO, CW_USEDEFAULT, CW_USEDEFAULT);
 //畫布大小暫定800 ,800
 initgraph(800 , 800);
 
 setfont(50 ,0 ,"宋體");
 outtextxy(250 , 0 , "簡(jiǎn)易五子棋");
 setfont(20 , 0 , "宋體");
 //畫出棋盤 
 //預(yù)定棋盤左上端點(diǎn)是100 ,100 像素點(diǎn)
 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); //線段畫出屏幕會(huì)出錯(cuò):什么也畫不出來 
 startpoint_Y+=singleGirdSize;
 } 
 
 startpoint_Y = 100; //重置起始點(diǎn)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; // 標(biāo)識(shí)黑方 白方身份 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<<"       請(qǐng)黑方落子(您的棋子顏色是紅色):"<<endl;
 cout<<"       請(qǐng)輸入橫向位置:"<<endl;
 cout<<"       ";
 cin>>x;
 cout<<"       請(qǐng)輸入縱向位置:"<<endl;
 cout<<"       ";
 cin>>y; 
 
 if( ! b.submit_chessman(x ,y) )
 {
 cout<<"      輸入位置超出棋盤大小或不合法,請(qǐng)重新輸入"<<endl;
 black_entry(x ,y);
 }
 
 if( !addchessman(b.getChessman_X(), b.getChessman_Y() , b.getIdentity()) )
 {
 cout<<"      落子失敗! 該位置已有棋子或棋盤邊界不能落子! 請(qǐng)重新輸入~~~"<<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<<"       當(dāng)前黑方落子總數(shù):"<<b.getChessmanCount()<<endl;
 cout<<"       當(dāng)前白方落子總數(shù):"<<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<<"       請(qǐng)白方落子(您的棋子顏色是白色):"<<endl;
 cout<<"       請(qǐng)輸入橫向位置:"<<endl;
 cout<<"       ";
 cin>>x;
 cout<<"       請(qǐng)輸入縱向位置:"<<endl;
 cout<<"       ";
 cin>>y; 
 
 if( ! w.submit_chessman(x ,y) )
 {
 cout<<"      輸入位置超出棋盤大小或不合法,請(qǐng)重新輸入"<<endl;
 black_entry(x ,y);
 }
 
 if( !addchessman(w.getChessman_X() ,w.getChessman_Y() ,w.getIdentity()) )
 {
 cout<<"      落子失敗! 該位置已有棋子或棋盤邊界不能落子! 請(qǐng)重新輸入~~~"<<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; //用來記錄黑方或白方累計(jì)連在一起的 棋子個(gè)數(shù) 
  
  //先以該子位置為基點(diǎn),向上(X軸不動(dòng) ,Y軸反方向) 逐一判斷
  while(allchessman[--x][y].message == message)
  {
   accumulative++; 
   //cout<<"累計(jì)的:"<<accumulative<<endl;
  } 
  /* 
  if(accumulative == 5) 
  {
   accumulative=0; //重置計(jì)數(shù)為0 
   return true;
  }
  else
  {
   return false;
  }
  */ 
  x = xReturnZero;
  y = yReturnZero;
  //先以該子位置為基點(diǎn) , 向下( X軸不動(dòng) , Y軸正方向) 逐一判斷 
  while(allchessman[++x][y].message == message)
  {
   accumulative++; 
   //cout<<"累計(jì)的:"<<accumulative<<endl;
  } 
  
  if(accumulative == 5) 
  {
   accumulative=0; //重置計(jì)數(shù)為0 
   return true;
  }
  else
  {
  // return false;
  }
  
  x = xReturnZero;
  y = yReturnZero;
  //先以該子位置為基點(diǎn) , 向左(Y軸不動(dòng) , X軸反方向) 逐一判斷
  while(allchessman[x][--y].message == message)
  {
   accumulative++; 
   //cout<<"累計(jì)的:"<<accumulative<<endl;
  } 
  
  /*
  if(accumulative == 5) 
  {
   accumulative=0; //重置計(jì)數(shù)為0 
   return true;
  }
  else
  {
   return false;
  }
  */
  x = xReturnZero;
  y = yReturnZero;
  //先以該子位置為基點(diǎn), 向右(Y軸不動(dòng) , X軸正方向) 逐一判斷
  while(allchessman[x][++y].message == message)
  {
   accumulative++; 
   //cout<<"累計(jì)的:"<<accumulative<<endl;
  } 
  
  if(accumulative == 5) 
  {
   accumulative=0; //重置計(jì)數(shù)為0 
   return true;
  }
  else
  {
  // return false;
  }
  
  x = xReturnZero;
  y = yReturnZero;
  //右下方
  while(allchessman[++x][++y].message == message)
  {
   accumulative++; 
   //cout<<"累計(jì)的:"<<accumulative<<endl;
  } 
  /* 
  if(accumulative == 5) 
  {
   accumulative=0; //重置計(jì)數(shù)為0 
   return true;
  }
  else
  {
   return false;
  } */ 
  
  x = xReturnZero;
  y = yReturnZero;
  //左上方
  while(allchessman[--x][--y].message == message)
  {
   accumulative++; 
   //cout<<"累計(jì)的:"<<accumulative<<endl;
  } 
  
  if(accumulative == 5) 
  {
   accumulative=0; //重置計(jì)數(shù)為0 
   return true;
  }
  else
  {
   //return false;
  } 
  
  x = xReturnZero;
  y = yReturnZero;
  //右上方
  while(allchessman[--x][++y].message == message)
  {
   accumulative++; 
   //cout<<"累計(jì)的:"<<accumulative<<endl;
  } 
  /* 
  if(accumulative == 5) 
  {
   accumulative=0; //重置計(jì)數(shù)為0 
   return true;
  }
  else
  {
   return false;
  } */ 
  x = xReturnZero;
  y = yReturnZero;
  //左下方
  while(allchessman[++x][--y].message == message)
  {
   accumulative++; 
   //cout<<"累計(jì)的:"<<accumulative<<endl;
  } 
  
  if(accumulative == 5) 
  {
   accumulative=0; //重置計(jì)數(shù)為0 
   return true;
  }
  else
  {
   return false;
  } 
 } 
 
 
 
 //要在界面上顯示黑方已下棋子個(gè)數(shù) 
 //這個(gè)方法目前沒有實(shí)現(xiàn) , 實(shí)際運(yùn)行有bug , 棋子數(shù)一直為初始值沒有改變 , 所以沒有用這個(gè)方法 
 char* showBlackChessmanCount(black b)
 {
  char str[50];
  sprintf(str , "black role chessman count:%d" , b.getChessmanCount());
  return str;
 }
 
 
};

主函數(shù)運(yùn)行:

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

用時(shí)兩天,希望大家喜歡!

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C++中函數(shù)模板的用法詳細(xì)解析

    C++中函數(shù)模板的用法詳細(xì)解析

    所謂函數(shù)模板實(shí)際上是建立一個(gè)通用函數(shù),其涵涵素類型額形參類型不具體指定,用一個(gè)虛擬的類型來代表,這個(gè)通用函數(shù)就稱為函數(shù)模板
    2013-10-10
  • 數(shù)據(jù)結(jié)構(gòu)與算法中二叉樹子結(jié)構(gòu)的詳解

    數(shù)據(jù)結(jié)構(gòu)與算法中二叉樹子結(jié)構(gòu)的詳解

    這篇文章主要介紹了數(shù)據(jù)結(jié)構(gòu)與算法中二叉樹子結(jié)構(gòu)的詳解的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • C語言實(shí)現(xiàn)輸入ascii碼,輸出對(duì)應(yīng)的字符方式

    C語言實(shí)現(xiàn)輸入ascii碼,輸出對(duì)應(yīng)的字符方式

    這篇文章主要介紹了C語言實(shí)現(xiàn)輸入ascii碼,輸出對(duì)應(yīng)的字符方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • 詳解次小生成樹以及相關(guān)的C++求解方法

    詳解次小生成樹以及相關(guān)的C++求解方法

    這篇文章主要介紹了詳解次小生成樹以及相關(guān)的C++求解方法,文中的練習(xí)示例采用了kruskal算法通過C++進(jìn)行求解,需要的朋友可以參考下
    2015-08-08
  • qt中 painter 的用法及原理示例詳解

    qt中 painter 的用法及原理示例詳解

    QPainter是Qt的一個(gè)繪圖類,它的主要任務(wù)是在繪圖設(shè)備上進(jìn)行2D圖形渲染,這篇文章主要介紹了qt中 painter 的用法以及原理,需要的朋友可以參考下
    2023-07-07
  • Qt學(xué)習(xí)教程之表格控件螞蟻線詳解

    Qt學(xué)習(xí)教程之表格控件螞蟻線詳解

    如果有用過PS的選區(qū)工具應(yīng)該就會(huì)知道螞蟻線是什么東西了,就是用來表示選區(qū)的一種虛線,關(guān)鍵還是要?jiǎng)討B(tài)的!下面這篇文章主要給大家介紹了關(guān)于Qt學(xué)習(xí)教程之表格控件螞蟻線的相關(guān)資料,需要的朋友可以參考下
    2018-07-07
  • 在vscode中快速新建html文件的2種方法總結(jié)

    在vscode中快速新建html文件的2種方法總結(jié)

    這篇文章主要給大家介紹了關(guān)于在vscode中快速新建html文件的2種方法,以及如何快速打開HTML文件查看編輯效果的方法,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2022-04-04
  • C語言實(shí)現(xiàn)冒泡排序算法的示例詳解

    C語言實(shí)現(xiàn)冒泡排序算法的示例詳解

    這篇文章主要介紹了C語言如何實(shí)現(xiàn)冒泡排序算法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • C語言中的正則表達(dá)式使用示例詳解

    C語言中的正則表達(dá)式使用示例詳解

    正則表達(dá)式是使用單個(gè)字符串來描述、匹配一系列符合某個(gè)句法規(guī)則的字符串。本文通過示例代碼給大家介紹了C語言中的正則表達(dá)式使用,感興趣的朋友跟隨小編一起看看吧
    2019-07-07
  • MFC實(shí)現(xiàn)連連看游戲之地圖顯示

    MFC實(shí)現(xiàn)連連看游戲之地圖顯示

    這篇文章主要為大家詳細(xì)介紹了MFC實(shí)現(xiàn)連連看游戲之地圖顯示,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-01-01

最新評(píng)論

常熟市| 龙州县| 铁岭县| 炎陵县| 钟山县| 武义县| 梁河县| 枝江市| 屏边| 临沭县| 娱乐| 莲花县| 阿合奇县| 峨眉山市| 顺平县| 嘉荫县| 两当县| 全椒县| 宝兴县| 泽州县| 准格尔旗| 富民县| 花莲市| 公安县| 漯河市| 临高县| 罗山县| 台南县| 秀山| 贵德县| 百色市| 防城港市| 青神县| 新营市| 三穗县| 庆城县| 巴中市| 丹江口市| 宁阳县| 邢台市| 腾冲县|