C++實現五子棋小遊戲

五子棋遊戲是在C++學習前期用於練手的很不錯的小項目,實現粗糙的五子棋小遊戲不需要對C++語法瞭解的非常深入也不許需要對數據結構的熟悉和理解達到多麼深的層次 所以是一個非常簡單的小程序

實現五子棋小程序需要合理的設計整個程序的架構 整個遊戲的實現分成:選擇遊戲方式、 初始化棋盤 、 每次落子更新棋盤 、 對是否已決出輸贏的判斷 和 勝者是誰的判斷這幾個模塊 。每個模塊對應實現函數取實現 最後將函數配合起來使用構成完整項目。

這裏寫圖片描述

#include<iostream>
#include<time.h>

#include<stdlib.h>

using namespace std;

const int N = 15;//棋盤大小
const char ChessBoardFlag = ' ';
const char flag1 = 'O';
const char flag2 = 'X';

typedef struct Coordinate
{
    int x;
    int y;
}ChessCoordi;

class FiveChess
{
public:
    FiveChess()
    {
        InitChessBoard();
    }

    void Play()
    {
        ChessCoordi Pos1;
        ChessCoordi Pos2;
        while (1){
            int mode = ChoseMode();
            while (1){
                if (mode == 1){//玩家VS電腦
                    static size_t count = 1;
                    PalyerGo(Pos1, 1, flag1);
                    if (count++ >= 9 && GetWiner(Pos1, 1, flag1))
                        break;
                    ComputerGo(Pos2, flag2);
                    if (count++ >= 10 && GetWiner(Pos2, 0, flag2))
                       break;
                }
                else if (mode == 2){//玩家VS玩家
                    static size_t count = 1;
                    PalyerGo(Pos1, 1, flag1);
                    if (count++ >= 9 && GetWiner(Pos1,1, flag1))
                        break;
                    PalyerGo(Pos2,2 ,flag2);
                    if (count++ >= 10 && GetWiner(Pos2,2, flag2))
                        break;
                }
            }
            cout << "再來一局 y or no" << endl;
            char chose = 'y';
            cin >> chose;
            if (chose == 'n')
                break;
        }
    }

    void PrintChessBoard()
    {
        system("cls");
        for (size_t i = 0; i < N + 1; ++i)
        {
            for (size_t j = 0; j < N + 1; ++j)
            {
                if (i == 0){
                    if (j != 0)
                        printf("%d   ", j);
                    else if (j == 0)
                        printf("  ");
                }
                else if (j == 0){
                    if (i != 0)
                        printf("%2d", i);
                }
                else{
                    printf("%c  |", ChessBoard[i][j]);
                }

            }
            cout << endl;
            cout << "  ";
            for (size_t i = 1; i < N + 1; ++i){
                cout << "---+";
            }
            cout << endl;
        }
    }

    void InitChessBoard()
    {
        for (size_t i = 0; i < N + 1; ++i){
            for (size_t j = 0; j < N + 1; ++j){
                ChessBoard[i][j] = ChessBoardFlag;
            }
        }
    }

protected:

    int ChoseMode()
    {
        system("cls");
        InitChessBoard();
        cout << "請選擇 1.玩家VS電腦 2.玩家VS玩家 3.推出" << endl;
        int chose = 0;
        cin >> chose;
        while (1){
            if (chose == 1)
                return chose;
            else if (chose == 2)
                return chose;
            else if (chose == 3)
                exit(1);
            else
                cout << "對不起 您的輸入有誤。。" << endl;
        }
    }

    void PalyerGo(ChessCoordi& Pos, int player, char flag)
    {
        PrintChessBoard();
        int x = 0;
        int y = 0;
        while (1){
            cout << "請玩家" << player << "下一步棋" << endl;
            cin >> Pos.x >> Pos.y;
            if (JudgePos(Pos))
                break;
            else
                cout << "玩家輸入錯誤 ! 請重新輸入" << endl;
        }
        ChessBoard[Pos.x][Pos.y] = flag;
    }

    void ComputerGo(ChessCoordi& Pos, char flag)
    {
        PrintChessBoard();
        int x = 0;
        int y = 0;
        while (1){
            x = rand() % N + 1;
            srand((unsigned int)time(NULL));
            y = rand() % N + 1;
            srand((unsigned int)time(NULL));//這種方式下生成的x,y一定在棋盤上
            if (ChessBoard[x][y] == ChessBoardFlag)
                break;
        }
        Pos.x = x;
        Pos.y = y;
        ChessBoard[Pos.x][Pos.y] = flag;
    }

