Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Option Strict On
- Module Module1
- Sub Main()
- Dim gridSize As Integer
- Dim valid As Boolean = False
- Dim GSinput As String
- While valid = False
- Console.WriteLine("Grid size?")
- GSinput = Console.ReadLine
- If Integer.TryParse(GSinput, gridSize) Then
- 'Tests if GSinput is an integer
- If gridSize > 9 And gridSize < 27 Then
- valid = True
- End If
- End If
- End While
- Dim hidden_grid(gridSize, gridSize) As Integer
- Dim user_grid(gridSize, gridSize) As Integer
- Dim tempLine As String
- Dim input As String
- 'Allows grid to be scaled from 10 to 26
- For i = 1 To gridSize
- hidden_grid(0, i) = Asc("A") + i - 1
- hidden_grid(i, 0) = i
- 'Loops from 1 to 10. It adds the Ascii code for A + i - 1 to each element in the first row of the hidden_grid array and i to each element in the first column of the hidden_grid array
- Next
- hidden_grid = placeShips(hidden_grid, gridSize)
- While (True)
- Console.Clear()
- tempLine = ""
- For topRowIndex = 0 To gridSize
- tempLine += "|" & CStr(Chr(hidden_grid(0, topRowIndex))).PadRight(4)
- 'For the top row, inputs the Ascii to character conversion into tempLine ready for writing (A, B, C, ect.) and ensures that it takes up four spaces '.padright(4)'
- Next
- Console.WriteLine(tempLine & "|")
- 'Writes tempLine
- tempLine = ""
- For i = 0 To gridSize
- tempLine += "_____"
- Next
- Console.WriteLine(tempLine)
- 'Allows lines for grid to scale
- For row = 1 To gridSize
- tempLine = ""
- tempLine += "|" & CStr(hidden_grid(row, 0)).PadRight(4)
- 'For the first column, inputs each number from hidden_grid into tempLine ready for writing
- For column = 1 To gridSize
- If hidden_grid(row, column) >= Asc("A") Then
- tempLine += "|" & CStr(Chr(hidden_grid(row, column))).PadRight(4)
- 'Ensures that the grid only displays charachters with an Ascii value above that of 'A' in each column
- Else
- tempLine += "|" & " "
- 'Otherwise, it writes a blank space
- End If
- Next
- Console.WriteLine(tempLine & "|")
- tempLine = ""
- For i = 0 To gridSize
- tempLine += "_____"
- Next
- Console.WriteLine(tempLine)
- Next
- Console.WriteLine("Enter the column letter")
- input = Console.ReadLine.ToUpper
- 'Converts to upper case so that program is harder to break
- For TopRowIndex = 0 To gridSize - 1
- If hidden_grid(0, TopRowIndex + 1) = Asc(input) Then
- Console.WriteLine("Enter the row ")
- input = Console.ReadLine
- For leftColumnIndex = 0 To gridSize - 1
- If hidden_grid(leftColumnIndex + 1, 0) = CDbl(input) Then
- Console.WriteLine("Enter option ")
- input = Console.ReadLine
- hidden_grid(leftColumnIndex + 1, TopRowIndex + 1) = Asc(input)
- Exit For
- End If
- Next
- End If
- Next
- End While
- Console.ReadLine()
- End Sub
- Function placeShips(ByRef hidden_grid As Integer(,), ByVal gridSize As Integer) As Integer(,)
- Dim shipsToBePlaced((CInt(gridSize ^ 2 / 100) * 4) - 1) As Integer
- Dim xAdd, yAdd, x, y, direction, spaceEmpty As Integer
- Dim foundPlace As Boolean = False
- Dim randomNumber As New Random()
- Dim count As Integer = 0
- For i = 2 To 5
- For x = 1 To (CInt(gridSize ^ 2 / 100))
- shipsToBePlaced(count) = i
- count += 1
- Next
- Next
- For Each ship In shipsToBePlaced
- foundPlace = False
- While foundPlace = False
- x = randomNumber.Next(1, gridSize)
- y = randomNumber.Next(1, gridSize)
- xAdd = 0
- yAdd = 0
- If hidden_grid(x, y) = 0 Then
- direction = randomNumber.Next(1, 5)
- Select Case direction
- Case 1
- xAdd = 1
- Case 2
- xAdd = -1
- Case 3
- yAdd = 1
- Case 4
- yAdd = -1
- End Select
- For i = 1 To ship
- Next
- End If
- End While
- Next
- Return hidden_grid
- End Function
- End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement