/**
*Date 2024/1/19 16:53
*@author 冯杰
*@version 1.0
*/
#include <iostream>
// 定义玩家颜色的常量
#define EMPTY 0
#define BLACK 1
#define WHITE 2
// 定义游戏棋盘的大小
#define SIZE 15
// 类声明
class GobangGame {
public:
GobangGame();
void playGame();
void menuView();
private:
void init();
bool isWin(int x, int y);
bool playerMove(int x, int y);
void gameView_ShowMap();
void winView();
int map[SIZE][SIZE];
int flag; // 初始化标志为0
};
// 构造函数
GobangGame::GobangGame() {
flag = 0;
}
// 函数实现
void GobangGame::init() {
// 将游戏棋盘的所有位置初始化为零(空)
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
map[i][j] = EMPTY;
}
}
}
bool GobangGame::isWin(int x, int y) {
int count;
// 横向
count = 1;
for (int i = 1; i < 5; i++) {
if (x + i < SIZE && map[x + i][y] == map[x][y]) {
count++;
} else {
break;
}
}
for (int i = 1; i < 5; i++) {
if (x - i >= 0 && map[x - i][y] == map[x][y]) {
count++;
} else {
break;
}
}
if (count >= 5) return true;
// 纵向
count = 1;
for (int i = 1; i < 5; i++) {
if (y + i < SIZE && map[x][y + i] == map[x][y]) {
count++;
} else {
break;
}
}
for (int i = 1; i < 5; i++) {
if (y - i >= 0 && map[x][y - i] == map[x][y]) {
count++;
} else {
break;
}
}
if (count >= 5) return true;
// 斜向(左上到右下)
count = 1;
for (int i = 1; i < 5; i++) {
if (x + i < SIZE && y + i < SIZE && map[x + i][y + i] == map[x][y]) {
count++;
} else {
break;
}
}
for (int i = 1; i < 5; i++) {
if (x - i >= 0 && y - i >= 0 && map[x - i][y - i] == map[x][y]) {
count++;
} else {
break;
}
}
if (count >= 5) return true;
// 斜向(左下到右上)
count = 1;
for (int i = 1; i < 5; i++) {
if (x + i < SIZE && y - i >= 0 && map[x + i][y - i] == map[x][y]) {
count++;
} else {
break;
}
}
for (int i = 1; i < 5; i++) {
if (x - i >= 0 && y + i < SIZE && map[x - i][y + i] == map[x][y]) {
count++;
} else {
break;
}
}
if (count >= 5) return true;
return false;
}
bool GobangGame::playerMove(int x, int y) {
// 检查选择的位置是否为空
if (map[x][y] == EMPTY) {
// 将选择的位置设置为当前玩家的颜色
map[x][y] = (flag % 2 == 0) ? BLACK : WHITE;
return true; // 移动成功
} else {
return false; // 移动失败(位置已被占据)
}
}
void GobangGame::menuView() {
// TODO: 实现菜单视图逻辑
std::cout << "欢迎来到五子棋游戏!" << std::endl;
playGame();
}
void GobangGame::gameView_ShowMap() {
// 打印当前游戏棋盘的状态
std::cout << "当前游戏棋盘:" << std::endl;
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (map[i][j] == EMPTY) {
std::cout << ". ";
} else if (map[i][j] == BLACK) {
std::cout << "X ";
} else {
std::cout << "O ";
}
}
std::cout << std::endl;
}
}
void GobangGame::winView() {
// 打印获胜视图
gameView_ShowMap();
std::cout << "玩家 " << ((flag % 2 == 0) ? 1 : 2) << " 获胜!" << std::endl;
}
void GobangGame::playGame() {
// 初始化游戏数据
init();
// 主游戏循环
while (true) {
// 显示游戏棋盘
gameView_ShowMap();
int x, y;
// 接受玩家坐标输入
std::cout << "玩家 " << ((flag % 2 == 0) ? 1 : 2) << ",请输入您的落子坐标(格式:行 列):";
std::cin >> x >> y;
// 调用playerMove函数并处理移动成功/失败
if (playerMove(x, y)) {
// 调用isWin函数并检查是否有获胜者
if (isWin(x, y)) {
// 打印获胜视图
winView();
break;
}
// 切换玩家(更新标志)
flag++;
} else {
std::cout << "该位置已被占据,请重新输入。" << std::endl;
}
}
}
int main() {
GobangGame game;
game.menuView();
return 0;
}
使用C++实现五子棋游戏(控制台版)
最新推荐文章于 2024-07-20 18:10:49 发布