Ejemplo n.º 1
0
        public static bool PlaceShip(PlayerInfoModel model, string location)
        {
            string row    = "";
            int    column = -1;

            (row, column) = SplitIntoRowAndColumns(location);

            bool isValidPlaceForShip = ValidateLocation(row, column);

            if (!isValidPlaceForShip)
            {
                return(false);
            }

            if (GetSpotFromList(model.ShipLocations, row, column) != null)
            {
                return(false);
            }

            GridSpotModel shipModel = new GridSpotModel {
                Status     = GridSpotStatus.Ship,
                SpotLetter = row,
                SpotNumber = column
            };

            model.ShipLocations.Add(shipModel);

            return(true);
        }
Ejemplo n.º 2
0
        internal static GridSpotModel AskForShot(PlayerInfoModel activePlayer, PlayerInfoModel opponent)
        {
            bool isValidShot = false;

            UIDisplay.DisplayGrid(activePlayer.ShotsGrid);
            Console.WriteLine();
            Console.Write($"{activePlayer.PlayerName}: Where would you like to fire: ");
            string gridSpotSelection = Console.ReadLine();

            GridSpotModel output = UILogic.ParseStringToGridSpot(gridSpotSelection);

            isValidShot = GameLogic.ValidateShot(output, opponent);

            while (isValidShot == false)
            {
                Console.Clear();
                Console.WriteLine($"Oops!  \"{gridSpotSelection}\" was not a valid shot.  Please try again.");

                UIDisplay.DisplayGrid(activePlayer.ShotsGrid);
                Console.WriteLine();
                Console.Write($"{activePlayer.PlayerName}: Where would you like to fire: ");
                gridSpotSelection = Console.ReadLine();

                output = UILogic.ParseStringToGridSpot(gridSpotSelection);

                isValidShot = GameLogic.ValidateShot(output, opponent);
            }
            return(output);
        }
Ejemplo n.º 3
0
        private static bool IsMatchingRowAndColumn(GridSpotModel gridSpot, string row, int column)
        {
            if (gridSpot.SpotLetter.Equals(row, StringComparison.OrdinalIgnoreCase) && gridSpot.SpotNumber == column)
            {
                return(true);
            }

            return(false);
        }
