public void RecordsLastShotCorrectly(int shipSize, ShotResult expectedShotResult)
        {
            var player1 = new Mock <IBattleShipShooter>();
            var player2 = new Mock <IBattleShipShooter>();
            var ships   = new[]
            {
                new Ship('A', 1, 'A', 1),
                new Ship('A', 1, 'A', 2),
            };
            var coordinates = new Coordinates('A', 1);

            player1.Setup(x => x.Shoot()).Returns(coordinates);
            player2.Setup(x => x.Shoot()).Returns(coordinates);
            player1.Setup(x => x.PrepareShipsForNewBattle()).Returns(ships.Where(s => s.Size == shipSize).ToArray);
            player2.Setup(x => x.PrepareShipsForNewBattle()).Returns(ships.Where(s => s.Size == shipSize).ToArray);

            var game = new BattleShipGame(player1.Object, player2.Object);

            game.StarNewGame();
            game.Shoot();

            if (game.Attacker.Shooter == player1.Object)
            {
                player1.Verify(x => x.ReportLastShotResult(coordinates, expectedShotResult));
                player2.Verify(x => x.ReportOponentsLastShotResult(coordinates, expectedShotResult));
            }
            else
            {
                player2.Verify(x => x.ReportLastShotResult(coordinates, expectedShotResult));
                player1.Verify(x => x.ReportOponentsLastShotResult(coordinates, expectedShotResult));
            }
        }
예제 #2
0
        public void InitGame()
        {
            BattleShipGame battleShipGame = null;

            do
            {
                Console.WriteLine(BattleShipGameMenu.GetMainMenu());
                var input = InputHandler.GetMenuInput(MenuType.MAIN);
                Console.WriteLine(Environment.NewLine);
                if (input == ConsoleKey.Enter)
                {
                    battleShipGame = new BattleShipGame();
                    battleShipGame.SetupGame();
                }
                else if (input == ConsoleKey.C)
                {
                    battleShipGame = ProcessMenuChoice(input);
                }
                else if (input == ConsoleKey.D)
                {
                    battleShipGame = ProcessMenuChoice(input);
                }
                else
                {
                    Environment.Exit(0);
                }
            }while (battleShipGame == null);
            bsg = battleShipGame;
            PlayGame();
        }
예제 #3
0
        public void Can_Retry_Generating_Ship_If_Ship_Was_On_Top_Of_Another_Ship()
        {
            var randomGeneratorMock = new Mock <IRandomGenerator>();

            randomGeneratorMock
            .SetupSequence(it => it.GetRandomBoolean())
            .Returns(false).Returns(false);
            randomGeneratorMock
            .SetupSequence(it => it.GetRandomNumber(0, ROWS - BATTLESHIP_SIZE))
            .Returns(1).Returns(2);     // 2nd time we return a 2 since first row contains a ship
            randomGeneratorMock
            .SetupSequence(it => it.GetRandomNumber(0, COLUMNS))
            .Returns(1).Returns(1);

            var grid = GetGrid();
            var ship = new Ship(BATTLESHIP_SIZE, ShipType.BattleShip);

            ship.SetPositions(new Point(1, 1), new Point(1, 5));
            grid.AddShip(ship);
            var battleShipGame = new BattleShipGame(grid, randomGeneratorMock.Object);

            battleShipGame.GenerateRandomShipPositions(new Ship(BATTLESHIP_SIZE, ShipType.BattleShip));

            Assert.True(battleShipGame.Grid.Matrix[2, 1]);
            Assert.True(battleShipGame.Grid.Matrix[3, 1]);
            Assert.True(battleShipGame.Grid.Matrix[4, 1]);
            Assert.True(battleShipGame.Grid.Matrix[5, 1]);
            Assert.True(battleShipGame.Grid.Matrix[6, 1]);
        }
