Example #1
0
        public int BoardCellAttacked(Coordinate location)
        {
            if (location.X < 0 || location.X > boardSize || location.Y < 0 || location.Y > boardSize)
            {
                ReportTool.WriteLine($" Type: Wrong Position {location} ");
                return(-1);
            }

            BoardCell  cell = Board[location.X, location.Y];
            Battleship ship;

            switch (cell.State)
            {
            case CoordinateState.OCCUPIED:
                cell.State = CoordinateState.HIT;
                ship       = BattleShips[cell.ShipNumber];
                ToCheckShipIsSunk(ship);
                ReportCellState("Hit", location);
                break;

            case CoordinateState.HIT:
                ship = BattleShips[cell.ShipNumber];
                if (ship.IsSunk)
                {
                    ReportCellState("Missed", location);      // Ship was sunk.
                }
                else
                {
                    ReportCellState("Hit", location);         // Hit the previous place, but ship isn't sunk.
                }
                break;

            default:
                ReportCellState("Missed", location);
                break;
            }
            return(cell.ShipNumber);
        }