用Qt代码写一个五子棋项目
时间: 2024-05-13 12:17:57 浏览: 147
好的,下面是一个简单的五子棋项目的代码示例,基于Qt 5:
mainwindow.h:
```c++
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPushButton>
#include <QLabel>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void onNewGameClicked();
void onQuitClicked();
void onGridClicked();
private:
void initGame();
void clearBoard();
bool checkWin(int row, int col);
enum { BoardSize = 15 };
enum { NoPlayer, BlackPlayer, WhitePlayer };
int m_board[BoardSize][BoardSize];
int m_currentPlayer;
QLabel* m_messageLabel;
QPushButton* m_newGameButton;
QPushButton* m_quitButton;
QPushButton* m_grid[BoardSize][BoardSize];
};
#endif // MAINWINDOW_H
```
mainwindow.cpp:
```c++
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
// Set window title and size
setWindowTitle(tr("Five in a Row"));
setFixedSize(600, 600);
// Create buttons
m_newGameButton = new QPushButton(tr("New Game"), this);
m_newGameButton->setGeometry(20, 20, 120, 40);
connect(m_newGameButton, SIGNAL(clicked()), this, SLOT(onNewGameClicked()));
m_quitButton = new QPushButton(tr("Quit"), this);
m_quitButton->setGeometry(20, 70, 120, 40);
connect(m_quitButton, SIGNAL(clicked()), this, SLOT(onQuitClicked()));
// Create message label
m_messageLabel = new QLabel(tr("Black player's turn"), this);
m_messageLabel->setGeometry(160, 20, 420, 40);
QFont font = m_messageLabel->font();
font.setPointSize(18);
m_messageLabel->setFont(font);
// Create grid buttons
for (int row = 0; row < BoardSize; ++row) {
for (int col = 0; col < BoardSize; ++col) {
m_grid[row][col] = new QPushButton(this);
m_grid[row][col]->setGeometry(160 + col * 36, 80 + row * 36, 36, 36);
m_grid[row][col]->setProperty("row", row);
m_grid[row][col]->setProperty("col", col);
connect(m_grid[row][col], SIGNAL(clicked()), this, SLOT(onGridClicked()));
}
}
// Initialize game
initGame();
}
MainWindow::~MainWindow()
{
}
void MainWindow::onNewGameClicked()
{
initGame();
}
void MainWindow::onQuitClicked()
{
close();
}
void MainWindow::onGridClicked()
{
QPushButton* button = qobject_cast<QPushButton*>(sender());
int row = button->property("row").toInt();
int col = button->property("col").toInt();
if (m_board[row][col] == NoPlayer) {
// Set player's mark on board
m_board[row][col] = m_currentPlayer;
button->setText(m_currentPlayer == BlackPlayer ? "X" : "O");
// Check if player won
if (checkWin(row, col)) {
QString message = m_currentPlayer == BlackPlayer ? "Black player wins!" : "White player wins!";
m_messageLabel->setText(message);
return;
}
// Check if game is a draw
bool draw = true;
for (int row = 0; row < BoardSize; ++row) {
for (int col = 0; col < BoardSize; ++col) {
if (m_board[row][col] == NoPlayer) {
draw = false;
break;
}
}
if (!draw) {
break;
}
}
if (draw) {
m_messageLabel->setText("Game is a draw");
return;
}
// Switch to other player's turn
m_currentPlayer = m_currentPlayer == BlackPlayer ? WhitePlayer : BlackPlayer;
QString message = m_currentPlayer == BlackPlayer ? "Black player's turn" : "White player's turn";
m_messageLabel->setText(message);
}
}
void MainWindow::initGame()
{
// Clear board
clearBoard();
// Set black player to start
m_currentPlayer = BlackPlayer;
// Set message label
QString message = m_currentPlayer == BlackPlayer ? "Black player's turn" : "White player's turn";
m_messageLabel->setText(message);
// Clear grid buttons
for (int row = 0; row < BoardSize; ++row) {
for (int col = 0; col < BoardSize; ++col) {
m_grid[row][col]->setText("");
}
}
}
void MainWindow::clearBoard()
{
for (int row = 0; row < BoardSize; ++row) {
for (int col = 0; col < BoardSize; ++col) {
m_board[row][col] = NoPlayer;
}
}
}
bool MainWindow::checkWin(int row, int col)
{
// Check horizontal
int count = 0;
for (int i = qMax(0, col - 4); i <= qMin(BoardSize - 1, col + 4); ++i) {
if (m_board[row][i] == m_currentPlayer) {
++count;
if (count == 5) {
return true;
}
} else {
count = 0;
}
}
// Check vertical
count = 0;
for (int i = qMax(0, row - 4); i <= qMin(BoardSize - 1, row + 4); ++i) {
if (m_board[i][col] == m_currentPlayer) {
++count;
if (count == 5) {
return true;
}
} else {
count = 0;
}
}
// Check diagonal
count = 0;
for (int i = -4; i <= 4; ++i) {
int r = row + i;
int c = col + i;
if (r >= 0 && r < BoardSize && c >= 0 && c < BoardSize) {
if (m_board[r][c] == m_currentPlayer) {
++count;
if (count == 5) {
return true;
}
} else {
count = 0;
}
}
}
// Check anti-diagonal
count = 0;
for (int i = -4; i <= 4; ++i) {
int r = row + i;
int c = col - i;
if (r >= 0 && r < BoardSize && c >= 0 && c < BoardSize) {
if (m_board[r][c] == m_currentPlayer) {
++count;
if (count == 5) {
return true;
}
} else {
count = 0;
}
}
}
return false;
}
```
这个五子棋项目包括一个主窗口类MainWindow,它有一个15x15的网格和两个按钮:一个用于开始新游戏,另一个用于退出。网格的每个单元格都是一个QPushButton,点击它将在该单元格上放置当前玩家的标记。游戏的规则是玩家必须放置五个相同的标记(“X”或“O”)以赢得比赛。当游戏结束时,程序会显示一个消息标签,告诉玩家是哪个玩家赢了,或者游戏是平局。
阅读全文