Beispiel #1
0
        static void ExecuteHumanTurn(Player activePlayer, ref Gameboard board, string prompt)
        {
            bool isMoveCompleted = false;

            do
            {
                int move = -1;
                do

                {
                    move = ParseInput(prompt);
                } while (move < 0);



                int x_coord = GetXCoord(move);
                int y_coord = GetYCoord(move);

                if (move < 1 || move > 9)
                {
                    Console.WriteLine($"The number {move} is not a valid entry. Please select a number 1-9 or type '/help' to see a helpful diagram.");
                }
                else if (board.tile[x_coord, y_coord].content == Symbol.X || board.tile[x_coord, y_coord].content == Symbol.O)
                {
                    Console.WriteLine($"Position {move.ToString()} is occupied. Please select another tile. Type '/help' to see a helpful diagram.");
                }
                else if (board.tile[x_coord, y_coord].content == Symbol.empty)
                {
                    isMoveCompleted = true;
                    board.tile[x_coord, y_coord].content = activePlayer.PlayersSymbol;
                    Console.WriteLine($"You Mark an {activePlayer.PlayersSymbol.ToString()} down on position {move.ToString()}.");
                    activePlayer.SetLastPlay(x_coord, y_coord);
                }
            } while (isMoveCompleted == false);
        }
Beispiel #2
0
        public static string GetPlayerInput(string prompt)

        {
            Console.WriteLine(prompt);
            string input = "";

            do

            {
                input = Console.ReadLine();

                if (input == "/help")

                {
                    Gameboard.PrintHelpKey();
                    Thread.Sleep(500);
                    Console.WriteLine("\nNow, " + prompt);
                }

                else

                {
                    return(input);
                }
            } while (input == "" || input == "/help");

            ThrowError(3, "Escaped GetPlayerInput Method Loop");
            return(input);
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            var  player1      = new Player();
            var  player2      = new Player();
            var  activePlayer = new Player();
            var  board        = new Gameboard();
            bool GameOver;
            bool playAgain;
            int  turnCount = 0;

            do
            {
                GameOver     = false;
                turnCount    = 0;
                board        = Setup(ref board, ref player1, ref player2); // Create a gameboard!
                activePlayer = SetActivePlayer(ref activePlayer, player1, player2);

                do
                {
                    Console.Clear();
                    board.Print();
                    TakeTurn(activePlayer, ref board);
                    GameOver = CheckForVictory(ref activePlayer, board);
                    SwapActivePlayer(ref activePlayer, ref player1, ref player2);
                    ++turnCount;
                } while (!GameOver && turnCount < 9);

                Console.WriteLine("\n*** Final Result ***");

                board.Print();

                OutputGameResult(player1, player2, turnCount);
                playAgain = PlayAgainOffer();
            } while (playAgain);
        }
Beispiel #4
0
 static void TakeTurn(Player activePlayer, ref Gameboard board)
 {
     // Player's Turn
     if (activePlayer.PlayerType == Player.Type.human)
     {
         string prompt = $"Your move {activePlayer.name}!";
         ExecuteHumanTurn(activePlayer, ref board, prompt);
     }
     else if (activePlayer.PlayerType == Player.Type.AI)
     {
         ExecuteAITurn(activePlayer, ref board);
     }
 }
Beispiel #5
0
        static int CountEmptyTiles(Gameboard board)
        {
            int count = 0;

            for (int row = 0; row < Gameboard.BOARD_LENGTH; row++)
            {
                for (int col = 0; col < Gameboard.BOARD_WIDTH; col++)
                {
                    count = board.tile[col, row].content == Symbol.empty ? ++count : count;
                }
            }

            return(count);
        }
Beispiel #6
0
        static bool CheckForVerticalVictory(Player activePlayer, Gameboard board)
        {
            int activeTile = activePlayer.lastPlay;
            int x_coord    = GetXCoord(activeTile);
            int y_coord    = GetYCoord(activeTile);

            if (y_coord == 0)
            {
                if (board.tile[x_coord, y_coord + 1].content == activePlayer.PlayersSymbol && board.tile[x_coord, y_coord + 2].content == activePlayer.PlayersSymbol)
                {
                    return(true);
                }

                else
                {
                    return(false);
                }
            }

            else if (y_coord == 1)
            {
                if (board.tile[x_coord, y_coord + 1].content == activePlayer.PlayersSymbol && board.tile[x_coord, y_coord - 1].content == activePlayer.PlayersSymbol)
                {
                    return(true);
                }

                else
                {
                    return(false);
                }
            }

            else if (y_coord == 2)
            {
                if (board.tile[x_coord, y_coord - 1].content == activePlayer.PlayersSymbol && board.tile[x_coord, y_coord - 2].content == activePlayer.PlayersSymbol)
                {
                    return(true);
                }

                else
                {
                    return(false);
                }
            }

            else
            {
                return(false);
            }
        }
