c语言代码实现三子棋(详解)

设计总体思路

实现游戏可以一直玩,先打印棋盘,玩家和电脑下棋,最后分出胜负。

具体步骤

目录

一、打印游戏菜单界面

二、打印棋盘

三、玩家下棋

四、电脑下棋

五、判断输赢


一、打印游戏菜单界面

首先界面有选项选择,开始你的游戏,代码以及效果图如下:

void menu()//打印菜单
{
	printf("******************\n");
	printf("******1.play******\n");
	printf("******0.exit******\n");
	printf("******************\n");
}
int main()
{
	int input = 0;
	srand((unsigned int)time(NULL));//电脑随机产生数字
	do
	{
		menu();
		printf("请选择->\n");
		scanf("%d", &input);
		switch (input)//选择选项
		{
		case 1:game();
			break;
		case 0:printf("退出游戏\n");
			break;
		default:printf("输入错误,请重新输入\n");
		}

	} while (input);
	return 0;
}

c语言代码实现三子棋(详解)_第1张图片

二、打印棋盘

创建并初始化数组,再打印棋盘,代码以及效果图如下:

//存放下棋数据
char board[ROW][COL] = { 0 };

//初始化棋盘为全空格
InitBoard(board, ROW, COL);

//初始化棋盘为空格
void InitBoard(char board[ROW][COL], int row, int col)
{
	int i = 0;
	for (i = 0; i < row; i++)
	{
		int j = 0;
		for (j = 0; j < col; j++)
		{
			board[i][j] = ' ';
		}
	}
}
//打印棋盘
void DisplayBoard(char board[ROW][COL], int row, int col)//打印棋盘
{
	//打印数据
	int i = 0;
	for (i = 0; i < row; i++)
	{
		int j = 0;
		for (j = 0; j < col; j++)
		{
			printf(" %c ",board[i][j]);//别忘记board[i][j]
			if (j < col - 1)
			{
				printf("|");
			}
		}
		printf("\n");
		//打印分割行
		if (i < row-1)//记住   row-1
		{
			for (j = 0; j < col; j++)
			{
				printf("---");
				if (j < col - 1)
				{
					printf("|");
				}
			}
			printf("\n");
		}
	}
}

c语言代码实现三子棋(详解)_第2张图片

三、玩家下棋

 '*' 代表玩家下的棋子。

代码思路,第一步创建玩家下棋的函数player_move()并输入坐标,代码图如下:

//玩家下棋
void player_move(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("玩家下棋\n");
	while (1)
	{
		printf("请输入坐标:");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (board[x - 1][y - 1] == ' ')
			{
				board[x - 1][y - 1] = '*';
				break;
			}
			else
			{
				printf("该坐标被占用,请重新输入\n");
			}
		}
		else
		{
			printf("非法坐标,请重新输入\n");
		}
	}
}

四、电脑下棋

 '#' 代表电脑下的棋子。

代码思路,第一步创建电脑下棋的函数computer_move()并输入坐标,代码图如下:

void computer_move(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("电脑下棋:>\n");
	while (1)
	{
		x = rand() % 3;
		y = rand() % 3;
		if (board[x][y] == ' ')
		{
			board[x][y] = '#';
			break;
		}
	}
}

五、判断输赢

创建判断输赢的is_win()函数,while()循环实现玩家和电脑可以轮流下直到一方胜利,

如果有三个 '*' 符号连成一条线,代表玩家获胜,如果有三个 '#' 符号连成一条线,代表电脑获胜,或者平局。is_win()函数中玩家赢返回'*',电脑赢返回-'#',平局返回'Q',继续返回'C',

	while (1)//玩家和电脑轮流下棋,直到一方胜利
	{   //玩家下棋
		player_move(board, ROW, COL);
		//打印棋盘
		DisplayBoard(board, ROW, COL);
		ret = is_win(board, ROW, COL);
		if (ret != 'C')
		{
			break;
		}
		//电脑下棋
		computer_move(board, ROW, COL);
		//打印棋盘
		DisplayBoard(board, ROW, COL);
		ret = is_win(board, ROW, COL);
		if (ret != 'C')
		{
			break;
		}
	}
	if (ret == '*')
	{
		printf("玩家获胜\n");
	}
	else if (ret == '#')
	{
		printf("电脑获胜\n");
	}
	else // (ret == 'Q')
	{
		printf("平局\n");
	}
	DisplayBoard(board, ROW, COL);
}

 //判断输赢
char is_win(char board[ROW][COL], int row, int col)
{
	//判断每一行是否都相等
	int i = 0;
	for (i = 0; i < col; i++)
	{
		if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
		{
			return  board[i][1];
		}
	}
	//判断每一列是否都相等
	for (i = 0; i < col; i++)
	{
		if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ')
		{
			return board[i][1];
		}
	}
	//判断对角线是否都相等
	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
	{
		return board[1][1];
	}
	if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
	{
		return board[1][1];
	}
	//判断平局
	if (is_full(board, row, col) == 1)
	{
		return 'Q';
	}
	//继续
	return 'C';
}

 最后整理代码

