1. 程式人生 > >三子棋遊戲練習!

三子棋遊戲練習!

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include<Windows.h>
#define ROW 3
#define COL 3
int mean()
{
	printf("*******************************************************************\n");
	printf("***************************1、開始遊戲*****************************\n");
	printf("*******************************************************************\n");
	printf("***************************2、結束遊戲*****************************\n");
	printf("*******************************************************************\n");
	printf("請輸入您的選擇:");
	int choice;
	scanf("%d", &choice);
	return choice;
}
char board[ROW][COL];
void init_board(char board[ROW][COL],int row,int col)
{
	for (row = 0; row < ROW; row++)
	{
		for (col = 0; col < COL; col++)
		{
			board[row][col] = ' ';
		}
	}
}
void computer_move(char board[ROW][COL], int row, int col)
{
	printf("電腦落子!\n");
	while (1)
	{
		int row = rand() % ROW;
		int col = rand() % COL;
		if (board[row][col] != ' ')
		{
			continue;
		}
		board[row][col] = 'o';
		break;
	}
}
void show_board(char board[ROW][COL],int row,int col)
{
	for (row=0; row < ROW; row++)
	{
		printf("|  %c  |  %c  |  %c  |\n", board[row][0], board[row][1],board[row][2]);
		if (row != ROW - 1)
			printf(" ---- | --- | ---- \n");
	}
}
int isfull(char board[ROW][COL], int row, int col)
{
	for (row = 0; row < ROW; row++)
	{
		for (col = 0; col < COL; col++)
		{
			if (board[row][col] == ' ')
			{
				return 0;
			}
		}
	}
	return 1;
}
char check_winner(char board[ROW][COL], int row, int col)
{
	for (row = 0; row < ROW; row++)
	{
		if (board[row][0] == board[row][1]
			&& board[row][0] == board[row][2]
			&& board[row][0] != ' ')
		{
			return board[row][0];
		}
	}
	for (col = 0; col < COL; col++)
	{
		if (board[0][col] == board[1][col]
			&& board[0][col] == board[2][col]
			&& board[0][col] != ' ')
		{
			return board[0][col];
		}
	}
	if (board[0][0] == board[1][1]
		&& board[0][0] == board[2][2]
		&& board[0][0] != ' ')
	{
		return board[0][0];
	}
	if (board[2][0] == board[1][1]
		&& board[2][0] == board[0][2]
		&& board[2][0] != ' ')
	{
		return board[2][0];
	}
	else if (isfull(board,ROW,COL))
	{
		return 'q';
	}
	return ' ';
}
void player_move(char board[ROW][COL], int row, int col)
{
	printf("玩家落子!\n");
	while(1)
	{
		printf("請輸入一組座標(row,col):");
		row = 0;
		row = 0;
		scanf("%d %d", &row, &col);
		if (row < 0 || row >= ROW || col < 0 || col >= COL)
		{
			printf("輸入有誤,請重新輸入!\n");
			continue;
		}
		if (board[row][col] != ' ')
		{
			printf("當前位置有子,請重新輸入!\n");
			continue;
		}
		board[row][col] = 'x';
		break;
	}
}

void game(char board[ROW][COL], int row, int col)
{
	srand((unsigned)time(0));
	init_board(board, ROW, COL);
	char winner = '\0';
	while (1)
	{
		show_board(board, ROW, COL);
		player_move(board, ROW, COL);	
	    winner = check_winner(board, ROW, COL);
		if (winner != ' ')
		{
			break;
		}
		computer_move(board,ROW,COL);
		winner=check_winner(board, ROW, COL);
		if (winner != ' ')
		{
			break;
		}
		show_board(board, ROW, COL);
		system("cls");
	}
	if (winner == 'x')
	{
		printf("你贏了!玩家勝利!\n");
	}
	else if (winner == 'o')
	{
	printf("你輸了! 電腦勝利! \n");
	}
	else
	{
		printf("平局!\n");
	}

	show_board(board,ROW,COL);
}
int main()
{
	while (1)
	{
		int choice = mean();
		if (choice == 1)
		{
			game(board,ROW,COL);
		}
		else if (choice == 2)
		{
			printf("遊戲結束\n");
			break;
		}
		else
		{
			printf("輸入錯誤,請重新輸入!\n");
		}
	}
	system("pause");
}