Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27513

VS 2008 Battleships

$
0
0
I have to create a simple battleships game. this involves the console choosing random places to place ships on a (10,10) grid. my code is very random, however the question i have is with the do until grid(firex,firey) = blablabla. this is because it is saying that the conversion from string to integer is not valid. i have tried many things, for example what you can see.
this is my code.
Sub Main()
Dim grid(9, 9) As Integer
Dim counter As Integer
Dim battleshipx As Integer
Dim battleshipy As Integer
Dim battleshipx2 As Integer
Dim battleshipy2 As Integer
Dim battleshipx3 As Integer
Dim battleshipy3 As Integer
Dim battleshipx4 As Integer
Dim battleshipy4 As Integer
Dim subx As Integer
Dim suby As Integer
Dim subx2 As Integer
Dim suby2 As Integer
Dim dingyx As Integer
Dim dingyy As Integer
Dim hit As Integer = 0
Dim miss As Integer = 0
Dim hitbattle As Integer = 0
Dim hitdingy As Integer = 0
Dim hitsub As Integer = 0
Dim number As Integer
Dim firey As String
Dim firex As String

For counter = 0 To 9
grid(counter, 0) = 0
grid(counter, 1) = 0
grid(counter, 2) = 0
grid(counter, 3) = 0
grid(counter, 4) = 0
grid(counter, 5) = 0
grid(counter, 6) = 0
grid(counter, 7) = 0
grid(counter, 8) = 0
grid(counter, 9) = 0

Next
Randomize()
For counter = 1 To 1
battleshipx = Int(Rnd() * 9) + 1
battleshipy = Int(Rnd() * 9) + 1
subx = Int(Rnd() * 9) + 1
suby = Int(Rnd() * 9) + 1
dingyx = Int(Rnd() * 9) + 1
dingyy = Int(Rnd() * 9) + 1
Next

If battleshipx > 6 Then
battleshipx2 = battleshipx - 1
battleshipx3 = battleshipx - 2
battleshipx4 = battleshipx - 3
ElseIf battleshipx < 6 Then
battleshipx2 = battleshipx + 1
battleshipx3 = battleshipx + 2
battleshipx4 = battleshipx + 3
ElseIf battleshipx = 6 & battleshipy > 6 Then
battleshipy2 = battleshipy - 1
battleshipy3 = battleshipy - 2
battleshipy4 = battleshipy - 3
Else
battleshipy2 = battleshipy + 1
battleshipy3 = battleshipy + 2
battleshipy3 = battleshipy + 3
End If

If subx > 8 Then
subx2 = subx - 1
ElseIf subx < 8 Then
subx2 = subx + 1
ElseIf subx = 8 & suby > 8 Then
suby2 = suby - 1
Else
suby2 = suby + 1
End If

grid(battleshipx, battleshipy) = 1
grid(battleshipx2, battleshipy) = 1
grid(battleshipx3, battleshipy) = 1
grid(battleshipx4, battleshipy) = 1
grid(battleshipy2, battleshipx) = 1
grid(battleshipy3, battleshipx) = 1
grid(battleshipy4, battleshipx) = 1

grid(subx, suby) = 2
grid(subx2, suby) = 2
grid(suby2, subx) = 2

grid(dingyx, dingyy) = 3

Do Until hitsub = 2 And hitbattle = 4 And hitdingy = 1
Console.WriteLine("please choose a co-ordinate to fire your missile, using the #,# format")
number = Console.ReadLine()
number = CStr(number)
firex = (Mid(number, 1, 1))
firey = (Mid(number, 3, 1))

battleshipx = CInt(battleshipx)
battleshipx2 = CInt(battleshipx2)
battleshipx3 = CInt(battleshipx3)
battleshipx4 = CInt(battleshipx4)
battleshipy = CInt(battleshipy)
battleshipy2 = CInt(battleshipy2)
battleshipy3 = CInt(battleshipy3)
battleshipy4 = CInt(battleshipy4)
subx = CInt(subx)
suby = CInt(suby)
subx2 = CInt(subx2)
suby2 = CInt(suby2)
dingyx = CInt(dingyx)
dingyy = CInt(dingyy)

If grid(firex, firey) = grid(battleshipx, battleshipy) Or grid(battleshipx2, battleshipy) Or grid(battleshipx3, battleshipy) Or grid(battleshipx4, battleshipy) Or grid(battleshipx, battleshipy2) Or grid(battleshipx, battleshipy3) Or grid(battleshipx, battleshipy4) Then
Console.WriteLine("hit")
hit = hit + 1
hitbattle = hitbattle + 1
ElseIf grid(firex, firey) = grid(subx, suby) Or grid(subx2, suby) Or grid(suby2, subx) Then
Console.WriteLine("hit")
hit = hit + 1
hitsub = hitsub + 1
ElseIf grid(firex, firey) = grid(dingyx, dingyy) Then
Console.WriteLine("hit")
hit = hit + 1
hitdingy = hitdingy + 1
Else
Console.WriteLine("miss")
miss = miss + 1

End If





Loop
Console.WriteLine("you had " & miss & " misses")
Console.ReadLine()

End Sub

Viewing all articles
Browse latest Browse all 27513

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>