#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include
#define ROW 3
#define COL 3
//初始化棋盘
void InitBoard(char board[ROW][COL], int row, int col);
//打印棋盘
void DisplayBoard(char board[ROW][COL], int row, int col);
//玩家下棋
void player_move(char board[ROW][COL], int row, int col);
//电脑下棋
void computer_move(char board[ROW][COL], int row, int col);
//判断输赢
char is_win(char board[ROW][COL], int row, int col);

//初始化棋盘为空格
void InitBoard(char board[ROW][COL], int row, int col)
{
	int i = 0;
	for (i = 0; i < row; i++)
	{
		int j = 0;
		for (j = 0; j < col; j++)
		{
			board[i][j] = ' ';
		}
	}
}
void DisplayBoard(char board[ROW][COL], int row, int col)//打印棋盘
{
	//打印数据
	int i = 0;
	for (i = 0; i < row; i++)
	{
		int j = 0;
		for (j = 0; j < col; j++)
		{
			printf(" %c ", board[i][j]);//别忘记board[i][j]
			if (j < col - 1)
			{
				printf("|");
			}
		}
		printf("\n");
		//打印分割行
		if (i < row - 1)//记住   row-1
		{
			for (j = 0; j < col; j++)
			{
				printf("---");
				if (j < col - 1)
				{
					printf("|");
				}
			}
			printf("\n");
		}
	}
}
void player_move(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("玩家下棋\n");
	while (1)
	{
		printf("请输入坐标:");
		scanf("%d %d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (board[x - 1][y - 1] == ' ')
			{
				board[x - 1][y - 1] = '*';
				break;
			}
			else
			{
				printf("该坐标被占用,请重新输入\n");
			}
		}
		else
		{
			printf("非法坐标,请重新输入\n");
		}
	}
}
void computer_move(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("电脑下棋:>\n");
	while (1)
	{
		x = rand() % 3;
		y = rand() % 3;
		if (board[x][y] == ' ')
		{
			board[x][y] = '#';
			break;
		}
	}
}
static int is_full(char board[ROW][COL], int row, int col)
{
	int i = 0;
	for (i = 0; i < row; i++)
	{
		int j = 0;
		for (j = 0; j < col; j++)
		{
			if (board[i][j] == ' ')
			{
				return 0;//没满
			}
		}
	}
	return 1;
}
//判断输赢
char is_win(char board[ROW][COL], int row, int col)
{
	//判断每一行是否都相等
	int i = 0;
	for (i = 0; i < col; i++)
	{
		if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
		{
			return  board[i][1];
		}
	}
	//判断每一列是否都相等
	for (i = 0; i < col; i++)
	{
		if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ')
		{
			return board[i][1];
		}
	}
	//判断对角线是否都相等
	if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
	{
		return board[1][1];
	}
	if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
	{
		return board[1][1];
	}
	//判断平局
	if (is_full(board, row, col) == 1)
	{
		return 'Q';
	}
	//继续
	return 'C';
}
void menu()
{
	printf("******************\n");
	printf("******1.play******\n");
	printf("******0.exit******\n");
	printf("******************\n");
}
void game()
{
	int ret = 0;
	//存放下棋数据
	char board[ROW][COL] = { 0 };
	//初始化棋盘为全空格
	InitBoard(board, ROW, COL);
	//打印棋盘
	DisplayBoard(board, ROW, COL);
//玩家赢返回'*'
//电脑赢返回'#'
//平局返回'Q'
//继续返回'C'
	while (1)
	{   //玩家下棋
		player_move(board, ROW, COL);
		//打印棋盘
		DisplayBoard(board, ROW, COL);
		ret = is_win(board, ROW, COL);
		if (ret != 'C')
		{
			break;
		}
		//电脑下棋
		computer_move(board, ROW, COL);
		//打印棋盘
		DisplayBoard(board, ROW, COL);
		ret = is_win(board, ROW, COL);
		if (ret != 'C')
		{
			break;
		}
	}
	if (ret == '*')
	{
		printf("玩家获胜\n");
	}
	else if (ret == '#')
	{
		printf("电脑获胜\n");
	}
	else // (ret == 'Q')
	{
		printf("平局\n");
	}
	DisplayBoard(board, ROW, COL);
}
int main()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
		menu();
		printf("请选择->\n");
		scanf("%d", &input);
		switch (input)
		{
		case 1:game();
			break;
		case 0:printf("退出游戏\n");
			break;
		default:printf("输入错误,请重新输入\n");
		}

	} while (input);
	return 0;
}

运行效果图:

c语言代码实现三子棋(详解)_第3张图片

 c语言代码实现三子棋(详解)_第4张图片

 希望对你们有帮助

 

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