예제 #4
0
        public void Can_Generate_Random_Horizontal_Ship()
        {
            var randomGeneratorMock = new Mock <IRandomGenerator>();

            randomGeneratorMock
            .Setup(it => it.GetRandomBoolean())
            .Returns(true);
            // Setup random row
            randomGeneratorMock
            .Setup(it => it.GetRandomNumber(0, ROWS))
            .Returns(1);
            // Setup column with ROWS - ship size in order to exclude invalid ships
            randomGeneratorMock
            .Setup(it => it.GetRandomNumber(0, COLUMNS - BATTLESHIP_SIZE))
            .Returns(1);

            var battleShipGame = new BattleShipGame(GetGrid(), randomGeneratorMock.Object);

            battleShipGame.GenerateRandomShipPositions(new Ship(BATTLESHIP_SIZE, ShipType.BattleShip));

            Assert.True(battleShipGame.Grid.Matrix[1, 1]);
            Assert.True(battleShipGame.Grid.Matrix[1, 2]);
            Assert.True(battleShipGame.Grid.Matrix[1, 3]);
            Assert.True(battleShipGame.Grid.Matrix[1, 4]);
            Assert.True(battleShipGame.Grid.Matrix[1, 5]);
        }
        public void IniPlayerShips_With_Void_Return_List_Of_Ships()
        {
            BattleShipGame battleShipGame = new BattleShipGame();

            var res = battleShipGame.InitPlayerShips();

            Assert.IsType <List <Ship> >(res);
        }
        public void IniPlayerShips_With_Void_Return_List_Of_5_Ships()
        {
            BattleShipGame battleShipGame = new BattleShipGame();

            var res = battleShipGame.InitPlayerShips();

            Assert.True(res.Count == 5);
        }
예제 #7
0
        public void Can_Initialize_With_Grid()
        {
            var battleShipGame = new BattleShipGame(GetGrid(), new RandomGenerator());

            Assert.NotNull(battleShipGame.Grid);
            Assert.NotNull(battleShipGame.RandomGenerator);
            Assert.NotNull(battleShipGame.Ships);
        }
        public void InitiateNewGame()
        {
            var player1 = new Mock <IBattleShipShooter>();
            var game    = new BattleShipGame(player1.Object, player1.Object);

            game.StarNewGame();

            Assert.IsNotNull(game);
        }
예제 #9
0
        public void Can_Add_Ship()
        {
            var ship           = new Ship(DESTROYER_SIZE, ShipType.Destroyer);
            var battleShipGame = new BattleShipGame(GetGrid(), new Mock <IRandomGenerator>().Object);

            battleShipGame.AddShip(ship);

            Assert.Single(battleShipGame.Ships);
        }
        public void ThrowsErrorWhenShotIsOutOfRange()
        {
            var player1 = new Mock <IBattleShipShooter>();

            player1.Setup(x => x.Shoot()).Returns(() => new Coordinates('W', 3));

            var game = new BattleShipGame(player1.Object, player1.Object);

            game.StarNewGame();

            Assert.Throws <ArgumentException>(() => game.Shoot());
        }
        public void ChooseFirstPlayer_With_Void_Should_Return_Player()
        {
            BattleShipGame battleShipGame = new BattleShipGame
            {
                Player1 = new Player(),
                Player2 = new Player()
            };

            var res = battleShipGame.ChooseFirstPlayer();

            Assert.IsType <Player>(res);
        }
        public void ChooseFirstPlayer_With_Void_Should_Return_Player_Of_BattleShipGame()
        {
            BattleShipGame battleShipGame = new BattleShipGame
            {
                Player1 = new Player(),
                Player2 = new Player()
            };

            var res = battleShipGame.ChooseFirstPlayer();

            Assert.True(res == battleShipGame.Player1 || res == battleShipGame.Player2);
        }
예제 #13
0
        public void Can_Hit_Ship(string coordinate, bool expectedResult)
        {
            var grid = GetGrid();
            var ship = new Ship(BATTLESHIP_SIZE, ShipType.BattleShip);

            ship.SetPositions(new Point(1, 1), new Point(1, 5));
            grid.AddShip(ship);
            var battleShipGame = new BattleShipGame(grid, new Mock <IRandomGenerator>().Object);

            var result = battleShipGame.Hit(coordinate);

            Assert.Equal(expectedResult, result);
        }
예제 #14
0
        private void MyEventHandler(object sender, EventArgs e)
        {
            if (sender == bt1)
            {
                game = BattleShipGame.getInstance();
                if (game != battleship)
                {
                    battleship = (BattleShipGame)game;
                    form       = new Form_Battleship();
                }
            }
            else if (sender == bt2)
            {
                game = KangarooGame.getInstance();
                if (game != kangaroo)
                {
                    kangaroo = (KangarooGame)game;
                    form     = new Form_Kangaroo();
                }
            }
            else if (sender == bt3)
            {
                game = MatchesGame.getInstance();
                if (game != matches)
                {
                    matches = (MatchesGame)game;
                    form    = new Form_Matches();
                }
            }

            //
            if (!form.IsDisposed && form != null)
            {
                form.Show();
            }
            else
            {
                if (battleship != null)
                {
                    battleship.setToNull();
                }
                if (kangaroo != null)
                {
                    kangaroo.setToNull();
                }
                if (matches != null)
                {
                    matches.setToNull();
                }
            }
        }
