亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C++代碼實(shí)現(xiàn)五子棋小游戲

 更新時(shí)間:2022年05月05日 14:46:05   作者:A_N_Huang  
這篇文章主要為大家詳細(xì)介紹了C++代碼實(shí)現(xiàn)五子棋小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

簡(jiǎn)單C++代碼實(shí)現(xiàn)五子棋任務(wù),供大家參考,具體內(nèi)容如下

首先先展示一下運(yùn)行的圖片

話也不多說(shuō),直接分不同代碼板塊來(lái)介紹程序不同功能以及是如何實(shí)現(xiàn)的

首先,對(duì)于一個(gè)五子棋程序,我們要思考的,在過(guò)程式的編程思想里面,如何將其功能分解為不同的函數(shù)

1.打印棋盤(pán)

由于使用的棋子每一個(gè)棋子占兩個(gè)字符,所以打印時(shí)要使用兩個(gè)空格

int b=0,w=0,player=1,x,y; //b和w分別作為參數(shù)標(biāo)記黑棋白棋的勝利,player用來(lái)指明每次是下黑棋還是白棋,x,y分別用來(lái)作為棋盤(pán)的橫縱坐標(biāo)
int chess[11][11];//初始化
void board()//每一次打印棋盤(pán)的函數(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ù)組本身無(wú)法同時(shí)對(duì)連續(xù)兩個(gè)字符位賦值,這里采用二位數(shù)組表示下棋位置并采用一個(gè)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;//初始化棋盤(pán)全為0
? ? ? ? }
}
void input(const int n)
{
? ? for(int i=n,j=0;j<10;j++)
? ? {
? ? ? ? switch(chess[i][j])//利用這個(gè)switch語(yǔ)句來(lái)將空白的棋盤(pán)中需要打印的棋子打印上去
? ? ? ? {
? ? ? ? ? ? case(0): cout << " ?";break;
? ? ? ? ? ? case(1): cout << "??";break;
? ? ? ? ? ? case(-1): cout << "??";break;
? ? ? ? ? ? }
? ? }
}

2.下棋部分

這一部分也是最為麻煩的部分,由于每次調(diào)用檢驗(yàn)函數(shù)檢驗(yàn)黑棋或白棋是否勝利會(huì)帶來(lái)不必要的麻煩,所以在每一次下棋之后直接在下棋的位置往各個(gè)方向檢索以判斷是否勝利

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,這里主要用來(lái)重復(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ù)

加下來(lái)就是main函數(shù)部分了,顯而易見(jiàn)了

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)整個(gè)五子棋代碼的功能了

附上完整的代碼:

#include <iostream>
using namespace std;
int b=0,w=0,player=1,x,y; //b和w分別作為參數(shù)標(biāo)記黑棋白棋的勝利,player用來(lái)指明每次是下黑棋還是白棋,x,y分別用來(lái)作為棋盤(pán)的橫縱坐標(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;//初始化棋盤(pán)全為0
? ? ? ? }
}
void input(const int n)
{
? ? for(int i=n,j=0;j<10;j++)
? ? {
? ? ? ? switch(chess[i][j])//利用這個(gè)switch語(yǔ)句來(lái)將空白的棋盤(pán)中需要打印的棋子打印上去
? ? ? ? {
? ? ? ? ? ? case(0): cout << " ?";break;
? ? ? ? ? ? case(1): cout << "??";break;
? ? ? ? ? ? case(-1): cout << "??";break;
? ? ? ? ? ? }
? ? }
}
void board()//每一次打印棋盤(pán)的函數(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,這里主要用來(lái)重復(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;
}

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

相關(guān)文章

  • C語(yǔ)言數(shù)組任意位置插入一個(gè)元素方法

    C語(yǔ)言數(shù)組任意位置插入一個(gè)元素方法

    這篇文章主要給大家分享C語(yǔ)言數(shù)組任意位置插入一個(gè)元素方法,
    2021-11-11
  • rapidjson解析json代碼實(shí)例以及常見(jiàn)的json core dump問(wèn)題

    rapidjson解析json代碼實(shí)例以及常見(jiàn)的json core dump問(wèn)題

    今天小編就為大家分享一篇關(guān)于rapidjson解析json代碼實(shí)例以及常見(jiàn)的json core dump問(wèn)題,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-04-04
  • C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)易貪吃蛇游戲的示例代碼

    C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)易貪吃蛇游戲的示例代碼

    這篇文章主要介紹了如何利用C語(yǔ)言實(shí)現(xiàn)一個(gè)經(jīng)典的小游戲——貪吃蛇,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下
    2022-10-10
  • 如何給隨機(jī)數(shù)加密

    如何給隨機(jī)數(shù)加密

    隨機(jī)數(shù)加密的簡(jiǎn)單算法,需要的朋友可以參考一下
    2013-03-03
  • C++類中變量也可以是引用的代碼實(shí)例

    C++類中變量也可以是引用的代碼實(shí)例

    今天小編就為大家分享一篇關(guān)于C++類中變量也可以是引用的代碼實(shí)例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-04-04
  • 詳解C++編程中的vector類容器用法

    詳解C++編程中的vector類容器用法

    vector是一個(gè)標(biāo)準(zhǔn)庫(kù)中的容器,使用時(shí)需要包含#include <vector>頭文件,也可以說(shuō)vector是一個(gè)類模板而不是一種數(shù)據(jù)類型,對(duì)它的定義,需要指定類型,需要的朋友可以參考下
    2016-05-05
  • C語(yǔ)言驅(qū)動(dòng)開(kāi)發(fā)之通過(guò)ReadFile與內(nèi)核層通信

    C語(yǔ)言驅(qū)動(dòng)開(kāi)發(fā)之通過(guò)ReadFile與內(nèi)核層通信

    驅(qū)動(dòng)與應(yīng)用程序的通信是非常有必要的,內(nèi)核中執(zhí)行代碼后需要將其動(dòng)態(tài)顯示給應(yīng)用層。為了實(shí)現(xiàn)內(nèi)核與應(yīng)用層數(shù)據(jù)交互則必須有通信的方法,微軟為我們提供了三種通信方式,本文先來(lái)介紹通過(guò)ReadFile系列函數(shù)實(shí)現(xiàn)的通信模式
    2022-09-09
  • C/C++中的內(nèi)存管理小結(jié)

    C/C++中的內(nèi)存管理小結(jié)

    這篇文章主要介紹了C/C++中的內(nèi)存管理小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • C語(yǔ)言三子棋小游戲的實(shí)現(xiàn)

    C語(yǔ)言三子棋小游戲的實(shí)現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言三子棋小游戲的實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • boost字符串處理函數(shù)format的用法

    boost字符串處理函數(shù)format的用法

    這篇文章介紹了boost字符串處理函數(shù)format的用法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06

最新評(píng)論