C语言实现三子棋

C语言实现三子棋,第1张

C语言实现三子棋

简单的三子棋,电脑随即下子,使用二维数组定义棋盘并存储棋子

#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#define ROWSIZE 3//宏定义行数
#define COLSIZE 3//宏定义列数

char jugde(char chess[ROWSIZE][COLSIZE]) {//判断胜负
	// 判断行
	for (int i = 0; i < ROWSIZE; i++) {
		if (chess[i][0] != ' ' && chess[i][0] == chess[i][1] && chess[i][0] == chess[i][2]) {
			return chess[i][0];
		}
	}
	//判断列
	for (int i = 0; i < COLSIZE; i++) {
		if (chess[0][i] != ' ' && chess[0][i] == chess[1][i] && chess[0][i] == chess[2][i]) {
			return chess[0][i];
		}
	}
	//判断对角线
	if (chess[0][0] != ' ' && chess[0][0] == chess[1][1] && chess[0][0] == chess[2][2]) {
		return chess[0][0];
	}
	//判断负对角线
	if (chess[0][2] != ' ' && chess[0][2] == chess[1][1] && chess[0][2] == chess[2][0]) {
		return chess[0][2];
	}

	return ' ';
}

int boardFall(char chess[RAND_MAX][COLSIZE]) {//判断棋盘是否满,如果满则退出循环
	int temp_fall = 0;
	for (int i = 0; i < ROWSIZE; i++) {
		for (int j = 0; j < COLSIZE; j++) {
			if (chess[i][j] != ' ') {
				temp_fall++;
			}
		}
	}
	if (temp_fall == 9) {
		return 1;
	}
	return 0;
}

int computerPut(char chess[ROWSIZE][COLSIZE]) {//电脑输入(采用时间戳赋值随机因子使电脑随机落子)
	int rand_row = 0;
	int rand_col = 0;
	while (1) {
	    rand_row = rand() % 3;
	    rand_col = rand() % 3;
		if (chess[rand_row][rand_col] == ' ') {
			chess[rand_row][rand_col] = 'O';
			break;
		}
		if (boardFall(chess) == 1) {
			break;
		}
	}
}

int playerPut(char chess[ROWSIZE][COLSIZE],int row,int col) {//玩家输入
	if ((row < ROWSIZE && row >= 0) && (col >= 0 && col < COLSIZE)) {
		if (chess[row][col] == ' ') {
			chess[row][col] = 'X';
			return 1;
		}
		else {
			printf("该位置已有棋子请重新落子n");
			return 0;
		}
	}
	else {
		printf("棋子位置超出棋盘请重新落子n");
		return 0;
	}
}

int inputBoard(char chess[ROWSIZE][COLSIZE]) {//打印棋盘
	for (int i = 0; i < ROWSIZE; i++) {
		printf("+---+---+---+n");
		for (int j = 0; j < COLSIZE; j++) {
			printf("|");
			printf(" %c ", chess[i][j]);
			if (j == 2) {
				printf("|");
				printf("n");
			}
		}
	}
	printf("+---+---+---+");
}

int initBoard(char chess[ROWSIZE][COLSIZE]) {//初始化棋盘
	for (int i = 0; i < ROWSIZE; i++) {
		for (int j = 0; j < COLSIZE; j++) {
			chess[i][j] =' ';
		}
	}
}

int main() {
    char chess[ROWSIZE][COLSIZE] = { 0 };
	initBoard(chess);//初始化棋盘
	int row_ = 0;
	int col_ = 0;
	char temp_3 = 0;
	int temp_ = 0;
	int temp_1 = 0;
	int temp_2 = 0;
	srand((unsigned int)time(0));
	while (1) {
		system("cls");
		//1.打印棋盘
		inputBoard(chess);
		printf("n");
		//2.用户输入
		while (1) {
			printf("玩家下入棋子坐标(中间用空格隔开):");
			scanf("%d %d", &row_, &col_);
			temp_ = playerPut(chess, row_ - 1, col_ - 1);
			if (temp_ == 1) {
				break;
			}
		}
		//胜负判断结束循环
		temp_3 = jugde(chess);
		temp_2 = boardFall(chess);
		if (temp_3 == 'X') {
			break;
		}
		if (temp_3 == 'O') {
			break;
		}
		if (temp_2 == 1) {
			break;
		}
		//3.电脑输入
		computerPut(chess);
		//判断胜负结束循环
		temp_3 = jugde(chess);
		temp_2 = boardFall(chess);
		if (temp_3 == 'X') {
			break;
		}
		if (temp_3 == 'O') {
			break;
		}
		if (temp_2 == 1) {
			break;
		}

	}

	system("cls");
	inputBoard(chess);
	temp_3 = jugde(chess);
	printf("n");
	if (temp_3 == 'X') {
		printf("玩家胜!");
	}
	if (temp_3 == 'O') {
		printf("电脑胜!");
	}
	if (temp_3 == ' ') {
		printf("平手!");
	}
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/3971660.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-10-21
下一篇 2022-10-21

发表评论

登录后才能评论

评论列表(0条)

保存