예제 #15
0
        public void TestMethod1()
        {
            // Here I'll test the Background functionality of the BattleShipGame unit
            // For another games also will be created other unit test classes with its
            // series of unit test methods...

            game = BattleShipGame.getInstance();
            if (game != battleship)
            {
                battleship = (BattleShipGame)game;
            }

            battleship.setToNull();
        }
예제 #16
0
        public void On_New_Game_Initialize_Three_Ships()
        {
            var randomGeneratorMock = new Mock <IRandomGenerator>();

            randomGeneratorMock
            .SetupSequence(it => it.GetRandomBoolean())
            .Returns(true).Returns(true).Returns(false);
            randomGeneratorMock
            .SetupSequence(it => it.GetRandomNumber(0, ROWS))
            .Returns(1).Returns(3);
            randomGeneratorMock
            .SetupSequence(it => it.GetRandomNumber(0, COLUMNS - BATTLESHIP_SIZE))
            .Returns(1);
            randomGeneratorMock
            .Setup(it => it.GetRandomNumber(0, COLUMNS - DESTROYER_SIZE))
            .Returns(3);
            randomGeneratorMock
            .SetupSequence(it => it.GetRandomNumber(0, ROWS - DESTROYER_SIZE))
            .Returns(5);
            randomGeneratorMock
            .Setup(it => it.GetRandomNumber(0, COLUMNS))
            .Returns(5);

            var battleShipGame = new BattleShipGame(GetGrid(), randomGeneratorMock.Object);

            battleShipGame.AddShip(new Ship(BATTLESHIP_SIZE, ShipType.BattleShip));
            battleShipGame.AddShip(new Ship(DESTROYER_SIZE, ShipType.Destroyer));
            battleShipGame.AddShip(new Ship(DESTROYER_SIZE, ShipType.Destroyer));
            battleShipGame.NewGame();

            // Battle Ship
            Assert.True(battleShipGame.Grid.Matrix[1, 1]);
            Assert.True(battleShipGame.Grid.Matrix[1, 2]);
            Assert.True(battleShipGame.Grid.Matrix[1, 3]);
            Assert.True(battleShipGame.Grid.Matrix[1, 4]);
            Assert.True(battleShipGame.Grid.Matrix[1, 5]);

            // First Destroyer
            Assert.True(battleShipGame.Grid.Matrix[3, 3]);
            Assert.True(battleShipGame.Grid.Matrix[3, 4]);
            Assert.True(battleShipGame.Grid.Matrix[3, 5]);
            Assert.True(battleShipGame.Grid.Matrix[3, 6]);

            // Second Destroyer
            Assert.True(battleShipGame.Grid.Matrix[5, 5]);
            Assert.True(battleShipGame.Grid.Matrix[6, 5]);
            Assert.True(battleShipGame.Grid.Matrix[7, 5]);
            Assert.True(battleShipGame.Grid.Matrix[8, 5]);
        }
        public void ThrowsAnExceptionWhenShipsAreTooCloseToEachOther(int startX1, int startY1, int endX1, int endY1, int startX2, int startY2, int endX2, int endY2)
        {
            var player1 = new Mock <IBattleShipShooter>();
            var ships   = new[]
            {
                new Ship(startX1, startY1, endX1, endY1),
                new Ship(startX2, startY2, endX2, endY2)
            };

            player1.Setup(x => x.PrepareShipsForNewBattle()).Returns(ships);

            var game = new BattleShipGame(player1.Object, player1.Object);

            Assert.That(() => game.StarNewGame(),
                        Throws.TypeOf <ArgumentException>().With.Message.EqualTo(", ships are too close to each other!"));
        }
        public void ThrowsErrorWhenTooMuchShipsBySize(int count, int size)
        {
            var player1 = new Mock <IBattleShipShooter>();
            var ships   = new[]
            {
                new Ship('A', 1, 'A', size),
                new Ship('B', 1, 'B', size),
                new Ship('C', 1, 'C', size),
                new Ship('D', 1, 'D', size),
                new Ship('F', 1, 'F', size)
            };

            player1.Setup(x => x.PrepareShipsForNewBattle()).Returns(ships.Take(count).ToArray);
            var game = new BattleShipGame(player1.Object, player1.Object);

            Assert.Throws <Exception>(() => game.StarNewGame());
        }
