三子棋-c语言

简易三子棋

#include
#include
#define ROW 3
#define COL 3

//初始化棋盘
void InitGame();
//开始游戏
void StartGame();
//展示棋盘
void ShowChessBoard();
//判空操作
int IsFullChessBoard();
//玩家落子
void PlayerMove();
//电脑落子
void ComputerMove();
//判断结果
char CheckResult();
//宣布结果
char UnveilEnd();
static char board[ROW][COL];//延长变量使用周期

void StartGame()
{
	printf("开始游戏\n");
	InitGame();//1初始化棋盘
	char chess;
	while (1)//2开始游戏
	{
		ShowChessBoard();//3 显示棋盘

		PlayerMove();//4 玩家下棋

		chess = CheckResult();//5 判断结果
		if (chess != 'c')
		{
			ShowChessBoard();
			break;
		}

		ComputerMove();//6 人机下棋

		chess = CheckResult();//7 判断结果
		if (chess != 'c')
		{
			ShowChessBoard();
			break;
		}
			
	}
}

void InitGame()
{
	for (int i = 0; i<ROW; ++i)
	{
		for (int j = 0; j<COL; ++j)
		{
			board[i][j] = ' ';
		}
	}
}
void ShowChessBoard()
{
	printf(" --- --- ---\n");
	for (int i = 0; i<ROW; ++i)
	{
		printf("| %c | %c | %c |\n", 
		board[i][0],board[i][1],board[i][2]);
		if (i != ROW )
			printf(" --- --- ---\n");
	}
}
void PlayerMove()
{
	printf("到你落子了,请输入位置》》》");
	int row, col;//棋盘行列
	while (1)
	{
		scanf("%d %d", &row, &col);
		if (board[row - 1][col - 1] != ' ')
		{
			printf("此处有子,请重新落子\n");
			continue;
		}
		if(board[row - 1][col - 1] = 'x')
		break;
	}
}
void ComputerMove()
{
	int row, col;
	srand(time(0));
	while (1)
	{
		//随机产生位置
		row = rand() % ROW;
		col = rand() % COL;

		if (board[row][col] == ' ')
		{
			board[row][col] = 'o';
			break;
		}
	}
}
int IsFullChessBoard()
{
	for (int i = 0; i<ROW; ++i)
	{
		for (int j = 0; j<COL; ++j)
		{
			if (board[i][j] == ' ')
				return 0;
		}
	}
	return 1;
}
//x:玩家赢
//o:电脑赢
//h:和棋
//c:继续
char CheckResult()
{
	for (int i = 0; i < ROW; i++)
	{
		if (board[i][0] !=' ' && board[i][0] == board[i][1] && board[i][1] == board[i][2])
			return board[i][0];//列判断
		if (board[0][i] !=' ' && board[0][i] == board[1][i] && board[1][i] == board[2][i])
			return board[0][i];//行判断
	}
	//对角线判断
	if (board[1][1] != ' ' && board[0][0] == board[1][1] && board[2][2] == board[1][1])
		return board[1][1];

	if (board[1][1] != ' ' && board[0][2] == board[1][1] && board[2][0] == board[1][1])
		return board[1][1];
	//是否和棋
	if (IsFullChessBoard())
		return 'h';
	//继续下棋
	return 'c';
}
char UnveilEnd()
{
	char end = CheckResult();
	if (end == 'x')
		printf("你胜利了\n");
	if (end == 'o')
		printf("电脑胜利了\n");
	if (end == 'h')
		printf("和棋\n");
	printf("是否再来一次?\n【y】>>是||【其他】>>否\n");
	char cycle=getchar();
	cycle = getchar();
	return cycle;
}int main()
{
	int select = 1;
	while (select)
	{
		printf("--------------------------\n");
		printf("**************************\n");
		printf("--------------------------\n");
		printf("**********三子棋**********\n\n");
		printf("*》》【1】开始游戏》》》》\n\n");
		printf("*》》【0】退出游戏》》》》\n\n");
		printf("**************************\n");
		printf("--------------------------\n");
		printf("请选择> > >");
		scanf("%d", &select);
		if (select == 0)
			break;
		if (select != 1)
		{
			printf("输入有误,请重新输入\n");
			continue;
		}
		char anew = 'y';
		while (select == 1 && anew == 'y')
		{

			StartGame();//进入游戏
			anew = UnveilEnd();//宣布结果
			if (anew == 'y')
				continue;
			else break;
		}
	}
	printf("Game Over\n");
	return 0;
}

利用基础的数组与循环来实现三子棋游戏。

你可能感兴趣的:(程序人生)