C语言实现三子棋(附代码)

三子棋

1.问题描述

使用c语言程序实现三子棋

2.基本流程

1.设置菜单界面
2.创建棋盘并初始化
3.打印棋盘
4.玩家落子
5.判定胜负关系
6.电脑落子
7.判定胜负关系
8.回到步骤2继续执行

3. 结果演示

结果:C语言实现三子棋(附代码)_第1张图片
C语言实现三子棋(附代码)_第2张图片

4.代码实现

在这里插入代码片
```#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 
#define MAX_ROW 3
#define MAX_COL 3

//1.创建棋盘并初始化
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 print_chessBoard(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)
	{
     
		int row = 0;
		int col = 0;
		printf("请输入坐标(row col):");
		scanf("%d %d", &row, &col);
		/*校验玩家输入是否合法,若非法则重新输入*/
		if (row < 0 || row >= MAX_ROW || col < 0 || col >= MAX_COL)
		{
     
			printf("你的坐标不在合法范围内 [0, 2],请重新输入:\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()
{
     
	char chessBoard[MAX_ROW][MAX_COL] = {
      0 };
	init(chessBoard);
	char winner = ' ';
	while (1)
	{
     
		system("cls");
		print_chessBoard(chessBoard);
		playerMove(chessBoard);
		winner = isWin(chessBoard);
		if (winner != ' ') 
		{
     
			break;
		}
		computerMove(chessBoard);
		winner = isWin(chessBoard);
		if (winner != ' ') 
		{
     
			break;
		}
	}
	print_chessBoard(chessBoard);
	if (winner == 'x')
	{
     
		printf("您赢了!\n");
	}
	else if (winner == 'o')
	{
     
		printf("您输了!\n");
	}
	else 
	{
     
		printf("平手!\n");
	}
}

int menu()
{
     
	printf("--------------------------\n");
	printf("1.开始游戏\n");
	printf("0.退出游戏\n");
	printf("--------------------------\n");
	int choice = 0;
	printf("请输入你的选择:");
	scanf("%d", &choice);
	return choice;
}
int main()
{
     
	/*此处使用强制类型转换*/
	srand((unsigned int)time(0));
	/*此处通过简单的菜单和玩家交互开始一局游戏*/
	while (1)
	{
     
		int choice = menu();
		if (choice == 1)
		{
     
			game();
		}
		else if (choice == 0)
		{
     
			printf("goodbye!\n");
			break;
		}
		else
		{
     
			printf("错误,请重新输入!\n");
			continue;
		}
	}
	system("pause");
	return 0;
}





你可能感兴趣的:(c语言)