예제 #1
0
 /// <summary>
 /// When a shot fired has already been attempted
 /// </summary>
 private void AlreadyAttempted(Game game)
 {
     Console.Clear();
     Visualiser.PrintCurrentGame(game);
     Console.WriteLine();
     Console.WriteLine("'Avast ye! You've already fired ere'");
 }
예제 #2
0
        /// <summary>
        /// The main entry point into BattleShips
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            // Dependency Injection
            var kernel = new StandardKernel();

            kernel.Load(Assembly.GetExecutingAssembly());
            var gameService = kernel.Get <IGameService>();

            // Print the splash screen
            PrintSplash();

            // Create a new game instance and set the player name
            var game = gameService.CreateGame();

            game.PlayerName = Visualiser.RequestPlayerName();

            // Print the grid
            Visualiser.PrintCurrentGame(game);

            // Prompt the user for input
            Console.WriteLine();
            Console.WriteLine("Take aim, type in a grid reference e.g C5 and hit Return to fire!");

            // The Game Loop
            do
            {
                var input = Console.ReadLine();
                try
                {
                    var y    = input.Substring(0, 1).ToUpper();
                    var x    = Convert.ToInt32(input.Substring(1));
                    var shot = new GridCoords(x, y);

                    game = gameService.FireShot(game, shot);
                } catch (Exception)
                {
                    Console.WriteLine("Shot Invalid, Try again");
                }
            } while (game.Active == true);
        }
예제 #3
0
        /// <summary>
        /// When a shot fired misses
        /// </summary>
        /// <param name="game"></param>
        /// <param name="indices"></param>
        private Game ShotMissed(Game game, Indices indices)
        {
            game.GameGrid.Cells[indices.X, indices.Y].Status = GridCellStatus.ShotMissed;

            // Lose a life
            game.Lives--;

            // Reset Streak
            game.Streak = 0;

            // Deduct from Score
            if (game.Score > 0)
            {
                game.Score = game.Score - 1;
            }
            ;

            Console.Clear();
            Visualiser.PrintCurrentGame(game);

            Console.WriteLine();
            Console.WriteLine("'Unlucky Squire! Ya misses.. Try agen'");

            if (game.Lives == 0)
            {
                Console.Clear();
                game.Active = false;
                game.Score  = 0;
                AsciiHelper.DrawYouLose();
                Console.WriteLine();
                Console.WriteLine();
                Console.ReadLine();
            }

            return(game);
        }
예제 #4
0
        /// <summary>
        /// When a shot is landed
        /// </summary>
        /// <param name="game"></param>
        /// <param name="indices"></param>
        private Game ShotLanded(Game game, Indices indices)
        {
            // Update cell and beep
            game.GameGrid.Cells[indices.X, indices.Y].Status = GridCellStatus.ShotLanded;
            Console.Beep(432, 750);

            // Incrememnt streak
            game.Streak++;

            // Initialise first points
            if (game.Score == 0)
            {
                game.Score = 1;
            }
            ;

            // Update score and apply streak if not negative.
            if (game.Score > 0)
            {
                game.Score = game.Score + (game.Score * game.Streak);
            }
            else
            {
                game.Score = game.Score + game.Score;
            }

            // Incremement the boat hit counter
            var target = game.GameGrid.Cells[indices.X, indices.Y];

            game.Boats
            .First(b => b.Id == target.BoatHeld.Id)
            .Hits++;

            Console.Clear();
            Visualiser.PrintCurrentGame(game);
            Console.WriteLine();

            Console.WriteLine("'Arrrgh you scurvy landlubber!!, You've hit me' boat!'");

            // Get the boat
            var boat = game.Boats
                       .First(b => b.Id == target.BoatHeld.Id);

            // Check length vs. hits
            if (boat.Hits == boat.Length)
            {
                game.BoatsSank++;
                Console.Beep(432, 1500);

                Console.ForegroundColor = ConsoleColor.Blue;
                Console.WriteLine("'Blimey! Boat down'");
                Console.ForegroundColor = ConsoleColor.Magenta;
            }
            ;

            if (game.Boats.Count() == game.BoatsSank)
            {
                Console.Clear();

                // Winner, Winner.
                game.Score  = game.Score + (game.Lives * 10);
                game.Active = false;
                AsciiHelper.DrawWinner();
                Console.WriteLine();
                Console.WriteLine();
                Console.Write("Congratulations, ");
                Console.Write(game.PlayerName);
                Console.Write(", Your Final Score was: ");
                Console.Write(game.Score);
                Console.WriteLine();
                Console.ReadLine();
            }
            ;

            return(game);
        }