欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

本文實(shí)例為大家分享了C++實(shí)現(xiàn)五子棋小游戲的具體代碼,供大家參考,具體內(nèi)容如下

思路:先用用system(“color 70”)改變控制臺(tái)的背景色為灰白色,前景色為黑色,然后用“■”打印棋盤,然后用SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), a)改變輸出顏色分別為白色和黑色,用字符“●”打印黑棋和白棋。
整個(gè)棋盤用一個(gè)char類型的二維數(shù)組保存,空的地方用‘ ’標(biāo)識(shí),玩家一下棋的地方用‘x’標(biāo)識(shí),玩家二下棋的地方用‘o’標(biāo)識(shí)(電腦算作玩家一)。
核心代碼模塊在于勝負(fù)判斷,如何在char類型的二維數(shù)組中找到五星連珠?

主要思路:每落一子判斷一次,從這個(gè)子向前數(shù)四個(gè)向后數(shù)四個(gè),在這九個(gè)子中從頭向后查找,看是否有五個(gè)子完全相同,若有這勝利

int judgewiner1(char a, coordinate temp)//判斷橫排
?? ?{
?? ??? ?coordinate begin, end;
?? ??? ?begin.x = end.x = temp.x;
?? ??? ?if (temp.y <= 5)begin.y = 1;
?? ??? ?else begin.y = temp.y - 4;
?? ??? ?if (temp.y >= N-5)end.y = N-1;
?? ??? ?else end.y = temp.y + 4;
?? ??? ?for (int i = begin.x, j = begin.y; ?j <= end.y - 4; ?++j)
?? ??? ?{
?? ??? ??? ?if (chessboard[i][j] == a && chessboard[i][j] == chessboard[i][j + 1] && chessboard[i][j + 1] == chessboard[i][j + 2] && chessboard[i][j + 2] == chessboard[i][j + 3] && chessboard[i][j + 3]== chessboard[i][j + 4])
?? ??? ??? ??? ?return 1;
?? ??? ?}
?? ??? ?return 0;
?? ?}
?? ?int judgewiner2(char a, coordinate temp)//判斷豎排
?? ?{
?? ??? ?coordinate begin, end;
?? ??? ?begin.y = end.y = temp.y;
?? ??? ?if (temp.x <= 5)begin.x = 1;
?? ??? ?else begin.x = temp.x - 4;
?? ??? ?if (temp.x >= ?N-5)end.x = N-1;
?? ??? ?else end.x = temp.x + 4;
?? ??? ?for (int i = begin.x,j = begin.y; i <= end.x - 4; ++i)
?? ??? ?{
?? ??? ??? ?if (chessboard[i][j] == a && chessboard[i][j] == chessboard[i + 1][j] && chessboard[i + 1][j] == chessboard[i + 2][j] && chessboard[i + 2][j] == chessboard[i + 3][j] && chessboard[i + 3][j]== chessboard[i + 4][j])
?? ??? ??? ??? ?return 1;
?? ??? ?}
?? ??? ?return 0;
?? ?}
?? ?int judgewiner3(char a, coordinate temp)//判斷主對(duì)角線
?? ?{
?? ??? ?coordinate begin, end;
?? ??? ?if (temp.x <= 5)begin.x = 1;
?? ??? ?else begin.x = temp.x - 4;
?? ??? ?if (temp.y <= 5)begin.y = 1;
?? ??? ?else begin.y = temp.y - 4;
?? ??? ?if (temp.x >= ?N-5)end.x = N-1;
?? ??? ?else end.x = temp.x + 4;
?? ??? ?if (temp.y >= ?N-5)end.y = N-1;
?? ??? ?else end.y = temp.y + 4;
?? ??? ?for (int i = begin.x,j = begin.y; i <= end.x-4&&j <= end.y-4; ++i, ++j)
?? ??? ?{
?? ??? ??? ?if (chessboard[i][j] == a && chessboard[i][j] == chessboard[i + 1][j + 1] && chessboard[i + 1][j + 1] == chessboard[i + 2][j + 2] && chessboard[i + 2][j + 2] == chessboard[i + 3][j + 3] && chessboard[i + 3][j + 3]==chessboard[i + 4][j + 4])
?? ??? ??? ??? ?return 1;
?? ??? ?}
?? ??? ?return 0;
?? ?}
?? ?int judgewiner4(char a, coordinate temp)//判斷負(fù)對(duì)角線
?? ?{
?? ??? ?coordinate begin, end;
?? ??? ?if (temp.x<=5)begin.x = 1;
?? ??? ?else begin.x = temp.x - 4;
?? ??? ?if (temp.y >= ?N-5)begin.y = N-1;
?? ??? ?else begin.y = temp.y + 4;
?? ??? ?if (temp.x >= ?N-5)end.x = N-1;
?? ??? ?else end.x = temp.x + 4;
?? ??? ?if (temp.y <= 5)end.y = 1;
?? ??? ?else end.y = temp.y - 4;
?? ??? ?for (int i = begin.x,j = begin.y; i <= end.x - 4 && j >= end.y - 4; ++i, --j)
?? ??? ?{
?? ??? ??? ?if (chessboard[i][j] == a && chessboard[i][j] == chessboard[i + 1][j - 1] && chessboard[i + 1][j - 1] == chessboard[i + 2][j - 2] && chessboard[i + 2][j - 2] == chessboard[i + 3][j - 3] && chessboard[i + 3][j - 3]==chessboard[i + 4][j - 4])
?? ??? ??? ??? ?return 1;
?? ??? ?}
?? ??? ?return 0;
?? ?}
?? ?int judgeheqi()//判斷和棋
?? ?{
?? ??? ?for(int i=1;i<N;++i)
?? ??? ??? ?for (int j = 1; j < N; ++j)
?? ??? ??? ??? ?if (chessboard[i][j] == ' ')return 1;
?? ??? ?return -1;
?? ?}

