コード例 #1
0
        public void Test_FindOrigin_CanMove()
        {
            // Init Game
            CustomGame         customGame = InitGame(null);
            List <Coordinates> list       = new List <Coordinates>();

            // Penguin in 0;0
            int x = 0;
            int y = 0;
            // Set cell with penguin
            Cell cell = (Cell)customGame.Board.Board[x, y];

            cell.FishCount      = 1;
            cell.CellType       = CellType.FishWithPenguin;
            cell.CurrentPenguin = new Penguin(customGame.CurrentPlayer);
            list.Add(new Coordinates(x, y));

            // Launch function
            AIEasy      aiEasy      = new AIEasy(customGame.Board, new AppRandom(), customGame.CurrentPlayer);
            Coordinates coordinates = aiEasy.FindOrigin(list);

            // Tests
            Assert.IsTrue(customGame.Board.Board[0, 0].CellType == CellType.FishWithPenguin);
            Assert.IsTrue(customGame.Board.Board[0, 0].FishCount == 1);
            Assert.IsTrue(customGame.Board.Board[0, 0].CurrentPenguin.Player == customGame.CurrentPlayer);
            Assert.IsTrue(customGame.CurrentPlayer.Points == 0);
            Assert.IsTrue(coordinates.X == 0 && coordinates.Y == 0);
        }
コード例 #2
0
        public void Test_PlacePenguin_NotFirstTry_FishWithPenguin()
        {
            // Mock 0;0 then 1;1
            Mock <IRandom> randomMock = new Mock <IRandom>();

            randomMock.SetupSequence(e => e.Next(0, 8))
            .Returns(0).Returns(0)
            .Returns(1).Returns(1);

            // Init game
            CustomGame customGame = InitGame(null);

            // Position of first cell
            int x = 0;
            int y = 0;
            // Set first cell
            Cell cell1 = (Cell)customGame.Board.Board[x, y];

            cell1.CellType = CellType.FishWithPenguin;

            // Position of second cell
            x = 1;
            y = 1;
            // Set second cell
            Cell cell2 = (Cell)customGame.Board.Board[x, y];

            cell2.FishCount = 1;

            // Launch function with Mock
            AIEasy      aiEasy      = new AIEasy(customGame.Board, randomMock.Object, customGame.CurrentPlayer);
            Coordinates coordinates = aiEasy.PlacePenguin();

            // Test if the penguin is place on the second cell
            Assert.IsTrue(coordinates.X == 1 && coordinates.Y == 1);
        }
コード例 #3
0
        public void Test_PlacePenguin_FirstTry()
        {
            // Mock 0;0
            Mock <IRandom> randomMock = new Mock <IRandom>();

            randomMock.SetupSequence(e => e.Next(0, 8)).Returns(0);

            // Init game
            CustomGame customGame = InitGame(null);

            // Position of cell to place the penguin
            int x = 0;
            int y = 0;
            // Set cell
            Cell cell = (Cell)customGame.Board.Board[x, y];

            cell.FishCount = 1;

            // Launch function with Mock
            AIEasy      aiEasy      = new AIEasy(customGame.Board, randomMock.Object, customGame.CurrentPlayer);
            Coordinates coordinates = aiEasy.PlacePenguin();

            // Test if the penguin is place on the cell
            Assert.IsTrue(coordinates.X == 0 && coordinates.Y == 0);
        }
