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);
        }
        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);
        }
예제 #3
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);
                }
            }
        }