Help with battleships VBA code

zakwilce

New Member
Joined
Apr 19, 2018
Messages
5
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:

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Why dont you tell us the bugs you know of, ie what is happening with the code that you are not expecting?
You don't go to the doctor and expect him to make you well without telling him about your illness do you?
You don't take a car to a garage and expect them to fix all the problems without telling them what's wrong with it do you?
 
Upvote 0
So when i run the code, a bug that is being highlighted is "sum = sum + board(iy, i)". It says it is a type mismatch. This code is part of the sub for drawing the ships
 
Upvote 0
Since you've already DIMmed sum as an integer then board(iy, i) must be text
You haven't yet DIMmed board, perhaps it assumes string ?
Try DIMming board in the first set of DIM statements
 
Upvote 0
Yeah, sorry i changed that now. My main problem is when i'm inputting the coordinates. It only inputs coordinates on the first row of the board, i,e if i put coordinates (0,1), it thinks it's the same as (1,1) or (2,1) etc...
 
Upvote 0
What happens if you put that subrouitine further down the code?
So the DIM for board is near the top and encountered first?
 
Upvote 0
Put "Option Explicit" at the top of your code module then try running it; it will help you.

WBD
 
Upvote 0
What's this?

Code:
  iy = Int(inputted / 10#)

Am no VBA expert but what's that hash sign doing there?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,644
Messages
6,120,709
Members
448,983
Latest member
Joaquim_Baptista

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top