コード例 #4
0
        /// <summary>
        /// Place penguin for AI
        /// </summary>
        public void PlacePenguin()
        {
            // If AI is easy
            if (CurrentPlayer.PlayerType == PlayerType.AIEasy)
            {
                // Call AIEasy
                AIEasy aiEasy = new AIEasy(Board, random, CurrentPlayer);

                // Get coordinates
                Coordinates coordinates = aiEasy.PlacePenguin();

                // Apply changes
                ChangeStatePlace(coordinates.X, coordinates.Y);

                // Log penguin placement
                ILog log = LogManager.GetLogger(GetType().ToString());
                log.Info($"{CurrentPlayer.Name} placed a penguin on cell ({coordinates.X}, {coordinates.Y})");
            }
            // If AI is medium
            else if (CurrentPlayer.PlayerType == PlayerType.AIMedium)
            {
                // Call AIMedium
                AIMedium aiMedium = new AIMedium(Board, random, CurrentPlayer);

                // Get coordinates
                Coordinates coordinates = aiMedium.PlacePenguin();

                // Apply changes
                ChangeStatePlace(coordinates.X, coordinates.Y);

                // Log penguin placement
                ILog log = LogManager.GetLogger(GetType().ToString());
                log.Info($"{CurrentPlayer.Name} placed a penguin on cell ({coordinates.X}, {coordinates.Y})");
            }
            // If AI is hard
            else if (CurrentPlayer.PlayerType == PlayerType.AIHard)
            {
                // Call AIHard
                AIHard aiHard = new AIHard(Board, random, CurrentPlayer);

                // Get coordinates
                Coordinates coordinates = aiHard.PlacePenguin();

                // Apply changes
                ChangeStatePlace(coordinates.X, coordinates.Y);

                // Log penguin placement
                ILog log = LogManager.GetLogger(GetType().ToString());
                log.Info($"{CurrentPlayer.Name} placed a penguin on cell ({coordinates.X}, {coordinates.Y})");
            }
        }
コード例 #5
0
        public void Test_FindOrigin_ApplyChange()
        {
            // Init Game
            CustomGame         customGame = InitGame(null);
            List <Coordinates> list       = new List <Coordinates>();

            // Penguin in 0;0
            int x = 0;
            int y = 0;
            // Set cell with penguin
            Cell cell = (Cell)customGame.Board.Board[x, y];

            cell.FishCount      = 1;
            cell.CellType       = CellType.FishWithPenguin;
            cell.CurrentPenguin = new Penguin(customGame.CurrentPlayer);
            list.Add(new Coordinates(x, y));

            // Cell water in 1;0
            x = 1;
            y = 0;
            // Set cell water
            cell          = (Cell)customGame.Board.Board[x, y];
            cell.CellType = CellType.Water;

            // Cell water in 0;1
            x = 0;
            y = 1;
            // Set cell water
            cell          = (Cell)customGame.Board.Board[x, y];
            cell.CellType = CellType.Water;

            // Launch function
            AIEasy      aiEasy      = new AIEasy(customGame.Board, new AppRandom(), customGame.CurrentPlayer);
            Coordinates coordinates = aiEasy.FindOrigin(list);

            // Tests
            Assert.IsTrue(coordinates.X == -1 && coordinates.Y == -1);
        }
コード例 #6
0
        public void Test_FindDestination()
        {
            //Init Game
            CustomGame customGame = InitGame(null);

            // Penguin in 0;0
            int x = 0;
            int y = 0;
            // Set cell with penguin
            Cell cell = (Cell)customGame.Board.Board[x, y];

            cell.FishCount      = 1;
            cell.CellType       = CellType.FishWithPenguin;
            cell.CurrentPenguin = new Penguin(customGame.CurrentPlayer);

            // Cell water in 0;1
            x = 0;
            y = 1;
            // Set cell water
            cell          = (Cell)customGame.Board.Board[x, y];
            cell.CellType = CellType.Water;

            // Cell water in 2;0
            x = 2;
            y = 0;
            // Set cell water
            cell          = (Cell)customGame.Board.Board[x, y];
            cell.CellType = CellType.Water;

            // Launch function
            AIEasy      aiEasy      = new AIEasy(customGame.Board, new AppRandom(), customGame.CurrentPlayer);
            Coordinates coordinates = aiEasy.FindDestination(new Coordinates(0, 0));

            // Test
            Assert.IsTrue(coordinates.X == 1 && coordinates.Y == 0);
        }