예제 #19
0
 /// <summary>
 /// Supprime les données
 /// </summary>
 /// <returns>une List<BattleShipGame> contenant tout les BattleShipGame existants</returns>
 public bool DeleteGame(BattleShipGame toDelete)
 {
     try
     {
         using var context = new BattleShipGameContext();
         context.Database.EnsureCreated();
         context.Remove(toDelete);
         context.SaveChanges();
         return(true);
     }
     catch (Exception e)
     {
         Console.WriteLine("Impossible de supprimer la partie");
         Console.WriteLine(e.Message);
         return(false);
     }
 }
예제 #20
0
 public void UpdateGame(BattleShipGame toUpdate)
 {
     try
     {
         using var context = new BattleShipGameContext();
         context.Database.EnsureCreated();
         context.Update(toUpdate);
         context.SaveChanges();
         Console.WriteLine(Environment.NewLine);
         Console.WriteLine("Partie sauvgardée avec succès !");
     }
     catch (Exception e)
     {
         Console.WriteLine("Impossible de sauvegarder la partie");
         Console.WriteLine(e.Message);
     }
 }
예제 #21
0
        /*protected override void OnResize(System.EventArgs e)
         * {
         *  base.OnResize(e);
         *  foreach (Control cnt in this.Controls)
         *  {
         *      ResizeAll(cnt, base.Size);
         *  }
         *  oldSize = base.Size;
         * }
         * private void ResizeAll(Control cnt, Size newSize)
         * {
         *  int iWidth = newSize.Width - oldSize.Width;
         *  cnt.Left += (cnt.Left * iWidth) / oldSize.Width;
         *  cnt.Width += (cnt.Width * iWidth) / oldSize.Width;
         *
         *  int iHeight = newSize.Height - oldSize.Height;
         *  cnt.Top += (cnt.Top * iHeight) / oldSize.Height;
         *  cnt.Height += (cnt.Height * iHeight) / oldSize.Height;
         * }*/

        private void OnClickHandler(object sender, EventArgs e)
        {
            Button[]  bt  = { bt1, bt2, bt3 };
            Label[]   lbl = { lbl1, lbl2, lbl3, lbl4 };
            ListBox[] lb  = { lb1, lb2 };
            if (sender == bt1)
            {
                foreach (var item in bt)
                {
                    item.Visible = false;
                }
                foreach (var item in lbl)
                {
                    item.Visible = false;
                }
                foreach (var item in lb)
                {
                    item.Visible = false;
                }
                // Parametrized Singleton + Abstract Factory
                // because the user is created with specific classes inside it which override
                // different methods and are incapsulated in the abstract factory...
                game = BattleShipGame.getInstance(new User(new UndefinedBehaviour()), new User(new UndefinedBehaviour()));
            }
            else if (sender == bt2)
            {
                lbl2.Visible = true;
                lbl3.Visible = true;
                lb1.Visible  = true;
            }
            else if (sender == bt3)
            {
                lbl2.Visible = true;
                lbl3.Visible = true;
                lbl4.Visible = true;
                lb1.Visible  = true;
                lb2.Visible  = true;
            }
            else if (sender == lb1)
            {
            }
            else if (sender == lb2)
            {
            }
        }
        public void KeepsAttackerAfterHit()
        {
            var player1     = new Mock <IBattleShipShooter>();
            var player2     = new Mock <IBattleShipShooter>();
            var ships       = new[] { new Ship('A', 1, 'A', 1) };
            var coordinates = new Coordinates('A', 1);

            player1.Setup(x => x.PrepareShipsForNewBattle()).Returns(ships);
            player2.Setup(x => x.PrepareShipsForNewBattle()).Returns(ships);
            player1.Setup(x => x.Shoot()).Returns(coordinates);
            player2.Setup(x => x.Shoot()).Returns(coordinates);

            var game = new BattleShipGame(player1.Object, player2.Object);

            game.StarNewGame();

            var attackerBefore = game.Attacker.Shooter;

            game.Shoot();
            Assert.AreEqual(attackerBefore, game.Attacker.Shooter);
        }
