Imports Microsoft.VisualBasic
Dim compBoard(10,10) As Char
Dim userBoard(10,10) As Char
Dim guessBoard(10,10)As Char
Dim userHits As Integer = 0
Dim compHits As Integer = 0
EmptyBoards(userBoard, compBoard, guessBoard)
PlaceUserShips(userboard)
PlaceCompShips(compBoard)
While userHits < 17 Or compHits < 17
Console.writeline("you have " & userHits & " succesful hits and your opponent has " & compHits & " successful hits")
If UserMove(compBoard, guessBoard) = True Then
If CompMove(userBoard) = True Then
Console.writeline("Congratulations!")
ElseIf compHits = 17 Then
Console.writeline("Better luck next time!")
Console.writeline("Error, this message shouldn't show")
Sub PlaceUserShips(ByRef userBoard(,) As Char)
Dim remainingShips() As Integer = {2, 3, 3, 4, 5}
Dim counter As Integer = 0
Dim startCoordinate As String
While remainingShips(4) <> 0
Console.writeline("Ship length = " & remainingShips(counter))
Console.writeline("Choose orientation of ship ('V' for vertical, 'H' for horizontal)")
orientation = console.readline()
orientation = UCase(orientation)
Console.writeline("Choose starting co-ordinate for ship (In the form x,y)")
Console.writeline("Vertical ships start at the lowest point")
Console.writeline("Horizontal ships start at the leftmost point")
startCoordinate = console.readline()
If Left(startCoordinate, 1) = "0" or Right(startCoordinate, 1) = "0" or (orientation <> "V" and orientation <> "H") Then
If CType(Left(startCoordinate, 1), Char) = "a" Then
x = CType(Left(startCoordinate, 1), Integer)
If CType(Right(startCoordinate, 1), Char) = "a" Then
y = CType(Right(startCoordinate, 1), Integer)
If orientation = "V" Then
For yCounter = y to y - (remainingShips(counter) - 1) step - 1
If userBoard(x, yCounter) <> "e" Then
For yCounter = y to y - (remainingShips(counter) - 1) step - 1
userBoard(x, yCounter) = "s"
If orientation = "H" Then
For xCounter = x to x + (remainingShips(counter) - 1) step 1
If userBoard(xCounter, y) <> "e" Then
For xCounter = x to x + (remainingShips(counter) - 1) step 1
userBoard(xCounter, y) = "s"
remainingShips(counter) = 0
Else If fireWall = False Then
Console.writeline("Invalid position, try again")
Sub PlaceCompShips(ByRef compBoard(,) As Char)
Dim remainingShips() As Integer = {2, 3, 3, 4, 5}
Dim counter As Integer = 0
Dim orientation As String = "q"
While remainingShips(4) <> 0
If orientation = "V" Then
For yCounter = y to y - (remainingShips(counter) - 1) step - 1
If yCounter < 11 & 0 < yCounter Then
If compBoard(x, yCounter) <> "e" Then
For yCounter = y to y - (remainingShips(counter) - 1) step - 1
compBoard(x, ycounter) = "s"
If orientation = "H" Then
For xCounter = x to x + (remainingShips(counter) - 1) step 1
If xCounter < 11 And 0 < xCounter Then
If compBoard(xCounter, y) <> "e" Then
For xCounter = x to x + (remainingShips(counter) - 1) step 1
compBoard(xCounter, y) = "s"
remainingShips(counter) = 0
Function CheckBoard(board(,) As Char)
Dim fireWall As Boolean = False
Function UserMove(ByRef compBoard(,) As Char, ByRef guessBoard(,) As Char) As Boolean
Dim fireWall As Boolean = False
Dim fireWall2 As Boolean = False
Console.writeline("Choose x co-ordinate to shoot")
xString = console.readline()
ElseIf xString <> "" Then
If (CType(xString, Integer) > 0 and CType(xString, Integer) < 11) Then
x = CType(xString, Integer)
Console.writeline("Invalid, try again")
Console.writeline("Choose y co-ordinate to shoot")
yString = console.readline()
ElseIf yString <> "" Then
If (CType(yString, Integer) > 0 and CType(yString, Integer) < 11) Then
y = CType(yString, Integer)
Console.writeline("Invalid, try again")
If compBoard(x,y) = "e" Then
Console.writeline("Miss!")
Else If compBoard(x,y) = "s"
Console.Writeline("Hit!")
ElseIf compBoard(x,y) = "m" or compBoard(x,y) = "h" Then
Console.writeline("co-ordinate already attempted, try again")
Function CompMove(ByRef userBoard(,) As Char) As Boolean
Dim fireWall As Boolean = False
If userBoard(x,y) = "e" Then
Console.writeline("Computer has missed!")
Console.writeline("Your board now looks like...")
Else If userBoard(x,y) = "s"
Console.Writeline("Computer has landed a hit!")
Console.writeline("Your board now looks like...")
sub PrintBoard(board(,) As Char)
ElseIf board(x,y) = "s" Then
ElseIf board(x,y) = "m" Then
ElseIf board(x,y) = "h" Then
Console.Write(board(x,y))
Sub EmptyBoards(ByRef userBoard(,) As Char, ByRef compBoard(,) As Char, ByRef guessBoard(,) As Char)
Public Function GetRandom(ByVal Min As Integer, ByVal Max As Integer) As Integer
return (Max - Min) * Rnd() + Min