C++基于qt 的五子棋悔棋代码
时间: 2023-11-16 18:04:09 浏览: 117
以下是基于Qt的五子棋悔棋代码示例:
(1)在mainwindow.h中添加如下代码:
```
private:
int chessboard[15][15]; // 用来存储棋盘信息
int player; // 玩家标志,1表示黑棋,2表示白棋
int step; // 记录步数
bool isUndo; // 是否悔棋标志
QList<QPair<int, int>> historyList; // 用来存储历史记录
```
(2)在mainwindow.cpp中添加如下代码:
```
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// 初始化棋盘信息
memset(chessboard, 0, sizeof(chessboard));
player = 1;
step = 0;
isUndo = false;
// 绑定悔棋按钮点击事件
connect(ui->undoBtn, &QPushButton::clicked, this, &MainWindow::undo);
// 绑定棋盘点击事件
connect(ui->chessBoard, &ChessBoard::clicked, this, &MainWindow::playChess);
}
void MainWindow::playChess(int row, int col)
{
if (chessboard[row][col] != 0) {
QMessageBox::warning(this, tr("Warning"), tr("The position has been occupied!"));
return;
}
// 绘制棋子
ui->chessBoard->drawChess(row, col, player);
// 记录历史记录
historyList.append(qMakePair(row, col));
// 更新棋盘信息
chessboard[row][col] = player;
step++;
// 判断是否有胜者
if (ui->chessBoard->isWin(row, col)) {
QMessageBox::information(this, tr("Congratulations"), tr("Player %1 wins!").arg(player == 1 ? tr("Black") : tr("White")));
ui->chessBoard->setEnabled(false);
return;
}
// 判断是否平局
if (step == 225) {
QMessageBox::information(this, tr("Tie"), tr("Tie game!"));
ui->chessBoard->setEnabled(false);
return;
}
// 切换玩家
player = player == 1 ? 2 : 1;
}
void MainWindow::undo()
{
if (step == 0) {
QMessageBox::warning(this, tr("Warning"), tr("There is no step to undo!"));
return;
}
// 获取历史记录中的最后一步
QPair<int, int> lastStep = historyList.last();
int row = lastStep.first;
int col = lastStep.second;
// 悔棋
ui->chessBoard->undoChess(row, col);
// 更新棋盘信息
chessboard[row][col] = 0;
step--;
// 切换玩家
player = player == 1 ? 2 : 1;
// 删除历史记录中的最后一步
historyList.removeLast();
}
```
(3)在chessboard.cpp中添加如下代码:
```
void ChessBoard::drawChess(int row, int col, int player)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(Qt::NoPen);
if (player == 1) {
painter.setBrush(Qt::black);
} else {
painter.setBrush(Qt::white);
}
painter.drawEllipse(QPointF((col + 0.5) * gridSize(), (row + 0.5) * gridSize()), gridSize() / 2 - 2, gridSize() / 2 - 2);
}
bool ChessBoard::isWin(int row, int col)
{
int count = 1; // 连续棋子的数量
// 判断横向是否有五个棋子相连
for (int i = col - 1; i >= 0; i--) {
if (m_chessboard[row][i] != m_chessboard[row][col]) {
break;
}
count++;
}
for (int i = col + 1; i < 15; i++) {
if (m_chessboard[row][i] != m_chessboard[row][col]) {
break;
}
count++;
}
if (count >= 5) {
return true;
}
// 判断纵向是否有五个棋子相连
count = 1;
for (int i = row - 1; i >= 0; i--) {
if (m_chessboard[i][col] != m_chessboard[row][col]) {
break;
}
count++;
}
for (int i = row + 1; i < 15; i++) {
if (m_chessboard[i][col] != m_chessboard[row][col]) {
break;
}
count++;
}
if (count >= 5) {
return true;
}
// 判断左上到右下是否有五个棋子相连
count = 1;
for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {
if (m_chessboard[i][j] != m_chessboard[row][col]) {
break;
}
count++;
}
for (int i = row + 1, j = col + 1; i < 15 && j < 15; i++, j++) {
if (m_chessboard[i][j] != m_chessboard[row][col]) {
break;
}
count++;
}
if (count >= 5) {
return true;
}
// 判断左下到右上是否有五个棋子相连
count = 1;
for (int i = row + 1, j = col - 1; i < 15 && j >= 0; i++, j--) {
if (m_chessboard[i][j] != m_chessboard[row][col]) {
break;
}
count++;
}
for (int i = row - 1, j = col + 1; i >= 0 && j < 15; i--, j++) {
if (m_chessboard[i][j] != m_chessboard[row][col]) {
break;
}
count++;
}
if (count >= 5) {
return true;
}
return false;
}
void ChessBoard::undoChess(int row, int col)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QPen(Qt::black, 2));
painter.setBrush(Qt::NoBrush);
painter.drawRect(col * gridSize(), row * gridSize(), gridSize(), gridSize());
if (m_chessboard[row][col] == 1) {
painter.setPen(QPen(Qt::black, 1));
painter.setBrush(Qt::black);
} else {
painter.setPen(QPen(Qt::white, 1));
painter.setBrush(Qt::white);
}
painter.drawEllipse(QPointF((col + 0.5) * gridSize(), (row + 0.5) * gridSize()), gridSize() / 2 - 2, gridSize() / 2 - 2);
m_chessboard[row][col] = 0;
}
```
以上代码实现了基于Qt的五子棋悔棋功能,可以参考编写。
阅读全文