Advertisement
Guest User

Untitled

a guest
Aug 6th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Strict
  2.  
  3. import mojo
  4.  
  5. Global myGame:MyGame
  6.  
  7. Global Suits:String[] = ["clubs", "diamonds", "hearts", "spades"]
  8.  
  9. Function Main:int()
  10.     myGame = new MyGame
  11.     return 0
  12. End Function
  13.  
  14. Class MyGame extends App
  15.     Field deck:Holder
  16.  
  17.     Method OnCreate:Int()
  18.    
  19.         deck = New Holder
  20.    
  21.         SetUpdateRate 60
  22.         return 0
  23.     End Method
  24.    
  25.     Method OnLoading:Int()
  26.         return 0
  27.     End Method
  28.    
  29.     Method OnUpdate:Int()
  30.         return 0
  31.     End Method
  32.    
  33.     Method OnRender:Int()
  34.         Cls
  35.         deck.draw()
  36.         return 0
  37.     End Method 
  38. end class
  39.  
  40. Class Holder
  41.     Field cardList:LinkedList<Card>
  42.    
  43.     Method new()
  44.         cardList = New LinkedList<Card>
  45.         Local c:Card
  46.         Local pos% = 0
  47.         Local s% = 0
  48.         For Local Suit:String = Eachin Suits
  49.             For Local n% = 0 to 12
  50.                 c = New Card(Suit, n + 1, pos)
  51.                 c.x = n * 50
  52.                 c.y = s * 100
  53.                 self.cardList.AddLast(c)
  54.                 pos+=1
  55.             Next
  56.             s+=1
  57.         Next
  58.         shuffle()
  59.     End
  60.    
  61.     Method shuffle:Void()
  62.         Local randSize:Int = Self.cardList.Count() * 2
  63.        
  64.         For Local c:Card = EachIn cardList
  65.             c.shuffleOrder = Rnd(randSize)
  66.         Next
  67.        
  68.         ' Sort stack here!!!
  69.        
  70.     End Method
  71.    
  72.     Method draw:Void()
  73.         For Local c:Card = EachIn cardList
  74.             c.draw()   
  75.         Next
  76.     End
  77. End
  78.  
  79.  
  80.  
  81. Class Card
  82.     Field value%
  83.     Field suit:String
  84.     Field shuffleOrder%
  85.     Field x#, y#
  86.    
  87.     Method new(suit:String, value:Int, shuffleOrder:Int)
  88.         self.suit = suit
  89.         self.value = value
  90.         self.shuffleOrder = shuffleOrder
  91.     End
  92.    
  93.     Method draw:Void()
  94.         DrawText Self.suit, x, y
  95.         DrawText Self.value, x, y+12
  96.        
  97.     End
  98. End
  99.  
  100. Class LinkedList<T>
  101.  
  102.     Method Equals:Object( lhs:Object,rhs:Object )
  103.         Return lhs=rhs
  104.     End
  105.    
  106.     Method Clear:Void()
  107.         _head=New LinkedNode<T>()
  108.     End
  109.  
  110.     Method Count:Int()
  111.         Local n:Int
  112.         local node:=_head._succ
  113.         While node<>_head
  114.             node=node._succ
  115.             n+=1
  116.         Wend
  117.         Return n
  118.     End
  119.    
  120.     Method IsEmpty?()
  121.         Return _head._succ=_head
  122.     End
  123.    
  124.     Method First:T()
  125.         Return _head._succ._data
  126.     End
  127.  
  128.     Method Last:T()
  129.         Return _head._pred._data
  130.     End
  131.    
  132.     Method AddFirst:LinkedNode<T>( data:T )
  133.         Return New LinkedNode<T>( _head._succ,_head,data )
  134.     End
  135.  
  136.     Method AddLast:LinkedNode<T>( data:T )
  137.         Return New LinkedNode<T>( _head,_head._pred,data )
  138.     End
  139.  
  140.     'I think this should GO!
  141.     Method Remove:Void( value:T )
  142.         RemoveEach value
  143.     End
  144.    
  145.     Method RemoveEach:Void( value:T )
  146.         Local node:=_head._succ
  147.         While node<>_head
  148.             node=node._succ
  149.             If Equals( node._pred._data,value ) node._pred.Remove
  150.         Wend
  151.     End
  152.  
  153.     Method RemoveFirst:T()
  154.         Local data:T=_head._succ._data
  155.         _head._succ.Remove
  156.         Return data
  157.     End
  158.  
  159.     Method RemoveLast:T()
  160.         Local data:T=_head._pred._data
  161.         _head._pred.Remove
  162.         Return data
  163.     End
  164.  
  165.     Method ObjectEnumerator:LinkedEnumerator<T>()
  166.         Return New LinkedEnumerator<T>( Self )
  167.     End Method
  168.  
  169.  
  170.     Field _head:=New LinkedNode<T>
  171.    
  172. End
  173.  
  174. Class LinkedEnumerator<T>
  175.  
  176.     Method New( list:LinkedList<T> )
  177.         _list=list
  178.         _curr=list._head._succ
  179.     End Method
  180.  
  181.     Method HasNext:Bool()
  182.         Return _curr<>_list._head
  183.     End
  184.  
  185.     Method NextObject:T()
  186.         Local data:T=_curr._data
  187.         _curr=_curr._succ
  188.         Return data
  189.     End
  190.  
  191.    
  192.     Field _list:LinkedList<T>
  193.     Field _curr:LinkedNode<T>
  194.  
  195. End
  196.  
  197. Class LinkedNode<T>
  198.  
  199.     'create a _head node
  200.     Method New()
  201.         _succ=Self
  202.         _pred=Self
  203.     End
  204.  
  205.     'create a link node
  206.     Method New( succ:LinkedNode,pred:LinkedNode,data:T )
  207.         _succ=succ
  208.         _pred=pred
  209.         _succ._pred=Self
  210.         _pred._succ=Self
  211.         _data=data
  212.     End
  213.    
  214.     Method Value:T()
  215.         Return _data
  216.     End
  217.  
  218.     Method Remove:Void()
  219.         _succ._pred=_pred
  220.         _pred._succ=_succ
  221.     End Method
  222.  
  223.    
  224.     Field _succ:LinkedNode
  225.     Field _pred:LinkedNode
  226.     Field _data:T
  227.  
  228. End
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement