五子棋小游戏

五子棋小游戏

好久没有碰C++了,最近写个五子棋的小游戏玩玩。

代码如下:

cpp 复制代码
#include<iostream>
#include<vector>
using namespace std;

// 棋盘的大小
const int SIZE = 15;
// 棋子的类型(空格、黑棋、白棋)
const char EMPTY = ' ';
const char BLACK = 'X';
const char WHITE = 'O';

// 初始化棋盘
void init_board(vector<vector<char>>& board) {
	for (int i = 0; i < SIZE; ++i) {
		board[i].assign(SIZE, EMPTY);
	}
}

// 打印棋盘
void print_board(vector<vector<char>>& board) {
	cout << "  ";//空两格
	// 打印列号
	for (int i = 0; i < SIZE; ++i) {
		printf("%2d", i + 1);
	}
	cout << endl;
	// 打印棋盘内容
	for (int i = 0; i < SIZE; ++i) {
		printf("%2d", i + 1);
		for (int j = 0; j < SIZE; ++j) {
			cout << "|" << board[i][j];
		}
		cout << "|" << endl;
		// 打印分隔线
		if (i != SIZE - 1) {
			cout << "  ";
			for (int j = 0; j < SIZE; ++j) {
				cout << "+-";
			}
			cout << "+" << endl;
		}
	}
	cout << endl;
}

// 胜负判断
bool check_win(vector<vector<char>>& board, int x, int y, char player) {
	// 用于遍历方向(水平、垂直、左斜、右斜)
	int dirs[4][2] = { {0, 1}, {1, 0}, {1, 1}, {1, -1} };

	for (auto dir : dirs) {
		int dx = dir[0];
		int dy = dir[1];
		int count = 0; // 统计该方向上的连续棋子数(不含当前位置)
		for (int i = -4; i <= 4; ++i) {
			if (i == 0) continue; // 跳过当前位置
			int nx = x + dx * i;
			int ny = y + dy * i;
			if (nx >= 0 && nx < SIZE && ny >= 0 && ny < SIZE && board[nx][ny] == player) {
				count++;
			}
			else {
				break; // 遇到边界或不同棋子,停止该方向检查
			}
		}
		if (count + 1 >= 5) return true; // 加上当前位置,共5个则获胜
	}
	return false;
}

int main() {
	cout << "五子棋游戏" << endl;
	cout << "输入格式:行号 列号。例如输入:1 2(表示在第1行第2列落子)" << endl;

	vector<vector<char>> board(SIZE, vector<char>(SIZE));
	init_board(board);  // 先初始化
	print_board(board); // 再打印初始棋盘

	char cur_player = BLACK; // 黑棋先行
	bool game_over = false;

	int x, y;
	while (!game_over) {
		cout << ((cur_player == BLACK) ? "黑棋(X)落子: " : "白棋(O)落子: ");
		cin >> x >> y;
		x--; // 转换为0-based索引(用户输入1~15 → 0~14)
		y--;

		// 检查输入合法性
		if (x < 0 || x >= SIZE || y < 0 || y >= SIZE) {
			cout << "输入无效,请输入1-" << SIZE << "之间的数字" << endl;
			continue;
		}
		if (board[x][y] != EMPTY) {
			cout << "该位置已经有棋子!请重新落子" << endl;
			continue;
		}

		// 落子
		board[x][y] = cur_player;
		print_board(board);

		// 检测是否获胜
		if (check_win(board, x, y, cur_player)) {
			cout << (cur_player == BLACK ? "黑棋(X)获胜!" : "白棋(O)获胜!") << endl;
			game_over = true;
		}
		// 检测是否平局
		else {
			bool is_full = true;
			for (int i = 0; i < SIZE; ++i) {
				for (int j = 0; j < SIZE; ++j) {
					if (board[i][j] == EMPTY) {
						is_full = false;
						break;
					}
				}
				if (!is_full) break;
			}
			if (is_full) {
				cout << "平局!" << endl;
				game_over = true;
			}
		}

		// 仅在游戏未结束时切换玩家
		if (!game_over) {
			cur_player = (cur_player == BLACK) ? WHITE : BLACK;
		}
	}

	cout << "游戏结束!" << endl;
	return 0;
}

大概效果如下:

相关推荐
BK贝壳3 小时前
布尔数组也能做按位取反、移位和乘法:bool-hybrid-array 的位运算在本机跑了一遍
开发语言·数据结构·python·性能优化·numpy
吴名氏.4 小时前
全栈跃迁:Java 后端 27 讲【3】Vue3工程化脚手架
java·开发语言·springboot·全栈工程师
codeejun4 小时前
每日一Go·MySQL-24、死锁排查与解决方案
开发语言·mysql·golang
人间乄惊鸿客5 小时前
c++ 右值
c++·算法
fanstuck6 小时前
AI 客服答错了,三分钟分清是 Prompt 还是系统的锅
算法
小此方6 小时前
QT从入门到精通(一)Qt新手保姆级教程:从官网安装到项目实战,四步带你彻底搞懂Qt框架
开发语言·qt
田里的水稻7 小时前
FA_编程算法200个分类列数-SUM
算法·分类·数据挖掘
ShineWinsu7 小时前
对于Qt:TCP的解析
linux·c++·qt·网络协议·面试·tcp·客户端
瑶池酒剑仙7 小时前
C++重构与代码质量完全指南:从设计模式、SOLID原则到代码审查与静态分析的工程实践(所赖君子见机)
c语言·开发语言·c++·设计模式·重构·哈希算法·visual studio