Esempio n. 1
0
        public void TestDisplayShotResult_ShipSunk()
        {
            var gridManager = new GridManager(ships);
            var ioManagerMocked = new Mock<IIOManager>();

            GameEngine.DisplayShotResult(gridManager, ioManagerMocked.Object, ShotResult.ShipSunk);

            ioManagerMocked.Verify(x => x.WriteLine("\t*** Sunk ***"), Times.Once);
            ioManagerMocked.Verify(x => x.WriteLine(It.IsAny<string>()), Times.Once);
        }
Esempio n. 2
0
        public void TestDisplayShotResult_AllShipsSunk()
        {
            var gridManager = new GridManager(ships);
            var ioManagerMocked = new Mock<IIOManager>();

            GameEngine.DisplayShotResult(gridManager, ioManagerMocked.Object, ShotResult.AllShipsSunk);

            ioManagerMocked.Verify(
                x => x.WriteLine(
                "Well done! You completed the game in {0} shots.",
                gridManager.ShotsCount),
                Times.Once);
            ioManagerMocked.Verify(x => x.WriteLine(It.IsAny<string>(), It.IsAny<int>()), Times.Once);
        }
Esempio n. 3
0
        public void TestRunWithIORedirected()
        {
            string inputFilePath = "../../Resources/SampleInput.in";
            string outputFilePath = "../../Resources/SampleOutput.out";
            string expectedOutputFilePath = "../../Resources/ExpectedOutput.out";

            GridManager gridManager = new GridManager(ships);
            IIOManager ioManager = new ConsoleManager();
            GameEngine.RunWithIORedirected(gridManager, ioManager, inputFilePath, outputFilePath);

            string actualOutput = File.ReadAllText(outputFilePath, Encoding.ASCII);
            string expectedOutput = File.ReadAllText(expectedOutputFilePath, Encoding.ASCII);

            Assert.AreEqual(expectedOutput, actualOutput);
        }
Esempio n. 4
0
 /// <summary>
 /// For testing purposes only. Plays the game with I/O redirected to/from files.
 /// </summary>
 /// <param name="gridManager">The <see cref="GridManager" /> which handles the game.</param>
 /// <param name="ioManager">An object specifying how the I/O should be handled.</param>
 /// <param name="inputFilePath">The path of the input file.</param>
 /// <param name="outputFilePath">The path of the output file.</param>
 /// <example>
 /// This example shows how to call the <see cref="RunWithIORedirected(GridManager, string, string)"/> method.
 /// <code>
 /// class TestClass
 /// {
 ///     static void Main()
 ///     {
 ///         GridManager gridManager = new GridManager();
 ///         
 ///         RunWithIORedirected(
 ///             gridManager,
 ///             Path.Combine(Environment.CurrentDirectory, "SampleInput.in"),
 ///             Path.Combine(Environment.CurrentDirectory, "SampleOutput.out"));
 ///     }
 /// }
 /// </code>
 /// </example>
 public static void RunWithIORedirected(
     GridManager gridManager,
     IIOManager ioManager,
     string inputFilePath,
     string outputFilePath)
 {
     using (StreamReader reader = new StreamReader(inputFilePath))
     {
         using (StreamWriter writer = new StreamWriter(outputFilePath))
         {
             ioManager.SetIn(reader);
             ioManager.SetOut(writer);
             Run(gridManager, ioManager);
         }
     }
 }