コード例 #7
0
        /// <summary>
        /// Move penguin for AI
        /// </summary>
        public void Move()
        {
            // If AI is easy
            if (CurrentPlayer.PlayerType == PlayerType.AIEasy)
            {
                // Call an AIEasy
                AIEasy aiEasy = new AIEasy(Board, random, CurrentPlayer);

                // Get positions of penguins of the current player
                PossibilitiesOfOrigin();

                // Get the penguin to move
                Coordinates origin = aiEasy.FindOrigin(PossibilitiesOrigin);

                // Get the destination of the penguin
                Coordinates test = new Coordinates(-1, -1);
                if (origin.X != test.X)
                {
                    Coordinates destination = aiEasy.FindDestination(origin);

                    // Log penguin movement
                    ILog log = LogManager.GetLogger(GetType().ToString());
                    log.Info($"{CurrentPlayer.Name} moved a penguin to cell ({destination.X}, {destination.Y}) and gained {Board.Board[origin.X, origin.Y].FishCount}.");

                    // Apply changes
                    ChangeStateMove(Board.Board[origin.X, origin.Y], Board.Board[destination.X, destination.Y]);

                    // Remove Penguins if they can't move
                    RemovePenguin();

                    // Close the game if the board contain 0 penguin
                    EndGame();

                    AffectedCurrentPlayer(ChangeType.Move);
                }
                else
                {
                    RemovePenguin();
                    EndGame();
                    AffectedCurrentPlayer(ChangeType.Move);
                }
            }
            // If AI is medium
            else if (CurrentPlayer.PlayerType == PlayerType.AIMedium)
            {
                // Call an AIMedium
                AIMedium aiMedium = new AIMedium(Board, random, CurrentPlayer);

                // Get positions of penguins of the current player
                PossibilitiesOfOrigin();
                var coordinates = aiMedium.FindOriginDestination();

                // Get the penguin to move
                Coordinates origin = coordinates["origin"];
                try
                {
                    Coordinates destination = coordinates["destination"];

                    // Log penguin movement
                    ILog log = LogManager.GetLogger(GetType().ToString());
                    log.Info($"{CurrentPlayer.Name} moved a penguin to cell ({destination.X}, {destination.Y}) and gained {Board.Board[origin.X, origin.Y].FishCount}.");

                    // Apply changes
                    ChangeStateMove(Board.Board[origin.X, origin.Y], Board.Board[destination.X, destination.Y]);

                    RemovePenguin();
                    EndGame();
                    AffectedCurrentPlayer(ChangeType.Move);
                }
                catch (Exception e)
                {
                    // Log penguin movement
                    ILog log = LogManager.GetLogger(GetType().ToString());
                    log.Warn($"Your penguins can't move : '{e}'");

                    RemovePenguin();
                    EndGame();
                    AffectedCurrentPlayer(ChangeType.Move);
                }
            }
            // If AI is hard
            else if (CurrentPlayer.PlayerType == PlayerType.AIHard)
            {
                // Call an AIHard
                AIHard aiHard = new AIHard(Board, random, CurrentPlayer);

                // Get positions of penguins of the current player
                PossibilitiesOfOrigin();
                var coordinates = aiHard.FindOriginDestination();

                // Get the penguin to move
                Coordinates origin = coordinates["origin"];
                try
                {
                    Coordinates destination = coordinates["destination"];

                    // Log penguin movement
                    ILog log = LogManager.GetLogger(GetType().ToString());
                    log.Info($"{CurrentPlayer.Name} moved a penguin to cell ({destination.X}, {destination.Y}) and gained {Board.Board[origin.X, origin.Y].FishCount}.");

                    // Apply changes
                    ChangeStateMove(Board.Board[origin.X, origin.Y], Board.Board[destination.X, destination.Y]);

                    RemovePenguin();

                    EndGame();

                    AffectedCurrentPlayer(ChangeType.Move);
                }
                catch (Exception e)
                {
                    // Log penguin movement
                    ILog log = LogManager.GetLogger(GetType().ToString());
                    log.Warn($"Your penguins can't move : '{e}'");

                    RemovePenguin();
                    EndGame();
                    AffectedCurrentPlayer(ChangeType.Move);
                }
            }
        }