Ejemplo n.º 4
0
        private static void AddGridSpot(PlayerInfoModel model, string letter, int number)
        {
            GridSpotModel spot = new GridSpotModel();

            spot.SpotLetter = letter;
            spot.SpotNumber = number;
            spot.Status     = GridSpotStatus.Empty;

            model.ShotGrid.Add(spot);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Creates a new GridSpotModel object and adds a grid spot to the
        /// PlayerModel object's ShotGrid list
        /// </summary>
        /// <param name="model"></param>
        /// <param name="letter"></param>
        /// <param name="number"></param>
        private static void AddGridSpot(PlayerModel model, string letter, int number)
        {
            GridSpotModel gridSpot = new GridSpotModel
            {
                SpotLetter = letter,
                SpotNumber = number,
                Status     = GridSpotStatus.Empty
            };

            model.ShotGrid.Add(gridSpot);
        }
Ejemplo n.º 6
0
        private static void AddGridSpot(PlayerInfoModel m, string l, int n)
        {
            GridSpotModel spot = new GridSpotModel
            {
                SpotLetter = l,
                SpotNumber = n,
                Status     = GridSpotStatus.Empty
            };

            m.ShotGrid.Add(spot);
        }
Ejemplo n.º 7
0
        public static bool IdentifyShotResult(PlayerInfoModel opponent, string row, int column)
        {
            GridSpotModel enemyShip = GetSpotFromList(opponent.ShipLocations, row, column);

            if (enemyShip != null)
            {
                enemyShip.Status = GridSpotStatus.Sunk;
                return(true);
            }
            return(false);
        }
        private static void AddGridSpot(PlayerInfoModel model, string letter, int number)
        {
            //gona take model and add gridspot
            GridSpotModel spot = new GridSpotModel
            {
                SpotLetter = letter,
                SpotNumber = number,
                Status     = GridSpotStatus.Empty
            };

            model.ShotGrid.Add(spot);
        }
Ejemplo n.º 9
0
        private static void AddSpot(PlayerInfoModel model, string letter, int number)
        {
            GridSpotModel spot = new GridSpotModel
            {
                SpotLetter = letter,
                SpotNumber = number,
                RowLetter  = letter,
                Status     = GridSpotStatus.Empty
            };

            model.PlayerShotGrid.Add(spot);
        }
Ejemplo n.º 10
0
        public static void AddShipToGrid(PlayerInfoModel player, string shipSpot)
        {
            (string letter, int number) = SplitPosition(shipSpot);

            GridSpotModel ship = new GridSpotModel
            {
                SpotLetter = letter,
                SpotNumber = number,
                Status     = GridSpotStatus.Ship
            };

            player.PlayerShipSpot.Add(ship);
        }
Ejemplo n.º 11
0
        internal static void GetShipPlacements(PlayerInfoModel newPlayer)
        {
            //Initialize a grid for the player's ship placements
            newPlayer.ShipLocations = GameLogic.InitializeGrid();

            List <GridSpotModel> shipPlacements = new List <GridSpotModel>();

            bool isValidPlacement = false;

            do
            {
                DisplayGrid(newPlayer.ShipLocations);
                Console.WriteLine("");


                //Ask player for their next ship location

                string newPlacement = GetShipPlacementSelection(shipPlacements.Count);

                //Parse selection into GridSpotModel.GridSpotLetter and GridSpotNumber
                GridSpotModel gridSpotSelection = UILogic.ParseStringToGridSpot(newPlacement);


                //Check to see if that spot is a valid spot on the grid or if they have already placed a ship there
                isValidPlacement = GameLogic.IsValidShipPlacement(gridSpotSelection, newPlayer.ShipLocations);
                while (isValidPlacement == false)
                {
                    Console.Clear();

                    Console.WriteLine("Oops!  That's not a valid selection on your grid.  Please try again.");

                    DisplayGrid(newPlayer.ShipLocations);
                    Console.WriteLine("");

                    newPlacement = GetShipPlacementSelection(shipPlacements.Count);

                    gridSpotSelection = UILogic.ParseStringToGridSpot(newPlacement);

                    isValidPlacement = GameLogic.IsValidShipPlacement(gridSpotSelection, newPlayer.ShipLocations);
                }

                //Mark gridspot status as "ship" if the selection is valid
                GameLogic.RecordShipPlacement(gridSpotSelection, newPlayer.ShipLocations);

                shipPlacements.Add(gridSpotSelection);

                Console.Clear();

                //Loop back through until player has placed all five ships
            } while (shipPlacements.Count < 5);
        }
Ejemplo n.º 12
0
        public static void MarkShotResult(PlayerInfoModel activePlayer, string row, int column, bool isAHit)
        {
            GridSpotModel shopGridSpot = GetSpotFromList(activePlayer.ShotsGrid, row, column);

            if (isAHit)
            {
                shopGridSpot.Status = GridSpotStatus.Hit;
            }
            else
            {
                shopGridSpot.Status = GridSpotStatus.Miss;
            }
            activePlayer.ShotsCount++;
        }
Ejemplo n.º 13
0
        public static bool ValidateShot(PlayerInfoModel activePlayer, string row, int column)
        {
            bool isValidShotLocation = ValidateLocation(row, column);

            if (!isValidShotLocation)
            {
                return(false);
            }

            GridSpotModel output = GetSpotFromList(activePlayer.ShotsGrid, row, column);

            if ((output != null) && (output.Status == GridSpotStatus.Empty))
            {
                return(true);
            }
            return(false);
        }
Ejemplo n.º 14
0
        internal static void TakeTurn(PlayerInfoModel activePlayer, PlayerInfoModel opponent)
        {
            UIDisplay.DisplayTurnHeader(activePlayer, opponent);

            GridSpotModel shotRequest = UIDisplay.AskForShot(activePlayer, opponent);

            //Validates the shot and loops until the shot is valid
            bool isValidShot = GameLogic.ValidateShot(shotRequest, opponent);

            while (isValidShot == false)
            {
                UIDisplay.DisplayGrid(activePlayer.ShotsGrid);

                shotRequest = UIDisplay.AskForShot(activePlayer, opponent);
                isValidShot = GameLogic.ValidateShot(shotRequest, opponent);
            }

            //Once the player calls a valid shot, the program determins if it's a hit or a miss,
            //records the shot, and changes the opponent's number of remianing ships property
            if (GameLogic.IsAHit(shotRequest, opponent) == true)
            {
                GameLogic.RecordShot(shotRequest, activePlayer, opponent);

                opponent.RemainingShips = GameLogic.QuantifyRemainingShips(opponent);

                UIDisplay.DisplayHitMessage(opponent);
            }

            else
            {
                GameLogic.RecordShot(shotRequest, activePlayer, opponent);

                opponent.RemainingShips = GameLogic.QuantifyRemainingShips(opponent);

                UIDisplay.DisplayMissMessage();
            }
        }
Ejemplo n.º 15
0
        internal static GridSpotModel ParseStringToGridSpot(string input)
        {
            //Figures out if the length of the input is correct
            GridSpotModel gridSpot = new GridSpotModel();

            if (input.Length != 2)
            {
                //In order to get the grid to display accurately each time, the output is set to something that will be
                //erronius in the next level of method up instead of having to pass in an active grid into this method as well
                gridSpot.SpotLetter = "Z";
                gridSpot.SpotNumber = 9;

                return(gridSpot);
            }

            //Net of if statements to assign the correct letter to the gridSpot
            if (input.Substring(0, 1).ToLower() == "a")
            {
                gridSpot.SpotLetter = "A";
            }

            else if (input.Substring(0, 1).ToLower() == "b")
            {
                gridSpot.SpotLetter = "B";
            }

            else if (input.Substring(0, 1).ToLower() == "c")
            {
                gridSpot.SpotLetter = "C";
            }

            else if (input.Substring(0, 1).ToLower() == "d")
            {
                gridSpot.SpotLetter = "D";
            }

            else if (input.Substring(0, 1).ToLower() == "e")
            {
                gridSpot.SpotLetter = "E";
            }

            else
            {
                gridSpot.SpotLetter = "Z";
                gridSpot.SpotNumber = 9;

                return(gridSpot);
            }

            //Tries to parse the second charicter of user's input into an int
            string inputNumberString  = input.Substring(1, 1);
            bool   isValidInputNumber = int.TryParse(inputNumberString, out int inputNumber);

            if (isValidInputNumber == false)
            {
                gridSpot.SpotLetter = "Z";
                gridSpot.SpotNumber = 9;

                return(gridSpot);
            }

            //Net of if statements to assign the correct number to gridSpot
            if (inputNumber == 1)
            {
                gridSpot.SpotNumber = 1;
            }

            else if (inputNumber == 2)
            {
                gridSpot.SpotNumber = 2;
            }

            else if (inputNumber == 3)
            {
                gridSpot.SpotNumber = 3;
            }

            else if (inputNumber == 4)
            {
                gridSpot.SpotNumber = 4;
            }

            else if (inputNumber == 5)
            {
                gridSpot.SpotNumber = 5;
            }

            else
            {
                gridSpot.SpotLetter = "Z";
                gridSpot.SpotNumber = 9;

                return(gridSpot);
            }

            return(gridSpot);
        }