/// <summary>
        /// Main method for having a user take their turn for tic-tac-toe. Polls the user for a position input, rejecting previously selected positions.
        /// </summary>
        /// <param name="board">The game board being interacted with. The values on the gameboard dictate if a selected position is valid to be selected or not.</param>
        public void TakeTurn(Board board)
        {
            IsTurn = true;

            bool isValid = false;

            Console.WriteLine($"{Name} it is your turn");

            while (!isValid)
            {
                Position position = GetPosition(board);

                if (Int32.TryParse(board.GameBoard[position.Row, position.Column], out int _))
                {
                    board.GameBoard[position.Row, position.Column] = Marker;
                    isValid = true;
                }
                else
                {
                    Console.WriteLine("This space is already occupied, please pick an unoccupied space.");
                    board.DisplayBoard();
                    Console.WriteLine($"{Name} it is still your turn");
                }
            }
        }
Example #2
0
 /// <summary>
 /// Require 2 players and a board to start a game.
 /// </summary>
 /// <param name="p1">Player 1</param>
 /// <param name="p2">Player 2</param>
 public Game(Player p1, Player p2)
 {
     PlayerOne = p1;
     PlayerTwo = p2;
     Board     = new Board();
     Board.DisplayBoard();
 }
Example #3
0
        /// <summary>
        /// Activate the Play of the game
        /// </summary>
        /// <returns>Winner</returns>
        public Player Play()
        {
            //TODO: Complete this method and utilize the rest of the class structure to play the game.

            /*
             * Complete this method by constructing the logic for the actual playing of Tic Tac Toe.
             *
             * A few things to get you started:
             * 1. A turn consists of a player picking a position on the board with their designated marker.
             * 2. Display the board after every turn to show the most up to date state of the game
             * 3. Once a Winner is determined, display the board one final time and return a winner
             *
             * Few additional hints:
             *  Be sure to keep track of the number of turns that have been taken to determine if a draw is required
             *  and make sure that the game continues while there are unmarked spots on the board.
             *
             * Use any and all pre-existing methods in this program to help construct the method logic.
             */


            int counter = 0;

            while (CheckForWinner(Board) == false)
            {
                if (counter < 9)
                {
                    Board.DisplayBoard();
                    SwitchPlayer();
                    NextPlayer().TakeTurn(Board);
                    counter++;
                }
                else
                {
                    Console.WriteLine("\n It's a Tie Game");
                    Console.WriteLine("Press 1 to play another game, otherwise press any key to exit.");
                    string userinput = Console.ReadLine();
                    if (userinput == "1")
                    {
                        PlayGame();
                    }
                    return(Winner);
                }
            }
            Board.DisplayBoard();
            Console.WriteLine($"Congrats you won");
            return(Winner);
        }
Example #4
0
        /// <summary>
        /// prompt player to take turn & place marker
        /// </summary>
        /// <param name="board">current game board</param>
        public void TakeTurn(Board board)
        {
            Turn = true;
            Console.WriteLine();
            Console.WriteLine($"{Name}, your turn!");
            Position position = GetPosition(board);

            if (Int32.TryParse(board.GameBoard[position.Row, position.Column], out int _))
            {
                board.GameBoard[position.Row, position.Column] = Marker;
            }
            else
            {
                Console.WriteLine("This position is taken!");
                board.DisplayBoard();
                TakeTurn(board);
            }
        }
Example #5
0
        /// <summary>
        /// Tells the next player it's their turn and calls the GetPosition method to request their next move.
        /// </summary>
        /// <param name="board">The current 3x3 game board</param>
        public void TakeTurn(Board board)
        {
            IsTurn = true;

            Console.WriteLine($"{Name} it is your turn");

            Position position = GetPosition(board);

            if (Int32.TryParse(board.GameBoard[position.Row, position.Column], out int _))
            {
                board.GameBoard[position.Row, position.Column] = Marker;
            }
            else
            {
                board.DisplayBoard();
                Console.WriteLine("This space is already occupied");
                this.TakeTurn(board);
            }
        }
Example #6
0
        /// <summary>
        /// Activate the Play of the game
        /// </summary>
        /// <returns>Winner</returns>
        public Player Play()
        {
            int counter = 0;

            while (!CheckForWinner(Board))
            {
                if (counter < 9)
                {
                    SwitchPlayer();
                    Board.DisplayBoard();
                    counter++;
                    NextPlayer().TakeTurn(Board);
                }
                else
                {
                    Console.WriteLine("Tie Game");
                    return(null);
                }
            }
            Console.WriteLine($"------>{Winner} just won the game");
            Board.DisplayBoard();
            return(Winner);
            //TODO: Complete this method and utilize the rest of the class structure to play the game.

            /*
             * Complete this method by constructing the logic for the actual playing of Tic Tac Toe.
             *
             * A few things to get you started:
             * 1. A turn consists of a player picking a position on the board with their designated marker.
             * 2. Display the board after every turn to show the most up to date state of the game
             * 3. Once a Winner is determined, display the board one final time and return a winner
             *
             * Few additional hints:
             *  Be sure to keep track of the number of turns that have been taken to determine if a draw is required
             *  and make sure that the game continues while there are unmarked spots on the board.
             *
             * Use any and all pre-existing methods in this program to help construct the method logic.
             */
        }