Beispiel #1
0
        public void ReportBoardState()
        {
            for (var x = 0; x < boardSize; x++)
            {
                for (var y = 0; y < boardSize; y++)
                {
                    BoardCell cell = Board[x, y];

                    ReportTool.Write($"{cell.State.ToString().Substring(0, 3)} ");
                }
                ReportTool.WriteLine("");
            }
        }
Beispiel #2
0
        public void ReportCellState(string type, Coordinate location)
        {
            BoardCell cell = Board[location.X, location.Y];

            if (cell.ShipNumber > 0)
            {
                ReportTool.WriteLine($" Type: {type} {location} {cell}");
            }
            else
            {
                ReportTool.WriteLine($" Type: {type} {location} ");         // shipNumber <=0 should be treated as exception
            }
        }
Beispiel #3
0
        public Boolean AddAShipToBoard(Battleship ship)
        {
            if (IsBoardReadyToPlay || ship.ShipNumber <= 0)
            {
                return(false);
            }

            if (BattleShips.ContainsKey(ship.ShipNumber))
            {
                ReportTool.WriteLine("ShipNumber is already in board.");
                return(false);
            }

            if (ship.Deployment.Count > boardSize || ship.Deployment.Count <= 0)
            {
                ReportTool.WriteLine("Ship must fit entirely on the board");
                return(false);
            }

            if (!ship.IsValidDeployment())
            {
                ReportTool.WriteLine("The ship should be 1-by-n sized");
                return(false);
            }

            // check if ship position had been occupied by another ship
            foreach (Coordinate location in ship.Deployment)
            {
                if (Board[location.X, location.Y].State == CoordinateState.OCCUPIED)
                {
                    ReportTool.WriteLine("Ship can't overlap another ship. {location}");
                    return(false);
                }
            }

            // put ship on the board
            foreach (Coordinate location in ship.Deployment)
            {
                BoardCell cell = Board[location.X, location.Y];
                cell.State      = CoordinateState.OCCUPIED;
                cell.ShipNumber = ship.ShipNumber;
            }

            BattleShips.Add(ship.ShipNumber, ship);
            return(true);
        }
Beispiel #4
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);
        }