private int MaxMove(Board board)
        {
            var winner = board.GetWinner();

            if (winner != 0 || board.IsFilled())
            {
                if (winner == 0)
                    return 0;
                if (winner == Value)
                    return Board.SIZE;
                return -Board.SIZE;
            }

            var moves = board.GetPossibleMoves();

            // Look for best move
            int bestMove = -(Board.SIZE + 1);

            foreach (Move move in moves)
            {
                var newBoard = board.Copy();
                newBoard.AddMove(move, Value);

                var grade = MinMove(newBoard);
                grade = decreaseOneStep(grade);
                if (grade > bestMove)
                    bestMove = grade;
            }

            return bestMove;
        }
Exemple #2
0
        public Board Copy()
        {
            var copy = new Board();

            for (int x = 0; x < X_SIZE; x++)
                for (int y = 0; y < Y_SIZE; y++)
                {
                    copy._board[x][y] = this._board[x][y];
                }

            return copy;
        }
        public void Test_Player_DiagonalAsc_Row2(IPlayer player, int value)
        {
            var board = new Board();
            board.AddMove(new Move(1, 1), 1);
            board.AddMove(new Move(2, 0), 1);
            board.AddMove(new Move(0, 1), 2);
            board.AddMove(new Move(1, 2), 2);

            player.Board = board;
            player.Value = value;

            var expectedMove = new Move(0, 2);
            for (int i = 0; i < AMOUNT_OF_ASSERTIONS; i++)
            {
                var move = player.Move();
                Assert.AreEqual(expectedMove, move);
                Thread.Sleep(1);
            }
        }
        private int MinMove(Board board)
        {
            var winner = board.GetWinner();

            if (winner != 0 || board.IsFilled())
            {
                if (winner == 0)
                    return 0;
                if (winner == Value)
                    return Board.SIZE;
                return -Board.SIZE;
            }

            var enemyValue = (this.Value == 1) ? 2 : 1;
            var moves = board.GetPossibleMoves();

            int worstMove = Board.SIZE + 1;

            foreach (Move move in moves)
            {
                var newBoard = board.Copy();
                newBoard.AddMove(move, enemyValue);

                var grade = MaxMove(newBoard);
                grade = decreaseOneStep(grade);
                if (grade < worstMove)
                    worstMove = grade;
            }

            return worstMove;
        }