Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Class Grid
- '' Basic grid class. i can probably build this as an extention to array called gridarray the same way bitarray exists.
- Private size As Integer
- Private len As Integer
- Private XY As BitArray 'the physical layer
- Private bOutputFlags As BitArray
- 'no need to do this following line right now, all outputs reside in bits 4 and 5, upwards. 0,1,2,3,(4,5)6,7,8,9,(10,11)...n-2(n-1,n)
- ' output is going to be moved to the outputMask class which extends Grid class to check for outputs.
- ' Constructor
- Public Sub New(ByVal k As Integer, ByVal fill As Boolean) ' k is LENGTH fill is FILL
- If (Int(Math.Sqrt(k)) Mod Math.Sqrt(k)) = 0 Then ' verifies that it's actually a square number
- size = Math.Sqrt(k) ' caching the value
- len = k ' storing it in the object
- Dim coord As New BitArray(len) ' construct the new Temporary grid of the right size.
- ' this can be done with an auto-resizing array (TODO)
- XY = coord 'New BitArray(coord.Length()) ' Copy tmp grid to object's grid.
- bOutputFlags = New BitArray(len)
- bOutputFlags.SetAll(False)
- XY.SetAll(fill) 'fill to fill bit (1 or 0s all the way down)
- Else
- Console.WriteLine("error: square assertion failed")
- Console.Error.WriteLine("error: Constructor assertion failure")
- End '' accidentally forgot this part. if it's not a square it fails.
- End If
- End Sub
- Public Function isOutputPin(ByVal position) As Boolean
- '' magic numbers, 4LUT for now, maybe 8LUT later.
- '' checks if the pin lay on the output pin boundary.
- Return ((position Mod 8) > 3)
- End Function
- '' destructor: garbage collection is sane... we hope.
- Overloads Sub finalize()
- End Sub
- ' getCoord takes a "POS" index and outputs an array(1) containing the x,y coordinate pair.
- Public Function getCoord(ByVal index) As Array
- Dim a(1) As Integer
- a(0) = Math.Floor(index / (size)) '' derived from (y * SCREEN_WIDTH) + x for images, or something. it works great.
- a(1) = index Mod size '' if you have the index, the int result of index/size is the X, and the remainder of that
- Return a '' is Y. 5x5 grid: 4,0 coords; 20 is the index. 20 / 5 = 4 MOD 0 ... see?
- End Function
- Public Function getTextCoord(ByVal index) As String
- Dim tmp3(1) As Integer
- Dim tmp4 As Integer = 0
- Dim sb As New System.Text.StringBuilder
- For Each bittle As Integer In Me.getCoord(index) ' this generates a random position and gets coords for it
- 'Console.Write(bittle)
- sb.Append(bittle.ToString())
- tmp3(tmp4) = bittle
- If tmp4 = 0 Then
- sb.Append(", ") ' hackass (read: lazy) tabulation
- End If
- tmp4 += 1
- Next
- Return sb.ToString
- End Function
- Public Function getBool(ByVal index As Integer) As Boolean
- Return XY(index) '' simple getPrivate
- End Function
- Public Function getBool(ByVal pos As Array) As Boolean
- '' this overrides the previous, allowing for both input types to work.
- '' as you see, both of them provide the EXACT same functionality under the hood...
- '' but the interface LOOKS different to the caller.
- Return XY(getPos(pos))
- End Function
- Public Function getPos(ByVal a As Array) As Integer
- '' remember that (y * SCREEN_WIDTH) + x thing? here it is getting a position from an array(1) of an X,Y coordinate pair.
- Dim result As Integer
- result = (a(0) * size) + a(1)
- Return result
- End Function
- Public Function setPOS(ByVal index As Integer, ByVal b As Boolean) As Boolean
- '' Set a bit at position(index) to b.
- XY(index) = b
- Return True
- End Function
- Public Function setCoord(ByVal a As Array, ByVal b As Boolean) As Boolean
- '' Same, except it does it by coordinates. converted to position index, first.
- XY(getPos(a)) = b
- End Function
- Public Function length() As Integer
- '' standard, since it is basically an extended array. which reminds me i should get around to actually just extending array.
- Return len
- End Function
- Public Function get4BitInputArray(ByVal startindex As Integer) As BitArray ''TODO make this generic
- Dim ba As BitArray
- ba = New BitArray(4)
- 'Dim pointerIndex As Integer = startindex
- For index As Integer = 0 To 3
- ba(index) = Me.getBool(startindex + index)
- 'pointerIndex += 1
- Next
- Return ba
- End Function
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement