c++蒟蒻五子棋游戏1.0双人版

感谢支持,欢迎评论
在这里插入图片描述
后续更新单人版。
尽情期待《c++蒟蒻五子棋2.0单人版》

#include <iostream>
#include <limits>
using namespace std;

const int BOARD_SIZE = 15;
char board[BOARD_SIZE][BOARD_SIZE];

// 初始化棋盘
void initBoard() {
	for (int i = 0; i < BOARD_SIZE; i++) {
		for (int j = 0; j < BOARD_SIZE; j++) {
			board[i][j] = '-';
		}
	}
}

// 显示棋盘
void displayBoard() {
	system("cls");
	// 打印列号
	cout << "   ";
	for (int j = 0; j < BOARD_SIZE; j++) {
		printf("%2d ", j + 1);
	}
	cout << "\n";
	
	// 打印棋盘内容
	for (int i = 0; i < BOARD_SIZE; i++) {
		printf("%2d ", i + 1);
		for (int j = 0; j < BOARD_SIZE; j++) {
			cout << " " << board[i][j] << " ";
		}
		cout << "\n";
	}
}

// 检查胜利条件
bool checkWin(int x, int y) {
	char current = board[x][y];
	int count;
	
	// 检查水平方向
	count = 1;
	for (int j = y - 1; j >= 0 && board[x][j] == current; j--) count++;
	for (int j = y + 1; j < BOARD_SIZE && board[x][j] == current; j++) count++;
	if (count >= 5) return true;
	
	// 检查垂直方向
	count = 1;
	for (int i = x - 1; i >= 0 && board[i][y] == current; i--) count++;
	for (int i = x + 1; i < BOARD_SIZE && board[i][y] == current; i++) count++;
	if (count >= 5) return true;
	
	// 检查主对角线
	count = 1;
	for (int i = x - 1, j = y - 1; i >= 0 && j >= 0 && board[i][j] == current; i--, j--) count++;
	for (int i = x + 1, j = y + 1; i < BOARD_SIZE && j < BOARD_SIZE && board[i][j] == current; i++, j++) count++;
	if (count >= 5) return true;
	
	// 检查副对角线
	count = 1;
	for (int i = x - 1, j = y + 1; i >= 0 && j < BOARD_SIZE && board[i][j] == current; i--, j++) count++;
	for (int i = x + 1, j = y - 1; i < BOARD_SIZE && j >= 0 && board[i][j] == current; i++, j--) count++;
	return count >= 5;
}

// 检查平局
bool checkDraw() {
	for (int i = 0; i < BOARD_SIZE; i++) {
		for (int j = 0; j < BOARD_SIZE; j++) {
			if (board[i][j] == '-') return false;
		}
	}
	return true;
}

int main() {
	initBoard();
	char currentPlayer = 'X';
	bool gameOver = false;
	
	while (!gameOver) {
		displayBoard();
		int row, col;
		bool valid = false;
		
		while (!valid) {
			cout << "玩家 " << currentPlayer << ",请输入行列号(1-15):";
			if (!(cin >> row >> col)) {
				cin.clear();
				cin.ignore(numeric_limits<streamsize>::max(), '\n');
				cout << "输入格式错误,请重新输入!\n";
				continue;
			}
			
			if (row < 1 || row > BOARD_SIZE || col < 1 || col > BOARD_SIZE) {
				cout << "输入超出范围,请重新输入!\n";
				continue;
			}
			
			row--;
			col--;
			
			if (board[row][col] != '-') {
				cout << "该位置已有棋子,请重新输入!\n";
			} else {
				valid = true;
			}
		}
		
		board[row][col] = currentPlayer;
		
		if (checkWin(row, col)) {
			displayBoard();
			cout << "恭喜玩家 " << currentPlayer << " 获胜!\n";
			gameOver = true;
		} else if (checkDraw()) {
			displayBoard();
			cout << "游戏结束,平局!\n";
			gameOver = true;
		} else {
			currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
		}
	}
	
	return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wangyuxuan1029

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值