C++之五子棋人机对战模式

该博客使用C++语言实现了黑白棋游戏。定义了游戏结果枚举,包含继续、黑胜、白胜、平局。创建了游戏树类用于评估局面,实现了检查胜负结果、显示胜负比例等功能,支持人机对战和机机对战两种模式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#define _CRT_SECURE_NO_WARNINGS
#ifdef UNICODE
#undef UNICODE
#endif

#include<iostream>
#include<string>
#include<memory.h>
#include<set>
#include<deque>
#include<vector>
#include<easyx.h>
#include<graphics.h>
#include<conio.h>
#define WIDTH 450
#define HEIGHT 450
using namespace std;
struct mm
{
    int x;
    int y;
};
mm my = { 0,0 };
//全局声明
// 结果枚举
enum GameResult { CONTINUE, BLACK_WIN, WHITE_WIN, DRAW };

// 全局变量,用于记录胜负结果
int blackWins = 0;//AI胜利
int whiteWins = 0;//玩家胜利

// 函数声明
GameResult checkGameResult(uint8_t board[15][15]);
void showResultDialog(GameResult result);
void drawWinRate();
void machine_human_play();
// 在游戏结束后显示胜负结果对话框,并根据用户选择决定下一步操作
void showResultDialog(GameResult result) {
    string message;
    if (result == BLACK_WIN) {
        message = "黑棋获胜!是否开始下一局?";
        blackWins++;
    }
    else if (result == WHITE_WIN) {
        message = "白棋获胜!是否开始下一局?";
        whiteWins++;
    }
    else {
        message = "平局!是否开始下一局?";
    }
    int choice = MessageBox(GetHWnd(), message.c_str(), "游戏结束", MB_YESNO);
    if (choice == IDYES) {
        // 清空棋盘并重新开始游戏
        cleardevice();
        machine_human_play();
    }
    else {
        // 退出游戏
        closegraph();
        exit(0);
    }
}

// 在左上角显示胜负比例
void drawWinRate() {
    char rateStr[256] = "";
    double totalGames = blackWins + whiteWins;
    // double blackWinRate = (blackWins / totalGames) * 100.0;
    // double whiteWinRate = (whiteWins / totalGames) * 100.0;
    double AIUSER = (whiteWins / totalGames) * 100;
    sprintf(rateStr, "<第%.0f场>_[AI黑 VS 玩家白]_<%d:%d>_玩家胜率<%0.2lf ﹪>",
        totalGames + 1, (int)blackWins, (int)whiteWins, AIUSER);
    settextstyle(15, 0, "宋体");
    settextcolor(YELLOW);
    outtextxy(25, 5, rateStr);
}

// 在游戏结束后检查胜负结果
GameResult checkGameResult(uint8_t board[15][15]) {
    // 这里添加你检查胜负结果的代码
    return CONTINUE;
}

//
class GameTree {
private:
    class Node {
    public:
        int32_t value;
        uint32_t depth;
        Node* father;
        set<Node*> children;
        uint8_t cntX, cntY;
        uint8_t board[15][15]{};

        Node() {
            father = nullptr;
            children.clear();
            value = INT32_MIN;
            depth = cntX = cntY = 0;
            memset(board, 0, sizeof(board));
        }

        Node(Node* node, uint8_t opeX, uint8_t opeY) {
            depth = node->depth + 1;
            value = is_max_node() ? INT32_MIN : INT32_MAX;
            father = node;
            children.clear();
            cntX = opeX;
            cntY = opeY;
            memcpy(board, node->board, sizeof(board));
            board[cntX][cntY] = (depth & 1u) ? 'B' : 'W';
        }

        bool is_max_node() {
            return (depth & 1u) ^ 1u;
        }

        static int32_t evaluate_black(string& s) {
            string patterns[31] = {
                    "B0000", "0B000", "00B00", "000B0", "0000B",
                    "BB000", "0BB00", "00BB0", "000BB", "B0B00", "0B0B0", "00B0B", "B00B0", "0B00B", "B000B",
                    "BBB00", "0BBB0", "00BBB", "BB0B0", "0BB0B", "B0BB0", "0B0BB", "BB00B", "B00BB", "B0B0B",
                    "BBBB0", "BBB0B", "BB0BB", "B0BBB", "0BBBB", "BBBBB",
            };
            int32_t scores[31] = {
                    1, 1, 1, 1, 1,
                    10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
                    100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
                    10000, 10000, 10000, 10000, 10000, 1000000,
            };
            for (uint8_t i = 0; i < 31; i++)
                if (s == patterns[i]) return scores[i];
            return 0;
        }

        static int32_t evaluate_white(string& s) {
            string patterns[31] = {
                    "W0000", "0W000", "00W00", "000W0", "0000W",
                    "WW000", "0WW00", "00WW0", "000WW", "W0W00", "0W0W0", "00W0W", "W00W0", "0W00W", "W000W",
                    "WWW00", "0WWW0", "00WWW", "WW0W0", "0WW0W", "W0WW0", "0W0WW", "WW00W", "W00WW", "W0W0W",
                    "WWWW0", "WWW0W", "WW0WW", "W0WWW", "0WWWW", "WWWWW",
            };
            int32_t scores[31] = {
                    1, 1, 1, 1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值