C++五子棋游戏代码:人机对战与双人对战
C++五子棋游戏:人机对战与双人对战
本项目使用C++实现一个五子棋游戏,包含人机对战和双人对战两种模式。代码结构清晰,包含ChessBoard、ChessPiece、Player和Game等类,并提供详细的设计说明和代码示例。
1. 游戏需求
- 实现五子棋游戏,支持人机对战和双人对战两种模式。
- 提供基本的悔棋和重玩功能。
- 玩家通过鼠标点击来下棋,程序自动判断胜负并给出结果。
- 人机对战模式下,电脑玩家根据棋盘情况和难度等级进行下棋,并自动判断胜负。
2. 详细设计
a) ChessBoard类
- **成员变量:**二维数组存储棋盘信息。
- 成员函数:
drawBoard(): 绘制棋盘。getBoard(int x, int y): 获取指定位置的棋子信息。setBoard(int x, int y, int color): 设置指定位置的棋子信息。undo(int x, int y): 悔棋,将指定位置的棋子信息重置为0。
b) ChessPiece类
- 成员变量:
x: 棋子横坐标。y: 棋子纵坐标。color: 棋子颜色。
- 成员函数:
getX(): 获取棋子横坐标。getY(): 获取棋子纵坐标。getColor(): 获取棋子颜色。
c) Player类
- 成员变量:
name: 玩家名称。color: 玩家执棋颜色。
- 成员函数:
getName(): 获取玩家名称。getColor(): 获取玩家执棋颜色。
d) Game类
- 成员变量:
board: 存储棋盘信息的ChessBoard对象。player1: 存储玩家1信息的Player对象。player2: 存储玩家2信息的Player对象。
- 成员函数:
play(): 控制游戏流程,包括下棋、判断胜负、悔棋、重玩等功能。checkWin(int x, int y): 判断当前棋子是否构成五子连珠。checkDraw(): 判断是否出现平局。reset(): 重置游戏,重新开始游戏。
3. 系统设计
a) 主函数创建Game类对象。
b) Game对象创建ChessBoard类对象和Player类对象。
c) 根据用户选择的模式,Game对象创建电脑玩家对象并随机决定先后手。
d) 玩家通过鼠标点击来下棋,程序自动判断胜负并给出结果。
e) 如果用户选择悔棋,则Game对象调用ChessBoard对象的悔棋函数。
f) 如果用户选择重新开始游戏,则Game对象重新创建ChessBoard对象和Player对象。
g) 电脑玩家根据棋盘情况和难度等级进行下棋,并自动判断胜负。
h) 游戏结束后,根据用户选择进行重新开始或退出游戏。
4. C++代码实现 (以人机对战为例)
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
const int ROW = 15;
const int COL = 15;
class ChessBoard {
private:
int board[ROW][COL];
public:
ChessBoard() {
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
board[i][j] = 0;
}
}
}
void drawBoard() {
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
if (board[i][j] == 0) {
cout << '+ ';
}
else if (board[i][j] == 1) {
cout << 'o ';
}
else {
cout << 'x ';
}
}
cout << endl;
}
}
int getBoard(int x, int y) {
return board[x][y];
}
void setBoard(int x, int y, int color) {
board[x][y] = color;
}
void undo(int x, int y) {
board[x][y] = 0;
}
};
class ChessPiece {
private:
int x, y, color;
public:
ChessPiece(int x, int y, int color) {
this->x = x;
this->y = y;
this->color = color;
}
int getX() {
return x;
}
int getY() {
return y;
}
int getColor() {
return color;
}
};
class Player {
private:
string name;
int color;
public:
Player(string name, int color) {
this->name = name;
this->color = color;
}
string getName() {
return name;
}
int getColor() {
return color;
}
};
class Game {
private:
ChessBoard board;
Player* player1;
Player* player2;
public:
Game() {
player1 = new Player('Player1', 1);
player2 = new Player('Computer', 2);
}
void play() {
int turn = 1;
int win = 0;
int x, y;
while (win == 0) {
if (turn == 1) {
cout << 'Player1's turn: ';
cin >> x >> y;
if (board.getBoard(x, y) != 0) {
cout << 'This position is already occupied!' << endl;
continue;
}
board.setBoard(x, y, player1->getColor());
}
else {
cout << 'Computer's turn: ';
if (turn == 2) {
srand(time(NULL));
x = rand() % 15;
y = rand() % 15;
}
else {
// AI algorithm
}
if (board.getBoard(x, y) != 0) {
continue;
}
board.setBoard(x, y, player2->getColor());
}
board.drawBoard();
// check win or draw
if (checkWin(x, y)) {
win = turn;
if (win == 1) {
cout << 'Player1 wins!' << endl;
}
else {
cout << 'Computer wins!' << endl;
}
}
else if (checkDraw()) {
win = 3;
cout << 'Draw!' << endl;
}
// switch turn
turn = (turn == 1) ? 2 : 1;
}
}
bool checkWin(int x, int y) {
int color = board.getBoard(x, y);
int count = 1;
// check row
for (int i = x + 1; i < ROW && board.getBoard(i, y) == color; i++) {
count++;
}
for (int i = x - 1; i >= 0 && board.getBoard(i, y) == color; i--) {
count++;
}
if (count >= 5) {
return true;
}
// check column
count = 1;
for (int i = y + 1; i < COL && board.getBoard(x, i) == color; i++) {
count++;
}
for (int i = y - 1; i >= 0 && board.getBoard(x, i) == color; i--) {
count++;
}
if (count >= 5) {
return true;
}
// check diagonal
count = 1;
for (int i = x + 1, j = y + 1; i < ROW && j < COL && board.getBoard(i, j) == color; i++, j++) {
count++;
}
for (int i = x - 1, j = y - 1; i >= 0 && j >= 0 && board.getBoard(i, j) == color; i--, j--) {
count++;
}
if (count >= 5) {
return true;
}
// check anti-diagonal
count = 1;
for (int i = x + 1, j = y - 1; i < ROW && j >= 0 && board.getBoard(i, j) == color; i++, j--) {
count++;
}
for (int i = x - 1, j = y + 1; i >= 0 && j < COL && board.getBoard(i, j) == color; i--, j++) {
count++;
}
if (count >= 5) {
return true;
}
return false;
}
bool checkDraw() {
for (int i = 0; i < ROW; i++) {
for (int j = 0; j < COL; j++) {
if (board.getBoard(i, j) == 0) {
return false;
}
}
}
return true;
}
void reset() {
board = ChessBoard();
}
};
int main() {
Game game;
game.play();
return 0;
}
原文地址: https://www.cveoy.top/t/topic/oRWU 著作权归作者所有。请勿转载和采集!