C++井字棋游戏,DOS界面版
據說有一個能保證不敗的算法。明天看看先再寫個PVC版的。
正題。今天無聊寫了個井字棋游戲,順便逐漸讓自己習慣良好的代碼風格,放上來給新手學習學習。
jzq2.cpp
/*N字棋游戲PVP版,DOS版本棋盤可擴充,僅僅需調整檢測條件就可以,其它接口不需改變。非人機對戰型。PVP類型;@author:天下無雙@date:2014-5-25@version:1.0 */ #include <iostream> #include <string> #define INVALIDINDEX -1 #define FULL -2 #define OK 0 #define WIN 1 #define ISIN -3 using namespace std; struct box{ //用box代表棋盤上每個格子int chess;//用一種顏色代表棋子,black和whiteint status;//0代表該格子沒有棋子,1代表已經有了棋子 }; enum COLOR{black,white}; class chessBoard { private:static const int MAXROW=10;static const int MAXCOLUMN=10;int row;int column;int blackBox;//剩余棋盤格子數,就可以落棋點box arr[MAXROW][MAXCOLUMN];void setRow(int r){row=r;};void setCol(int c){column=c;};int GetRow()const{return row;};int GetCol()const{return column;}; public:chessBoard(int r,int col){if(r>MAXROW||col>MAXCOLUMN){cerr<<"棋盤大小超出范圍"<<endl;setRow(MAXROW);setCol(MAXCOLUMN);}else{setRow(r); setCol(col);//int rr=GetRow();//int cc=GetCol();//blackBox=r*col;//初始化可落棋點//無法在這里設置blackBox,這是什么情況?}initialize();creat();}void initialize(){int r=chessBoard::GetRow();int col=chessBoard::GetCol();blackBox=r*col;for(int i=0;i<r;i++)for(int j=0;j<col;j++){arr[i][j].chess=-1;arr[i][j].status=0;}}//超出范圍。返回 INVALIDINDEX -1//已經贏了,返回 WIN 1//棋盤滿了,返回 FULL -2//正常落棋 返回 OK 0//該點存在棋子 返回 ISIN -3int insertChess(int i,int j,COLOR c)//落棋 //僅提供落棋接口{int r=chessBoard::GetRow();int col=chessBoard::GetCol();if(i<0||j<0||i>=r||j>=col)return INVALIDINDEX;//if(c!=black&&c!=white)//return INVALIDINDEX;if(arr[i][j].status==0){//將棋子落入棋盤arr[i][j].chess=c;arr[i][j].status=1;//標識此格flush();//刷新blackBox--;if(isGameOver())return WIN; if(isFull())return FULL;return OK;}return ISIN;} protected:void creat(){//初始化棋盤int r=chessBoard::GetRow();int col=chessBoard::GetCol();for(int i=0;i<r;i++){for(int j=0;j<col-1;j++){cout<<" |";}cout<<endl;}};void flush(){//重繪棋盤system("cls");int r=chessBoard::GetRow();int col=chessBoard::GetCol();for(int i=0;i<r;i++){for(int j=0;j<col;j++){if(white==arr[i][j].chess)cout<<"0";else if(black==arr[i][j].chess)cout<<"*";elsecout<<" ";if(j!=col-1)cout<<"|";}cout<<endl;}}bool isFull()const{//推斷棋盤是否已經落滿棋子return blackBox==0;};bool isEmpty()const{;//推斷棋盤是否為空return blackBox==GetRow()*GetCol();};//由棋盤自己檢測是否已經滿了。
//或者游戲是否結束 bool isFinish()const{//檢測棋盤是否已滿 return isFull();//若棋盤滿了。則游戲結束 }; bool isGameOver()const{ int r=chessBoard::GetRow(); int col=chessBoard::GetCol(); int color=-1; for(int i=0;i<r;i++){//檢測每一行。是否連成一排。 if(arr[i][0].chess==black||arr[i][0].chess==white) color=arr[i][0].chess;//假設每行的第一個box有內容且為black||white else continue;//檢測下一行 for(int j=1;j<col;j++){ if(color==arr[i][j].chess)//假設后面的跟第一個顏色同樣 if(col==j+1){//假設到了比較最后一個且相等時 string colors; if(color==1) colors="white"; else colors="black"; //cout<<endl<<colors<<" is winner!"<<endl; cout<<endl<<"恭喜"<<colors<<"贏得了本次游戲!
"<<endl; return true; } else //假設不是最后一個。繼續比較 continue; else //假設顏色不同 break; } } //檢測每一列 for(int i=0;i<col;i++){//檢測每一列,是否連成一排, if(arr[0][i].chess==black||arr[0][i].chess==white) color=arr[0][i].chess;//假設每列的第一個box有內容且為black||white else continue;//檢測下一列 for(int j=1;j<r;j++){ if(color==arr[j][i].chess)//假設后面的跟第一個顏色同樣 if(r==j+1){//假設到了比較最后一個且相等時 string colors; if(color==1) colors="white"; else colors="black"; //cout<<endl<<colors<<" is winner!"<<endl; cout<<endl<<"恭喜"<<colors<<"贏得了本次游戲。"<<endl; return true; } else //假設不是最后一個,繼續比較 continue; else //假設顏色不同 break; } } //檢測正對角線 color=arr[0][0].chess; bool falg=false; if(color==black||color==white)//第一格是否有棋子 falg=true; if(falg) //假設有棋子 for(int i=1,j=1;i<r&&j<col;i++,j++){ if(arr[i][j].chess==color) if(i==r-1){ string colors; if(color==1) colors="white"; else colors="black"; //cout<<endl<<colors<<" is winner!"<<endl; cout<<endl<<"恭喜"<<colors<<"贏得了本次游戲!
"<<endl; return true; } else continue; else break; } //檢測側對角線 X color=arr[r-1][0].chess; falg=false; if(color==black||color==white)//第一格是否有棋子 falg=true; if(falg) //假設有棋子 for(int i=r-2,j=1;i>=0&&j<col;i--,j++){ if(arr[i][j].chess==color) if(i==0){ string colors; if(color==1) colors="white"; else colors="black"; //cout<<endl<<colors<<" is winner!"<<endl; cout<<endl<<"恭喜"<<colors<<"贏得了本次游戲!
"<<endl; return true; } else continue; else break; } return false;//假設都不滿足,說明游戲還沒有結束 }; };
main.cpp#include <iostream> #include "jzq2.cpp" using namespace std; int main() { //3,3代表棋盤為3*3,而且是指三個一排即為勝利//相同的。5,5代表5字棋。可是棋盤大小也是5*5//擴展棋盤將在下一版本號推出chessBoard cb(3,3);int status;COLOR c=black;//記錄下一步輪到誰走int x,y;bool falg=false;//用于記錄是否成功落棋bool isExit=false;//用于記錄游戲是否結束while(!isExit){cout<<"\n\"0\"代表white,\"*\"代表black"<<endl;cout<<"請輸入落棋點:如(1,1)則輸入:1 1"<<endl;string colors;if(c==black)colors="black";elsecolors="white";cout<<"如今輪到"<<colors<<"走下一步棋:";cin>>x>>y;/*if(falg)c=c==black?white:black;//換人走*/status=cb.insertChess(x,y,c);switch(status){//超出范圍。返回 INVALIDINDEX -1//已經贏了。返回 WIN 1//棋盤滿了,返回 FULL -2//正常落棋 返回 OK 0case 0:falg=true;c=c==black?
white:black;//假設成功落棋,換人走下一步棋 break; case -1:cout<<"\n\n輸入坐標不正確。超出范圍"<<endl; falg=false; break; case 1:cout<<"\n\n游戲結束。"<<endl; falg=false; isExit=true; break; case -2:cout<<"\n\n棋盤已滿!
"<<endl; cout<<"\n\n游戲即將結束。"<<endl; falg=false; isExit=true; break; case -3:cout<<"\n\n該點已有棋子"<<endl; falg=false; break; } } cin.get(); cin.get(); };
已經測試過了3*3的無BUG。當然前提是你輸入的是數字。你要是輸入字母的話,果斷崩!
先放到PVP的來玩玩,哈哈哈。
今天跑去多益網絡機試。回來的途中竟然想起來最后一道題少寫了一個推斷。郁悶。
另一道回來的途中才大概想了出來。
郁悶ing......
好了,睡了,各位晚安。
總結
以上是生活随笔為你收集整理的C++井字棋游戏,DOS界面版的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 深度学习中tensorflow框架的学习
- 下一篇: 特殊符号及其用法