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

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

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

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

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

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

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

1.打印棋盤

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

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í)對(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;//初始化棋盤全為0
? ? ? ? }
}
void input(const int n)
{
? ? for(int i=n,j=0;j<10;j++)
? ? {
? ? ? ? switch(chess[i][j])//利用這個(gè)switch語句來將空白的棋盤中需要打印的棋子打印上去
? ? ? ? {
? ? ? ? ? ? case(0): cout << " ?";break;
? ? ? ? ? ? case(1): cout << "??";break;
? ? ? ? ? ? case(-1): cout << "??";break;
? ? ? ? ? ? }
? ? }
}

2.下棋部分

這一部分也是最為麻煩的部分,由于每次調(diào)用檢驗(yàn)函數(shù)檢驗(yàn)黑棋或白棋是否勝利會(huì)帶來不必要的麻煩,所以在每一次下棋之后直接在下棋的位置往各個(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,這里主要用來重復(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)整個(gè)五子棋代碼的功能了

附上完整的代碼:

#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])//利用這個(gè)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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C語言各種符號(hào)的使用介紹上篇

    C語言各種符號(hào)的使用介紹上篇

    C 語言的基本符號(hào)就有 20 多個(gè),每個(gè)符號(hào)可能同時(shí)具有多重含義,而且這些符號(hào)之間相互組合又使得 C 語言中的符號(hào)變得更加復(fù)雜起來
    2022-08-08
  • 用C語言winform編寫滲透測試工具實(shí)現(xiàn)SQL注入功能

    用C語言winform編寫滲透測試工具實(shí)現(xiàn)SQL注入功能

    本篇文章主要介紹使用C#winform編寫滲透測試工具,實(shí)現(xiàn)SQL注入的功能。使用python編寫SQL注入腳本,基于get顯錯(cuò)注入的方式進(jìn)行數(shù)據(jù)庫的識(shí)別、獲取表名、獲取字段名,最終獲取用戶名和密碼;使用C#winform編寫windows客戶端軟件調(diào)用.py腳本,實(shí)現(xiàn)用戶名和密碼的獲取
    2021-08-08
  • C++和java設(shè)計(jì)模式之單例模式

    C++和java設(shè)計(jì)模式之單例模式

    這篇文章主要為大家詳細(xì)介紹了C++和java設(shè)計(jì)模式之單例模式的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • c++中template對(duì)字符串的處理方法

    c++中template對(duì)字符串的處理方法

    這篇文章主要介紹了c++中template對(duì)字符串的處理方法,需要的朋友可以參考下
    2014-07-07
  • C++智能指針的使用

    C++智能指針的使用

    本文主要介紹了C++智能指針的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-02-02
  • C語言中自動(dòng)與強(qiáng)制轉(zhuǎn)換全解析

    C語言中自動(dòng)與強(qiáng)制轉(zhuǎn)換全解析

    在編寫C程序時(shí),類型轉(zhuǎn)換是確保數(shù)據(jù)正確性和一致性的關(guān)鍵環(huán)節(jié),無論是隱式轉(zhuǎn)換還是顯式轉(zhuǎn)換,都各有特點(diǎn)和應(yīng)用場景,本文將詳細(xì)探討C語言中的類型轉(zhuǎn)換機(jī)制,幫助您更好地理解并在實(shí)際編碼中靈活運(yùn)用這些知識(shí),需要的朋友可以參考下
    2025-02-02
  • C++中的模板template小結(jié)

    C++中的模板template小結(jié)

    這篇文章主要介紹了C++中的模板template的相關(guān)知識(shí),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03
  • C++使用WideCharToMultiByte函數(shù)生成UTF-8編碼文件的方法

    C++使用WideCharToMultiByte函數(shù)生成UTF-8編碼文件的方法

    用來映射Unicode字符串的WideCharToMultiByte函數(shù)經(jīng)常被用來進(jìn)行UTF-8編碼的轉(zhuǎn)換,以下我們將看到C++使用WideCharToMultiByte函數(shù)生成UTF-8編碼文件的方法,首先先來對(duì)WideCharToMultiByte作一個(gè)詳細(xì)的了解:
    2016-06-06
  • C++中使用正則匹配問題

    C++中使用正則匹配問題

    這篇文章主要介紹了C++中使用正則匹配問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • C++二叉搜索樹BSTree使用詳解

    C++二叉搜索樹BSTree使用詳解

    二叉搜索樹(Binary Search Tree)又稱二叉排序樹,也稱作二叉查找樹它或者是一棵空樹,或者是具有以下性質(zhì)的二叉樹,若它的左子樹不為空,則左子樹上所有節(jié)點(diǎn)的值都小于根節(jié)點(diǎn)的值,若它的右子樹不為空,則右子樹上所有節(jié)點(diǎn)的值都大于根節(jié)點(diǎn)的值
    2023-03-03

最新評(píng)論

轮台县| 梁平县| 精河县| 称多县| 美姑县| 海晏县| 金湖县| 崇明县| 依安县| 永济市| 高阳县| 大竹县| 临潭县| 房山区| 华亭县| 礼泉县| 桐城市| 黄龙县| 咸丰县| 伽师县| 新津县| 磴口县| 平顶山市| 长岭县| 汝南县| 塘沽区| 绩溪县| 延川县| 开鲁县| 分宜县| 娄底市| 扶风县| 临海市| 舒城县| 大埔县| 马边| 石楼县| 徐州市| 霍山县| 福安市| 慈溪市|