三子棋

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 3
#define COL 3
char g_broad[ROW + 1][COL + 1];
//菜单
int menu(){
	printf("_____________________________\n");
	printf("1.开始游戏。\n");
	printf("2.退出游戏。\n");
	printf("_____________________________\n");
	int choice;
	printf("请输入你的选择:");
	scanf("%d", &choice);
	return choice;
}
//初始化二维数组为空格
void Init(char broad[ROW + 1][COL + 1]){
	for (int row = 0; row < ROW + 1; row++){
		for (int col = 0; col < COL + 1; col++){
			broad[row][col] = ' ';
		}
	}
}
//显示两方落子情况
void print(char broad[ROW + 1][COL + 1]){
	printf("   1   2   3 \n");
	printf("   ___________\n");
	for (int row = 1; row < ROW + 1; row++){
		printf("%d | %c | %c | %c |\n", row, broad[row][1],
			broad[row][2], broad[row][3]);
		printf("  |___|___|___|\n");
	}
}
//玩家落子
void playmove(char broad[ROW + 1][COL + 1]){
	printf("玩家落子!\n");
	while (1){
		int row = 0;
		int col = 0;
		printf("请输入你选的坐标(row col):");
		scanf("%d %d", &row, &col);
		if (row < 1 || row >= ROW + 1 || col < 1 || col >= COL + 1){
			printf("输入非法,请重新输入!\n");
			continue;
		}
		if (broad[row][col] != ' '){
			printf("你选择的地方已有落子,请重新选择!\n");
			continue;
		}
		broad[row][col] = 'x';
		break;
	}
}
//电脑落子
void computermove(char broad[ROW + 1][COL + 1]){
	printf("电脑落子!\n");
	while (1){
		int row = rand() % ROW + 1;
		int col = rand() % COL + 1;
		if (broad[row][col] != ' '){
			continue;
		}
		broad[row][col] = 'o';
		break;
	}
}
int isfull(char broad[ROW + 1][COL + 1]){
	for (int row = 1; row < ROW + 1; row++){
		for (int col = 1; col < COL + 1; col++){
			if (broad[row][col] == ' '){
				return 0;
				break;
			}
		}
	}
	return 1;
}
//检查游戏是否结束
char checkwinner(char broad[ROW + 1][COL + 1]){
	for (int row = 1; row < ROW + 1; row++){
		if (broad[row][1] == broad[row][2] &&
			broad[row][1] == broad[row][3] &&
			broad[row][1] != ' '){
			return broad[row][1];
		}
	}
	for (int col = 1; col < COL + 1; col++){
		if (broad[1][col] == broad[2][col] &&
			broad[1][col] == broad[3][col] &&
			broad[1][col] != ' '){
			return broad[1][col];
		}
	}
	if (broad[1][1] == broad[2][2] &&
		broad[1][1] == broad[3][3] &&
		broad[1][1] != ' '){
		return broad[1][1];
	}
	if (broad[3][1] == broad[2][2] &&
		broad[3][1] == broad[1][3] &&
		broad[3][1] != ' '){
		return broad[3][1];
	}
	if (isfull(broad)){
		return 'p';
	}
	return ' ';
}
//游戏过程
void game(){
	Init(g_broad);
	char winner = ' ';
	while (1){
		print(g_broad);
		playmove(g_broad);
		winner = checkwinner(g_broad);
		if (winner != ' '){
			break;
		}
		computermove(g_broad);
		winner = checkwinner(g_broad);
		if (winner != ' '){
			break;
		}
	}
	if (winner == 'x'){
		print(g_broad);
		printf("玩家胜利!\n");
	}
	else if (winner == 'o'){
		print(g_broad);
		printf("电脑胜利!\n");
	}
	else if (winner == 'p'){
		print(g_broad);
		printf("和棋!\n");
	}
	else{
		printf("程序出错!\n");
	}
}
int main(){
	srand((unsigned int)time(0));
	while (1){
		int choice = menu();
		if (choice == 1){
			game();
		}
		else if (choice == 2){
			printf("欢迎你的下次使用!\n");
			break;
		}
		else{
			printf("输入非法,请重新输入!\n");
		}
	}
	system("pause");
	return 0;
}

如果要改成五子棋将宏定义中ROW COL改成10,就将棋盘改成十行十列。还需改一下代码:1.棋盘显示的代码。

//显示两方落子情况
void print(char broad[ROW + 1][COL + 1]){
	printf("    1   2   3   4   5   6   7   8   9   10\n");
	printf("   _______________________________________\n");
	for (int row = 1; row < ROW + 1; row++){
		if (row == 10){
			printf("%d", row);
		}
		else{
			printf("%d ", row);
		}	
		printf("| %c | %c | %c | %c | %c | %c | %c | %c | %c | %c |\n",
			broad[row][1], broad[row][2], broad[row][3], broad[row][4], 
			broad[row][5], broad[row][6], broad[row][7], broad[row][8], 
			broad[row][9], broad[row][10]);
		printf("  |___|___|___|___|___|___|___|___|___|___|\n");
	}
}

2.判断胜利的条件。

//检查游戏是否结束
char checkwinner(char broad[ROW + 1][COL + 1]){
    //检查行
	for (int row = 1; row < ROW + 1; row++){
		for (int col = 1; col < COL - 3; col++){
			if (broad[row][col] == broad[row][col + 1] &&
				broad[row][col] == broad[row][col + 2] &&
				broad[row][col] == broad[row][col + 3] &&
				broad[row][col] == broad[row][col + 4] &&
				broad[row][col] != ' '){
				return broad[row][col];
			}
		}
	}
	//检查列
	for (int col = 1; col < COL + 1; col++){
		for (int row = 1; row < ROW - 3; row++){
			if (broad[row][col] == broad[row + 1][col] &&
				broad[row][col] == broad[row + 2][col] &&
				broad[row][col] == broad[row + 3][col] &&
				broad[row][col] == broad[row + 4][col] &&
				broad[row][col] != ' '){
				return broad[row][col];
			}
		}
	}
	//检查主对角线
	for (int row = 1; row < ROW - 3; row++){
		for (int col = 1; col < COL - 3; col++){
			if (broad[row][col] == broad[row + 1][col + 1] &&
				broad[row][col] == broad[row + 2][col + 2] &&
				broad[row][col] == broad[row + 3][col + 3] &&
				broad[row][col] == broad[row + 4][col + 4] &&
				broad[row][col] != ' '){
				return broad[row][col];
			}
		}
	}
	//检查副对角线
	for (int row = ROW; row > 4; row--){
		for (int col = 1; col < COL - 3; col++){
			if (broad[row][col] == broad[row - 1][col + 1] &&
				broad[row][col] == broad[row - 2][col + 2] &&
				broad[row][col] == broad[row - 3][col + 3] &&
				broad[row][col] == broad[row - 4][col + 4] &&
				broad[row][col] != ' '){
				return broad[row][col];
			}
		}
	}
	//检查是否和棋
	if (isfull(broad)){
		return 'p';
	}
	return ' ';
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值