I've been stuck on this for so long, i have no idea how to fix the bugs that keep occurring. Please could someone point out the bugs i have made and how to fix them.
Code:
Sub draw_ship(lengthy, board)
Dim completed As Integer
Dim sum As Integer
Dim i As Integer
Dim iy As Integer
Dim iend As Integer
Randomize
completed = 0
Do While (completed = 0)
ix = Int((((9 - lengthy)) - 0 + 1) * Rnd() + 0)
iy = Int((9 - 0 + 1) * Rnd() + 0)
sum = 0
iend = ix + lengthy - 1
For i = ix To iend Step 1
sum = sum + board(iy, i)
Next i
If (sum = 0) Then
Rem paint in ship
For i = ix To iend Step 1
board(iy, i) = lengthy
Next i
completed = 1
End If
Loop
End Sub
Sub pick_a_square(ix, y)
Dim inputted As Integer
inputted = InputBox("Please enter 2 coord digits")
iy = Int(inputted / 10#)
ix = inputted Mod 10
End Sub
Sub battleships()
Dim board(10, 10) As Integer
Dim i As Integer
Dim j As Integer
Dim ix As Integer
Dim iy As Integer
Dim ship As Integer
Dim totally(5) As Integer
Dim fleet(4) As Integer
Dim score As Integer
Rem initialize the boards
For j = 0 To 9 Step 1
For i = 0 To 9 Step 1
board(j, i) = 0
Cells(3 + j, 8 + i).Value = " "
Next i
Next j
Rem now initialise the fleet No. of squares not sunk
fleet(4) = 1 * 5 ' aircraft carrier 5 squares long
fleet(3) = 2 * 4 ' battleship 4 squares long
fleet(2) = 3 * 3 ' frigate 3 squares long
fleet(1) = 4 * 2 ' mine sweeper 2 squares long
Rem now let VBA fill up the board with ships
For i = 1 To 10
If (i = 1) Then ' Aircraft Carrier
ship = 5
Call draw_ship(ship, bored)
End If
If ((i >= 2) And (i <= 3)) Then ' Battleship
ship = 4
Call draw_ship(ship, board)
End If
If ((i >= 4) And (i <= 6)) Then ' Frigate
ship = 3
Call draw_ship(ship, board)
End If
If ((i >= 7) And (i <= 20)) Then ' Mine Sweeper
ship = 2
Call draw_ship(ship, board)
End If
Next i
Rem now we let the human user go and blow up all the ships
score = 0
Do While (score < 30)
Call pick_a_square(ix, iy)
ship = board(iy, ix)
If (ship > 0) Then
Cells(3 + iy, 8 + ix).Value = "X"
score = score + 1
Else
Cells(3 + iy, 8 + ix).Value = "-"
End If
Cells(13, 4).Value = score
If (ship < 0) Then fleet(ship) = fleet(ship) - 1
For i = 2 To 5 Step 1
totally(i) = fleet(i) / i
Next i
Cells(5, 4).Value = totally(3) ' Aircraft carrier
Cells(7, 4).Value = totally(4) ' Battleship
Cells(9, 4).Value = totally(3) ' Frigate
Cells(11, 4).Value = totally(2) ' Mine Sweeper
Loop
End Sub
Last edited by a moderator: