五子棋理解C++思想

双人五子棋项目目录:
在这里插入图片描述

class Game {
public:
	Game();
	void init();
	bool waitPlayerPutChess(Player* player, int& oldi, int& oldj);
	void draw();
	void play();
	bool isOver(int playerId);

public:
	int whoWin = -1;		
	// 谁赢了(0:白棋,1:黑棋,2:平局)
	//谁赢了(0:白棋,1:黑棋,2:red,3:平局)
	int total = 0;			// 一共下了多少个棋子
	Player* player[3];		// 两个玩家  3ge
	ChessBoard* chessBoard; // 棋盘
};

胜负判断:

// 判断游戏是否结束
bool Game::isOver(int playerId) {
    bool isInit = true; // 是否刚刚开局
    for (int i = 0; i < MAP_SIZE; i++) {
        for (int j = 0; j < MAP_SIZE; j++) {
            if (!this->chessBoard->chess[i][j].isEmpty()) {
                // 遍历每个可能的位置
                isInit = false; // 如果有棋子,那么就不是刚刚开局
                int nowcolor = this->chessBoard->chess[i][j].getValue(); // 当前棋子的颜色
                int length[4] = { 0, 0, 0, 0 }; // 四个方向的长度

                for (int k = 0; k < 4; k++) {
                    // 检查每个方向
                    int nowi = i;
                    int nowj = j;
                    // 正向检查当前颜色的连续棋子
                    while (nowi < MAP_SIZE && nowj < MAP_SIZE && nowi >= 0 && nowj >= 0 &&
                           this->chessBoard->chess[nowi][nowj].getValue() == nowcolor) {
                        length[k]++;
                        nowj += dx[k];
                        nowi += dy[k];
                    }
                    nowi = i;
                    nowj = j;
                    // 反向检查当前颜色的连续棋子
                    while (nowi < MAP_SIZE && nowj < MAP_SIZE && nowi >= 0 && nowj >= 0 &&
                           this->chessBoard->chess[nowi][nowj].getValue() == nowcolor) {
                        length[k]++;
                        nowj -= dx[k];
                        nowi -= dy[k];
                    }
                    length[k]--; // 去掉重复计算的当前棋子
                }

                for (int k = 0; k < 4; k++) {
                    // 如果某个方向有五个或更多连续棋子
                    if (length[k] >= 5) {
                        this->whoWin = playerId; // 设置当前玩家为获胜者
                    }
                }

                // 如果棋盘已满,则为平局
                if (this->total == MAP_SIZE * MAP_SIZE) {
                    this->whoWin = 2; // 平局
                }
            }
        }
    }

    if (this->whoWin == -1) {
        // 如果没有人胜利,继续游戏
        Sleep(500);
        return false;
    }
    return true; // 游戏结束,有人胜利或平局
}

game.cpp完整代码:

#include "Game.h"

int dx[4]{ 1, 0, 1, 1 }; // - | \ / 四个方向
int dy[4]{ 0, 1, 1, -1 };

TCHAR message[3][20] = { _T("白胜"), _T("黑胜"), _T("平局")};

Game::Game() {
	this->player[0] = new WhitePlayer("白棋");
	this->player[1] = new BlackPlayer("黑棋");
	this->chessBoard = new ChessBoard();
}

// 游戏初始化
void Game::init() {
	initgraph(WINDOW_WIDTH, WINDOW_HEIGHT, EX_NOMINIMIZE);	// 初始化窗口,NOMINIMIZE表示不允许最小化
	setbkcolor(WHITE);					// 设置背景颜色
	setbkmode(TRANSPARENT);				// 设置透明文字输出背景
}

// 等待玩家下棋
bool Game::waitPlayerPutChess(Player* player, int& oldi, int& oldj) {
	while (1) {
		ExMessage mouse = getmessage(EX_MOUSE); // 获取鼠标信息
		for (int i = 0; i < MAP_SIZE; i++) {
			for (int j = 0; j < MAP_SIZE; j++) {
				if (this->chessBoard->canPut(i, j, mouse)) {
					// 如果停在某一个空位置上面
					if (mouse.lbutton) {
						// 如果按下了
						this->total++;						// 下棋个数+1
						player->placeChess(chessBoard, i, j);
						oldi = -1;
						oldj = -1;
						return true;
					}
					// 更新选择框
					this->chessBoard->chess[oldi][oldj].setIsnew(false);
					this->chessBoard->chess[oldi][oldj].draw();
					this->chessBoard->chess[i][j].setIsnew(true);
					this->chessBoard->chess[i][j].draw();
					oldi = i;
					oldj = j;
				}
			}
		}
	}
}

void Game::draw() {
	this->whoWin = -1;// 谁赢了
	this->total = 0;
	for (int i = 0; i < MAP_SIZE; i++) {
		for (int j = 0; j < MAP_SIZE; j++) {
			this->chessBoard->chess[i][j].setValue(-1);	// 表示空位置
		}
	}
	cleardevice();
	// 绘制背景
	setfillcolor(RGB(255, 205, 150));
	solidrectangle(40, 25, 475, 460);
	// 设置字体样式
	settextstyle(30, 15, 0, 0, 0, 1000, false, false, false);
	settextcolor(BLACK);
	this->chessBoard->draw(); // 绘制
}

