C语言实现三子棋

今天给大家分享一下学完数组之后,对数组的一个应用。下面是用c语言写的一个三子棋的小游戏。

#define _CRT_SECURE_NO_WARNINGS 1
#define ROW  3
#define COL  3
#include
#include
#include
void menu()
{
	printf("****************************************\n");
	printf("***********     1.Play       ***********\n");
	printf("***********     0.Exit       ***********\n");
	printf("****************************************\n");

}
void Init_board(char board[ROW][COL], int row, int  col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			board[i][j] = ' ';
		}
	}
}
//void Display_board(char board[ROW][COL], int row, int  col)
//{
//	int i = 0;
//	int j = 0;
//	for (i = 0; i < row; i++)
//	{
//		for (j = 0; j < col; j++)
//		{
//			printf("%c", board[i][j]);
//		}
//		printf("\n");
//	}
//}
//void Display_board(char board[ROW][COL], int row, int  col)
//{
//	int i = 0;
//	int j = 0;
//	for (i = 0; i < row; i++)
//	{
//		//先打印数据
//		printf(" %c | %c | %c\n ", board[i][0], board[i][1], board[i][2]);
//		//再打印分割行
//		if (i < row - 1)
//		{
//			printf("---|---|---\n");
//		}
//	}
//}以上俩个显示函数,第一个并不能显示初始化时的界面,结果是一片空白
//第二个函数,当用户要求玩三子棋以上,比如五子棋的时候,并不能满足要求
void Display_board(char board[ROW][COL], int row, int  col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			printf(" %c ", board[i][j]);
			if (j < col - 1)
				printf("|");
		}
		printf("\n");
		if (i < 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("您输入的坐标已经被棋子占有,请重新输入坐标,用空格分开:>");
			}
		}
		else
		{
			printf("坐标输入非法,请重新输入:>");
		}

	}
}
//电脑随机下棋
void Computer_Move(char board[ROW][COL], int  row, int  col)
{
	printf("电脑下棋:>\n");
	int x = 0;
	int y = 0;
	while (1)
	{
		x = rand() % row;
		y = rand() % col;
		if (board[x][y] == ' ')
		{
			board[x][y] = '#';
			break;
		}
	}
}
//下面这个函数是为了方便判断是否是平局而设计,因为当平局时,棋盘上面所有的坐标都已有了元素
static int Is_Full(char board[ROW][COL], int  row, int  col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			if (board[i][j] == ' ')
				return 0;
		}
	}
	return 1;
}
char Judge_Win_or_Fail(char board[ROW][COL], int  row, int  col)
{
	int i = 0;
	for (i = 0; i < row; i++)
	{
		if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ')
		{
			return board[i][0];
		}
	}
	for (i = 0; i < col; i++)
	{
		if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
		{
			return board[0][i];
		}
	}
	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))
	{
		return 'Q';
	}
	return 'C';

}
void game()
{
	char ret = 0;
	char board[ROW][COL];
	//初始化并显示初始化界面
	Init_board(board, ROW, COL);
	Display_board(board, ROW, COL);
	//开始下棋
	while (1)
	{
		Player_Move(board, ROW, COL);
		Display_board(board, ROW, COL);
		//判断输赢
		ret = Judge_Win_or_Fail(board, ROW, COL);
		if (ret != 'C')
		{
			break;
		}
		Computer_Move(board, ROW, COL);
		Display_board(board, ROW, COL);
		ret = Judge_Win_or_Fail(board, ROW, COL);
		if (ret != 'C')
		{
			break;
		}
	}
	if (ret == '*')
	{
		printf("玩家赢\n");
	}
	else if (ret == '#')
	{
		printf("电脑赢\n");
	}
	else
	{
		printf("平局\n");
	}

}
int main()
{
	int input = 0;
	srand((unsigned int)time(NULL));//设置随机种子生成器,为后面的电脑随机下棋做准备
	do
	{
		menu();
		printf("请选择:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("您已退出游戏\n");
			break;
		default:
			printf("选择错误,请重新选择:>");
			break;
		}
	} while (input);
	return 0;
}

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