C++代碼實(shí)現(xiàn)五子棋小游戲
簡單C++代碼實(shí)現(xiàn)五子棋任務(wù),供大家參考,具體內(nèi)容如下
首先先展示一下運(yùn)行的圖片

話也不多說,直接分不同代碼板塊來介紹程序不同功能以及是如何實(shí)現(xiàn)的
首先,對于一個五子棋程序,我們要思考的,在過程式的編程思想里面,如何將其功能分解為不同的函數(shù)
1.打印棋盤
由于使用的棋子每一個棋子占兩個字符,所以打印時(shí)要使用兩個空格
int b=0,w=0,player=1,x,y; //b和w分別作為參數(shù)標(biāo)記黑棋白棋的勝利,player用來指明每次是下黑棋還是白棋,x,y分別用來作為棋盤的橫縱坐標(biāo)
int chess[11][11];//初始化
void board()//每一次打印棋盤的函數(shù)
{ ??
? ? cout << " ? ?1 2 3 4 5 6 7 8 9 10" <<endl;
? ? cout << " ?+--------------------+" <<endl;
? ? for(int i=1;i<=9;i++)
? ? {
? ? ? ? cout<<" "<<i<<"|";
? ? ? ? input(i-1);//input函數(shù)在后文介紹
? ? ? ? cout<<"|"<<endl;
? ? }
? ? cout << "10|";input(9); cout <<"|" <<endl;
? ? cout << " ?+--------------------+" <<endl;
}考慮到字符數(shù)組本身無法同時(shí)對連續(xù)兩個字符位賦值,這里采用二位數(shù)組表示下棋位置并采用一個input函數(shù)將二維數(shù)組轉(zhuǎn)化為棋子
void init()
{
? ? for(int i=0;i<11;i++)
? ? {
? ? ? ? for(int j=0;j<11;j++)
? ? ? ? chess[i][j]=0;//初始化棋盤全為0
? ? ? ? }
}
void input(const int n)
{
? ? for(int i=n,j=0;j<10;j++)
? ? {
? ? ? ? switch(chess[i][j])//利用這個switch語句來將空白的棋盤中需要打印的棋子打印上去
? ? ? ? {
? ? ? ? ? ? case(0): cout << " ?";break;
? ? ? ? ? ? case(1): cout << "??";break;
? ? ? ? ? ? case(-1): cout << "??";break;
? ? ? ? ? ? }
? ? }
}2.下棋部分
這一部分也是最為麻煩的部分,由于每次調(diào)用檢驗(yàn)函數(shù)檢驗(yàn)黑棋或白棋是否勝利會帶來不必要的麻煩,所以在每一次下棋之后直接在下棋的位置往各個方向檢索以判斷是否勝利
void play(int x,int y)
{
?? ?if(player==1)
?? ?{chess[x-1][y-1]=1;//表示下黑棋
?? ? if(chess[x-1][y]==1&&chess[x-1][y+1]==1&&chess[x-1][y+2]==1&&chess[x-1][y+3]==1)//重復(fù)的判斷代碼,每一次復(fù)制粘貼即可
?? ? b=5;
?? ? else if(chess[x-1][y]==1&&chess[x-1][y-1]==1&&chess[x-1][y-2]==1&&chess[x-1][y-3]==1)
?? ? b=5;
?? ? else if(chess[x][y-1]==1&&chess[x+1][y-1]==1&&chess[x+2][y-1]==1&&chess[x+3][y-1]==1)
?? ? b=5;
?? ? else if(chess[x][y-1]==1&&chess[x-1][y-1]==1&&chess[x-2][y-1]==1&&chess[x-3][y-1]==1)
?? ? b=5;
?? ? else if(chess[x][y]==1&&chess[x+1][y+1]==1&&chess[x+2][y+2]==1&&chess[x+3][y+3]==1)
?? ? b=5;
?? ? else if(chess[x-2][y-2]==1&&chess[x-3][y-3]==1&&chess[x-4][y-4]==1&&chess[x-5][y-5]==1)
?? ? b=5;
?? ? else if(chess[x-2][y]==1&&chess[x-3][y+1]==1&&chess[x-4][y+2]==1&&chess[x-5][y+3]==1)
?? ? b=5;
?? ? else if(chess[x][y-2]==1&&chess[x+1][y-3]==1&&chess[x+2][y-4]==1&&chess[x+3][y-5]==1)
?? ? b=5;
?? ? player=2;}
?? ?else if(player==2)
?? ?{chess[x-1][y-1]=-1;//表示下白棋
?? ? if(chess[x-1][y]==1&&chess[x-1][y+1]==1&&chess[x-1][y+2]==1&&chess[x-1][y+3]==1)
?? ? w=5;
?? ? else if(chess[x-1][y]==1&&chess[x-1][y-1]==1&&chess[x-1][y-2]==1&&chess[x-1][y-3]==1)
?? ? w=5;
?? ? else if(chess[x][y-1]==1&&chess[x+1][y-1]==1&&chess[x+2][y-1]==1&&chess[x+3][y-1]==1)
?? ? w=5;
?? ? else if(chess[x][y-1]==1&&chess[x-1][y-1]==1&&chess[x-2][y-1]==1&&chess[x-3][y-1]==1)
?? ? w=5;
?? ? else if(chess[x][y]==1&&chess[x+1][y+1]==1&&chess[x+2][y+2]==1&&chess[x+3][y+3]==1)
?? ? w=5;
?? ? else if(chess[x-2][y-2]==1&&chess[x-3][y-3]==1&&chess[x-4][y-4]==1&&chess[x-5][y-5]==1)
?? ? w=5;
?? ? else if(chess[x-2][y]==1&&chess[x-3][y+1]==1&&chess[x-4][y+2]==1&&chess[x-5][y+3]==1)
?? ? w=5;
?? ? else if(chess[x][y-2]==1&&chess[x+1][y-3]==1&&chess[x+2][y-4]==1&&chess[x+3][y-5]==1)
?? ? w=5;
?? ? player=1;}
}同時(shí),我們還需要一點(diǎn)小小的附加代碼,因?yàn)槟悴荒鼙WC每一次棋手下棋都是在合法位置
void judge()
{
? ? while(1)//c++類似的使用很多,用永真的表達(dá)式,然后判斷跳出條件break,這里主要用來重復(fù)判斷落子是否合法
? ? {
? ? ? ? if(x<=0||x>10||y<=0||y>10)
? ? ? ? {
? ? ? ? ? ? cout <<"invalid position,input again:"<<endl;
? ? ? ? ? ? cin >>x>>y;
? ? ? ? ? ? }
? ? ? ? else if(chess[x-1][y-1]!=0)
? ? ? ? {
? ? ? ? ? ? cout <<"wrong place,input again:"<<endl;
? ? ? ? ? ? cin >>x>>y;
? ? ? ? ? ? }
? ? ? ? else if(x>0&&x<=10&&y>0&&y<=10&&chess[x-1][y-1]==0)
? ? ? ? ? ? break;
? ? ? ? }
}3.主函數(shù)
加下來就是main函數(shù)部分了,顯而易見了
int main()
{ ??
? ? init();
? ? board();
? ? while(1)
? ?{
? ? ? ? cout << "Black: ";
? ? ? ? cin>>x>>y;
? ? ? ? judge();
? ? ? ? play(x,y);
? ? ? ? system("cls");//清屏功能
? ? ? ? board();
? ? ? ? if(b==5)
? ? ? ? {
? ? ? ? ? ? system("cls");cout << "Black win";break;
? ? ? ? ? ? }
? ? ? ? else if(w==5)
? ? ? ? {
? ? ? ? ? ? system("cls");cout << "White win";break;
? ? ? ? ? ? }
? ? ? ? cout << "White: " ;
? ? ? ? cin >>x>>y;
? ? ? ? judge();
? ? ? ? play(x,y);
? ? ? ? system("cls");
? ? ? ? board();
? ? ? ? if(b==5)
? ? ? ? {
? ? ? ? ? ? system("cls");cout << "Black win";break;
? ? ? ? ? ? }
? ? ? ? else if(w==5)
? ? ? ? {
? ? ? ? ? ? system("cls");cout << "White win";break;
? ? ? ? ? ? }
? ? ? ? }
? ? return 0;
}至此,就可以實(shí)現(xiàn)整個五子棋代碼的功能了
附上完整的代碼:
#include <iostream>
using namespace std;
int b=0,w=0,player=1,x,y; //b和w分別作為參數(shù)標(biāo)記黑棋白棋的勝利,player用來指明每次是下黑棋還是白棋,x,y分別用來作為棋盤的橫縱坐標(biāo)
int chess[11][11];//初始化
void init()
{
? ? for(int i=0;i<11;i++)
? ? {
? ? ? ? for(int j=0;j<11;j++)
? ? ? ? chess[i][j]=0;//初始化棋盤全為0
? ? ? ? }
}
void input(const int n)
{
? ? for(int i=n,j=0;j<10;j++)
? ? {
? ? ? ? switch(chess[i][j])//利用這個switch語句來將空白的棋盤中需要打印的棋子打印上去
? ? ? ? {
? ? ? ? ? ? case(0): cout << " ?";break;
? ? ? ? ? ? case(1): cout << "??";break;
? ? ? ? ? ? case(-1): cout << "??";break;
? ? ? ? ? ? }
? ? }
}
void board()//每一次打印棋盤的函數(shù)
{ ??
? ? cout << " ? ?1 2 3 4 5 6 7 8 9 10" <<endl;
? ? cout << " ?+--------------------+" <<endl;
? ? for(int i=1;i<=9;i++)
? ? {
? ? ? ? cout<<" "<<i<<"|";
? ? ? ? input(i-1);//input函數(shù)在后文介紹
? ? ? ? cout<<"|"<<endl;
? ? }
? ? cout << "10|";input(9); cout <<"|" <<endl;
? ? cout << " ?+--------------------+" <<endl;
}
void play(int x,int y)
{
?? ?if(player==1)
?? ?{chess[x-1][y-1]=1;//表示下黑棋
?? ? if(chess[x-1][y]==1&&chess[x-1][y+1]==1&&chess[x-1][y+2]==1&&chess[x-1][y+3]==1)//重復(fù)的判斷代碼,每一次復(fù)制粘貼即可
?? ? b=5;
?? ? else if(chess[x-1][y]==1&&chess[x-1][y-1]==1&&chess[x-1][y-2]==1&&chess[x-1][y-3]==1)
?? ? b=5;
?? ? else if(chess[x][y-1]==1&&chess[x+1][y-1]==1&&chess[x+2][y-1]==1&&chess[x+3][y-1]==1)
?? ? b=5;
?? ? else if(chess[x][y-1]==1&&chess[x-1][y-1]==1&&chess[x-2][y-1]==1&&chess[x-3][y-1]==1)
?? ? b=5;
?? ? else if(chess[x][y]==1&&chess[x+1][y+1]==1&&chess[x+2][y+2]==1&&chess[x+3][y+3]==1)
?? ? b=5;
?? ? else if(chess[x-2][y-2]==1&&chess[x-3][y-3]==1&&chess[x-4][y-4]==1&&chess[x-5][y-5]==1)
?? ? b=5;
?? ? else if(chess[x-2][y]==1&&chess[x-3][y+1]==1&&chess[x-4][y+2]==1&&chess[x-5][y+3]==1)
?? ? b=5;
?? ? else if(chess[x][y-2]==1&&chess[x+1][y-3]==1&&chess[x+2][y-4]==1&&chess[x+3][y-5]==1)
?? ? b=5;
?? ? player=2;}
?? ?else if(player==2)
?? ?{chess[x-1][y-1]=-1;//表示下白棋
?? ? if(chess[x-1][y]==1&&chess[x-1][y+1]==1&&chess[x-1][y+2]==1&&chess[x-1][y+3]==1)
?? ? w=5;
?? ? else if(chess[x-1][y]==1&&chess[x-1][y-1]==1&&chess[x-1][y-2]==1&&chess[x-1][y-3]==1)
?? ? w=5;
?? ? else if(chess[x][y-1]==1&&chess[x+1][y-1]==1&&chess[x+2][y-1]==1&&chess[x+3][y-1]==1)
?? ? w=5;
?? ? else if(chess[x][y-1]==1&&chess[x-1][y-1]==1&&chess[x-2][y-1]==1&&chess[x-3][y-1]==1)
?? ? w=5;
?? ? else if(chess[x][y]==1&&chess[x+1][y+1]==1&&chess[x+2][y+2]==1&&chess[x+3][y+3]==1)
?? ? w=5;
?? ? else if(chess[x-2][y-2]==1&&chess[x-3][y-3]==1&&chess[x-4][y-4]==1&&chess[x-5][y-5]==1)
?? ? w=5;
?? ? else if(chess[x-2][y]==1&&chess[x-3][y+1]==1&&chess[x-4][y+2]==1&&chess[x-5][y+3]==1)
?? ? w=5;
?? ? else if(chess[x][y-2]==1&&chess[x+1][y-3]==1&&chess[x+2][y-4]==1&&chess[x+3][y-5]==1)
?? ? w=5;
?? ? player=1;}
}
void judge()
{
? ? while(1)//c++類似的使用很多,用永真的表達(dá)式,然后判斷跳出條件break,這里主要用來重復(fù)判斷落子是否合法
? ? {
? ? ? ? if(x<=0||x>10||y<=0||y>10)
? ? ? ? {
? ? ? ? ? ? cout <<"invalid position,input again:"<<endl;
? ? ? ? ? ? cin >>x>>y;
? ? ? ? ? ? }
? ? ? ? else if(chess[x-1][y-1]!=0)
? ? ? ? {
? ? ? ? ? ? cout <<"wrong place,input again:"<<endl;
? ? ? ? ? ? cin >>x>>y;
? ? ? ? ? ? }
? ? ? ? else if(x>0&&x<=10&&y>0&&y<=10&&chess[x-1][y-1]==0)
? ? ? ? ? ? break;
? ? ? ? }
}
int main()
{ ??
? ? init();
? ? board();
? ? while(1)
? ?{
? ? ? ? cout << "Black: ";
? ? ? ? cin>>x>>y;
? ? ? ? judge();
? ? ? ? play(x,y);
? ? ? ? system("cls");//清屏功能
? ? ? ? board();
? ? ? ? if(b==5)
? ? ? ? {
? ? ? ? ? ? system("cls");cout << "Black win";break;
? ? ? ? ? ? }
? ? ? ? else if(w==5)
? ? ? ? {
? ? ? ? ? ? system("cls");cout << "White win";break;
? ? ? ? ? ? }
? ? ? ? cout << "White: " ;
? ? ? ? cin >>x>>y;
? ? ? ? judge();
? ? ? ? play(x,y);
? ? ? ? system("cls");
? ? ? ? board();
? ? ? ? if(b==5)
? ? ? ? {
? ? ? ? ? ? system("cls");cout << "Black win";break;
? ? ? ? ? ? }
? ? ? ? else if(w==5)
? ? ? ? {
? ? ? ? ? ? system("cls");cout << "White win";break;
? ? ? ? ? ? }
? ? ? ? }
? ? return 0;
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
rapidjson解析json代碼實(shí)例以及常見的json core dump問題
今天小編就為大家分享一篇關(guān)于rapidjson解析json代碼實(shí)例以及常見的json core dump問題,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-04-04
C語言驅(qū)動開發(fā)之通過ReadFile與內(nèi)核層通信
驅(qū)動與應(yīng)用程序的通信是非常有必要的,內(nèi)核中執(zhí)行代碼后需要將其動態(tài)顯示給應(yīng)用層。為了實(shí)現(xiàn)內(nèi)核與應(yīng)用層數(shù)據(jù)交互則必須有通信的方法,微軟為我們提供了三種通信方式,本文先來介紹通過ReadFile系列函數(shù)實(shí)現(xiàn)的通信模式2022-09-09

