C++五子棋人对对战代码
时间: 2023-10-11 20:10:00 浏览: 37
以下是一个简单的 C++ 五子棋人对对战代码,可以作为参考:
```c++
#include <iostream>
using namespace std;
const int BOARD_SIZE = 15; // 棋盘大小
const int WIN_COUNT = 5; // 获胜所需棋子数
enum Piece {NONE, BLACK, WHITE}; // 棋子类型
class Board {
private:
Piece board[BOARD_SIZE][BOARD_SIZE];
public:
Board() {
for(int i = 0; i < BOARD_SIZE; i++) {
for(int j = 0; j < BOARD_SIZE; j++) {
board[i][j] = NONE; // 初始化棋盘
}
}
}
Piece getPiece(int x, int y) const {
if(x < 0 || x >= BOARD_SIZE || y < 0 || y >= BOARD_SIZE) {
return NONE; // 超出棋盘范围,返回 NONE
}
return board[x][y];
}
bool putPiece(int x, int y, Piece piece) {
if(x < 0 || x >= BOARD_SIZE || y < 0 || y >= BOARD_SIZE) {
return false; // 超出棋盘范围,下棋失败
}
if(board[x][y] != NONE) {
return false; // 此处已经有棋子,下棋失败
}
board[x][y] = piece;
return true; // 下棋成功
}
bool checkWin(int x, int y) const {
Piece piece = getPiece(x, y);
int count;
// 横向检查
count = 1; // 这里从 1 开始计数,因为已经有一个棋子了
for(int i = x - 1; i >= 0 && getPiece(i, y) == piece; i--) {
count++;
}
for(int i = x + 1; i < BOARD_SIZE && getPiece(i, y) == piece; i++) {
count++;
}
if(count >= WIN_COUNT) {
return true; // 获胜
}
// 竖向检查
count = 1;
for(int j = y - 1; j >= 0 && getPiece(x, j) == piece; j--) {
count++;
}
for(int j = y + 1; j < BOARD_SIZE && getPiece(x, j) == piece; j++) {
count++;
}
if(count >= WIN_COUNT) {
return true; // 获胜
}
// 左上到右下检查
count = 1;
for(int i = x - 1, j = y - 1; i >= 0 && j >= 0 && getPiece(i, j) == piece; i--, j--) {
count++;
}
for(int i = x + 1, j = y + 1; i < BOARD_SIZE && j < BOARD_SIZE && getPiece(i, j) == piece; i++, j++) {
count++;
}
if(count >= WIN_COUNT) {
return true; // 获胜
}
// 右上到左下检查
count = 1;
for(int i = x - 1, j = y + 1; i >= 0 && j < BOARD_SIZE && getPiece(i, j) == piece; i--, j++) {
count++;
}
for(int i = x + 1, j = y - 1; i < BOARD_SIZE && j >= 0 && getPiece(i, j) == piece; i++, j--) {
count++;
}
if(count >= WIN_COUNT) {
return true; // 获胜
}
return false; // 没有获胜
}
};
class Player {
private:
Piece piece;
public:
Player(Piece piece) : piece(piece) {}
bool putPiece(Board& board, int x, int y) {
return board.putPiece(x, y, piece);
}
bool checkWin(Board& board, int x, int y) const {
return board.checkWin(x, y);
}
};
class Game {
private:
Board board;
Player* players[2];
int currentPlayerIndex;
public:
Game(Player* p1, Player* p2) {
players[0] = p1;
players[1] = p2;
currentPlayerIndex = 0; // 先手玩家先下棋
}
bool play(int x, int y) {
bool success = players[currentPlayerIndex]->putPiece(board, x, y);
if(success) {
if(players[currentPlayerIndex]->checkWin(board, x, y)) {
cout << "Player " << currentPlayerIndex + 1 << " wins!" << endl;
return true; // 游戏结束
}
currentPlayerIndex = 1 - currentPlayerIndex; // 切换玩家
}
return false;
}
};
int main() {
Player p1(BLACK), p2(WHITE);
Game game(&p1, &p2);
int x, y;
while(true) {
cout << "Player " << game.getCurrentPlayerIndex() + 1 << "'s turn:" << endl;
cin >> x >> y;
bool end = game.play(x, y);
if(end) {
break; // 结束游戏
}
}
return 0;
}
```
这个代码中,定义了三个类:`Board`、`Player` 和 `Game`。`Board` 类表示棋盘,包含了下棋、获取棋子和判断获胜等方法;`Player` 类表示玩家,包含了下棋和判断获胜等方法;`Game` 类表示游戏,包含了两个玩家和当前玩家等属性,以及下棋和判断游戏结束等方法。
在主函数中,先创建了两个玩家(黑方和白方),然后创建了游戏对象,并通过循环让玩家轮流下棋,直到游戏结束。