下面是完整代碼:

//玩家一與電腦用‘x'標(biāo)識(shí),玩家二用'o'標(biāo)識(shí)
#include<iostream>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
using namespace::std;

#define N 20
char chessboardflag = ' '; ?//棋盤標(biāo)志
void color(int a)//改變顏色的函數(shù)
{
?? ?SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), a);
}
class coordinate ? ? ? ? ? ?//坐標(biāo)類
{
public:
?? ?int x;
?? ?int y;
};
class fivechess
{
public:
?? ?void initchessboard()//初始化棋盤
?? ?{
?? ??? ?for (int i = 0; i < N; ++i)
?? ??? ??? ?for (int j = 0; j < N; ++j)
?? ??? ??? ??? ?chessboard[i][j] = chessboardflag;
?? ??? ?printchessboard();
?? ?}
?? ?void printchessboard()//打印棋盤
?? ?{
?? ??? ?system("cls");
?? ??? ?system("color 70");
?? ??? ?cout << " ?1 2 3 4 5 6 7 8 9 10111213141516171819"<<endl;
?? ??? ?for (int i = 1; i < N; ++i) {
?? ??? ??? ?for (int j = 0; j < N; ++j)
?? ??? ??? ?{
?? ??? ??? ??? ?if (j == 0) {
?? ??? ??? ??? ??? ?if (i <= 9)cout << i << " ";
?? ??? ??? ??? ??? ?else cout << i;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?else
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?if (chessboard[i][j] == 'x')
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?color(0x70);
?? ??? ??? ??? ??? ??? ?cout << "●";
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?else if (chessboard[i][j] == 'o')
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?color(0x7f);
?? ??? ??? ??? ??? ??? ?cout << "●";
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?else?
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?color(0x74);
?? ??? ??? ??? ??? ??? ?cout << "■";
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ??? ?cout << endl;
?? ??? ??? ?color(0x70);
?? ??? ?}
?? ?}
?? ?coordinate playchess1() ?//玩家一下棋
?? ?{
?? ??? ?cout << "請(qǐng)玩家一輸入坐標(biāo):" << endl;
?? ??? ?int x1, y1;
?? ??? ?while (cin >> x1 >> y1)
?? ??? ?{
?? ??? ??? ?if (x1 > N-1 || y1 > N-1 || x1 < 0 || y1 < 0)
?? ??? ??? ?{
?? ??? ??? ??? ?cout << "輸入超界,請(qǐng)從新輸入" << endl;
?? ??? ??? ??? ?continue;
?? ??? ??? ?}
?? ??? ??? ?if (chessboard[x1][y1] == ' ') {
?? ??? ??? ??? ?chessboard[x1][y1] = 'x';
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ??? ?else
?? ??? ??? ?{
?? ??? ??? ??? ?cout << "輸入錯(cuò)誤,請(qǐng)重新輸入";
?? ??? ??? ?}
?? ??? ?}
?? ??? ?coordinate temp;
?? ??? ?temp.x = x1;
?? ??? ?temp.y = y1;
?? ??? ?return temp;
?? ?}
?? ?coordinate playchess2() ?//玩家二下棋
?? ?{
?? ??? ?cout << "請(qǐng)玩家2輸入坐標(biāo):" << endl;
?? ??? ?int x2, y2;
?? ??? ?while (cin >> x2 >> y2)
?? ??? ?{
?? ??? ??? ?if (x2 > N-1 || y2 > N-1 || x2 < 0 || y2 < 0)
?? ??? ??? ?{
?? ??? ??? ??? ?cout << "輸入超界,請(qǐng)從新輸入" << endl;
?? ??? ??? ??? ?continue;
?? ??? ??? ?}
?? ??? ??? ?if (chessboard[x2][y2] == ' ') {
?? ??? ??? ??? ?chessboard[x2][y2] = 'o';
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ??? ?else
?? ??? ??? ?{
?? ??? ??? ??? ?cout << "輸入錯(cuò)誤,請(qǐng)重新輸入";
?? ??? ??? ?}
?? ??? ?}
?? ??? ?coordinate temp;
?? ??? ?temp.x = x2;
?? ??? ?temp.y = y2;
?? ??? ?return temp;
?? ?}
?? ?coordinate computerplayer()//電腦下棋
?? ?{
?? ??? ?coordinate temp;
?? ??? ?srand((unsigned)time(NULL));
?? ??? ?int x1 = 0, y1 = 0;
?? ??? ?while ((x1 = (rand() % (N-1)) + 1) && (y1 = (rand() % (N-1)) + 1))
?? ??? ?{
?? ??? ??? ?if (chessboard[x1][y1] == ' ') {
?? ??? ??? ??? ?chessboard[x1][y1] = 'x';
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ??? ?else continue;
?? ??? ?}
?? ??? ?temp.x = x1; temp.y = y1;
?? ??? ?return temp;
?? ?}
?? ?int judgewiner1(char a, coordinate temp)//判斷橫排
?? ?{
?? ??? ?coordinate begin, end;
?? ??? ?begin.x = end.x = temp.x;
?? ??? ?if (temp.y <= 5)begin.y = 1;
?? ??? ?else begin.y = temp.y - 4;
?? ??? ?if (temp.y >= N-5)end.y = N-1;
?? ??? ?else end.y = temp.y + 4;
?? ??? ?for (int i = begin.x, j = begin.y; j <= end.y - 4; ++j)
?? ??? ?{
?? ??? ??? ?if (chessboard[i][j] == a?
?? ??? ??? ??? ?&& chessboard[i][j] == chessboard[i][j + 1]?
?? ??? ??? ??? ?&& chessboard[i][j + 1] == chessboard[i][j + 2]
?? ??? ??? ??? ?&& chessboard[i][j + 2] == chessboard[i][j + 3]?
?? ??? ??? ??? ?&& chessboard[i][j + 3] == chessboard[i][j + 4])
?? ??? ??? ??? ?return 1;
?? ??? ?}
?? ??? ?return 0;
?? ?}
?? ?int judgewiner2(char a, coordinate temp)//判斷豎排
?? ?{
?? ??? ?coordinate begin, end;
?? ??? ?begin.y = end.y = temp.y;
?? ??? ?if (temp.x <= 5)begin.x = 1;
?? ??? ?else begin.x = temp.x - 4;
?? ??? ?if (temp.x >= N-5)end.x =N-1;
?? ??? ?else end.x = temp.x + 4;
?? ??? ?for (int i = begin.x, j = begin.y; i <= end.x - 4; ++i)
?? ??? ?{
?? ??? ??? ?if (chessboard[i][j] == a
?? ??? ??? ??? ?&& chessboard[i][j] == chessboard[i + 1][j]?
?? ??? ??? ??? ?&& chessboard[i + 1][j] == chessboard[i + 2][j]?
?? ??? ??? ??? ?&& chessboard[i + 2][j] == chessboard[i + 3][j]?
?? ??? ??? ??? ?&& chessboard[i + 3][j] == chessboard[i + 4][j])
?? ??? ??? ??? ?return 1;
?? ??? ?}
?? ??? ?return 0;
?? ?}
?? ?int judgewiner3(char a, coordinate temp)//判斷主對(duì)角線
?? ?{
?? ??? ?coordinate begin, end;
?? ??? ?if (temp.x <= 5)begin.x = 1;
?? ??? ?else begin.x = temp.x - 4;
?? ??? ?if (temp.y <= 5)begin.y = 1;
?? ??? ?else begin.y = temp.y - 4;
?? ??? ?if (temp.x >= N-5)end.x = N-1;
?? ??? ?else end.x = temp.x + 4;
?? ??? ?if (temp.y >= N-5)end.y = N-1;
?? ??? ?else end.y = temp.y + 4;
?? ??? ?for (int i = begin.x, j = begin.y; i <= end.x - 4 && j <= end.y - 4; ++i, ++j)
?? ??? ?{
?? ??? ??? ?if (chessboard[i][j] == a && chessboard[i][j] == chessboard[i + 1][j + 1]?
?? ??? ??? ??? ?&& chessboard[i + 1][j + 1] == chessboard[i + 2][j + 2]?
?? ??? ??? ??? ?&& chessboard[i + 2][j + 2] == chessboard[i + 3][j + 3]?
?? ??? ??? ??? ?&& chessboard[i + 3][j + 3] == chessboard[i + 4][j + 4])
?? ??? ??? ??? ?return 1;
?? ??? ?}
?? ??? ?return 0;
?? ?}
?? ?int judgewiner4(char a, coordinate temp)//判斷負(fù)對(duì)角線
?? ?{
?? ??? ?coordinate begin, end;
?? ??? ?if (temp.x <= 5)begin.x = 1;
?? ??? ?else begin.x = temp.x - 4;
?? ??? ?if (temp.y >= N-5)begin.y = N-1;
?? ??? ?else begin.y = temp.y + 4;
?? ??? ?if (temp.x >= N-5)end.x = N-1;
?? ??? ?else end.x = temp.x + 4;
?? ??? ?if (temp.y <= 5)end.y = 1;
?? ??? ?else end.y = temp.y - 4;
?? ??? ?for (int i = begin.x, j = begin.y; i <= end.x - 4 && j >= end.y - 4; ++i, --j)
?? ??? ?{
?? ??? ??? ?if (chessboard[i][j] == a?
?? ??? ??? ??? ?&& chessboard[i][j] == chessboard[i + 1][j - 1]?
?? ??? ??? ??? ?&& chessboard[i + 1][j - 1] == chessboard[i + 2][j - 2]
?? ??? ??? ??? ?&& chessboard[i + 2][j - 2] == chessboard[i + 3][j - 3]?
?? ??? ??? ??? ?&& chessboard[i + 3][j - 3] == chessboard[i + 4][j - 4])
?? ??? ??? ??? ?return 1;
?? ??? ?}
?? ??? ?return 0;
?? ?}
?? ?int judgeheqi()//判斷和棋
?? ?{
?? ??? ?for (int i = 1; i < N; ++i)
?? ??? ??? ?for (int j = 1; j < N; ++j)
?? ??? ??? ??? ?if (chessboard[i][j] == ' ')return 1;
?? ??? ?return -1;
?? ?}
?? ?void play()
?? ?{
?? ??? ?initchessboard();//初始化棋盤
?? ??? ?int t;
?? ??? ?cout << "請(qǐng)選擇模式,人機(jī)模式輸入1,人人模式輸入2。" << endl;
?? ??? ?cin >> t;
?? ??? ?while (t == 1)
?? ??? ?{
?? ??? ??? ?int m = judgeheqi();
?? ??? ??? ?if (m == -1)
?? ??? ??? ?{
?? ??? ??? ??? ?cout << "和棋!!!" << endl;
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ??? ?coordinate temp1 = computerplayer();//電腦下棋
?? ??? ??? ?printchessboard();//打印棋盤
?? ??? ??? ?if (judgewiner1('x', temp1) || judgewiner2('x', temp1) || judgewiner3('x', temp1) || judgewiner4('x', temp1))
?? ??? ??? ?{
?? ??? ??? ??? ?cout << "電腦勝?。?!";
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ??? ?coordinate temp2 = playchess2();//玩家2下棋
?? ??? ??? ?printchessboard();//打印棋盤
?? ??? ??? ?if (judgewiner1('o', temp2) || judgewiner2('o', temp2) || judgewiner3('o', temp2) || judgewiner4('o', temp2))
?? ??? ??? ?{
?? ??? ??? ??? ?cout << "玩家2勝!!!";
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?while (t == 2)
?? ??? ?{
?? ??? ??? ?int m = judgeheqi();
?? ??? ??? ?if (m == -1)
?? ??? ??? ?{
?? ??? ??? ??? ?cout << "和棋!!!" << endl;
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ??? ?coordinate temp1 = playchess1();//電腦下棋
?? ??? ??? ?printchessboard();//打印棋盤
?? ??? ??? ?if (judgewiner1('x', temp1) || judgewiner2('x', temp1) || judgewiner3('x', temp1) || judgewiner4('x', temp1))
?? ??? ??? ?{
?? ??? ??? ??? ?cout << "玩家1勝?。?!";
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ??? ?coordinate temp2 = playchess2();//玩家2下棋
?? ??? ??? ?printchessboard();//打印棋盤
?? ??? ??? ?if (judgewiner1('o', temp2) || judgewiner2('o', temp2) || judgewiner3('o', temp2) || judgewiner4('o', temp2))
?? ??? ??? ?{
?? ??? ??? ??? ?cout << "玩家2勝!!!";
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ?}
?? ?}
private:
?? ?char chessboard[N][N]; ?//棋盤
};
int main()
{
?? ?fivechess one;
?? ?one.play();
?? ?return 0;
}

運(yùn)行結(jié)果:

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

相關(guān)文章

  • 數(shù)據(jù)結(jié)構(gòu) 數(shù)組順序存儲(chǔ)詳細(xì)介紹

    數(shù)據(jù)結(jié)構(gòu) 數(shù)組順序存儲(chǔ)詳細(xì)介紹

    這篇文章主要介紹了數(shù)據(jù)結(jié)構(gòu) 數(shù)組順序存儲(chǔ)詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • 淺析C語言中printf(),sprintf(),scanf(),sscanf()的用法和區(qū)別

    淺析C語言中printf(),sprintf(),scanf(),sscanf()的用法和區(qū)別

    以下是對(duì)C語言中printf(),sprintf(),scanf(),sscanf()的用法以及區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以參考下
    2013-07-07
  • C/C++ 中memset() 函數(shù)詳解及其作用介紹

    C/C++ 中memset() 函數(shù)詳解及其作用介紹

    這篇文章主要介紹了C/C++ 中memset() 函數(shù)詳解及其作用介紹,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • C++運(yùn)算符重載的方法詳細(xì)解析

    C++運(yùn)算符重載的方法詳細(xì)解析

    運(yùn)算符重載的方法是定義一個(gè)重載運(yùn)算符的函數(shù),在需要執(zhí)行被重載的運(yùn)算符時(shí),系統(tǒng)就自動(dòng)調(diào)用該函數(shù),以實(shí)現(xiàn)相應(yīng)的運(yùn)算。也就是說,運(yùn)算符重載是通過定義函數(shù)實(shí)現(xiàn)的
    2013-10-10
  • C語言高效編程的幾招小技巧

    C語言高效編程的幾招小技巧

    這篇文章主要介紹了C語言高效編程的幾招小技巧,本文講解了以空間換時(shí)間、用數(shù)學(xué)方法解決問題以及使用位操作等編輯技巧,并給出若干方法和代碼實(shí)例,需要的朋友可以參考下
    2015-05-05
  • C++命名空間域的實(shí)現(xiàn)示例

    C++命名空間域的實(shí)現(xiàn)示例

    命名空間域就是一個(gè)獨(dú)立的空間外面不能直接調(diào)用該空間域只能用訪問限定符指定訪問該空間域,本文主要介紹了C++命名空間域的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-01-01
  • C語言全面細(xì)致講解文件操作

    C語言全面細(xì)致講解文件操作

    這篇文章主要為大家詳細(xì)介紹了C語言的文件操作,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-05-05
  • linux中查詢dns示例

    linux中查詢dns示例

    這篇文章主要介紹了linux中查詢dns示例,需要的朋友可以參考下
    2014-04-04
  • C++中類型推斷(auto和decltype)的使用

    C++中類型推斷(auto和decltype)的使用

    在C++11之前,每個(gè)數(shù)據(jù)類型都需要在編譯時(shí)顯示聲明,在運(yùn)行時(shí)限制表達(dá)式的值,但在C++的新版本之后,引入了 auto 和 decltype等關(guān)鍵字,本文就來介紹一下C++中類型推斷(auto和decltype)的使用,感興趣的可以了解一下
    2023-12-12
  • C/C++實(shí)現(xiàn)三路快速排序算法原理

    C/C++實(shí)現(xiàn)三路快速排序算法原理

    這篇文章主要為大家詳細(xì)介紹了C/C++實(shí)現(xiàn)三路快速排序算法原理,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-05-05

最新評(píng)論