三子棋

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

#define MAX_ROW 3
#define MAX_COL 3
//三子棋
//1.建立3*3棋盘并初始化
//2.显示棋盘
//3.玩家落子(输入坐标)
//4.判断胜负
//5.电脑落子(随机落子)
//6.判断胜负
//回到2继续执行
int menu(){
	printf("-----------------\n");
	printf("1.开始游戏\n");
	printf("0.结束游戏\n");
	printf("-----------------\n");
	printf("请输入你的选择:");
	int choice = 0;
	scanf("%d", &choice);
	return choice;
}

void init(char  chessBoard[MAX_ROW][MAX_COL]){
//把数组中的每个元素都设为空格
	for (int row = 0; row < MAX_ROW; row++){
		for (int col = 0; col < MAX_COL; col++){
			chessBoard[row][col] = ' ';

		}
	}
}

void printChessBoard(char  chessBoard[MAX_ROW][MAX_COL]){
	printf("+---+---+---+\n");
	for (int row = 0; row < MAX_ROW; row++){
		printf("| %c | %c | %c |\n", chessBoard[row][0],
			chessBoard[row][1], chessBoard[row][2]);
		printf("+---+---+---+\n");
	}
}

void playerMove(char  chessBoard[MAX_ROW][MAX_COL]){
	while (1){
	printf("请输入坐标:(row col):");
	int row = 0;
	int col = 0;
	scanf("%d %d", &row, &col);
//验证玩家输入是否有误
	if (row < 0 || row >= MAX_ROW ||
		col < 0 || col >= MAX_COL){
		printf("你的输入有误,请重新输入\n");
		continue;
	}
	//验证玩家落子处有没有子
	if (chessBoard[row][col] != ' '){
		printf("你的坐标有子了,重新输入\n");
		continue;
	}
	//真正落子
	chessBoard[row][col] = 'x';
	break;
	}
}

void computerMove(char  chessBoard[MAX_ROW][MAX_COL]){
	
	while(1){
    int row = rand() % MAX_ROW;
	int col = rand() % MAX_COL;
	if (chessBoard[row][col] != ' '){
		//保证随机数下的地方无子
		continue;
	}
	chessBoard[row][col] = 'o';
	break;
	}

}

int isFull(char  chessBoard[MAX_ROW][MAX_COL]){
	//找空格,找到了就是满了
	for(int row = 0; row < MAX_ROW; row++){
		for (int col = 0; col < MAX_COL; col++){
			if (chessBoard[row][col] == ' '){
				return 0;
			}
		}
	}
	return 1;
}
//返回'x' 玩家获胜
//返回'o' 电脑获胜
//返回' ' 继续下棋
//返回'q' 和棋
char isWin(char  chessBoard[MAX_ROW][MAX_COL]){
	//判断所有行
	for (int row = 0; row < MAX_ROW; row++){
		//先使元素都不为空格
		if (chessBoard[row][0] != ' '
			&&chessBoard[row][0] == chessBoard[row][1]
			&& chessBoard[row][0] == chessBoard[row][2])
			return chessBoard[row][0];
	}
	//判断所有列
	for (int col = 0; col < MAX_COL; col++){
		if (chessBoard[0][col] != ' '
			&&chessBoard[0][col] == chessBoard[1][col]
			&& chessBoard[0][col] == chessBoard[2][col])
			return chessBoard[0][col];
	}
	//判断对角线
	if (chessBoard[0][0] != ' '
		&&chessBoard[0][0] == chessBoard[1][1]
		&& chessBoard[0][0] == chessBoard[2][2]){
		return chessBoard[0][0];
	}
	if (chessBoard[2][0] != ' '
		&&chessBoard[2][0] == chessBoard[1][1]
		&& chessBoard[2][0] == chessBoard[0][2]){
		return chessBoard[2][0];
	}
	//判断是否和棋,是否有剩余空间
	if (isFull(chessBoard)){
		return 'q';
	}
	return ' ';
}
	void game(){
	//1.建立3*3棋盘并初始化
	char  chessBoard[MAX_ROW][MAX_COL] = { 0 };
	//创建一个函数, 把每一个元素都初始化为'空格'
	init(chessBoard);
	char winner = ' ';
	while (1){
		//2.显示棋盘
		printChessBoard(chessBoard);
		//3.玩家落子(输入坐标)
		playerMove(chessBoard);
		//4.判断胜负
		winner = isWin(chessBoard);
		if (winner != ' '){
			break;
		}
		//5.电脑落子(随机落子)
		computerMove(chessBoard);
		//6.判断胜负
		winner = isWin(chessBoard);
		if (winner != ' '){
			break;
		}
	}
	//打印最后一次的结果
	printChessBoard(chessBoard);
	if (winner == 'x'){
		printf("恭喜你,你赢了\n");
	}
	else if (winner == 'o'){
		printf("很遗憾,你连人工智障都下不过\n");
	}
	else{
		printf("很遗憾,你和人工智障五五开\n");
	}
}
int main(){
	//随机种子
	srand((unsigned int)time(0));
	/*打印菜单*/
	while (1){
		int choice = menu();
		if (choice == 1){
			game();
		}
		else if (choice == 0){
			printf("游戏结束,拜拜\n");
			break;
		}
		}
	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值