Sorry for the slow reply earlier, I was 4 hrs. late, but as my excuse, I live in the real world, and I had to write the code from scratch.
It works for the most part, but I thought I saw a repeat once. But I haven't seen it for a bit, so it might be ok.
First if you plan on doing much of anything with BlitzMax I'd suggest you get familiar with types. I'll see what I can do about adding comments.
Types are basically a way to organize information. Theoretically you can write programs without them, but it makes your job ALOT easier. Like arrays they organize information under a single name. Int and Stream are types, they're just preset and you don't have to declare them the same way in arrays.
Methods are like fields, except they're functions within the type.
When I created the TCard Type, every instance of the TCard set will have a String Variable named Suit, and a String Variable named Face.
Think of each type as a Templete, within each Templete it has preset Variables, so every Instance of the type/templete will have the variables declared in the type. You've used types before whether you know it or not.
I didn't comment on the setting Ace-King or Spades - Diamonds as i figured you'd figure it out.
This way in order to make a traditional Card Deck, all you have to do is:
And its all ready to be used, then to shuffle it, you do:
The reason I make you Set and Shuffle is you may not want that, with a little more work, and merging with Perturbatio's this could work for most decks. Like you could have another method that would be, "SetPokerDeck()" or something like that I think.
It works for the most part, but I thought I saw a repeat once. But I haven't seen it for a bit, so it might be ok.
First if you plan on doing much of anything with BlitzMax I'd suggest you get familiar with types. I'll see what I can do about adding comments.
Types are basically a way to organize information. Theoretically you can write programs without them, but it makes your job ALOT easier. Like arrays they organize information under a single name. Int and Stream are types, they're just preset and you don't have to declare them the same way in arrays.
Methods are like fields, except they're functions within the type.
When I created the TCard Type, every instance of the TCard set will have a String Variable named Suit, and a String Variable named Face.
Think of each type as a Templete, within each Templete it has preset Variables, so every Instance of the type/templete will have the variables declared in the type. You've used types before whether you know it or not.
I didn't comment on the setting Ace-King or Spades - Diamonds as i figured you'd figure it out.
SuperStrict 'Put into SuperStrict mode. SeedRnd MilliSecs() 'Seed the random generator by milliseconds. Type TCard 'Make a Card Type Field Face:String 'Have a Face field within the Card Type Field Suit:String 'Have a Suit field within the Card Type End Type 'Close the Card Type Type TDeck 'Make a Deck Type Field Cards:TCard[] 'Make a Cards field that's an Array, that contains Null elements as I want to set how many elements later. Method Create(NumCards:Int = 52) 'Create a Create method with a parameter of NumCards to set the number of Elements in Cards. Local Loop_Var:Int 'Create a Loop Variable that I'm going to use and re-use later. Cards:TCard = New TCard[NumCards] 'Redeclare the Array so I can have a custom number of cards for Cards with Jokers. For Loop_Var = 0 To Cards.Length - 1 'Go in a loop from 0 to the Length of the Cards Array - 1 Cards[Loop_Var] = New TCard 'Declare the Type Next End Method 'End the method Method Shuffle() 'Create a Shuffle method that will mix up the cards. Local TempArray:TCard[Cards.Length] 'A Temporary array that we will test against, it will be equal to the Cards Array. Local Active:Int[Cards.Length] 'An array that we use to ensure that no cards are repeated. Local Return_Array:TCard[Cards.Length] 'This will be the new Cards Array. Local Loop_Var:Int 'Loop Variable Local New_Field:Int 'A random number used for the mixing Local TimesAccessed:Int 'Helps us determine what field to acess of the Arrays. TempArray = Cards 'Set the TempArray and all its fields to that of the Card Array. For Loop_Var = 0 To Cards.Length - 1 'Enter a for loop Repeat 'A repeat forever loop New_Field = Rnd(0, Cards.Length - 1) 'Random number If Active[New_Field] = False 'If we haven't used this field before Return_Array[Loop_Var] = TempArray[New_Field] 'Set the new Cards field uip Active[New_Field] = True 'Make sure we don't use it again Exit 'Exit the Repeat...Forver loop Else 'Otherwise. If TimesAccessed > 52 'If we've gone through it too many times. Exit 'Exit repeat forever loop End If EndIf If Loop_Var = 51 'If this is the last appropriate card Return_Array[Loop_Var] = TempArray[New_Field] 'Set it up Active[New_Field] = True Exit 'Exit out of the repeat forever loop EndIf Forever Next Cards = Return_Array 'Set the Cards array to the new correct array. End Method Method Set() Local Loop_Var1:Int 'Loop Variable 1 Local Loop_Var2:Int 'Loop Variable 2 Local Loop_Var3:Int 'Loop Variable 3 For Loop_Var1 = 0 To 12 'Cycle through 13 times (once for each card) For Loop_Var2 = 0 To 3 'Cycle through 4 times (once for each suit) If Loop_Var1 = 0 'If its ace Cards[Loop_Var3].Face = "Ace" ElseIf Loop_Var1 = 1 Cards[Loop_Var3].Face = "2" ElseIf Loop_Var1 = 2 Cards[Loop_Var3].Face = "3" ElseIf Loop_Var1 = 3 Cards[Loop_Var3].Face = "4" ElseIf Loop_Var1 = 4 Cards[Loop_Var3].Face = "5" ElseIf Loop_Var1 = 5 Cards[Loop_Var3].Face = "6" ElseIf Loop_Var1 = 6 Cards[Loop_Var3].Face = "7" ElseIf Loop_Var1 = 7 Cards[Loop_Var3].Face = "8" ElseIf Loop_Var1 = 8 Cards[Loop_Var3].Face = "9" ElseIf Loop_Var1 = 9 Cards[Loop_Var3].Face = "10" ElseIf Loop_Var1 = 10 Cards[Loop_Var3].Face = "Jack" ElseIf Loop_Var1 = 11 Cards[Loop_Var3].Face = "Queen" ElseIf Loop_Var1 = 12 Cards[Loop_Var3].Face = "King" EndIf If Loop_Var2 = 0 Cards[Loop_Var3].Suit = "Spades" ElseIf Loop_Var2 = 1 Cards[Loop_Var3].Suit = "Clubs" ElseIf Loop_Var2 = 2 Cards[Loop_Var3].Suit = "Hearts" ElseIf Loop_var2 = 3 Cards[Loop_Var3].Suit = "Diamonds" EndIf Loop_Var3:+1 'Increment the 3rd Loop Variable by 1 AKA add to it by 1 Next Next End Method End Type Global MyDeck:TDeck = New TDeck 'Make a new deck MyDeck.Create() 'Use the create Method of the deck MyDeck.Set() 'Use the Set Method of the deck MyDeck.Shuffle() 'Use the Shuffle Method of the deck Local Loop_Var:Int 'A LoopVariable For Loop_Var = 0 To MyDeck.Cards.Length - 1 'Cycle through the cards. Print MyDeck.Cards[Loop_Var].Face + " of " + MyDeck.Cards[Loop_Var].Suit 'Print it out. Next
This way in order to make a traditional Card Deck, all you have to do is:
NewDeck:TDeck NewDeck.Create() NewDeck.Set()
And its all ready to be used, then to shuffle it, you do:
NewDeck.Shuffle()
The reason I make you Set and Shuffle is you may not want that, with a little more work, and merging with Perturbatio's this could work for most decks. Like you could have another method that would be, "SetPokerDeck()" or something like that I think.