Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Structure Variables
- Public loop_game, loop_spawn As Integer ' Loop variables
- End Structure
- Module Module1
- Function Checker(ByRef Spawn_Possible As Boolean, BShip_Pos1 As Integer, BShip_Pos2 As Integer, Direction As Integer, Lenght As Integer, GridID(,) As Integer)
- ' Checks if ship doesn't overlap other non-empty cells
- Dim temp As Integer
- If GridID(BShip_Pos1, BShip_Pos2) = 0 Then
- temp += 1
- Else
- Direction = 5 ' Prevents from going on to the If statements, if the Base Ship is not available
- End If
- For i = 1 To Lenght
- If Direction = 1 Then ' Overflow bug
- If BShip_Pos1 + i <= 10 And BShip_Pos2 + i <= 10 Then
- If GridID(BShip_Pos1 + i, BShip_Pos2) = 0 Then ' Down
- temp += 1
- End If
- End If
- ElseIf Direction = 2 Then
- If BShip_Pos1 + i <= 10 And BShip_Pos2 + i <= 10 Then
- If GridID(BShip_Pos1, BShip_Pos2 + i) = 0 Then ' Right
- temp += 1
- End If
- End If
- ElseIf Direction = 3 Then
- If BShip_Pos1 - i >= 1 And BShip_Pos2 - i >= 1 Then
- If GridID(BShip_Pos1 - i, BShip_Pos2) = 0 Then ' Up
- temp += 1
- End If
- End If
- ElseIf Direction = 4 Then
- If BShip_Pos1 - i >= 1 And BShip_Pos2 - i >= 1 Then
- If GridID(BShip_Pos1, BShip_Pos2 - i) = 0 Then ' Left
- temp += 1
- End If
- End If
- Else
- Spawn_Possible = False
- End If
- Next
- If temp = Lenght Then
- Spawn_Possible = True
- Else
- Spawn_Possible = False
- End If
- temp = 0
- Return Spawn_Possible
- End Function
- Function IDConverter(ByRef GridID(,) As Integer, ByRef Pos1 As Integer, ByRef Pos2 As Integer)
- ' Converts ID's into string values for ease of use.
- Dim grid(10, 10) As String
- If GridID(Pos1, Pos2) = 0 Then
- grid(Pos1, Pos2) = " "
- ElseIf GridID(Pos1, Pos2) = 1 Then
- grid(Pos1, Pos2) = "SH"
- ElseIf GridID(Pos1, Pos2) = 2 Then
- grid(Pos1, Pos2) = "X "
- ElseIf GridID(Pos1, Pos2) = 3 Then
- grid(Pos1, Pos2) = "S "
- ElseIf GridID(Pos1, Pos2) = 4 Then
- grid(Pos1, Pos2) = "M "
- ' 5 is boundaries
- End If
- Return grid(Pos1, Pos2)
- End Function
- Sub Main()
- Randomize()
- Dim v As Variables
- Dim grid(10, 10) As String
- Dim gridID(10, 10) As Integer
- v.loop_game = 1
- Call Setup(v, grid, gridID)
- End Sub
- Sub Game(v, grid, gridID)
- Do While v.loop_Game = 1
- For r = 1 To 10
- For c = 1 To 10
- Dim pos1 As Integer = r
- Dim pos2 As Integer = c
- grid(r, c) = IDConverter(gridID, pos1, pos2)
- Next
- Next
- Console.WriteLine(" ______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ______ ")
- For r = 0 To 10
- Console.WriteLine("| | | | | | | | | | | |")
- For c = 0 To 10
- Console.Write("| " & grid(r, c) & " ")
- Next
- Console.WriteLine("|")
- For i = 0 To 10
- Console.Write("|______")
- Next
- Console.WriteLine("|")
- Next
- Dim q = Console.ReadLine()
- If q = "attac" Then
- ElseIf q = "exit" Then
- v.loop_Game = 0
- Else
- Console.Clear()
- End If
- Loop
- End Sub
- Sub Setup(v, grid, gridID)
- grid(0, 0) = " " ' The first empty cell
- For r = 1 To 10 ' -----------//LABELING START\\-------------
- gridID(r, 0) = 5
- If r = 10 Then
- grid(r, 0) = r
- Else
- grid(r, 0) = r & " "
- End If
- Next
- For c = 1 To 10
- If c = 10 Then
- gridID(0, c) = 5
- grid(0, c) = c
- Else
- grid(0, c) = c & " "
- End If
- Next ' -------------//LABELING END\\--------------
- Call ShipGen(v, grid, gridID)
- End Sub
- Sub ShipGen(v, grid, gridID)
- ' Generates ships
- Dim rand, rand2 As New Random
- Dim Ship_count As Integer = 0
- Dim DoubleCount As Integer
- Dim TripleCount As Integer
- Dim QuadCount As Integer
- Dim Spawn_Possible As Boolean
- DoubleCount = 0
- TripleCount = 0
- QuadCount = 0
- Do While QuadCount < 2
- Dim BShip_Pos1, BShip_Pos2 As Integer
- BShip_Pos1 = rand.Next(1, 10)
- BShip_Pos2 = rand.Next(1, 10)
- Dim Lenght As Integer
- Dim Direction As Integer = rand2.Next(1, 4)
- If DoubleCount < 2 Then
- Lenght = 2
- If Checker(Spawn_Possible, BShip_Pos1, BShip_Pos2, Lenght, Direction, gridID) = True Then
- Call Ship_Placement(Lenght, BShip_Pos1, BShip_Pos2, Direction, gridID)
- DoubleCount += 1
- End If
- ElseIf TripleCount < 2 Then
- Lenght = 3
- If Checker(Spawn_Possible, BShip_Pos1, BShip_Pos2, Lenght, Direction, gridID) = True Then
- Call Ship_Placement(Lenght, BShip_Pos1, BShip_Pos2, Direction, gridID)
- TripleCount += 1
- End If
- ElseIf QuadCount < 2 Then
- Lenght = 4
- If Checker(Spawn_Possible, BShip_Pos1, BShip_Pos2, Lenght, Direction, gridID) = True Then
- Call Ship_Placement(Lenght, BShip_Pos1, BShip_Pos2, Direction, gridID)
- QuadCount += 1
- End If
- End If
- Loop
- Call Game(v, grid, gridID)
- End Sub
- Sub Ship_Placement(Lenght, BShip_Pos1, BShip_Pos2, Direction, gridID)
- For i = 0 To Lenght - 1
- If Direction = 1 Then 'Down
- gridID(BShip_Pos1 + i, BShip_Pos2) = 1
- ElseIf Direction = 2 Then 'Right
- gridID(BShip_Pos1, BShip_Pos2 + i) = 1
- ElseIf Direction = 3 Then 'Up
- gridID(BShip_Pos1 - i, BShip_Pos2) = 1
- ElseIf Direction = 4 Then 'Left
- gridID(BShip_Pos1, BShip_Pos2 - i) = 1
- End If
- Next
- End Sub
- End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement