为大家编写了一段五子棋游戏的建议代码!
以下是一个简单的C++控制台版五子棋游戏的示例。该示例包含基本的游戏逻辑,包括棋盘的显示、玩家轮流下棋、胜利条件检查等。
#include <iostream>
#include <vector>
#include <limits>
const int BOARD_SIZE = 15;
enum class Player { EMPTY, PLAYER1, PLAYER2 };
class GomokuGame {
private:
std::vector<std::vector<Player>> board;
public:
GomokuGame() : board(BOARD_SIZE, std::vector<Player>(BOARD_SIZE, Player::EMPTY)) {}
void printBoard() const {
for (int i = 0; i < BOARD_SIZE; ++i) {
for (int j = 0; j < BOARD_SIZE; ++j) {
char symbol = ' ';
switch (board[i][j]) {
case Player::EMPTY:
symbol = '.';
break;
case Player::PLAYER1:
symbol = 'X';
break;
case Player::PLAYER2:
symbol = 'O';
break;
}
std::cout << symbol << ' ';
}
std::cout << std::endl;
}
}
bool isMoveValid(int row, int col) const {
return row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE && board[row][col] == Player::EMPTY;
}
bool makeMove(int row, int col, Player player) {
if (isMoveValid(row, col)) {
board[row][col] = player;
return true;
}
return false;
}
bool checkWin(int row, int col, Player player) const {
// Check horizontally
int count = 1;
for (int i = 1; i < 5; ++i) {
if (col - i >= 0 && board[row][col - i] == player) {
++count;
} else {
break;
}
}
for (int i = 1; i < 5; ++i) {
if (col + i < BOARD_SIZE && board[row][col + i] == player) {
++count;
} else {
break;
}
}
if (count >= 5) {
return true;
}
// Check vertically
count = 1;
for (int i = 1; i < 5; ++i) {
if (row - i >= 0 && board[row - i][col] == player) {
++count;
} else {
break;
}
}
for (int i = 1; i < 5; ++i) {
if (row + i < BOARD_SIZE && board[row + i][col] == player) {
++count;
} else {
break;
}
}
if (count >= 5) {
return true;
}
// Check diagonally (top-left to bottom-right)
count = 1;
for (int i = 1; i < 5; ++i) {
if (row - i >= 0 && col - i >= 0 && board[row - i][col - i] == player) {
++count;
} else {
break;
}
}
for (int i = 1; i < 5; ++i) {
if (row + i < BOARD_SIZE && col + i < BOARD_SIZE && board[row + i][col + i] == player) {
++count;
} else {
break;
}
}
if (count >= 5) {
return true;
}
// Check diagonally (bottom-left to top-right)
count = 1;
for (int i = 1; i < 5; ++i) {
if (row + i < BOARD_SIZE && col - i >= 0 && board[row + i][col - i] == player) {
++count;
} else {
break;
}
}
for (int i = 1; i < 5; ++i) {
if (row - i >= 0 && col + i < BOARD_SIZE && board[row - i][col + i] == player) {
++count;
} else {
break;
}
}
return count >= 5;
}
bool isBoardFull() const {
for (int i = 0; i < BOARD_SIZE; ++i) {
for (int j = 0; j < BOARD_SIZE; ++j) {
if (board[i][j] == Player::EMPTY) {
return false;
}
}
}
return true;
}
};
int main() {
GomokuGame game;
Player currentPlayer = Player::PLAYER1;
while (true) {
game.printBoard();
int row, col;
std::cout << "Player " << (currentPlayer == Player::PLAYER1 ? "X" : "O") << "'s turn. Enter row and column (e.g., 1 2): ";
if (!(std::cin >> row >> col) || !game.isMoveValid(row - 1, col - 1)) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Invalid move. Try again." << std::endl;
continue;
}
if (!game.makeMove(row - 1, col - 1, currentPlayer)) {
std::cout << "Invalid move. The cell is already occupied. Try again." << std::endl;
continue;
}
if (game.checkWin(row - 1, col - 1, currentPlayer)) {
game.printBoard();
std::cout << "Player " << (currentPlayer == Player::PLAYER1 ? "X" : "O") << " wins!" << std::endl;
break;
}
if (game.isBoardFull()) {
game.printBoard();
std::cout << "It's a draw!" << std::endl;
break;
}
currentPlayer = (currentPlayer == Player::PLAYER1) ? Player::PLAYER2 : Player::PLAYER1;
}
return 0;
}
这是一个简单的控制台版五子棋游戏。玩家轮流输入行和列来下棋,游戏会检查是否有玩家胜利或者平局。请注意,这只是一个基本示例,你可以根据需要进行扩展和改进。