Esempio n. 1
0
        public override bool PlayBestMove(Game game)
        {
            if (game.Finished())
            {
                return(false);
            }

            Square[] emptySquares = game.EmptySquares();
            int      length       = emptySquares.Length;

            if (length == 0) // no empty squares left; should not occur, but test anyway
            {
                return(false);
            }

            Random random = new Random();

            while (true)
            {
                int    value  = random.Next() % length;
                Square square = emptySquares[value];
                game.Play(square);
                return(true);
            }
            ;
        }
Esempio n. 2
0
        public override bool PlayBestMove(Game game)
        {
            // strategy 0: no possible moves
            if (game.Finished())
            {
                return(false);
            }

            // strategy 1: play a winning move
            // find a line that is winning for the player

            // optimization:
            // only needed when you have played 2 times (and the opponent 1 or 2 times)
            // so only if there are 6 or less empty Squares, you need to check for this.
            if (TryPlayThreeInARowFor(Game.PlayerToContent(game.CurrentPlayer), game))
            {
                return(true);
            }

            // strategy 2: play a blocking move
            // find a line that is winning for the opponent
            if (TryPlayThreeInARowFor(Game.PlayerToContent(game.CurrentOpponent), game))
            {
                return(true);
            }

            // strategy 3: try to move on a priority square
            // we base the priority on the number of lines that each square contains
            Square[] emptySquares = game.EmptySquares();
            int      length       = emptySquares.Length;

            if (length == 0) // no empty squares left; should not occur, but test anyway
            {
                return(false);
            }

            Square prioritySquare = emptySquares[0];
            int    linesForPrioritySquareLength = Board.LinesForSquare(prioritySquare).Length;

            foreach (Square square in emptySquares)
            {
                int linesForSquareLength = Board.LinesForSquare(square).Length;
                if (linesForSquareLength > linesForPrioritySquareLength)
                {
                    prioritySquare = square;
                    linesForPrioritySquareLength = linesForSquareLength;
                }
            }
            game.Play(prioritySquare);
            return(true);

            // strategy 4: random move
            // not needed any more because we can always find a priority square
            //TicTacToePlayer randomPlayer = new RandomTicTacToePlayer();
            //randomPlayer.PlayBestMove(game);
        }
Esempio n. 3
0
        public override bool PlayBestMove(Game game)
        {
            // strategy 0: no possible moves
            if (game.Finished())
                return false;

            // strategy 1: play a winning move
            // find a line that is winning for the player

            // optimization: 
            // only needed when you have played 2 times (and the opponent 1 or 2 times)
            // so only if there are 6 or less empty Squares, you need to check for this.
            if (TryPlayThreeInARowFor(Game.PlayerToContent(game.CurrentPlayer), game))
            {
                return true;
            }

            // strategy 2: play a blocking move
            // find a line that is winning for the opponent
            if (TryPlayThreeInARowFor(Game.PlayerToContent(game.CurrentOpponent), game))
            {
                return true;
            }

            // strategy 3: try to move on a priority square
            // we base the priority on the number of lines that each square contains
            Square[] emptySquares = game.EmptySquares();
            int length = emptySquares.Length;
            if (length == 0) // no empty squares left; should not occur, but test anyway
                return false;

            Square prioritySquare = emptySquares[0];
            int linesForPrioritySquareLength = Board.LinesForSquare(prioritySquare).Length;
            foreach (Square square in emptySquares)
            {
                int linesForSquareLength = Board.LinesForSquare(square).Length;
                if (linesForSquareLength > linesForPrioritySquareLength)
                {
                    prioritySquare = square;
                    linesForPrioritySquareLength = linesForSquareLength;
                }
            }
            game.Play(prioritySquare);
            return true;

            // strategy 4: random move
            // not needed any more because we can always find a priority square
            //TicTacToePlayer randomPlayer = new RandomTicTacToePlayer();
            //randomPlayer.PlayBestMove(game);
        }
 private void computerPlay()
 {
     if (!game.Finished())
     {
         bool computerIsPlayingForAnyPlayer =
             isComputerPlaying(Player.Cross, ComputerPlayerXOnOff)   // computer plays X
             ||
             isComputerPlaying(Player.Nought, ComputerPlayerOOnOff); // computer plays O
         if (computerIsPlayingForAnyPlayer)
         {
             BasePlayer player = CreatePlayer();
             player.PlayBestMove(game);
             gameToView();
             showWinner(game.Winner());
         }
         ;
     }
 }
Esempio n. 5
0
        public override bool PlayBestMove(Game game)
        {
            if (game.Finished())
                return false;

            Square[] emptySquares = game.EmptySquares();
            int length = emptySquares.Length;
            if (length == 0) // no empty squares left; should not occur, but test anyway
                return false;

            Random random = new Random();
            while (true)
            {
                int value = random.Next() % length;
                Square square = emptySquares[value];
                game.Play(square);
                return true;
            };
        }