Exemplo n.º 1
0
        public OtheloGame(string i_PlayerOneName, string i_PlayerTwoName, int i_SizeOfBoard, eNumGameType i_GameType)
        {
            m_Players    = new Player[2];
            m_Players[0] = new Player(i_PlayerOneName, eNumSquare.BlackCell, 0);
            m_Players[1] = new Player(i_PlayerTwoName, eNumSquare.WhiteCell, 0);

            m_OtheloBoard = new OtheloBoard(i_SizeOfBoard);

            initilizePossibleMovesForBothPlayers();

            m_PlayerTurn = 0;
            k_Type       = i_GameType;
        }
Exemplo n.º 2
0
        private void runGame(string i_FirstName, string i_SecondName, int i_BoardSize, eNumGameType i_GameType)
        {
            OtheloGame game = new OtheloGame(i_FirstName, i_SecondName, i_BoardSize, i_GameType);

            printIntroduction(i_FirstName, i_SecondName);
            string selectedMove = null;

            while (game.IsStillPlayable())
            {
                Ex02.ConsoleUtils.Screen.Clear();
                printGameBoard(game);
                if (!game.IsPlayerHasMove())
                {
                    game.SwitchTurn();
                    Console.WriteLine("There are no possible legal moves, The turn pass to : {0}!", game.PlayerNameTurn);
                    System.Threading.Thread.Sleep(4500);
                }
                else
                {
                    if (i_GameType == eNumGameType.PlayerVSPlayer || game.Turn == 0)
                    {
                        selectedMove = chooseMove(game.PlayerNameTurn);
                        if (selectedMove != "Q")
                        {
                            if (isValidString(selectedMove))
                            {
                                if (game.IsPlayerMoveExceedBoard(selectedMove))
                                {
                                    Console.WriteLine("Your input move is exceed the board size!");
                                    System.Threading.Thread.Sleep(4000);
                                }
                                else
                                {
                                    Coords newCoord = new Coords(selectedMove[1] - '1', selectedMove[0] - 'A');
                                    if (!game.IsLegalMoveTurn(newCoord))
                                    {
                                        Console.WriteLine("Your input move is illigal! {0}You must choose a move which blocks an opponent's  sequence", Environment.NewLine);
                                        System.Threading.Thread.Sleep(5000);
                                    }
                                    else
                                    {
                                        game.MakeMove(newCoord);
                                    }
                                }
                            }
                            else
                            {
                                Console.WriteLine("InValid input!");
                                System.Threading.Thread.Sleep(2000);
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                    else
                    {
                        Console.WriteLine("Comupter playing..");
                        System.Threading.Thread.Sleep(1500);
                        game.MakeCpuMove();
                    }
                }
            }

            if (selectedMove != null)
            {
                Ex02.ConsoleUtils.Screen.Clear();
                printGameBoard(game);
                printTheScoreAndTheWinner(game);
            }
        }
Exemplo n.º 3
0
        public void StartUI()
        {
            string       firstPlayerName = string.Empty, secondPlayerName = string.Empty;
            int          boardSize = 0, selcetedAnswer = 0;
            eNumGameType gameType     = 0;
            bool         isValidInput = false;
            bool         keepPlaying  = true;

            Console.WriteLine("Welcome to Othelo game !{0}Please enter your name:", Environment.NewLine);
            firstPlayerName = Console.ReadLine();

            Console.WriteLine("{0} select the game mode you would like to play:", firstPlayerName);
            while (!isValidInput)
            {
                Console.WriteLine("Press 1 for playing against the computer Or press 2 for playing against opponent:");
                isValidInput = int.TryParse(Console.ReadLine(), out selcetedAnswer);
                if (isValidInput && selcetedAnswer == 1)
                {
                    gameType         = eNumGameType.PlayerVSCpu;
                    secondPlayerName = "CPU";
                    isValidInput     = true;
                }
                else if (isValidInput && selcetedAnswer == 2)
                {
                    gameType = eNumGameType.PlayerVSPlayer;
                    Console.WriteLine("Please enter your oppenent name:");
                    secondPlayerName = Console.ReadLine();
                    isValidInput     = true;
                }
                else
                {
                    Console.WriteLine("Wrong choice!");
                    isValidInput = false;
                }
            }

            isValidInput = false;
            Console.WriteLine("Please select the size of the board for the game:");
            while (!isValidInput)
            {
                Console.WriteLine("Press 6 for 6X6 board size Or press 8 for 8X8 board size:");
                isValidInput = int.TryParse(Console.ReadLine(), out boardSize);
                if (isValidInput && (boardSize == 6 || boardSize == 8))
                {
                    isValidInput = true;
                }
                else
                {
                    Console.WriteLine("Worng choice!");
                    isValidInput = false;
                }
            }

            isValidInput = false;

            while (keepPlaying)
            {
                runGame(firstPlayerName, secondPlayerName, boardSize, gameType);

                while (!isValidInput)
                {
                    Console.WriteLine("Would you like to play again?{0}Press 1 for YES Or 0 to Quit:", Environment.NewLine);
                    isValidInput = int.TryParse(Console.ReadLine(), out selcetedAnswer);

                    if (isValidInput && selcetedAnswer == 0)
                    {
                        keepPlaying  = false;
                        isValidInput = true;
                    }
                    else if (isValidInput && selcetedAnswer == 1)
                    {
                        Console.WriteLine("Loading new game ...");
                        isValidInput = true;
                    }
                    else
                    {
                        Console.WriteLine("Worng choice!");
                        isValidInput = false;
                    }
                }

                isValidInput = false;
            }

            Console.WriteLine("Thank you and GoodBye!");
        }