Ejemplo n.º 1
0
        public Move FindMove(Board board, Color color)
        {
            var competition = new Competition(board);
            var moves       = competition.GetAvailableMoves(color);

            return(moves.Any() ? moves.ElementAt(_random.Next(0, moves.Count)) : Move.Up);
        }
Ejemplo n.º 2
0
        public Move FindMove(Board board, Color color)
        {
            var competition    = new Competition(board);
            var availableMoves = competition.GetAvailableMoves(color);

            if (availableMoves.Count == 0)
            {
                return(Move.Up);
            }

            var scoredMoves = new List <ScoredMove>();

            foreach (var move in availableMoves)
            {
                scoredMoves.Add(PlayOut.Play(board, color, move, PlayOutsPerMove));
            }

            var bestMove = new ScoredMove(Move.Up, -1, -1);

            foreach (var scoredMove in scoredMoves)
            {
                if (scoredMove.Won > bestMove.Won)
                {
                    bestMove = scoredMove;
                }
            }

            return(bestMove.Move);
        }
Ejemplo n.º 3
0
        private static Result PlayOnce(Board board, Color color, Move move)
        {
            var competition = new Competition(board);
            var isFirstPass = true;

            while (true)
            {
                var blueAvailableMoves = competition.GetAvailableMoves(Color.Blue);
                var redAvailableMoves  = competition.GetAvailableMoves(Color.Red);

                var blueIsOver = blueAvailableMoves.Count == 0;
                var redIsOver  = redAvailableMoves.Count == 0;

                if (blueIsOver && redIsOver)
                {
                    return(Result.Draw);
                }
                if (blueIsOver)
                {
                    return(Result.RedWon);
                }
                if (redIsOver)
                {
                    return(Result.BlueWon);
                }

                var blueMove = isFirstPass && color == Color.Blue ? move : GetRandom(blueAvailableMoves);
                var redMove  = isFirstPass && color == Color.Red ? move : GetRandom(redAvailableMoves);

                competition.MakeMove(Color.Blue, blueMove);

                var blueAndRedChosenTheSameField = !competition.GetAvailableMoves(Color.Red).Contains(redMove);

                if (blueAndRedChosenTheSameField)
                {
                    return(Result.Draw);
                }

                competition.MakeMove(Color.Red, redMove);

                isFirstPass = false;
            }
        }
Ejemplo n.º 4
0
        public static Result Run(IBot blue, IBot red, Board initialState)
        {
            var competitionBoard = initialState.Clone();
            var competition = new Competition(competitionBoard);

            while (true)
            {
                var blueMove = blue.FindMove(competitionBoard.Clone(), Color.Blue);
                var redMove = red.FindMove(competitionBoard.Clone(), Color.Red);

                var blueCrashed = !competition.GetAvailableMoves(Color.Blue).Contains(blueMove);
                var redCrashed = !competition.GetAvailableMoves(Color.Red).Contains(redMove);

                if (blueCrashed && redCrashed)
                {
                    return Result.Draw;
                }

                if (blueCrashed)
                {
                    return Result.RedWon;
                }

                if (redCrashed)
                {
                    return Result.BlueWon;
                }

                competition.MakeMove(Color.Blue, blueMove);

                if (!competition.GetAvailableMoves(Color.Red).Contains(redMove))
                {
                    return Result.Draw;
                }

                competition.MakeMove(Color.Red, redMove);
            }
        }
Ejemplo n.º 5
0
        public void GetAvailableMoves_ShouldReturnMovesForFieldsThatAreEmpty(
            Color color, bool upIsEmpty, bool downIsEmpty, bool leftIsEmpty, bool rightIsEmpty, Move[] expectedMoves)
        {
            // Arrange
            var board = color == Color.Blue ? GetValidBoard(bluePosition: (5, 10)) : GetValidBoard(redPosition: (5, 10));

            board.SetField(5, 9, upIsEmpty ? Field.Empty : Field.Obstacle);
            board.SetField(5, 11, downIsEmpty ? Field.Empty : Field.Obstacle);
            board.SetField(4, 10, leftIsEmpty ? Field.Empty : Field.Obstacle);
            board.SetField(6, 10, rightIsEmpty ? Field.Empty : Field.Obstacle);

            var competition = new Competition(board);

            // Act
            var availableMoves = competition.GetAvailableMoves(color);

            // Assert
            Assert.That(availableMoves, Is.EquivalentTo(expectedMoves));
        }
Ejemplo n.º 6
0
        private static ScoredMove MiniMax(Competition competition, MiniMaxColor color, int depth)
        {
            var availableMoves = competition.GetAvailableMoves(color.Value);

            if (depth == 0 || availableMoves.Count == 0)
            {
                var score = Evaluate(competition, color);
                return(new ScoredMove(Move.Up, score));
            }

            var scoredMoves = new List <ScoredMove>();

            foreach (var move in availableMoves)
            {
                competition.MakeMove(color.Value, move);
                var scoredMove = MiniMax(competition, color.Opposite(), depth - 1);
                competition.RevertMove(color.Value, move);
                scoredMoves.Add(new ScoredMove(move, scoredMove.Score));
            }

            var bestMove = GetInfinityMove(color.IsMaximizing);

            foreach (var move in scoredMoves)
            {
                if (color.IsMaximizing)
                {
                    if (move.Score > bestMove.Score)
                    {
                        bestMove = move;
                    }
                }
                else
                {
                    if (move.Score < bestMove.Score)
                    {
                        bestMove = move;
                    }
                }
            }

            return(bestMove);
        }
Ejemplo n.º 7
0
        private static int GetScoreForColor(Competition competition, Color color)
        {
            var numberOfAvailableMoves = competition.GetAvailableMoves(color).Count;

            return(numberOfAvailableMoves > 0 ? numberOfAvailableMoves : -1000);
        }