C语言实现三子棋

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

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#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("平手!");
	}
}

上一篇:状态压缩——2019 CTU G


下一篇:暴力枚举+stl——CTU 2019 J