Пример #1
0
        public void ProcessAttack(Coordinate coord)
        {
            var panel = Board.Panels.FirstOrDefault(x => x.Coordinates.Row == coord.Row && x.Coordinates.Column == coord.Column);

            //if panel is not occupied, call it a miss
            if (!panel.IsShipOccupied)
            {
                //print Miss
                panel.Type = Type.Miss;
            }
            else
            {
                //print Hit

                //panel occupied, get the ship that is in the panel
                var ship = Ships.FirstOrDefault(x => x.Type == panel.Type);

                ship.Hits++; //increment hits counter for ship

                //check if the ship is sunk
                if (ship.IsSunk)
                {
                    //Print The Ship is sunk
                }
            }
        }
Пример #2
0
        public static string ShipLocationInterpretation(Player player, Ships ship, string userStartLocation)
        {
            Regex regex  = new Regex(@"[a-z]\d\d?");
            Regex regex1 = new Regex(@"[a-z]\s\d\d?");

            if (regex.IsMatch(userStartLocation))
            {
                string curStr = "";
                if (userStartLocation.Length == 2)
                {
                    curStr += userStartLocation[0];
                    curStr += " ";
                    curStr += userStartLocation[1];
                    return(curStr);
                }
                else if (userStartLocation.Length == 3)
                {
                    curStr += userStartLocation[0];
                    curStr += " ";
                    curStr += userStartLocation[1];
                    curStr += userStartLocation[2];
                    return(curStr);
                }
            }
            else if (regex1.IsMatch(userStartLocation))
            {
                return(userStartLocation);
            }
            player.PlaceShip(player, ship);
            Console.WriteLine("\r\nYour Move Was Not Valid.");
            return("Invalid");
        }
Пример #3
0
        public virtual void PlaceShip(Player player, Ships ship)
        {
            bool isValid = false;

            Console.WriteLine($"\r\n{player.name}, Enter Starting Location Of {ship.name}");
            string shipPlacement = Console.ReadLine().ToLower().Trim();

            shipPlacement = UI.ShipLocationInterpretation(player, ship, shipPlacement);
            Console.WriteLine("\r\nEnter Its Orientation: \r\n('Up', 'Down', 'Left', 'Right')");
            string shipOrientation = Console.ReadLine();

            isValid = Game.ValidPlacement(ship, player.MoveInterpritation(shipPlacement), shipOrientation);
            if (player.shipPlacements.Count > 0 && isValid == true)
            {
                isValid = Game.CheckOverlappingShips(player, ship, player.MoveInterpritation(shipPlacement), shipOrientation);
            }
            if (isValid)
            {
                player.ShipPlacement(player, ship, shipOrientation, player.MoveInterpritation(shipPlacement));
                player.playerBoard.DisplayBoard(player);
            }
            else
            {
                PlaceShip(player, ship);
            }
        }
Пример #4
0
        public override void PlaceShip(Player player, Ships ship)
        {
            int randomX = Game.rng.Next(1, 21);

            System.Threading.Thread.Sleep(20);
            int randomY = Game.rng.Next(1, 21);

            int[] randomStartLocation = { randomX, randomY };
            System.Threading.Thread.Sleep(20);
            int    randomOri = Game.rng.Next(0, 4);
            string randomOrientation;
            bool   isValid = false;

            switch (randomOri)
            {
            case 0:
                randomOrientation = "right";
                break;

            case 1:
                randomOrientation = "down";
                break;

            case 2:
                randomOrientation = "left";
                break;

            case 3:
                randomOrientation = "up";
                break;

            default:
                randomOrientation = "right";
                break;
            }

            string shipOrientation = randomOrientation;

            isValid = Game.ValidPlacement(ship, randomStartLocation, shipOrientation);
            if (player.shipPlacements.Count > 0 && isValid == true)
            {
                isValid = Game.CheckOverlappingShips(player, ship, randomStartLocation, shipOrientation);
            }
            if (isValid)
            {
                player.ShipPlacement(player, ship, shipOrientation, randomStartLocation);
                if (Game.numberOfComputers == 2)
                {
                    player.playerBoard.DisplayBoard(player);
                }
            }
            else
            {
                this.PlaceShip(player, ship);
            }
        }
