Beispiel #1
0
        /// <summary>
        /// This method simulates the game. It initializes two player objects with their respective signs. A while loop
        /// continues to run, until a player has won the game or the game has been drawn.
        /// </summary>
        public static void Play()
        {
            // initialize the two players and give the first turn to playerX
            Player playerX       = new Player(Field.X);
            Player playerO       = new Player(Field.O);
            Player currentPlayer = playerX;

            int moveCounter = 0; // store the count of total number of moves so far

            while (true)
            {
                // indicate the turn of current player
                GameBoard.PrintBoard();
                Console.WriteLine($"Player: {currentPlayer.Sign}. Enter the field where you want to put your character");

                // mark the board with the user's input
                GameBoard.MarkBoard(currentPlayer, currentPlayer.Turn());
                moveCounter += 1;

                // check for the result and if the game is still in motion change the current player
                if (CheckForWin())
                {
                    GameBoard.PrintBoard();
                    Console.WriteLine($"Player: {currentPlayer.Sign} won!");
                    break;
                }
                else if (CheckDraw(moveCounter))
                {
                    GameBoard.PrintBoard();
                    Console.WriteLine("Draw");
                    break;
                }
                ChangeCurrentPlayer(ref currentPlayer, playerX, playerO);
            }
        }