예제 #23
0
        /// <summary>
        /// Affiche les parties sauvgardées et charge celle selectionnée
        /// </summary>
        /// <returns></returns>
        public BattleShipGame ProcessMenuChoice(ConsoleKey enteredKey)
        {
            BattleShipGame loadedGame = null;
            var            savedGames = dataMapper.GetSavedGamesWithPlayers();

            if (savedGames != null && savedGames.Count > 0)
            {
                Console.WriteLine("Des parties sauvgardées ont étés trouvées :");
                foreach (var game in savedGames)
                {
                    Console.WriteLine("Partie " + (savedGames.IndexOf(game) + 1) + " " + game.Player1.Name + " vs " + game.Player2.Name);
                }
                Console.WriteLine(Environment.NewLine);
                if (enteredKey == ConsoleKey.C)
                {
                    Console.WriteLine("Saisissez le numéro de la partie à charger ou 0 pour revenir au menu principal");
                    var gameNumber = InputHandler.GetLoadGameInput();
                    if (gameNumber <= savedGames.Count() && gameNumber >= 1)
                    {
                        loadedGame = dataMapper.LoadGame(savedGames[gameNumber - 1].Id);
                    }
                }
                if (enteredKey == ConsoleKey.D)
                {
                    Console.WriteLine("Saisissez le numéro de la partie à supprimer ou 0 pour revenir au menu principal");
                    var gameNumber = InputHandler.GetLoadGameInput();
                    if (gameNumber <= savedGames.Count() && gameNumber >= 1)
                    {
                        if (dataMapper.DeleteGame(savedGames[gameNumber - 1]))
                        {
                            Console.WriteLine("Partie Supprimée avec succès !");
                        }
                    }
                }
            }
            Console.WriteLine(Environment.NewLine);
            return(loadedGame);
        }
        public void DoesntThrowAnExceptionWhenShipsPlacementIsValid()
        {
            var player1 = new Mock <IBattleShipShooter>();
            var ships   = new[]
            {
                new Ship('A', 1, 'C', 1),
                new Ship('G', 2, 'H', 2),
                new Ship('J', 3, 'J', 3),
                new Ship('B', 4, 'D', 4),
                new Ship('F', 6, 'F', 6),
                new Ship('H', 6, 'I', 6),
                new Ship('D', 7, 'D', 10),
                new Ship('A', 8, 'A', 8),
                new Ship('F', 10, 'G', 10),
                new Ship('J', 9, 'J', 9)
            };

            player1.Setup(x => x.PrepareShipsForNewBattle()).Returns(ships);

            var game = new BattleShipGame(player1.Object, player1.Object);

            Assert.That(() => game.StarNewGame(), Throws.Nothing);
        }
예제 #25
0
        public void Can_Generate_Random_Vertical_Ship()
        {
            var randomGeneratorMock = new Mock <IRandomGenerator>();

            randomGeneratorMock
            .Setup(it => it.GetRandomBoolean())
            .Returns(false);
            randomGeneratorMock
            .Setup(it => it.GetRandomNumber(0, ROWS - BATTLESHIP_SIZE))
            .Returns(1);
            randomGeneratorMock
            .Setup(it => it.GetRandomNumber(0, COLUMNS))
            .Returns(1);

            var battleShipGame = new BattleShipGame(GetGrid(), randomGeneratorMock.Object);

            battleShipGame.GenerateRandomShipPositions(new Ship(BATTLESHIP_SIZE, ShipType.BattleShip));

            Assert.True(battleShipGame.Grid.Matrix[1, 1]);
            Assert.True(battleShipGame.Grid.Matrix[2, 1]);
            Assert.True(battleShipGame.Grid.Matrix[3, 1]);
            Assert.True(battleShipGame.Grid.Matrix[4, 1]);
            Assert.True(battleShipGame.Grid.Matrix[5, 1]);
        }
예제 #26
0
 public StartScreen(BattleShipGame Game, SpriteFont StartFont)
 {
     this.StartFont = StartFont;
     this.Game = Game;
 }
예제 #27
0
 public GamePlayScreen(BattleShipGame Game, SpriteFont HudFont)
 {
     this.Game = Game;
     this.HudFont = HudFont;
     LoadPictures();
 }
 public BattleShipGameTests()
 {
     _battleShipGame = new BattleShipGame();
 }