#include<stdio.h>
#include<stdlib.h>
#include<windows.h>

#define amu 3 
void menu()//菜单界面
{
	printf("***********************************************\n");
	printf("********** 欢 迎 来 到 三 子 棋 游 戏 *********\n");
	printf("***********************************************\n");
	printf("**********  1.开始游戏       0.退出   *********\n");
	printf("***********************************************\n");
	printf("请选择上述选项\n");
	return;
}
void printboard(char board[][amu])//棋盘打印
{
	system("cls");
	printf("    1  |  2  |  3  \n");
	printf("\n1 | %c  | %c   | %c  \n", board[0][0], board[0][1], board[0][2]);
	printf("\n2 | %c  | %c   | %c  \n", board[1][0], board[1][1], board[1][2]);
	printf("\n3 | %c  | %c   | %c  \n", board[2][0], board[2][1], board[2][2]);
}
void playermove(char board[][amu], int coo1, int coo2)//玩家下棋
{
	int x, y;
	printf("请输入下一步棋子坐标\n");
	scanf("%d%d", &x, &y);
	if (x >= 1 && x<= coo1 && y >= 1 && y <= coo2 && board[x-1][y-1] == ' ')
	{
		board[x - 1][y - 1] = 'X';
		return;
	}
	else
	{
		printf("输入的棋子坐标有误\n");
		playermove(board, amu, amu);
	}
}
void Aimove(char board[][amu], int coo1, int coo2)//电脑下棋
{
	int x, y;
	while (1)
	{
		x = rand() % coo1;
		y = rand() % coo2;
		if (board[x][y] == ' ')
		{
			board[x][y] = 'O';
			break;
		}
	}
}

char Judge(char board[][amu])//第一次判断
{
	int i = 0, m = 0, sum = 0;
	for (i=0; i < amu; 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 < amu; 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[0][0] == board[2][2] && board[0][0] != ' ')
	{
		return board[0][0];
	}
	if (board[0][2] == board[1][1] && board[0][2] == board[2][0] && board[0][2] != ' ')
	{
		return board[0][2];
	}
	for (i=0; i < amu; i++)
	{
		for (m = 0; m < amu; m++)
		{
			if (board[i][m] != ' ')
			{
				sum++;
			}
		}
	}
	if (sum == 9)
	{
		return 'S';
	}
	return 'q';
}
int winorlost(char board[][amu],char result)//判断胜负
{
	int input;
	if (result == 'q')
	{
		return 1;
	}
	if (result == 'S')
	{
		printf("游戏结束,平局:>\n1.重新开始游戏  0.退出\n");
		scanf("%d", &input);
		if (input == 1)
		{
			memset(board, ' ', sizeof(board)*amu);
			return 2;
		}
		else
		{
			return 0;
		}
	}
	else
	{
		switch (result)
		{
		case 'X':
			printf("游戏结束,玩家获胜:>\n");
			break;
		case 'O':
			printf("游戏结束,电脑获胜:>\n");
			break;
		}
		printf("\n1.重新开始游戏  0.退出\n");
		scanf("%d", &input);
		if (input == 1)
		{
		    memset(board, ' ', sizeof(board)*amu);
			return 2;
		}
		else
		{
			return 0;
		}
	}

}
void game(int input,char board[][amu],char result,int coo1,int coo2)//总游戏构成
{
	while (input)
	{
		system("cls");
		printboard(board);//打印当前棋盘
		playermove(board, coo1, coo2);//玩家走
		printboard(board);//打印当前棋盘
		result = Judge(board);
		input = winorlost(board,result);
		if (input == 2)
		{
			continue;
		}
		if (input == 0)
		{
			return;
		}
		Aimove(board, coo1, coo2);//电脑走
		printboard(board);//打印当前棋盘
		result = Judge(board);//判断胜负
		input = winorlost(board,result);
		if (input == 2)
		{
			continue;
		}
	}
}
int main()
{
	int input, coo1 = 1, coo2 = 1;
	char result=' ';
	char board[amu][amu];
	memset(board, ' ', sizeof(board));
	menu();
	scanf("%d", &input);
	game(input,board,result,amu,amu);
	system("pause");
	return 0;
}