Advertisement
L0c4710n

stuff i dont remember which one is that (master)

Jan 19th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 6.82 KB | None | 0 0
  1. Structure Variables
  2.     Public loop_game, loop_spawn As Integer ' Loop variables
  3. End Structure
  4.  
  5. Module Module1
  6.  
  7.     Function Checker(ByRef Spawn_Possible As Boolean, BShip_Pos1 As Integer, BShip_Pos2 As Integer, Direction As Integer, Lenght As Integer, GridID(,) As Integer)
  8.         ' Checks if ship doesn't overlap other non-empty cells
  9.         Dim temp As Integer
  10.         If GridID(BShip_Pos1, BShip_Pos2) = 0 Then
  11.             temp += 1
  12.         Else
  13.             Direction = 5 ' Prevents from going on to the If statements, if the Base Ship is not available
  14.         End If
  15.         For i = 1 To Lenght
  16.             If Direction = 1 Then ' Overflow bug
  17.                 If BShip_Pos1 + i <= 10 And BShip_Pos2 + i <= 10 Then
  18.                     If GridID(BShip_Pos1 + i, BShip_Pos2) = 0 Then  ' Down
  19.                         temp += 1
  20.                     End If
  21.                 End If
  22.             ElseIf Direction = 2 Then
  23.                 If BShip_Pos1 + i <= 10 And BShip_Pos2 + i <= 10 Then
  24.                     If GridID(BShip_Pos1, BShip_Pos2 + i) = 0 Then ' Right
  25.                         temp += 1
  26.                     End If
  27.                 End If
  28.             ElseIf Direction = 3 Then
  29.                 If BShip_Pos1 - i >= 1 And BShip_Pos2 - i >= 1 Then
  30.                     If GridID(BShip_Pos1 - i, BShip_Pos2) = 0 Then  ' Up
  31.                         temp += 1
  32.                     End If
  33.                 End If
  34.             ElseIf Direction = 4 Then
  35.                 If BShip_Pos1 - i >= 1 And BShip_Pos2 - i >= 1 Then
  36.                     If GridID(BShip_Pos1, BShip_Pos2 - i) = 0 Then ' Left
  37.                         temp += 1
  38.                     End If
  39.                 End If
  40.             Else
  41.                 Spawn_Possible = False
  42.             End If
  43.         Next
  44.         If temp = Lenght Then
  45.             Spawn_Possible = True
  46.         Else
  47.             Spawn_Possible = False
  48.         End If
  49.         temp = 0
  50.         Return Spawn_Possible
  51.     End Function
  52.  
  53.     Function IDConverter(ByRef GridID(,) As Integer, ByRef Pos1 As Integer, ByRef Pos2 As Integer)
  54.         ' Converts ID's into string values for ease of use.
  55.         Dim grid(10, 10) As String
  56.         If GridID(Pos1, Pos2) = 0 Then
  57.             grid(Pos1, Pos2) = "  "
  58.         ElseIf GridID(Pos1, Pos2) = 1 Then
  59.             grid(Pos1, Pos2) = "SH"
  60.         ElseIf GridID(Pos1, Pos2) = 2 Then
  61.             grid(Pos1, Pos2) = "X "
  62.         ElseIf GridID(Pos1, Pos2) = 3 Then
  63.             grid(Pos1, Pos2) = "S "
  64.         ElseIf GridID(Pos1, Pos2) = 4 Then
  65.             grid(Pos1, Pos2) = "M "
  66.             ' 5 is boundaries
  67.         End If
  68.         Return grid(Pos1, Pos2)
  69.     End Function
  70.     Sub Main()
  71.         Randomize()
  72.         Dim v As Variables
  73.         Dim grid(10, 10) As String
  74.         Dim gridID(10, 10) As Integer
  75.         v.loop_game = 1
  76.         Call Setup(v, grid, gridID)
  77.  
  78.     End Sub
  79.     Sub Game(v, grid, gridID)
  80.         Do While v.loop_Game = 1
  81.             For r = 1 To 10
  82.                 For c = 1 To 10
  83.                     Dim pos1 As Integer = r
  84.                     Dim pos2 As Integer = c
  85.                     grid(r, c) = IDConverter(gridID, pos1, pos2)
  86.                 Next
  87.             Next
  88.             Console.WriteLine(" ______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ")
  89.             For r = 0 To 10
  90.                 Console.WriteLine("|      |      |      |      |      |      |      |      |      |      |      |")
  91.                 For c = 0 To 10
  92.                     Console.Write("|  " & grid(r, c) & "  ")
  93.                 Next
  94.                 Console.WriteLine("|")
  95.                 For i = 0 To 10
  96.                     Console.Write("|______")
  97.                 Next
  98.  
  99.                 Console.WriteLine("|")
  100.             Next
  101.             Dim q = Console.ReadLine()
  102.  
  103.             If q = "attac" Then
  104.  
  105.             ElseIf q = "exit" Then
  106.                 v.loop_Game = 0
  107.             Else
  108.                 Console.Clear()
  109.             End If
  110.         Loop
  111.     End Sub
  112.     Sub Setup(v, grid, gridID)
  113.         grid(0, 0) = "  "  ' The first empty cell
  114.         For r = 1 To 10          ' -----------//LABELING START\\-------------
  115.             gridID(r, 0) = 5
  116.             If r = 10 Then
  117.                 grid(r, 0) = r
  118.             Else
  119.                 grid(r, 0) = r & " "
  120.             End If
  121.         Next
  122.         For c = 1 To 10
  123.             If c = 10 Then
  124.                 gridID(0, c) = 5
  125.                 grid(0, c) = c
  126.             Else
  127.                 grid(0, c) = c & " "
  128.             End If
  129.         Next                    ' -------------//LABELING END\\--------------
  130.  
  131.         Call ShipGen(v, grid, gridID)
  132.     End Sub
  133.     Sub ShipGen(v, grid, gridID)
  134.         ' Generates ships
  135.         Dim rand, rand2 As New Random
  136.         Dim Ship_count As Integer = 0
  137.         Dim DoubleCount As Integer
  138.         Dim TripleCount As Integer
  139.         Dim QuadCount As Integer
  140.         Dim Spawn_Possible As Boolean
  141.         DoubleCount = 0
  142.         TripleCount = 0
  143.         QuadCount = 0
  144.  
  145.         Do While QuadCount < 2
  146.             Dim BShip_Pos1, BShip_Pos2 As Integer
  147.             BShip_Pos1 = rand.Next(1, 10)
  148.             BShip_Pos2 = rand.Next(1, 10)
  149.             Dim Lenght As Integer
  150.             Dim Direction As Integer = rand2.Next(1, 4)
  151.             If DoubleCount < 2 Then
  152.                 Lenght = 2
  153.                 If Checker(Spawn_Possible, BShip_Pos1, BShip_Pos2, Lenght, Direction, gridID) = True Then
  154.                     Call Ship_Placement(Lenght, BShip_Pos1, BShip_Pos2, Direction, gridID)
  155.                     DoubleCount += 1
  156.                 End If
  157.             ElseIf TripleCount < 2 Then
  158.                 Lenght = 3
  159.                 If Checker(Spawn_Possible, BShip_Pos1, BShip_Pos2, Lenght, Direction, gridID) = True Then
  160.                     Call Ship_Placement(Lenght, BShip_Pos1, BShip_Pos2, Direction, gridID)
  161.                     TripleCount += 1
  162.                 End If
  163.             ElseIf QuadCount < 2 Then
  164.                 Lenght = 4
  165.                 If Checker(Spawn_Possible, BShip_Pos1, BShip_Pos2, Lenght, Direction, gridID) = True Then
  166.                     Call Ship_Placement(Lenght, BShip_Pos1, BShip_Pos2, Direction, gridID)
  167.                     QuadCount += 1
  168.                 End If
  169.             End If
  170.         Loop
  171.         Call Game(v, grid, gridID)
  172.     End Sub
  173.     Sub Ship_Placement(Lenght, BShip_Pos1, BShip_Pos2, Direction, gridID)
  174.         For i = 0 To Lenght - 1
  175.             If Direction = 1 Then     'Down
  176.                 gridID(BShip_Pos1 + i, BShip_Pos2) = 1
  177.             ElseIf Direction = 2 Then 'Right
  178.                 gridID(BShip_Pos1, BShip_Pos2 + i) = 1
  179.             ElseIf Direction = 3 Then 'Up
  180.                 gridID(BShip_Pos1 - i, BShip_Pos2) = 1
  181.             ElseIf Direction = 4 Then 'Left
  182.                 gridID(BShip_Pos1, BShip_Pos2 - i) = 1
  183.             End If
  184.         Next
  185.     End Sub
  186. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement