Advertisement
Guest User

grid class

a guest
Mar 9th, 2011
559
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.97 KB | None | 0 0
  1. Public Class Grid
  2.     '' Basic grid class. i can probably build this as an extention to array called gridarray the same way bitarray exists.
  3.  
  4.     Private size As Integer
  5.     Private len As Integer
  6.     Private XY As BitArray 'the physical layer
  7.     Private bOutputFlags As BitArray
  8.     '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)
  9.     ' output is going to be moved to the outputMask class which extends Grid class to check for outputs.
  10.    
  11.  
  12.  
  13.     ' Constructor
  14.     Public Sub New(ByVal k As Integer, ByVal fill As Boolean)   ' k is LENGTH fill is FILL
  15.         If (Int(Math.Sqrt(k)) Mod Math.Sqrt(k)) = 0 Then        ' verifies that it's actually a square number
  16.             size = Math.Sqrt(k)                                 ' caching the value
  17.             len = k                                             ' storing it in the object
  18.             Dim coord As New BitArray(len)                      ' construct the new Temporary grid of the right size.
  19.             ' this can be done with an auto-resizing array (TODO)
  20.             XY = coord 'New BitArray(coord.Length())                   ' Copy tmp grid to object's grid.
  21.             bOutputFlags = New BitArray(len)
  22.             bOutputFlags.SetAll(False)
  23.             XY.SetAll(fill)                                     'fill to fill bit (1 or 0s all the way down)
  24.  
  25.         Else
  26.         Console.WriteLine("error: square assertion failed")
  27.         Console.Error.WriteLine("error: Constructor assertion failure")
  28.         End                                                 '' accidentally forgot this part. if it's not a square it fails.
  29.         End If
  30.  
  31.     End Sub
  32.  
  33.     Public Function isOutputPin(ByVal position) As Boolean
  34.         '' magic numbers, 4LUT for now, maybe 8LUT later.
  35.         '' checks if the pin lay on the output pin boundary.
  36.         Return ((position Mod 8) > 3)
  37.  
  38.     End Function
  39.  
  40.     '' destructor: garbage collection is sane... we hope.
  41.     Overloads Sub finalize()
  42.  
  43.     End Sub
  44.  
  45.     ' getCoord takes a "POS" index and outputs an array(1) containing the x,y coordinate pair.
  46.     Public Function getCoord(ByVal index) As Array
  47.         Dim a(1) As Integer
  48.         a(0) = Math.Floor(index / (size))   '' derived from (y * SCREEN_WIDTH) + x for images, or something. it works great.
  49.         a(1) = index Mod size               '' if you have the index, the int result of index/size is the X, and the remainder of that
  50.         Return a                            '' is Y. 5x5 grid: 4,0 coords; 20 is the index. 20 / 5 = 4 MOD 0  ... see?
  51.     End Function
  52.  
  53.     Public Function getTextCoord(ByVal index) As String
  54.         Dim tmp3(1) As Integer
  55.         Dim tmp4 As Integer = 0
  56.         Dim sb As New System.Text.StringBuilder
  57.         For Each bittle As Integer In Me.getCoord(index) ' this generates a random position and gets coords for it
  58.             'Console.Write(bittle)
  59.             sb.Append(bittle.ToString())
  60.             tmp3(tmp4) = bittle
  61.             If tmp4 = 0 Then
  62.                 sb.Append(", ") ' hackass (read: lazy) tabulation
  63.             End If
  64.             tmp4 += 1
  65.         Next
  66.         Return sb.ToString
  67.     End Function
  68.  
  69.     Public Function getBool(ByVal index As Integer) As Boolean
  70.         Return XY(index)                    '' simple getPrivate
  71.     End Function
  72.  
  73.     Public Function getBool(ByVal pos As Array) As Boolean
  74.         '' this overrides the previous, allowing for both input types to work.
  75.         '' as you see, both of them provide the EXACT same functionality under the hood...
  76.         '' but the interface LOOKS different to the caller.
  77.         Return XY(getPos(pos))
  78.     End Function
  79.  
  80.     Public Function getPos(ByVal a As Array) As Integer
  81.         '' remember that (y * SCREEN_WIDTH) + x thing? here it is getting a position from an array(1) of an X,Y coordinate pair.
  82.         Dim result As Integer
  83.         result = (a(0) * size) + a(1)
  84.         Return result
  85.     End Function
  86.  
  87.     Public Function setPOS(ByVal index As Integer, ByVal b As Boolean) As Boolean
  88.         '' Set a bit at position(index) to b.
  89.         XY(index) = b
  90.         Return True
  91.  
  92.     End Function
  93.  
  94.     Public Function setCoord(ByVal a As Array, ByVal b As Boolean) As Boolean
  95.         '' Same, except it does it by coordinates. converted to position index, first.
  96.         XY(getPos(a)) = b
  97.     End Function
  98.  
  99.     Public Function length() As Integer
  100.         '' standard, since it is basically an extended array. which reminds me i should get around to actually just extending array.
  101.         Return len
  102.     End Function
  103.  
  104.     Public Function get4BitInputArray(ByVal startindex As Integer) As BitArray ''TODO make this generic
  105.         Dim ba As BitArray
  106.         ba = New BitArray(4)
  107.         'Dim pointerIndex As Integer = startindex
  108.         For index As Integer = 0 To 3
  109.             ba(index) = Me.getBool(startindex + index)
  110.             'pointerIndex += 1
  111.         Next
  112.  
  113.         Return ba
  114.     End Function
  115.  
  116. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement