1. 程式人生 > >簡易版三子棋小遊戲:

簡易版三子棋小遊戲:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int Menu(int choice)
{
	printf("==============================\n");
	printf("1    開始遊戲\n");
	printf("0    退出遊戲\n");
	printf("==============================\n");
	printf("請輸入您的選擇:\n");
	scanf("%d", &choice);
	return choice;
}
#define ROW 3
#define COL 3
char g_chess_board[ROW][COL];
void Init()
{
	for (int row = 0; row < ROW; ++row)
	{
		for (int col = 0; col < COL; ++col)
		{
			g_chess_board[row][col] = ' ';
		}
	}
}
void Print()
{
	for (int row = 0; row < ROW; ++row)
	{
		printf("| %c | %c | %c |\n", g_chess_board[row][0],g_chess_board[row][1],g_chess_board[row][2]);
		printf("------------\n");
	}
	printf("\n");
		
}
void PlayerMove()
{
	//玩家落子,檢查玩家落子是否合理,如果不合理,重新輸入,如果合理,檢查當前位置是否已經有子,
	//如果有子,請重新輸入,如果沒有子,就將該位置改為x
	int row = 0;
	int col = 0;
	while (1)
	{
		printf("請玩家落子:(row col)\n");
		scanf("%d %d", &row, &col);
		if (row < 0 || row >= ROW || col < 0 || col >= COL)
		{
			printf("您的輸入不合理,請重新輸入:\n");
			continue;
		}
		else if (g_chess_board[row][col] != ' ')
		{
			printf("當前位置已經有子,請重新輸入\n");
			continue;
		}
		else
		{
			g_chess_board[row][col] = 'x';
			break;
		}
	}
}
void ComputerMove()
{
	printf("請電腦落子:\n");
	int row = 0;
	int col = 0;
	while (1)
	{
		row = rand() % ROW;
		col = rand() % COL;
		if (g_chess_board[row][col] == ' ')
		{
			g_chess_board[row][col] = 'o';
			break;
		}
	}
}
int Isfull()
{
	for (int row = 0; row < ROW; ++row)
	{
		for (int col = 0; col < COL; ++col)
		{
			if (g_chess_board[row][col] == ' ')
			{
				return 0;
			}
		}
	}
	return 1;
}
char CheckWinner()
{
	//檢查行是否一樣
	//檢查列是否一樣
	//檢查對角線
	//檢查是否下滿
	for (int col = 0; col < COL; ++col)
	{
		if (g_chess_board[0][col] == g_chess_board[1][col]
			&& g_chess_board[0][col] == g_chess_board[2][col]
			&& g_chess_board[0][col] != ' ')
		{
			return g_chess_board[0][col];
		}
	}
	for (int row = 0; row < ROW; ++row)
	{
		if (g_chess_board[row][0] == g_chess_board[row][1] &&
			g_chess_board[row][0] == g_chess_board[row][2] &&
			g_chess_board[row][0] != ' ')
		{
			return g_chess_board[row][0];
		}
	}
	if (g_chess_board[0][0] == g_chess_board[1][1] &&
		g_chess_board[0][0] == g_chess_board[2][2] &&
		g_chess_board[0][0] != ' ')
	{
		return g_chess_board[0][0];
	}
	if (g_chess_board[0][2] == g_chess_board[1][1] &&
		g_chess_board[0][2] == g_chess_board[2][0] &&
		g_chess_board[0][2] != ' ')
	{
		return g_chess_board[0][2];
	}
	if (Isfull())
	{
		return 'p';
	}
	return ' ';
}
void Game()
{
	char winner = ' ';
	Init();//初始化陣列,用空格填充,
	Print();
	while (1)
	{
		PlayerMove();//玩家落子,
		Print();
		winner = CheckWinner();
		if (winner != ' ')
		{
			break;
		}
		ComputerMove();
		Print();
		winner = CheckWinner();
		if (winner != ' ')
		{
			break;
		}
	}
	if (winner == 'x')
	{
		printf("玩家勝利\n");
	}
	else if (winner == 'o')
	{
		printf("電腦獲勝\n");
	}
	else if (winner == 'p')
	{
		printf("平局\n");
	}

}
int main()
{
	srand((unsigned int)time(0));
	int choice = 0;
	while (1)
	{
		choice = Menu(choice);
		if (choice == 1)
		{
			Game();
		}
		else if (choice == 0)
		{
			printf("拜拜\n");
			break;
		}
		else
		{
			printf("您的輸入有誤,請重新輸入:\n");
			continue;
		}
	}
	system("pause");
	return 0;
}