// 开始游戏
void Game::play() {
	// 上一个鼠标停的坐标
	int oldi = 0;
	int oldj = 0;
	// 0 白棋先下, 1 黑棋后下, 轮循
	int curPlayerId = 0;
	while (1) {
		// 等待玩家放棋子
		if (this->waitPlayerPutChess(this->player[curPlayerId], oldi, oldj)) {
			this->chessBoard->draw();
			if (this->isOver(curPlayerId)) {
				// 弹框提示
				MessageBox(GetHWnd(), message[this->whoWin], _T("游戏结束"), MB_ICONWARNING);
				break;
			}
			curPlayerId = !curPlayerId;  // 0 变 1, 1 变 0 
		}
	}
}

// 判断游戏是否结束
bool Game::isOver(int playerId) {
	bool isInit = true; // 是否刚刚开局
	for (int i = 0; i < MAP_SIZE; i++) {
		for (int j = 0; j < MAP_SIZE; j++) {
			if (!this->chessBoard->chess[i][j].isEmpty()) {
				// 遍历每个可能的位置
				isInit = false;                 // 如果有,那么就不是刚刚开局
				int nowcolor = this->chessBoard->chess[i][j].getValue(); // 现在遍历到的颜色
				int length[4] = { 0, 0, 0, 0 };    // 四个方向的长度
				for (int k = 0; k < 4; k++) {
					// 原理同寻找最佳位置
					int nowi = i;
					int nowj = j;
					while (nowi < MAP_SIZE && nowj < MAP_SIZE && nowi >= 0 && nowj >= 0 && this->chessBoard->chess[nowi][nowj].getValue() == nowcolor)
					{
						length[k]++;
						nowj += dx[k];
						nowi += dy[k];
					}
					nowi = i;
					nowj = j;
					while (nowi < MAP_SIZE && nowj < MAP_SIZE && nowi >= 0 && nowj >= 0 && this->chessBoard->chess[nowi][nowj].getValue() == 1 - nowcolor)
					{
						length[k]++;
						nowj -= dx[k];
						nowi -= dy[k];
					}
				}
				for (int k = 0; k < 4; k++) {
					// 如果满五子
					if (length[k] >= 5) {
						this->whoWin = playerId;
					}
				}
				// 全都下满了都没有位置了
				if (this->total == MAP_SIZE * MAP_SIZE) {
					this->whoWin = 2; // 平局
				}
			}
		}
	}
	if (this->whoWin == -1) {
		// 如果没有人胜利
		Sleep(500);
		return false;
	}
	return true;
}

在这里插入图片描述
定义四个方向

int dx[4]{ 1, 0, 1, 1 }; // -  |  \  /  四个方向
int dy[4]{ 0, 1, 1, -1 };

dx数组对应水平方向的偏移量,dy数组对应垂直方向的偏移量。
留作胜负判断时候正方向和反方向偏移判断是否有棋子
在判断游戏是否结束的函数isOver中被使用。通过遍历棋盘上的每个位置,对于非空位置,尝试在四个方向上进行延伸搜索,以判断是否有连续五个相同颜色的棋子。
在搜索过程中,通过不断更新当前位置的坐标nowi和nowj,使用nowi += dy[k]和nowj += dx[k]来沿着特定方向前进,从而检查该方向上的棋子颜色是否相同。
例如,当k = 0时,表示水平向右的方向,每次循环会将当前位置的横坐标增加 1(即nowj += dx[k]和nowi += dy[k]变为nowj += 1和nowi += 0),以检查水平方向上是否有连续相同颜色的棋子。
length[k]>= 5则返回玩家标识ID

/
加一个玩家,暂且定为红棋
因为有player类,在头文件加上玩家标识,添加RedPlayer.h .cpp,继承player

enum Color {
	White,	// 0
	Black,	// 1
	Red  //2
};

继承便新增一红棋玩家,胜负逻辑不变,二人轮流下棋原本直接取反便可切换角色
//curPlayerId = !curPlayerId; // 0 变 1, 1 变 0
当三人下棋时,便要用别的方式,取余数:

// 0 白棋先下, 1 黑棋后下, 轮循
int numPlayers = 3;
int curPlayerId = 0;
...
curPlayerId = (curPlayerId + 1) % numPlayers;

使得三个玩家切换

以此类推,N个玩家都行
当然,创建game对象时,其构造函数也要初始化一个新的玩家对象

Game::Game() {
    this->player[0] = new WhitePlayer("白棋");
    this->player[1] = new BlackPlayer("黑棋");
    this->player[2] = new RedPlayer("红棋");
}

消息数组也要新增一列:

TCHAR message[4][20] = { _T("白胜"), _T("黑胜"), _T("红胜"), _T("平局")};

在这里插入图片描述
在这里插入图片描述

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

挥剑决浮云 -

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值