Esempio n. 5
0
        public void TestConstructor_ShipsShouldBePlacedCorrectly()
        {
            GridManager gridManager = new GridManager(ships);

            Square[,] grid = gridManager.Grid;

            foreach (Ship ship in ships)
            {
                int deltaRow = ship.Direction == ShipDirection.Horizontal ? 0 : 1;
                int deltaCol = ship.Direction == ShipDirection.Horizontal ? 1 : 0;

                for (int i = 0; i < ship.Size; i++)
                {
                    int row = ship.Bow.Row + (i * deltaRow);
                    int col = ship.Bow.Col + (i * deltaCol);

                    Assert.AreSame(ship, grid[row, col].Ship);
                    Assert.AreEqual(SquareState.OccupiedNoShot, grid[row, col].State);
                }
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Starts the game loop.
        /// </summary>
        /// <param name="gridManager">The <see cref="GridManager" /> which handles the game.</param>
        /// <param name="ioManager">An object specifying how the I/O should be handled.</param>
        public static void Run(GridManager gridManager, IIOManager ioManager)
        {
            ioManager.WriteLine(
                "\t\tBATTLESHIP by ANONYMOUS SOLUTIONS INC.{0}{0}" +
                "You are given a square {1}x{1} grid. The individual squares in the grid{0}" +
                "are identified by letter and number, e.g. A5. Several ships have been{0}" +
                "secretly arranged. Each ship occupies a number of consecutive squares{0}" +
                "on the grid, arranged either horizontally or vertically. The ships can{0}" +
                "touch each other and cannot overlap. The fleet consists of {2} ship(s).{0}" +
                "Your task is to sink them all.{0}",
                Environment.NewLine,
                GridManager.GridSize,
                gridManager.ShipsCount);

            ioManager.WriteLine(gridManager.DisplayShips(false));

            while (true)
            {
                ioManager.Write("Enter target square to shoot at: ");

                string command = ioManager.ReadLine();
                if (command == GridManager.BackdoorCommand)
                {
                    ioManager.WriteLine(gridManager.DisplayShips(true));
                }
                else
                {
                    ShotResult shotResult = gridManager.ShootTarget(command);

                    DisplayShotResult(gridManager, ioManager, shotResult);
                    if (shotResult == ShotResult.AllShipsSunk)
                    {
                        return;
                    }

                    ioManager.WriteLine(gridManager.DisplayShips(false));
                }
            }
        }
Esempio n. 7
0
        public void TestDisplayShips_BackdoorMode()
        {
            GridManager gridManager = new GridManager(ships);

            string gridAsString = gridManager.DisplayShips(true);

            string expectedString = 
                "    1 2 3 4 5 6 7 8 9 0\r\n" + 
                "   ---------------------\r\n" + 
                "A |             X   X   |\r\n" + 
                "B |             X   X   |\r\n" + 
                "C |     X       X   X   |\r\n" + 
                "D |     X       X   X   |\r\n" + 
                "E |     X       X       |\r\n" + 
                "F |     X       X       |\r\n" + 
                "G |     X               |\r\n" + 
                "H |             X X     |\r\n" + 
                "I |             X X X X |\r\n" + 
                "J |                     |\r\n" + 
                "   ---------------------\r\n";

            Assert.AreEqual(expectedString, gridAsString);
        }
Esempio n. 8
0
        public void TestDisplayShips_NormalMode()
        {
            GridManager gridManager = new GridManager(ships);

            gridManager.ShootTarget("D4");
            gridManager.ShootTarget("G3");
            gridManager.ShootTarget("I7");

            string gridAsString = gridManager.DisplayShips(false);

            string expectedString =
                "    1 2 3 4 5 6 7 8 9 0\r\n" +
                "   ---------------------\r\n" +
                "A | . . . . . . . . . . |\r\n" +
                "B | . . . . . . . . . . |\r\n" +
                "C | . . . . . . . . . . |\r\n" +
                "D | . . . - . . . . . . |\r\n" +
                "E | . . . . . . . . . . |\r\n" +
                "F | . . . . . . . . . . |\r\n" +
                "G | . . X . . . . . . . |\r\n" +
                "H | . . . . . . . . . . |\r\n" +
                "I | . . . . . . X . . . |\r\n" +
                "J | . . . . . . . . . . |\r\n" +
                "   ---------------------\r\n";

            Assert.AreEqual(expectedString, gridAsString);
        }
Esempio n. 9
0
 public void TestConstructor_ShipsEqualToNullProvided()
 {
     Ship[] testShips = new Ship[] { null };
     GridManager gridManager = new GridManager(testShips);
 }
Esempio n. 10
0
 public void TestConstructor_NoShipsProvided()
 {
     GridManager gridManager = new GridManager(null);
 }
Esempio n. 11
0
        public void TestShootTarget_AllShipsSunk()
        {
            GridManager gridManager = new GridManager(ships);
            ShotResult shotResult = ShotResult.Error;

            foreach (Ship ship in ships)
            {
                int deltaRow = ship.Direction == ShipDirection.Horizontal ? 0 : 1;
                int deltaCol = ship.Direction == ShipDirection.Horizontal ? 1 : 0;

                for (int i = 0; i < ship.Size; i++)
                {
                    int row = ship.Bow.Row + (i * deltaRow);
                    int col = ship.Bow.Col + (i * deltaCol);

                    string input = gridManager.GetMatchingInput(row, col);

                    shotResult = gridManager.ShootTarget(input);
                }
            }

            Assert.AreEqual(ShotResult.AllShipsSunk, shotResult);
        }
Esempio n. 12
0
        public void TestShootTarget_MissTwice()
        {
            GridManager gridManager = new GridManager(ships);

            string input = gridManager.GetMatchingInput(0, 0);

            ShotResult shotResult = gridManager.ShootTarget(input);
            shotResult = gridManager.ShootTarget(input);

            Assert.AreEqual(ShotResult.Miss, shotResult);
        }
Esempio n. 13
0
        public void TestShootTarget_HitTwice()
        {
            GridManager gridManager = new GridManager(ships);

            string input = gridManager.GetMatchingInput(ships[0].Bow.Row, ships[0].Bow.Col);

            ShotResult shotResult = gridManager.ShootTarget(input);
            shotResult = gridManager.ShootTarget(input);

            Assert.AreEqual(ShotResult.Hit, shotResult);
        }
Esempio n. 14
0
        public void TestShootTarget_InvalidUserInput()
        {
            GridManager gridManager = new GridManager();

            ShotResult shotResult = gridManager.ShootTarget(null);
            Assert.AreEqual(ShotResult.Error, shotResult);

            shotResult = gridManager.ShootTarget(string.Empty);
            Assert.AreEqual(ShotResult.Error, shotResult);

            shotResult = gridManager.ShootTarget("   ");
            Assert.AreEqual(ShotResult.Error, shotResult);

            shotResult = gridManager.ShootTarget("A");
            Assert.AreEqual(ShotResult.Error, shotResult);

            shotResult = gridManager.ShootTarget("BBBB");
            Assert.AreEqual(ShotResult.Error, shotResult);
        }
Esempio n. 15
0
 /// <summary>
 /// Displays a string depending on the specified ShotResult.
 /// </summary>
 /// <param name="gridManager">The <see cref="GridManager" /> which handles the game.</param>
 /// <param name="ioManager">An object specifying how the I/O should be handled.</param>
 /// <param name="value">The value to display.</param>
 public static void DisplayShotResult(GridManager gridManager, IIOManager ioManager, ShotResult value)
 {
     switch (value)
     {
         case ShotResult.Hit:
             {
                 ioManager.WriteLine("\t*** Hit ***");
                 break;
             }
         case ShotResult.ShipSunk:
             {
                 ioManager.WriteLine("\t*** Sunk ***");
                 break;
             }
         case ShotResult.AllShipsSunk:
             {
                 ioManager.WriteLine("Well done! You completed the game in {0} shots.", gridManager.ShotsCount);
                 break;
             }
         case ShotResult.Miss:
             {
                 ioManager.WriteLine("\t*** Miss ***");
                 break;
             }
         case ShotResult.Error:
             {
                 ioManager.WriteLine("\t*** Error ***");
                 break;
             }
         default:
             {
                 throw new BattleshipException("Unknown ShotResult.");
             }
     }
 }