Пример #5
0
        static public bool CheckOverlappingShips(Player player, Ships ship, int[] startLocation, string shipOrientation)
        {
            List <int[]> currentPlacement = new List <int[]> {
            };

            switch (shipOrientation)
            {
            case "left":
                for (int i = 0; i < ship.length; i++)
                {
                    int[] nextLocation = new int[] { startLocation[0], startLocation[1] - i };
                    currentPlacement.Add(nextLocation);
                }
                break;

            case "right":
                for (
                    int i = 0; i < ship.length; i++)
                {
                    int[] nextLocation = new int[] { startLocation[0], startLocation[1] + i };
                    currentPlacement.Add(nextLocation);
                }
                break;

            case "up":
                for (int i = 0; i < ship.length; i++)
                {
                    int[] nextLocation = new int[] { startLocation[0] - i, startLocation[1] };
                    currentPlacement.Add(nextLocation);
                }
                break;

            case "down":
                for (int i = 0; i < ship.length; i++)
                {
                    int[] nextLocation = new int[] { startLocation[0] + i, startLocation[1] };
                    currentPlacement.Add(nextLocation);
                }
                break;
            }
            for (int i = 0; i < currentPlacement.Count; i++)
            {
                for (int j = 0; j < player.shipPlacements.Count; j++)
                {
                    if (currentPlacement[i][0] == player.shipPlacements[j][0] && currentPlacement[i][1] == player.shipPlacements[j][1])
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
Пример #6
0
        public static bool ValidPlacement(Ships ship, int[] startLocation, string shipOrientation)
        {
            switch (shipOrientation)
            {
            case "left":
                if (startLocation[1] - ship.length <= 1)
                {
                    return(false);
                }
                else
                {
                    return(true);
                }

            case "right":
                if (startLocation[1] + ship.length >= 21)
                {
                    return(false);
                }
                else
                {
                    return(true);
                }

            case "up":
                if (startLocation[0] - ship.length <= 1)
                {
                    return(false);
                }
                else
                {
                    return(true);
                }

            case "down":
                if (startLocation[0] + ship.length >= 21)
                {
                    return(false);
                }
                else
                {
                    return(true);
                }

            default:
                return(false);
            }
        }
Пример #7
0
        // Member Methods
        public virtual void ShipPlacement(Player guesser, Ships ship, string shipOrientation, int[] startLocation)
        {
            playerBoard.gameBoard[startLocation[0], startLocation[1]] = ship.abbreviation;
            switch (shipOrientation)
            {
            case "left":
                for (int i = 0; i < ship.length; i++)
                {
                    int[] nextLocation = new int[] { startLocation[0], startLocation[1] - i };
                    playerBoard.gameBoard[nextLocation[0], nextLocation[1]] = ship.abbreviation;
                    guesser.shipPlacements.Add(nextLocation);
                }
                break;

            case "right":
                for (
                    int i = 0; i < ship.length; i++)
                {
                    int[] nextLocation = new int[] { startLocation[0], startLocation[1] + i };
                    playerBoard.gameBoard[nextLocation[0], nextLocation[1]] = ship.abbreviation;
                    guesser.shipPlacements.Add(nextLocation);
                }
                break;

            case "up":
                for (int i = 0; i < ship.length; i++)
                {
                    int[] nextLocation = new int[] { startLocation[0] - i, startLocation[1] };
                    playerBoard.gameBoard[nextLocation[0], nextLocation[1]] = ship.abbreviation;
                    guesser.shipPlacements.Add(nextLocation);
                }
                break;

            case "down":
                for (int i = 0; i < ship.length; i++)
                {
                    int[] nextLocation = new int[] { startLocation[0] + i, startLocation[1] };
                    playerBoard.gameBoard[nextLocation[0], nextLocation[1]] = ship.abbreviation;
                    guesser.shipPlacements.Add(nextLocation);
                }
                break;

            default:
                break;
            }
        }
Пример #8
0
 // Constructor
 public Player()
 {
     playerBoard     = new GameBoard();
     opponentBoard   = new GameBoard();
     battleShip      = new BattleShip();
     destroyer       = new Destroyer();
     aircraftCarrier = new AircraftCarrier();
     submarine       = new Submarine();
     totalGuesses    = new List <int[]> {
     };
     shipPlacements  = new List <int[]> {
     };
     hits            = new List <int[]> {
     };
     sunkBools       = new bool[4] {
         false, false, false, false
     };
 }