C语言学习历程之三子棋游戏

以下内容为小编整理的近期学习C语言三子棋游戏流程及代码,以下内容对初学C语言的童鞋们巩固循环、数组、函数等内容有一定的帮助!!

1.三子棋游戏简单流程图

C语言学习历程之三子棋游戏_第1张图片

上图为一轮游戏的简单流程图

2.三子棋游戏代码

#include "chess.h"

//定义游戏开始菜单
void Menu()
{
	printf("################################\n");
	printf("####   欢迎进入三子棋游戏   ####\n");
	printf("################################\n");
	printf("## 1.Play            2.Exit ####\n");
	printf("################################\n");
	printf("Please enter Select:->");
}

int main()
{
	int quit = 0;
	while (!quit){
		int select = 0;
		Menu();
		scanf("%d", &select);

		switch (select){
		case 1:
			Game();
			break;
		case 2:
			quit = 1;
			break;
		default:
			printf("Enter Error!\n");
			break;
		}
	}

	system("pause");
	return 0;
}
#include "chess.h"

void InitBoard(char board[][COL], int row, int col)
{
	int i = 0;
	//初始化棋盘上所有的位置为空字符。
	for (; i < row; i++){
		int j = 0;
		for (; j < col; j++){
			board[i][j] = ' ';
		}
	}
}

//显示整个棋盘
ShowBoard(char board[][COL], int row, int col)
{
	printf("  | 1 | 2 | 3 |\n");
	int i = 0;
	for (; i < row; i++){
		printf("------------------\n");
		printf(" %d | %c | %c | %c |\n", i + 1, board[i][0], board[i][1], board[i][2]);
	}
	printf("----------------\n");
}

int PlayerMove(char board[][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("Please Enter Your Pos # ");
	scanf("%d %d", &x, &y);       //定义棋手落子坐标
	if (x >= 1 && x <= 3 && y >= 1 && y <= 3){
		if (board[x - 1][y - 1] != ' '){
			return 2; //坐标被占用
		}
		board[x - 1][y - 1] = BLACK_PIECE;
		return 0;
	}
	return 1;  //用户输入的坐标有误
}

//用户赢了 BLACK_PLECE 'X'
//电脑赢了 WHITE_PIECE 'O'
//平局‘E'
//next 'N'
char JudgeResult(char board[][COL], int row, int col)
{
	int i = 0;
	for (; i < row; i++){
		if (board[i][0] != ' '&&board[i][0] == board[i][1] == board[i][2]){
			//判断每行是否存在三子相同的情况
			return board[i][0];
		}
	}

	for (i = 0; i < col; i++){
		if (board[0][i] != ' '&&board[0][i] == board[1][i]&&board[1][i] == board[2][i]){
			//判断每列是否存在三子相同的情况
			return board[0][i];
		}
	}

	//判断两个对角线是否存在三子相同的情况
	if (board[0][0] != ' '&&board[0][0] == board[1][1] && board[1][1] == board[2][2]){
		return board[0][0];
	}
	if (board[0][2] != ' '&&board[0][2] == board[1][1] && board[1][1] == board[2][0]){
		return board[1][1];
	}

	//如果无以上情况,判断棋盘上是否存在空位,
	//有则返回N,继续游戏,无则返回E,平局游戏结束
	for (i = 0; i < row; i++){
		int j = 0;
		for (; j < col; j++){
			if (board[i][j] == ' '){
				return 'N';
			}
		}
	}
	return 'E';
}

void ComputerMove(char board[][COL], int row, int col){
	while (1){
		//产生0-2之间的随机数,(i,j)为电脑落子的坐标
		int i = rand() % row;
		int j = rand() % col;
		//判断落子的坐标是否为空字符,是则落子
		if (board[i][j] == ' '){
			board[i][j] = WHITE_PIECE;
			break;
		}
	}
	printf("Computer .... Done");
	Sleep(1000);
}


void Game()
{
	//定义3*3数组,保存棋盘上棋子的分布情况
	char board[ROW][COL];
	//初始化棋盘
	InitBoard(board, ROW, COL);
	char result = 'N';    //定义返回值,用于每次落子后的判定
	srand((unsigned long)time(NULL));    //种随机数种子,用于电脑的随机落子
	while (1){
		ShowBoard(board, ROW, COL);
		int type = PlayerMove(board, ROW, COL);
		if (1 == type){
			printf("你输入的坐标有误,请重新输入!\n");
			continue;
		}
		else if (2 == type){
			printf("你输入的坐标已经被占用,请重新输入\n");
			continue;
		}
		else{
			printf("Player ..... Done!\n");
		}
		result = JudgeResult(board, ROW, COL);
		if (result != 'N'){
			break;
		}

		ComputerMove(board, ROW, COL);
		result = JudgeResult(board, ROW, COL);
		if (result != 'N'){
			break;
		}

	}

	ShowBoard(board, ROW, COL);

	switch (result){
	case 'E':
		printf("平局\n");
		break;
	case BLACK_PIECE:
		printf("你赢了\n");
		break;
	case WHITE_PIECE:
		printf("电脑赢了\n");
		break;
	default:
		break;
	}

}

 

你可能感兴趣的:(C语言学习)