基于c++ ege圖形庫實現(xiàn)五子棋游戲
本文分享的五子棋實例,制作基于ege圖像庫, 首先需要安裝配置ege環(huán)境 就可以編寫小游戲了. 用到的ege庫函數(shù)不多 , 主要是基于c++的.
先看界面效果:
輸入界面:(就是控制臺)
游戲勝利界面:
文檔如下:
關(guān)于五子棋的構(gòu)思:
實現(xiàn)人人對戰(zhàn)的五子棋游戲.使用面向?qū)ο蟮腸++ 和 ege庫實現(xiàn).
ege的安裝過程不在說明 , 在添加編譯鏈接時去掉 -mwindows 選項.
dev c++ 的運行環(huán)境設(shè)置為 TDM-GCC 4.8.1.32-bit Debug
為保險起見,編譯時選擇菜單欄里的 運行-全部重新編譯(F12)
需要3個對象 :
1:棋盤對象
2:黑方棋手對象
3:白方棋手對象
需要說明,對五子棋的實現(xiàn)來說,棋子的數(shù)據(jù)結(jié)構(gòu)和游戲使用界面相互分離.對棋子的操作基于二維數(shù)組,棋盤和棋子的顯示用單獨的方法實現(xiàn).
棋盤對象名: chessboard
屬性:
1:所有棋子-allchessman 二維數(shù)組,用來存放整個棋盤上棋子的分布和選手信息
數(shù)組元素值為0 表示該位置無子 值為1表示該位置為白方落子 值為-1表示該位置為黑方落子
二維數(shù)組元素以結(jié)構(gòu)體來表示 , 存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: 落子總個數(shù) black_chessman_count
4: 身份標識 int black_chessplayer
方法:
1: 提交棋子 submit_chessman(int , int)
白方對象:
屬性:
1:棋子橫向位置 int chessman_X
2:棋子縱向位置 int chessman_Y
3:落子總個數(shù) 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 ; //落子總數(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è)置減一是因為畫圖從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 ; } };
棋盤對象:
#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: //在構(gòu)造方法中初始化所有棋子 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<<" 當前黑方落子總數(shù):"<<b.getChessmanCount()<<endl; cout<<" 當前白方落子總數(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<<" 請白方落子(您的棋子顏色是白色):"<<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; //用來記錄黑方或白方累計連在一起的 棋子個數(shù) //先以該子位置為基點,向上(X軸不動 ,Y軸反方向) 逐一判斷 while(allchessman[--x][y].message == message) { accumulative++; //cout<<"累計的:"<<accumulative<<endl; } /* if(accumulative == 5) { accumulative=0; //重置計數(shù)為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; //重置計數(shù)為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; //重置計數(shù)為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; //重置計數(shù)為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; //重置計數(shù)為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; //重置計數(shù)為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; //重置計數(shù)為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; //重置計數(shù)為0 return true; } else { return false; } } //要在界面上顯示黑方已下棋子個數(shù) //這個方法目前沒有實現(xiàn) , 實際運行有bug , 棋子數(shù)一直為初始值沒有改變 , 所以沒有用這個方法 char* showBlackChessmanCount(black b) { char str[50]; sprintf(str , "black role chessman count:%d" , b.getChessmanCount()); return str; } };
主函數(shù)運行:
#include<iostream> #include"chessboard.h" using namespace std; int main() { chessboard chman; chman.drawchessboard(); chman.playchess(); }
用時兩天,希望大家喜歡!
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持我們。
上一篇:關(guān)于C++內(nèi)部類的介紹與使用示例
欄 目:C語言
下一篇:嵌入式C語言查表法在項目中的應用
本文標題:基于c++ ege圖形庫實現(xiàn)五子棋游戲
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/589.html
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10使用C++實現(xiàn)全排列算法的方法詳解
- 01-10c++中inline的用法分析
- 01-10用C++實現(xiàn)DBSCAN聚類算法
- 01-10全排列算法的非遞歸實現(xiàn)與遞歸實現(xiàn)的方法(C++)
- 01-10C++大數(shù)模板(推薦)
- 01-10淺談C/C++中的static與extern關(guān)鍵字的使用詳解
- 01-10深入C/C++浮點數(shù)在內(nèi)存中的存儲方式詳解
- 01-10基于atoi()與itoa()函數(shù)的內(nèi)部實現(xiàn)方法詳解


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10C#中split用法實例總結(jié)
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置