Beispiel #1
0
        /// <summary>
        /// Takes row and cloumn input from user and fires a shot.
        /// </summary>
        /// <returns>The coordinates of fired shot</returns>
        public BoardCorrdinates FireShot()
        {
            BoardCorrdinates coordinates = null;

            try
            {
                //Take user input to fire a shot
                Console.WriteLine("\nPlease enter coordinates to fire a shot: ");
                Console.Write("row: ");
                var row = int.Parse(Console.ReadLine());
                Console.Write("column: ");
                var column = int.Parse(Console.ReadLine());

                if (CheckUserInput(row, column))
                {
                    //create coordiantes object to fire a shot
                    coordinates = new BoardCorrdinates(row, column);
                    Console.WriteLine(" > \"Firing shot at " + row + ", " + column + "\"");
                }
                else
                {
                    Console.WriteLine("---->>>>>>>> Please enter valid coordinates.<<<<<<<<----");
                }
            }
            catch (System.FormatException)
            {
                Console.WriteLine("---->>>>>>>> Please enter valid coordinates.<<<<<<<<----");
            }


            return(coordinates);
        }
Beispiel #2
0
        /// <summary>
        /// Updated a shot on fired board.
        /// </summary>
        /// <param name="coords">Coords.</param>
        /// <param name="result">Result.</param>
        public void ProcessShotResult(BoardCorrdinates coords, ShotResult result)
        {
            var panel = FiredShotBoard.Panels.At(coords.Row, coords.Column);

            switch (result)
            {
            case ShotResult.Hit:
                panel.AttackerShipType = AttackerShipType.Hit;
                break;

            default:
                panel.AttackerShipType = AttackerShipType.Miss;
                break;
            }
        }
Beispiel #3
0
        public void PlayRound()
        {
            //create coordiantes object to fire a shot
            BoardCorrdinates coordinates = Player.FireShot();


            if (coordinates != null)
            {
                //process a shot
                var result = Player.ProcessShot(coordinates);

                //Updates user fired shot coordinates on fire board
                Player.ProcessShotResult(coordinates, result);
            }
        }
Beispiel #4
0
        /// <summary>
        /// Processes the fired shot
        /// </summary>
        /// <returns>Shot is missed or hit</returns>
        /// <param name="coords">Coordinates</param>
        public ShotResult ProcessShot(BoardCorrdinates coords)
        {
            var panel = GameBoard.Panels.At(coords.Row, coords.Column);

            if (!panel.IsOccupied)
            {
                Console.WriteLine(" > \"You missed shot!\"");
                return(ShotResult.Miss);
            }
            var ship = ShipTypes.First(x => x.AttackerShipType == panel.AttackerShipType);

            ship.Hits++;
            Console.WriteLine(" > \"It's a Hit!\"");
            if (ship.IsSunk)
            {
                Console.WriteLine(" > \"You have sunk " + ship.Name + ".\"\n\n");
            }
            return(ShotResult.Hit);
        }