    int GetVictory(ChessCoordi Pos, char flag)//判斷是否有贏家
    {
        int begin = 0;//在檢查對角線時 作爲行座標的開始 結束
        int end = 0;
        //檢查行是否連續5個子
        int beginl = 0;//在檢查對角線時 作爲列座標的開始 結束
        int endl = 0;
        (Pos.y - 4) > 0 ? begin = Pos.y - 4 : begin = 1;
        (Pos.y + 4) < N ? end = Pos.y + 4 : end = N;
        for (size_t i = Pos.x, j = begin; j + 4 <= end; ++j)
        {
            if (flag == ChessBoard[i][j] && flag == ChessBoard[i][j + 1] && \
                flag == ChessBoard[i][j + 2] && flag == ChessBoard[i][j + 3] && \
                flag == ChessBoard[i][j + 4])
                return 1;
        }
        //檢查列是否連續5個子
        (Pos.x - 4) > 0 ? begin = Pos.x - 4 : begin = 1;
        (Pos.x + 4) > N ? end = Pos.x + 4 : end = N;
        for (size_t j = Pos.y, i = begin; i + 4 <= end; ++i)
        {
            if (flag == ChessBoard[i][j] && flag == ChessBoard[i + 1][j] && \
                flag == ChessBoard[i + 2][j] && flag == ChessBoard[i + 3][j] && \
                flag == ChessBoard[i + 4][j])
                return 1;
        }

        int len = 0;
        //檢查主對角線是否滿五個子
        (Pos.x > Pos.y) ? len = Pos.y - 1 : len = Pos.x - 1;
        if (len > 4)//找落子點到上 左兩邊的垂直距離較短的地方 如果其大於4 取4 不大於4 取其本身
            len = 4;
        begin = Pos.x - len;//向上 左移動適當距離找可能的五連子的起始位置
        beginl = Pos.y - len;
        (Pos.x > Pos.y) ? len = N - Pos.x : len = N - Pos.y;
        if (len > 4)
            len = 4;
        end = Pos.x + len;//向下 右移動適當距離找可能的五連子的終止位置
        endl = Pos.y + len;
        for (size_t i = begin, j = beginl; i + 4 <= end && j + 4 <= endl; ++i, ++j)
        {
            if (flag == ChessBoard[i][j] && flag == ChessBoard[i + 1][j + 1] && \
                flag == ChessBoard[i + 2][j + 2] && flag == ChessBoard[i + 3][j + 3] && \
                flag == ChessBoard[i + 4][j + 4])
                return 1;
        }
        //檢查副對角線是否滿五個子
        (Pos.x - 1 > N - Pos.y) ? len = N - Pos.y : Pos.x - 1;
        if (len > 4)//找落子點到右 下兩邊的垂直距離較短的地方 如果其大於4 取4 不大於4 取其本身
            len = 4;
        begin = Pos.x - len;//向上 右移動適當距離找可能的五連子的起始位置
        beginl = Pos.y + len;
        (N - Pos.x > Pos.y - 1) ? len = Pos.y - 1 : len = N - Pos.x;
        end = Pos.x + len;//向下 左移動適當距離找可能的五連子的最終位置
        endl = Pos.y - len;
        for (size_t i = begin, j = beginl; i + 4 <= end && j - 4 >= endl; ++i, ++j)
        {
            if (flag == ChessBoard[i][j] && flag == ChessBoard[i + 1][j - 1] && \
                flag == ChessBoard[i + 2][j - 2] && flag == ChessBoard[i + 3][j - 3] && \
                flag == ChessBoard[i + 4][j - 4])
                return 1;
        }
        //檢查棋盤是否已滿
        for (size_t i = 1; i < N + 1; ++i){
            for (size_t j = 1; j < N + 1; ++j){
                if (ChessBoard[i][j] == ChessBoardFlag)
                    return 0;//表示棋盤沒滿
            }
        }
        //和棋
        return -1;
    }

    bool GetWiner(ChessCoordi& Pos, int player, char flag)//判斷是誰贏了
    {
        int n = 0;
        n = GetVictory(Pos, flag);
        PrintChessBoard();
        if (1 == n){
            if (0 == player)
                cout << "玩家1獲勝" << endl;
            else if (1 == player)
                cout << "玩家1獲勝" << endl;
            else
                cout << "電腦獲勝"<<endl;
            return true;
        }
        else if (-1 == n){
            cout << "和棋" << endl;
            return true;
        }
        else{
            //還未分出勝負
            return false;
        }

    }

    bool JudgePos(const ChessCoordi& Pos)
    {
        if (Pos.x < N + 1 && Pos.x > 0 && Pos.y < N + 1 && Pos.x > 0\
            && ChessBoard[Pos.x][Pos.y] == ChessBoardFlag)
            return true;

        return false;
    }

private:
    char ChessBoard[N + 1][N + 1];
};

//#include"FiveChess.cpp"
int main()
{
    //char a[] = "exit";
    //for (size_t i = 0; i < sizeof(a) / sizeof(char);++i)
    //printf(":%d", a[i]);
    FiveChess a;
    a.InitChessBoard();
    a.PrintChessBoard();
    a.Play();
    system("pause");
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章