Advertisement
Guest User

Defector

a guest
Jan 17th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. Option Strict On
  2.  
  3. Module Module1
  4.  
  5. Sub Main()
  6. Dim gridSize As Integer
  7. Dim valid As Boolean = False
  8. Dim GSinput As String
  9.  
  10.  
  11.  
  12. While valid = False
  13. Console.WriteLine("Grid size?")
  14. GSinput = Console.ReadLine
  15. If Integer.TryParse(GSinput, gridSize) Then
  16. 'Tests if GSinput is an integer
  17. If gridSize > 9 And gridSize < 27 Then
  18. valid = True
  19. End If
  20. End If
  21. End While
  22.  
  23. Dim hidden_grid(gridSize, gridSize) As Integer
  24. Dim user_grid(gridSize, gridSize) As Integer
  25. Dim tempLine As String
  26. Dim input As String
  27.  
  28. 'Allows grid to be scaled from 10 to 26
  29. For i = 1 To gridSize
  30. hidden_grid(0, i) = Asc("A") + i - 1
  31. hidden_grid(i, 0) = i
  32. '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
  33. Next
  34.  
  35. hidden_grid = placeShips(hidden_grid, gridSize)
  36.  
  37. While (True)
  38. Console.Clear()
  39. tempLine = ""
  40. For topRowIndex = 0 To gridSize
  41. tempLine += "|" & CStr(Chr(hidden_grid(0, topRowIndex))).PadRight(4)
  42. '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)'
  43. Next
  44. Console.WriteLine(tempLine & "|")
  45. 'Writes tempLine
  46. tempLine = ""
  47. For i = 0 To gridSize
  48. tempLine += "_____"
  49. Next
  50. Console.WriteLine(tempLine)
  51. 'Allows lines for grid to scale
  52. For row = 1 To gridSize
  53. tempLine = ""
  54. tempLine += "|" & CStr(hidden_grid(row, 0)).PadRight(4)
  55. 'For the first column, inputs each number from hidden_grid into tempLine ready for writing
  56. For column = 1 To gridSize
  57. If hidden_grid(row, column) >= Asc("A") Then
  58. tempLine += "|" & CStr(Chr(hidden_grid(row, column))).PadRight(4)
  59. 'Ensures that the grid only displays charachters with an Ascii value above that of 'A' in each column
  60. Else
  61. tempLine += "|" & " "
  62. 'Otherwise, it writes a blank space
  63. End If
  64. Next
  65. Console.WriteLine(tempLine & "|")
  66. tempLine = ""
  67. For i = 0 To gridSize
  68. tempLine += "_____"
  69. Next
  70. Console.WriteLine(tempLine)
  71. Next
  72. Console.WriteLine("Enter the column letter")
  73. input = Console.ReadLine.ToUpper
  74. 'Converts to upper case so that program is harder to break
  75. For TopRowIndex = 0 To gridSize - 1
  76. If hidden_grid(0, TopRowIndex + 1) = Asc(input) Then
  77. Console.WriteLine("Enter the row ")
  78. input = Console.ReadLine
  79. For leftColumnIndex = 0 To gridSize - 1
  80. If hidden_grid(leftColumnIndex + 1, 0) = CDbl(input) Then
  81. Console.WriteLine("Enter option ")
  82. input = Console.ReadLine
  83. hidden_grid(leftColumnIndex + 1, TopRowIndex + 1) = Asc(input)
  84. Exit For
  85. End If
  86. Next
  87. End If
  88. Next
  89. End While
  90. Console.ReadLine()
  91.  
  92.  
  93. End Sub
  94.  
  95. Function placeShips(ByRef hidden_grid As Integer(,), ByVal gridSize As Integer) As Integer(,)
  96. Dim shipsToBePlaced((CInt(gridSize ^ 2 / 100) * 4) - 1) As Integer
  97. Dim xAdd, yAdd, x, y, direction, spaceEmpty As Integer
  98. Dim foundPlace As Boolean = False
  99. Dim randomNumber As New Random()
  100. Dim count As Integer = 0
  101.  
  102. For i = 2 To 5
  103. For x = 1 To (CInt(gridSize ^ 2 / 100))
  104. shipsToBePlaced(count) = i
  105. count += 1
  106. Next
  107. Next
  108.  
  109. For Each ship In shipsToBePlaced
  110. foundPlace = False
  111.  
  112. While foundPlace = False
  113. x = randomNumber.Next(1, gridSize)
  114. y = randomNumber.Next(1, gridSize)
  115. xAdd = 0
  116. yAdd = 0
  117. If hidden_grid(x, y) = 0 Then
  118. direction = randomNumber.Next(1, 5)
  119. Select Case direction
  120. Case 1
  121. xAdd = 1
  122. Case 2
  123. xAdd = -1
  124. Case 3
  125. yAdd = 1
  126. Case 4
  127. yAdd = -1
  128. End Select
  129. For i = 1 To ship
  130.  
  131. Next
  132. End If
  133.  
  134. End While
  135.  
  136. Next
  137.  
  138.  
  139. Return hidden_grid
  140.  
  141.  
  142.  
  143. End Function
  144. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement