Example #1
0
        public ShotResult ProcessShot(Coordinates attackedCoordinates, List <BoardCell> gameBoard)
        {
            //Locate the targeted boardcell on the GameBoard
            var boardCell = gameBoard.FirstOrDefault(x => x.Coordinates.Row == attackedCoordinates.Row &&
                                                     x.Coordinates.Column == attackedCoordinates.Column);

            //If the boardcell is NOT occupied by a ship
            if (!boardCell.IsOccupied)
            {
                //Call out a miss
                Console.WriteLine(Name + " says: \"Miss!\"");
                return(ShotResult.Miss);
            }

            //If the boardcell IS occupied by a ship, determine which one.
            var ship = new Battleship();

            foreach (var aShip in this.Battleships)
            {
                foreach (var c in aShip.coords)
                {
                    if (c.Row == attackedCoordinates.Row && c.Column == attackedCoordinates.Column)
                    {
                        ship = aShip;
                        break;
                    }
                }
            }
            //Increment the hit counter
            ship.Hits++;

            //Call out a hit
            Console.WriteLine(Name + " says: \"Hit!\"");

            //If the ship is now sunk, call out which ship was sunk
            if (ship.IsSunk)
            {
                Console.WriteLine(Name + " says: \"You sunk my " + ship.Name + "!\"");
            }

            //For either a hit or a sunk, return a Hit status
            return(ShotResult.Hit);
        }
Example #2
0
        public List <BoardCell> PlaceShips(List <BoardCell> boardCells, int shipWidth)
        {
            Random rand = new Random(Guid.NewGuid().GetHashCode());
            //foreach (var ship in Battleships)
            //{
            bool isOpen = true;

            while (isOpen)
            {
                var startcolumn = rand.Next(1, 11);
                var startrow = rand.Next(1, 11);
                int endrow = startrow, endcolumn = startcolumn;
                var orientation = rand.Next(1, 101) % 2;     //0 for Horizontal

                List <int> cellNumbers = new List <int>();
                if (orientation == 0)
                {
                    for (int i = 1; i < shipWidth; i++)
                    {
                        endrow++;
                    }
                }
                else
                {
                    for (int i = 1; i < shipWidth; i++)
                    {
                        endcolumn++;
                    }
                }

                //We cannot place ships beyond the boundaries of the board
                if (endrow > 10 || endcolumn > 10)
                {
                    isOpen = true;
                    continue;     //Restart the while loop to select a new random boardcells
                }

                //Check if specified boardcells are occupied
                var affectedBoardCells = Range(boardCells, startrow,
                                               startcolumn,
                                               endrow,
                                               endcolumn);

                if (affectedBoardCells.Any(x => x.IsOccupied))
                {
                    isOpen = true;
                    continue;
                }

                isOpen = false;
                var listShipCoords = new List <Coordinates>();
                foreach (var boardCell in affectedBoardCells)
                {
                    listShipCoords.Add(boardCell.Coordinates);
                    boardCell.IsOccupied = true;
                }
                var playerShip = new Battleship(listShipCoords);
                this.Battleships = new List <Battleship>();
                this.Battleships.Add(playerShip);
            }
            //}

            return(boardCells);
        }