Beispiel #7
0
        static bool CheckForHorizontalVictory(Player activePlayer, Gameboard board)
        {
            int activeTile = activePlayer.lastPlay;
            int x_coord    = GetXCoord(activeTile);
            int y_coord    = GetYCoord(activeTile);

            if (x_coord == 0) // Left Column
            {
                if (board.tile[x_coord + 1, y_coord].content == activePlayer.PlayersSymbol && board.tile[x_coord + 2, y_coord].content == activePlayer.PlayersSymbol)
                {
                    return(true);
                }

                else
                {
                    return(false);
                }
            }

            else if (x_coord == 1) // Middle Column
            {
                if (board.tile[x_coord - 1, y_coord].content == activePlayer.PlayersSymbol && board.tile[x_coord + 1, y_coord].content == activePlayer.PlayersSymbol)
                {
                    return(true);
                }

                else
                {
                    return(false);
                }
            }

            else if (x_coord == 2) // Right Column
            {
                if (board.tile[x_coord - 1, y_coord].content == activePlayer.PlayersSymbol && board.tile[x_coord - 2, y_coord].content == activePlayer.PlayersSymbol)
                {
                    return(true);
                }

                else
                {
                    return(false);
                }
            }

            else
            {
                return(false);
            }
        }
Beispiel #8
0
        static void ExecuteAITurn(Player activePlayer, ref Gameboard board)
        {
            int tile_x;
            int tile_y;

            DisplayAIThinkingMessage(board);

            do
            {
                var randomRnd_x = new Random();
                var randomRnd_y = new Random();
                tile_x = randomRnd_x.Next(3);
                tile_y = randomRnd_y.Next(3);
            } while (board.tile[tile_x, tile_y].content != Symbol.empty);

            board.tile[tile_x, tile_y].content = activePlayer.PlayersSymbol;
            Console.Clear();
            board.Print();
            Console.WriteLine($"The AI opts to take position {GetTileID(tile_x, tile_y)} for its turn.");
            activePlayer.SetLastPlay(tile_x, tile_y);
            Thread.Sleep(2000);
        }
Beispiel #9
0
        static Gameboard Setup(ref Gameboard board, ref Player player1, ref Player player2)
        {
            Console.WriteLine("Welcome to Tic Tac Toe!" + "\n");

            if (player1.score == 0 && player2.score == 0)
            {
                Thread.Sleep(1000);
                Gameboard.PrintHelpKey();
            }

            else
            {
                PrintScoreboard(player1, player2);
                board.WipeBoard();
            }

            player1.UpdatePlayerVictoryStatus(false);
            player2.UpdatePlayerVictoryStatus(false);

            AssignPlayerTypes(ref player1, ref player2);
            AssignPlayerSymbolsAndTurn(ref player1, ref player2);

            return(board);
        }
Beispiel #10
0
        static void DisplayAIThinkingMessage(Gameboard board)
        {
            int emptyTileCount = 0;

            var randomizer      = new Random();
            int randomThinkTime = 25 * randomizer.Next(21);

            emptyTileCount = CountEmptyTiles(board);
            int totalThinkTime = randomThinkTime * emptyTileCount;

            Console.WriteLine($"It is the AI's Turn. \n");
            Thread.Sleep(500);

            if (totalThinkTime < 1200)
            {
                Thread.Sleep(totalThinkTime);
            }
            else
            {
                Thread.Sleep(700);
                Console.WriteLine("The AI is thinking... \n");
                Thread.Sleep(totalThinkTime);
            }
        }
Beispiel #11
0
        static bool CheckForVictory(ref Player activePlayer, Gameboard board)
        {
            bool victoryTestResult = false;

            // IF LAST PLAY WAS ON A "MIDDLE" SQUARE, ONLY HORIZONTAL AND VERTICAL VICTORIES ARE POSSIBLE
            if (activePlayer.lastPlay % 2 == 0)
            {
                victoryTestResult = CheckForHorizontalVictory(activePlayer, board);
                if (!victoryTestResult)
                {
                    victoryTestResult = CheckForVerticalVictory(activePlayer, board);
                }
            }

            //IF LAST PLAY WAS ON A "CORNER" SQUARE, OR IN THE CENTER, ALL THREE VICTORY TYPES ARE POSSIBLE
            else if (activePlayer.lastPlay % 2 == 1)
            {
                victoryTestResult = CheckForHorizontalVictory(activePlayer, board);
                if (!victoryTestResult)
                {
                    victoryTestResult = CheckForVerticalVictory(activePlayer, board);
                    if (!victoryTestResult)
                    {
                        victoryTestResult = CheckForDiagonalVictory(activePlayer, board);
                    }
                }
            }

            if (victoryTestResult)
            {
                activePlayer.UpdatePlayerScore();
                activePlayer.UpdatePlayerVictoryStatus(true);
            }

            return(victoryTestResult);
        }