请写出符合5.14.2qt版本的代码,符合五子棋规则,能悔棋,用c++
时间: 2023-09-12 15:05:29 浏览: 101
qt5.14.2 qtwebengine
### 回答1:
以下是符合5.14.2qt版本的代码,实现五子棋规则和悔棋功能:
mainwindow.h
```
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPushButton>
#include <QLabel>
#include <QGridLayout>
#include <QMessageBox>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void buttonClicked();
void undoClicked();
private:
QPushButton *buttons[15][15];
QPushButton *undoButton;
QLabel *messageLabel;
QGridLayout *gridLayout;
int chessboard[15][15];
int currentPlayer;
bool gameOver;
int steps;
int history[225][3];
void initUI();
void initChessboard();
bool checkWin(int row, int col);
bool checkHorizontal(int row, int col);
bool checkVertical(int row, int col);
bool checkDiagonal1(int row, int col);
bool checkDiagonal2(int row, int col);
void showMessage(QString message);
void updateChessboard(int row, int col);
void undoChessboard();
};
#endif // MAINWINDOW_H
```
mainwindow.cpp
```
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
initUI();
initChessboard();
}
MainWindow::~MainWindow()
{
}
void MainWindow::initUI()
{
QWidget *centralWidget = new QWidget(this);
setCentralWidget(centralWidget);
gridLayout = new QGridLayout(centralWidget);
gridLayout->setSpacing(0);
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
buttons[i][j] = new QPushButton(centralWidget);
buttons[i][j]->setFixedSize(40, 40);
buttons[i][j]->setProperty("row", i);
buttons[i][j]->setProperty("col", j);
gridLayout->addWidget(buttons[i][j], i, j);
connect(buttons[i][j], SIGNAL(clicked()), this, SLOT(buttonClicked()));
}
}
undoButton = new QPushButton(tr("悔棋"), centralWidget);
undoButton->setFixedSize(80, 40);
connect(undoButton, SIGNAL(clicked()), this, SLOT(undoClicked()));
gridLayout->addWidget(undoButton, 15, 0, 1, 2);
messageLabel = new QLabel(centralWidget);
messageLabel->setFixedSize(150, 40);
gridLayout->addWidget(messageLabel, 15, 2, 1, 13, Qt::AlignLeft | Qt::AlignVCenter);
setFixedSize(620, 620);
setWindowTitle(tr("五子棋"));
}
void MainWindow::initChessboard()
{
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
chessboard[i][j] = 0;
}
}
currentPlayer = 1;
gameOver = false;
steps = 0;
memset(history, 0, sizeof(history));
showMessage(tr("黑方先手"));
}
bool MainWindow::checkWin(int row, int col)
{
if (checkHorizontal(row, col) || checkVertical(row, col) ||
checkDiagonal1(row, col) || checkDiagonal2(row, col)) {
return true;
}
return false;
}
bool MainWindow::checkHorizontal(int row, int col)
{
int count = 1;
for (int i = col - 1; i >= 0; i--) {
if (chessboard[row][i] == chessboard[row][col]) {
count++;
} else {
break;
}
}
for (int i = col + 1; i < 15; i++) {
if (chessboard[row][i] == chessboard[row][col]) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
return false;
}
bool MainWindow::checkVertical(int row, int col)
{
int count = 1;
for (int i = row - 1; i >= 0; i--) {
if (chessboard[i][col] == chessboard[row][col]) {
count++;
} else {
break;
}
}
for (int i = row + 1; i < 15; i++) {
if (chessboard[i][col] == chessboard[row][col]) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
return false;
}
bool MainWindow::checkDiagonal1(int row, int col)
{
int count = 1;
for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {
if (chessboard[i][j] == chessboard[row][col]) {
count++;
} else {
break;
}
}
for (int i = row + 1, j = col + 1; i < 15 && j < 15; i++, j++) {
if (chessboard[i][j] == chessboard[row][col]) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
return false;
}
bool MainWindow::checkDiagonal2(int row, int col)
{
int count = 1;
for (int i = row - 1, j = col + 1; i >= 0 && j < 15; i--, j++) {
if (chessboard[i][j] == chessboard[row][col]) {
count++;
} else {
break;
}
}
for (int i = row + 1, j = col - 1; i < 15 && j >= 0; i++, j--) {
if (chessboard[i][j] == chessboard[row][col]) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
return false;
}
void MainWindow::showMessage(QString message)
{
messageLabel->setText(message);
}
void MainWindow::updateChessboard(int row, int col)
{
buttons[row][col]->setIcon(currentPlayer == 1 ? QIcon(":/black.png") : QIcon(":/white.png"));
chessboard[row][col] = currentPlayer;
history[steps][0] = row;
history[steps][1] = col;
history[steps][2] = currentPlayer;
steps++;
if (checkWin(row, col)) {
gameOver = true;
QString message = QString("恭喜%1方获胜!").arg(currentPlayer == 1 ? "黑" : "白");
showMessage(message);
QMessageBox::information(this, "游戏结束", message);
} else if (steps == 15 * 15) {
gameOver = true;
showMessage("平局");
QMessageBox::information(this, "游戏结束", "平局");
} else {
currentPlayer = currentPlayer == 1 ? 2 : 1;
QString message = QString("%1方落子").arg(currentPlayer == 1 ? "黑" : "白");
showMessage(message);
}
}
void MainWindow::undoChessboard()
{
if (steps > 0) {
steps--;
int row = history[steps][0];
int col = history[steps][1];
int player = history[steps][2];
buttons[row][col]->setIcon(QIcon());
chessboard[row][col] = 0;
currentPlayer = player;
QString message = QString("%1方悔棋").arg(currentPlayer == 1 ? "黑" : "白");
showMessage(message);
}
}
void MainWindow::buttonClicked()
{
if (gameOver) {
return;
}
QPushButton *button = qobject_cast<QPushButton *>(sender());
int row = button->property("row").toInt();
int col = button->property("col").toInt();
if (chessboard[row][col] == 0) {
updateChessboard(row, col);
}
}
void MainWindow::undoClicked()
{
if (gameOver) {
return;
}
undoChessboard();
}
```
注意:在工程的资源文件 (.qrc) 中需要添加黑棋图标(black.png)和白棋图标(white.png)。
### 回答2:
下面是一个简单的五子棋游戏实现的C代码,基于Qt 5.14.2版本。该程序可以进行双人对战,满足五子棋的规则,并且支持悔棋操作。
```c
#include <QCoreApplication>
#include <iostream>
#include <vector>
const int SIZE = 15; // 棋盘大小
enum class Player { BLACK, WHITE };
enum class Chess { NONE, BLACK, WHITE };
class Game {
public:
Game() {
board.resize(SIZE, std::vector<Chess>(SIZE, Chess::NONE));
currentPlayer = Player::BLACK;
}
// 下棋
void playChess(int x, int y) {
if (x < 0 || x >= SIZE || y < 0 || y >= SIZE || board[x][y] != Chess::NONE) {
std::cout << "非法操作!" << std::endl;
return;
}
board[x][y] = (currentPlayer == Player::BLACK) ? Chess::BLACK : Chess::WHITE;
currentPlayer = (currentPlayer == Player::BLACK) ? Player::WHITE : Player::BLACK;
}
// 悔棋
void undoChess() {
if (history.empty()) {
std::cout << "无可悔棋的步骤!" << std::endl;
return;
}
auto lastStep = history.back();
board[lastStep.first][lastStep.second] = Chess::NONE;
currentPlayer = (currentPlayer == Player::BLACK) ? Player::WHITE : Player::BLACK;
history.pop_back();
}
// 打印当前棋盘
void printBoard() const {
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j) {
if (board[i][j] == Chess::NONE) {
std::cout << " +";
} else if (board[i][j] == Chess::BLACK) {
std::cout << " x";
} else {
std::cout << " o";
}
}
std::cout << std::endl;
}
}
private:
std::vector<std::vector<Chess>> board; // 棋盘状态
Player currentPlayer; // 当前玩家
std::vector<std::pair<int, int>> history; // 记录下棋历史
};
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
Game game;
int x, y;
bool gameOver = false;
std::cout << "欢迎来到五子棋游戏!" << std::endl;
std::cout << "请输入坐标进行下棋,输入-1结束游戏,输入-2悔棋:" << std::endl;
game.printBoard();
while (!gameOver) {
std::cin >> x;
if (x == -1) {
gameOver = true;
break;
} else if (x == -2) {
game.undoChess();
game.printBoard();
continue;
}
std::cin >> y;
game.playChess(x, y);
game.printBoard();
// TODO: 检查是否有一方胜利,即五子连珠
}
return a.exec();
}
```
在上面的代码中,使用了一个`Game`类来表示游戏,其中包含了棋盘状态、当前玩家以及下棋历史等成员变量。通过`playChess`函数来下棋,`undoChess`函数实现悔棋操作。`printBoard`函数用于打印当前棋盘状态。
主函数中实例化了一个`Game`对象,并通过循环接受用户输入的坐标,调用`playChess`函数进行下棋操作,并通过`printBoard`打印出当前棋盘状态。用户输入-2时,调用`undoChess`函数进行悔棋操作。用户输入-1时,游戏结束。
暂时未实现检查胜利条件的代码部分,你可以自行添加。
### 回答3:
下面是符合5.14.2qt版本的五子棋代码,能够实现游戏规则和悔棋功能:
```c
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QMessageBox>
#include <QDebug>
#include <QMouseEvent>
// 棋盘大小
#define CHESSBOARD_SIZE 15
class Chessboard : public QWidget {
public:
Chessboard(QWidget *parent = nullptr) : QWidget(parent) {
// 初始化棋盘
for (int i = 0; i < CHESSBOARD_SIZE; i++) {
for (int j = 0; j < CHESSBOARD_SIZE; j++) {
chessboard[i][j] = 0;
}
}
currentPlayer = 1; // 玩家1先手
setFixedSize(600, 600);
}
protected:
void paintEvent(QPaintEvent *event) override {
Q_UNUSED(event);
// 绘制棋盘
QPainter painter(this);
for (int i = 0; i < CHESSBOARD_SIZE; i++) {
painter.drawLine(50, 50 + i * 40, 610, 50 + i * 40);
painter.drawLine(50 + i * 40, 50, 50 + i * 40, 610);
}
// 绘制棋子
for (int i = 0; i < CHESSBOARD_SIZE; i++) {
for (int j = 0; j < CHESSBOARD_SIZE; j++) {
if (chessboard[i][j] == 1) {
painter.setBrush(QBrush(Qt::black));
painter.drawEllipse(50 + i * 40 - 20, 50 + j * 40 - 20, 40, 40);
} else if (chessboard[i][j] == -1) {
painter.setBrush(QBrush(Qt::white));
painter.drawEllipse(50 + i * 40 - 20, 50 + j * 40 - 20, 40, 40);
}
}
}
}
void mousePressEvent(QMouseEvent *event) override {
int x = event->x();
int y = event->y();
int i = (x - 50) / 40;
int j = (y - 50) / 40;
if (i >= 0 && i < CHESSBOARD_SIZE && j >= 0 && j < CHESSBOARD_SIZE && chessboard[i][j] == 0) {
// 判断胜负
if (isGameWin(i, j)) {
QMessageBox::information(this, "胜利", "玩家 " + QString::number(currentPlayer) + " 胜利!");
resetGame();
return;
}
// 下子
chessboard[i][j] = currentPlayer;
currentPlayer = -currentPlayer;
update();
}
}
void keyPressEvent(QKeyEvent *event) override {
if (event->key() == Qt::Key_Z && event->modifiers() == Qt::ControlModifier) {
rollbackLastMove();
}
}
private:
int chessboard[CHESSBOARD_SIZE][CHESSBOARD_SIZE]; // 棋盘
int currentPlayer; // 当前玩家
bool isGameWin(int row, int col) {
int player = chessboard[row][col];
int count = 1;
// 横向
int i = row - 1;
while (i >= 0 && chessboard[i][col] == player) {
count++;
i--;
}
i = row + 1;
while (i < CHESSBOARD_SIZE && chessboard[i][col] == player) {
count++;
i++;
}
if (count >= 5) {
return true;
}
// 纵向
count = 1;
int j = col - 1;
while (j >= 0 && chessboard[row][j] == player) {
count++;
j--;
}
j = col + 1;
while (j < CHESSBOARD_SIZE && chessboard[row][j] == player) {
count++;
j++;
}
if (count >= 5) {
return true;
}
// 斜向(左上到右下)
count = 1;
i = row - 1;
j = col - 1;
while (i >= 0 && j >= 0 && chessboard[i][j] == player) {
count++;
i--;
j--;
}
i = row + 1;
j = col + 1;
while (i < CHESSBOARD_SIZE && j < CHESSBOARD_SIZE && chessboard[i][j] == player) {
count++;
i++;
j++;
}
if (count >= 5) {
return true;
}
// 斜向(左下到右上)
count = 1;
i = row + 1;
j = col - 1;
while (i < CHESSBOARD_SIZE && j >= 0 && chessboard[i][j] == player) {
count++;
i++;
j--;
}
i = row - 1;
j = col + 1;
while (i >= 0 && j < CHESSBOARD_SIZE && chessboard[i][j] == player) {
count++;
i--;
j++;
}
if (count >= 5) {
return true;
}
return false;
}
void resetGame() {
for (int i = 0; i < CHESSBOARD_SIZE; i++) {
for (int j = 0; j < CHESSBOARD_SIZE; j++) {
chessboard[i][j] = 0;
}
}
currentPlayer = 1;
update();
}
void rollbackLastMove() {
for (int i = CHESSBOARD_SIZE - 1; i >= 0; i--) {
for (int j = CHESSBOARD_SIZE - 1; j >= 0; j--) {
if (chessboard[i][j] != 0) {
chessboard[i][j] = 0;
currentPlayer = -currentPlayer;
update();
return;
}
}
}
}
};
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
Chessboard chessboard;
chessboard.show();
return app.exec();
}
```
以上就是一个简单的五子棋游戏代码,通过QPainter绘制棋盘和棋子,通过鼠标点击来下子,并实现了胜利判断和悔棋功能。
阅读全文