用c加加写出五子棋。
下面是一个用C++编写的五子棋游戏的基本框架。请注意,这只是一个示例,可能还需要进行进一步的完善和优化。
#include <iostream>
#include <vector>
const int SIZE = 15;
enum class Piece { NONE, BLACK, WHITE };
class Game {
public:
Game() : currentPiece(Piece::BLACK), board(SIZE, std::vector<Piece>(SIZE, Piece::NONE)) {}
void play() {
while (true) {
std::cout << "Player " << (currentPiece == Piece::BLACK ? "BLACK" : "WHITE") << "'s turn:" << std::endl;
displayBoard();
int row, col;
std::cout << "Enter the row (1-" << SIZE << "): ";
std::cin >> row;
std::cout << "Enter the column (1-" << SIZE << "): ";
std::cin >> col;
if (isValidMove(row - 1, col - 1)) {
makeMove(row - 1, col - 1);
if (isWinningMove(row - 1, col - 1)) {
std::cout << "Player " << (currentPiece == Piece::BLACK ? "BLACK" : "WHITE") << " wins!" << std::endl;
break;
}
switchPlayers();
} else {
std::cout << "Invalid move. Please try again." << std::endl;
}
}
}
private:
Piece currentPiece;
std::vector<std::vector<Piece>> board;
bool isValidMove(int row, int col) const {
if (row < 0 || row >= SIZE || col < 0 || col >= SIZE || board[row][col] != Piece::NONE) {
return false;
}
return true;
}
void makeMove(int row, int col) {
board[row][col] = currentPiece;
}
bool isWinningMove(int row, int col) const {
Piece piece = board[row][col];
int count = 0;
// Check horizontally
for (int i = col - 4; i <= col + 4; ++i) {
if (i >= 0 && i + 4 < SIZE && board[row][i] == piece && board[row][i + 1] == piece &&
board[row][i + 2] == piece && board[row][i + 3] == piece && board[row][i + 4] == piece) {
return true;
}
}
// Check vertically
for (int i = row - 4; i <= row + 4; ++i) {
if (i >= 0 && i + 4 < SIZE && board[i][col] == piece && board[i + 1][col] == piece &&
board[i + 2][col] == piece && board[i + 3][col] == piece && board[i + 4][col] == piece) {
return true;
}
}
// Check diagonally (top-left to bottom-right)
for (int i = -4; i <= 4; ++i) {
int r = row + i;
int c = col + i;
if (r >= 0 && r + 4 < SIZE && c >= 0 && c + 4 < SIZE && board[r][c] == piece && board[r + 1][c + 1] == piece &&
board[r + 2][c + 2] == piece && board[r + 3][c + 3] == piece && board[r + 4][c + 4] == piece) {
return true;
}
}
// Check diagonally (top-right to bottom-left)
for (int i = -4; i <= 4; ++i) {
int r = row + i;
int c = col - i;
if (r >= 0 && r + 4 < SIZE && c >= 0 && c + 4 < SIZE && board[r][c] == piece && board[r + 1][c - 1] == piece &&
board[r + 2][c - 2] == piece && board[r + 3][c - 3] == piece && board[r + 4][c - 4] == piece) {
return true;
}
}
return false;
}
void switchPlayers() {
currentPiece = (currentPiece == Piece::BLACK) ? Piece::WHITE : Piece::BLACK;
}
void displayBoard() const {
for (int i = 0; i < SIZE; ++i) {
for (int j = 0; j < SIZE; ++j) {
char pieceChar;
switch (board[i][j]) {
case Piece::NONE:
pieceChar = '.';
break;
case Piece::BLACK:
pieceChar = 'B';
break;
case Piece::WHITE:
pieceChar = 'W';
break;
}
std::cout << pieceChar << " ";
}
std::cout << std::endl;
}
}
};
int main() {
Game game;
game.play();
return 0;
}
这个程序使用一个二维向量表示棋盘,其中Piece::NONE表示空位,Piece::BLACK表示黑子,Piece::WHITE表示白子。游戏通过循环进行,直到有一方获胜。在每个回合中,程序提示当前玩家输入所下棋子的位置,然后检查该位置是否有效,如果有效则落子,并检查是否获胜。最后,切换玩家并显示当前棋盘状态
原文地址: https://www.cveoy.top/t/topic/h6ju 著作权归作者所有。请勿转载和采集!