Ooops
January 15, 2021, 06:35:17 PM

Author Topic: [bmx] Shuffle by cps [ 9 months ago ]  (Read 618 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] Shuffle by cps [ 9 months ago ]
« on: June 29, 2017, 12:28:42 AM »
Title : Shuffle
Author : cps
Posted : 9 months ago

Description : Shuffles 10 cards, Black Cards 1 to 5 and Red Cards 1 to 5

Code :
Code: BlitzMax
  1. 'An easy to follow card shuffler ?  Have fun Cps.
  2. 'for 10 cards indexed 0-9
  3. 'Takes index 0 and swaps contents with a randomly choosen index number between 1-9
  4. 'Takes index 1 and swaps contents with a randomly choosen index number between 2-9
  5. 'Takes index 2 and swaps contents with a randomly choosen index number between 3-9 etc
  6. 'Stops after index 7 contents swaped with a randomly choosen index number between 8-9.
  7.  
  8. SuperStrict
  9. Import MaxGui.Drivers
  10.  
  11.  
  12. Local Deck:AllCards= New AllCards' generates the cards and methods
  13.  
  14. Deck.Initilise()' create deck
  15. Deck.Show()' show ordered deck
  16. deck.Shuffle()' shuffle deck
  17. Deck.Show()' show shuffled deck
  18. Print "---------------  And Again  -------------------"
  19. Deck.SetValues()'reset deck
  20. Deck.Show()' show ordered deck
  21. Deck.Shuffle()' shuffle deck
  22. Deck.Show()' show shuffled deck
  23.  
  24. End
  25.  
  26.  
  27. Type AllCards Extends ACard' the full deck
  28.         Field Card:ACard[10]'10 cards in pack
  29.  
  30.         Method Initilise()
  31.                 Local Tb1:Byte
  32.                 For Tb1=0 To 9 Card[Tb1]=New Acard Next' create the cards (index 1 les than total number of cards)
  33.                 SeedRnd MilliSecs()     'seed the random number generator              
  34.                 SetValues()
  35.         End Method
  36.  
  37.         Method SetValues()' card(0 to 4) = black cards values 1 to 5,  card(5 to 9) = red cards values 1 to 5
  38.                 Local Tb1:Byte' sets up the ordered deck
  39.                 For Tb1=0 To 4
  40.                         SetVal(Tb1,Tb1+1); SetVal(Tb1+5,Tb1+1)
  41.                         SetCol(Tb1,0); SetCol(Tb1+5,1)
  42.                 Next
  43.         End Method
  44.        
  45.         Method Shuffle()'shuffles the deck
  46.                 Local Tb1:Byte; Local Nt1:Byte; Local Nt2:Byte
  47.                 Local VSwap:Byte' temp stroe for value swap
  48.                 Local CSwap:Byte'temp store for colur swap
  49.                
  50.                 For Tb1=0 To 7' 2 less than max card index
  51.                         Nt1=Tb1' card index numbers from top to bottom
  52.                         Nt2=RandomNum((Tb1+1),9)' a random card index number from the remaining cards
  53.                         VSwap=GetVal(Nt1); CSwap=GetCol(Nt1)
  54.                         SetVal(Nt1,GetVal(Nt2)); SetCol(Nt1,GetCol(Nt2))
  55.                         SetVal(Nt2,VSwap); SetCol(Nt2,CSwap)
  56.                 Next
  57.         End Method
  58.        
  59.         Method RandomNum:Byte(T1:Byte,T2:Byte)'returns a random number between T1 and T2
  60.                 Local Tb1:Byte
  61.                 Tb1=Rand(T1,T2)
  62.                 Return Tb1
  63.         End Method
  64.  
  65.         Method Show()'prints cards by card number
  66.                 Local Tb1:Byte; Local Ts1$
  67.                 For Tb1=0 To 9
  68.                         Ts1="Card Number "+String(Tb1)+" : Value = "+String(GetVal(Tb1))+" : Colour = "
  69.                         If GetCol(Tb1)=0 Then Ts1=Ts1+"Black" Else Ts1=Ts1+"Red"
  70.                         Print Ts1
  71.                 Next
  72.                 Print "-----------------------------------------"
  73.         End Method
  74.        
  75.         Method SetVal(T1:Byte,T2:Byte)' T1=card number,T2=value
  76.                 Card[T1].Val=T2
  77.         End Method
  78.  
  79.         Method GetVal:Byte(T1:Byte)' T1=card number, returns card value
  80.                 Local Tb1:Byte=Card[T1].Val; Return Tb1
  81.         End Method
  82.        
  83.         Method SetCol(T1:Byte,T2:Byte)' T1=card number,T2=Colour 0=black, 1=red
  84.                 Card[T1].Col=T2
  85.         End Method
  86.        
  87.         Method GetCol:Byte(T1:Byte)' T1=card number, returns card colour 0=black, 1=red
  88.                 Local Tb1:Byte=Card[T1].Col; Return Tb1
  89.         End Method
  90. End Type
  91.  
  92.  
  93. Type ACard'a card
  94.         Field Val:Byte'1 to 5 (the cards value)
  95.         Field Col:Byte'0=black, 1=red (the cards colour)
  96. End Type


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal