C语言—井字棋的实现

模拟人机对战。

首先,我们需要一个简单的井字棋棋盘。

#define COL 3
#define ROW 3
//利用宏定义来定义棋盘的大小,因为井字棋需要一个3*3的棋盘,为了避免魔幻数字(在后续代码中出现,不能很直观的看出是长还是宽)的出现,我们用ROW和COl来代替

void init_board(char board[ROW][COL]) {
	for (int row = 0; row < ROW; row++) {
		for (int col = 0; col < COL; col++) {
			board[row][col] = '  ';//将棋盘初始化为空		}
	}
}

当棋盘初始化好之后,虽然已经创建好,但是因为棋盘都是空,不能很直观的看到棋盘,所以我们给棋盘加一个边框。

void print_board(char board[ROW][COL]) {
	system("cls");
	for (int row = 0; row < ROW; row++) {
		printf("+---+---+---+");
		printf("\n");
		for (int col = 0; col < COL; col++) {
			printf("| %c ", board[row][col]);
		}
		printf("|");
		printf("\n");
	}
	printf("+---+---+---+\n");
}

这样,一个简单的棋盘就出现了。

然后,我们开始游戏。我们让玩家先落子。

void palyer(char board[ROW][COL]) {
	printf("请玩家落子:");
	printf("请输入您落子的位置(row,col):");
	while (1) {
//我们用棋盘的位置信息来控制落子地点
		int row = 0;
		int col = 0;
		scanf("%d %d", &row, &col);
//在玩家落子之前需要检测一下玩家落子是否合法(位置是否为空;位置是否超出棋盘大小)
		if (board[row][col] == ' ') {
			board[row][col] = 'x';//定义玩家落子为x
			break;
		}
		else {
			printf("落子有误,请重新落子!");
			continue;//如果是非法落子,利用continue来提示玩家重新落子
		}
	}
}

玩家落子完成后,轮到电脑落子。电脑落子我们用随机数来模拟。

#include <time.h>//rand函数所需要包含的头文件
rand((unsigned int)time(NULL));//设置随机数种子

void computer(char board[ROW][COL]) {
	while (1) {
		int row = rand() % ROW;//用生成的随机数来模除棋盘大小,这样就得到一个符合棋盘大小的随机数
		int col = rand() % COL;
		if (board[row][col] == ' ') {
			board[row][col] = 'o';
			break;
		}
		continue;
	}
}

判定胜负

char  win(char board[ROW][COL]) {
//检测行是否想同
	for (int row = 0; row < ROW; row++) {
		if (board[row][0] == board[row][1] && board[row][0] == board[row][2] && board[row][0] != ' ') {
			return board[row][0];
		}
	}
//检测列是否想同
	for (int col = 0; col < COL; col++) {
		if (board[0][col] == board[1][col] && board[0][col] == board[2][col] && board[0][col] != ' ') {
			return board[0][col];
		}
	}
//检测两条对角线是否想同
	if (board[0][0] != ' ' && board[0][0] == board[1][1] && board[0][0] == board[2][2]) {
		return board[0][0];
	}
	if (board[0][2] != ' ' && board[0][2] == board[1][1] && board[0][2] == board[2][0]) {
		return board[0][2];
	}
//检测棋盘是否还有空余的位置
	for (int row = 0; row < ROW; row++) {
		for (int col = 0; col < COL; col++) {
			if (board[row][col] == ' ') {
				return	'q';
			}
		}
	}
	return 'p';
}

总代码

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <time.h>
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#define COL 3
#define ROW 3
//准备棋盘
void init_board(char board[ROW][COL]) {
	for (int row = 0; row < ROW; row++) {
		for (int col = 0; col < COL; col++) {
			board[row][col] = '  ';
		}
	}
}

void print_board(char board[ROW][COL]) {
	system("cls");
	for (int row = 0; row < ROW; row++) {
		printf("+---+---+---+");
		printf("\n");
		for (int col = 0; col < COL; col++) {
			printf("| %c ", board[row][col]);
		}
		printf("|");
		printf("\n");
	}
	printf("+---+---+---+\n");
}
void palyer(char board[ROW][COL]) {
	printf("请玩家落子:");
	printf("请输入您落子的位置(row,col):");
	while (1) {
		int row = 0;
		int col = 0;
		scanf("%d %d", &row, &col);
		if (board[row][col] == ' ') {
			board[row][col] = 'x';
			break;
		}
		else {
			printf("落子有误,请重新落子!");
			continue;
		}
	}
}

void computer(char board[ROW][COL]) {
	while (1) {
		int row = rand() % ROW;
		int col = rand() % COL;
		if (board[row][col] == ' ') {
			board[row][col] = 'o';
			break;
		}
		continue;
	}
}


char  win(char board[ROW][COL]) {
	for (int row = 0; row < ROW; row++) {
		if (board[row][0] == board[row][1] && board[row][0] == board[row][2] && board[row][0] != ' ') {
			return board[row][0];
		}
	}
	for (int col = 0; col < COL; col++) {
		if (board[0][col] == board[1][col] && board[0][col] == board[2][col] && board[0][col] != ' ') {
			return board[0][col];
		}
	}
	if (board[0][0] != ' ' && board[0][0] == board[1][1] && board[0][0] == board[2][2]) {
		return board[0][0];
	}
	if (board[0][2] != ' ' && board[0][2] == board[1][1] && board[0][2] == board[2][0]) {
		return board[0][2];
	}
	for (int row = 0; row < ROW; row++) {
		for (int col = 0; col < COL; col++) {
			if (board[row][col] == ' ') {
				return	'q';
			}
		}
	}
	return 'p';
}

int main() {
	rand((unsigned int)time(NULL));
	char board[ROW][COL] = { 0 };
	init_board(board);
	char winner = ' ';
	while (1) {
		//打印棋盘
		print_board(board);
		//玩家落子
		palyer(board);
		winner = win(board);
		if (winner != 'q') {
			break;
		}
		//电脑落子
		computer(board);
		//判定胜负
		winner = win(board);
		if (winner != 'q') {
			break;
		}
	}
	if (winner == 'x') {
		printf("玩家获胜!");
	}
	else if (winner == 'o') {
		printf("电脑获胜!");
	}
	else if(winner == 'p'){
		printf("和棋");
	}
}

上一篇:C语言实现三子棋游戏


下一篇:面试题12:矩阵中的路径