Beispiel #1
0
        public static void VictoryMessage(Player winningPlayer, Player losingPlayer, DateTime startingTime)
        {
            Gameboard.PrintGameBoard(true);
            Console.Write("\nCongratulations to ");
            CX.Print(winningPlayer.Name, winningPlayer.Colour, null, true);
            Console.Write("Commiseration to ");
            CX.Print(losingPlayer.Name, losingPlayer.Colour, null, true);

            CX.Print($"\n{winningPlayer.Name}", winningPlayer.Colour);
            Console.Write($" made {winningPlayer.Moves} during the game.\n");

            CX.Print($"\n{losingPlayer.Name}", losingPlayer.Colour);
            Console.Write($" made {losingPlayer.Moves} during the game.\n");

            Console.WriteLine($"A total of {winningPlayer.Moves + losingPlayer.Moves} where made between both players.\n");

            Console.WriteLine($"The game took {DateTime.Now - startingTime}.\n");
            Console.WriteLine("Would you like to play again?");
            Console.WriteLine("1 - Yes\n2 - No\n0 - Show Game Replay");
        }
Beispiel #2
0
        // * THE GAME * ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        private static int PlayGame(Player player1, Player player2)
        {
            // Used to decide who makes the first move. It is only used once in the next statement.
            Random random       = new Random();
            Player activePlayer = random.Next(2) == 0 ? player1 : player2;


            // These are used for declaring a winner and displaying information at the end of the game.
            Player winningPlayer = null;
            Player losingPlayer  = null;


            // Setting up the game.
            Gameboard.Mode = GameEngine.ChooseGameMode(player1, player2); // Get game mode.
            GameEngine.ChooseNumberOfPlayers(player1, player2);           // Declare and name players.
            Gameboard.SetUpBoard();

            // Printing a last message to the user / users playing.
            Console.Clear();
            CX.Print(player1.Name, player1.Colour);
            Console.Write(" vs ");
            CX.Print(player2.Name, player2.Colour);
            Console.WriteLine("!\nPress any key to play...");
            Console.ReadKey();
            Console.Clear();

            // IN-GAME
            DateTime startingTime = DateTime.Now; // This will be used to show how long a game has been played for.

            while (true)
            {
                // Printing to the terminal who is playing and whose move it is.
                Console.Clear();
                CX.Print(player1.Name, player1.Colour);
                Console.Write(" vs ");
                CX.Print(player2.Name, player2.Colour);
                Console.WriteLine($" | \u001b[4mConnect {Gameboard.VictoryNumber} to win!\u001b[0m");
                Console.Write("\nIt is ");
                CX.Print(activePlayer.Name + "'s", activePlayer.Colour);
                Console.WriteLine(" turn...\n");

                // Printing the game board with player instructions.
                Gameboard.PrintGameBoard();

                // PLAYER INPUT
                if (!activePlayer.IsRobot)
                {
                    byte playerChoice = CX.GetKey(limit: Gameboard.BoardWidth);
                    // Access pause menu.
                    if (playerChoice == 0) // Entering '0' accesses the pause menu.
                    {
                        byte pauseMenuValue = PauseMenu(player1, player2);
                        if (pauseMenuValue == 0)
                        {
                            return(0);                     // Quit the game.
                        }
                        else if (pauseMenuValue == 6)
                        {
                            Gameboard.ResetBoard(true, player1, player2);
                        }
                        continue;
                    }
                    else if (playerChoice == 10 && Gameboard.History.Count > 0)
                    {
                        Gameboard.UndoMove(); // Undo Move... 'CX.GetKey()' Returns 10 when you enter [Z].
                        if (player2.IsRobot)
                        {
                            continue;
                        }
                    }
                    else if (playerChoice < 10)
                    {
                        if (!activePlayer.MakeMove(playerChoice))
                        {
                            continue;                                                               // Place a counter on the board. If invalid (false) try again.
                        }
                    }
                }
                else
                {
                    activePlayer.ComputerMove(activePlayer == player1 ? player2 : player1);
                }

                // It makes no sense to check for victory if the player hasn't made enough moves to win.
                if (activePlayer.Moves >= Gameboard.VictoryNumber)
                {
                    winningPlayer = Gameboard.CheckVictory();
                    losingPlayer  = winningPlayer != null ? activePlayer == player1 ? player2 : player1 : null;
                }

                // This is the end game screen. This code only runs when a winner has been declared.
                if (winningPlayer != null)
                {
                    Console.Clear();
                    while (true)
                    {
                        GameEngine.VictoryMessage(winningPlayer, losingPlayer, startingTime);

                        byte answer = CX.GetKey(limit: 2);
                        if (answer == 0)
                        {
                            Gameboard.MatchReplay();
                        }
                        else if (answer == 1 || answer == 2)
                        {
                            Console.Clear();
                            Gameboard.ResetBoard(true, player1, player2);
                            Gameboard.History.RemoveRange(0, Gameboard.History.Count);
                            Console.WriteLine(answer == 1 ? "Press any key to continue..." : "Thank you for playing\nPress any key to continue...");
                            Console.ReadKey();
                            Console.Clear();
                            winningPlayer = null;
                            losingPlayer  = null;
                            if (answer == 1)
                            {
                                break;
                            }
                            if (answer == 2)
                            {
                                return(0);
                            }
                        }
                        Console.Clear();
                    }
                }
                activePlayer = activePlayer == player1 ? player2 : player1;
                if (player1.IsRobot && player2.IsRobot)
                {
                    Thread.Sleep(500);
                }
            }
        }