Esempio n. 1
0
        static void Main(string[] args)
        {
            IBoard board = new Board();
            bool   gameOver = false, playerTurn = true;

            IPlayer   p1       = new Player("Player 1", Chip.O, board);
            IPlayer   p2       = new Computer("Computer", Chip.X, board);
            IComputer computer = p2 as IComputer;

            while (!gameOver)
            {
                RenderBoard(board);

                IPlayer p = playerTurn ? p1 : p2;

                string turnMsg = "Your turn " + p.Name + ":";
                Console.WriteLine(turnMsg);
                Console.Write("Choose available slot from 1 - 9: ");
                int slot = playerTurn ? Convert.ToInt32(Console.ReadLine()) : computer.BestMove();

                while (!p.Play(slot))
                {
                    Console.Write("Please choose another one: ");
                    slot = playerTurn ? Convert.ToInt32(Console.ReadLine()) : computer.BestMove();
                }

                playerTurn = !playerTurn;

                Console.WriteLine();

                bool isDraw = board.IsFull();
                gameOver = isDraw || p1.Win() || p2.Win();

                string announcement = default;

                if (p1.Win())
                {
                    announcement = p1.Name + " wins!";
                }
                else if (p2.Win())
                {
                    announcement = p2.Name + " wins!";
                }
                else if (isDraw)
                {
                    announcement = "Draw!";
                }

                if (gameOver)
                {
                    RenderBoard(board);
                    Console.WriteLine(announcement);
                }
            }
        }
Esempio n. 2
0
        private async void GameViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "IsPlayerTurn")
            {
                if (!IsPlayerTurn)
                {
                    int slot = await Task.Run(() => _computer.BestMove());

                    Play(_player2, slot);
                    Update();
                    IsPlayerTurn = true;
                }
            }
        }