Creating a Deck of Cards
Code:
;First we will create a variable that will store the ranks of the cards (From Ace all the way to King)
Ranks := Object()
Ranks.Insert(0, "Ace", 2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King")
;Next, we will store the different suits
Suits := Object()
Suits.Insert(0, "Clubs", "Diamonds", "Hearts", "Spades")
;Now we will define the total number of cards in the deck by multiplying the number of rank cards we have (13) by the number of suits that we have (4)
Total := (Ranks.MaxIndex() + 1) * (Suits.MaxIndex() + 1) ; 52
;Note: We are adding one because our Index starts at Ranks[0], meaning our 13th card is actually in Ranks[12]
;Now that we have the total number of cards in the deck, we can create it
Deck := Object()
Loop %Total%
{
Index := A_Index - 1
Deck.Insert(Index, Ranks[Mod(Index, Ranks.MaxIndex() + 1)] " of " Suits[Mod(Index, Suits.MaxIndex() + 1)])
}
;Now we can output the deck to make sure it created properly
Loop %Total%
{
Index := A_Index - 1
FileAppend, % Deck[Index] "`n", Deck.txt
}
;Explaining the 'Deck.Insert(Index, Ranks[Mod(Index, Ranks.MaxIndex() + 1)] " of " Suits[Mod(Index, Suits.MaxIndex() + 1)])'
; First, lets simplify this line so that it's easier to read
;
; We can say that our ranks go from Rank[0] = 'Ace' to Rank[12] = 'King'
; Let us also say that Rank.Length is equal to the number of elements in 'Rank'
; this means that we have 13 rank cards in all Rank[0], Rank[1], Rank[2] ... Rank[12]
; so Rank.Length = 13
;
; The suits are similar, Suits[0] = 'Clubs', Suits[1] = 'Diamonds' .. Suits[3] = 'Spades'
; Suits.Length = 4
;
; The 'Mod(a, b)' function returns the remainder of a when it is divided by b
; For example Mod(3,2) returns 1
; Mod(13,3) returns 1 --> 3 goes into 13 4 times, then you will have a 1 left over
;
; The mod function can be written as a symbol, %
; 3%2 = 1
; 13%3 = 1
;
; So we can write our line as:
;
;
; Output = Ranks[Index%Ranks.Length] " of " Suits[Index%Suits.Length]
;
;
; Lets look at Ranks[Index%Ranks.Length] ==> Ranks[Index%13]
; Index%Ranks.Length will start at 0 and go up to 12
; after it reaches 12, it will go back to 0
; If we look at what we said before, we know that Ranks[0] = Ace, and that Ranks[12] = King
; So, this section will go from Ace to King (Ace, 2, 3, 4, ..., King)
;
; While the Ranks cycles through, we are doing the same with the suits
; Suits[Index%Suits.Length] ==> Suits[Index%4]
; The suits cycles through the 4 suits, going from
; Suits[0] = Clubs, Suits[1] = Diamonds .. Suits[3] = Spades
; So, this section will go from Clubs, Diamonds, Hearts, and Spades, then start all over again
;
; While both of the sections cycle, it will result in something like looks like this:
;
; Ace of Clubs
; 2 of Diamonds
; 3 of Hearts
; 4 of Spades
; 5 of Clubs
; 6 of Diamonds
; 7 of Hearts
; 8 of Spades
; 9 of Clubs
; 10 of Diamonds
; Jack of Hearts
; Queen of Spades
; King of Clubs
;
; Now, when the cycle starts over again, it will start at the next suit (Because 13%4 = 1)
; This means that after every 13 cards, the suit cards will cycle through the rank cards, however
; their suit will be rotated one over, and this cycle will restart after 4 cycles (However we will
; run out of cards by that time, so it does not matter)
Example using Java (Might be easier to understand using different syntax)
Code:
String Suits[] = {"Spades", "Hearts", "Diamonds", "Clubs"};
String Ranks[] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
int Total = Suits.length * Ranks.length;
String Deck[] = new String [Total];
for (int i = 0; i < Total; i++)
Deck [i] = Ranks [i % 13] + " of " + Suits [i % 4]; //Generate deck
Shuffling a Deck of CardsCode:
;From previous example
Ranks := Object()
Ranks.Insert(0, "Ace", 2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King")
Suits := Object()
Suits.Insert(0, "Clubs", "Diamonds", "Hearts", "Spades")
Total := (Ranks.MaxIndex() + 1) * (Suits.MaxIndex() + 1) ; 52
Deck := Object()
Loop %Total%
{
Index := A_Index - 1
Deck.Insert(Index, Ranks[Mod(Index, Ranks.MaxIndex() + 1)] " of " Suits[Mod(Index, Suits.MaxIndex() + 1)])
}
;Now, using the previously created deck we will shuffle it
;In this example, we will randomly choose a location to cut the deck, and then move
;the bottom half to the top and repeat several times
;We will start by making a 'buffer' that will hold the top cards while we move them
Top_Buffer := Object()
;We randomly select a number from 1 to 51 as the cutting location
Cut_Location := rand()
;We will now take the cards from the top of the deck to the cut location and store them in the buffer
Loop %Cut_Location%
Top_Buffer[A_Index-1] := Deck[A_Index-1]
;Now that the top half of the deck has been stored, we can move the bottom half of the deck
;to the top
Loop % Deck.MaxIndex() - Cut_Location +1
Deck[A_Index-1] := Deck[Cut_Location+A_Index-1]
;All that's left to do now is to put the cards that were once at the top to the bottom of the deck
Loop % Cut_Location
Deck[Deck.MaxIndex() - Cut_Location + A_Index] := Top_Buffer[A_Index-1]
Loop 52
FileAppend % Deck[A_Index-1] "`n", Shuffled Deck.txt
;This only cuts the deck once, try to modify the code so that it will perform these steps multiple times
; HINT: You will have to clear the Top_Buffer array often, because it's size may vary from 1 ~ 51
rand(){
Random X, 1, 51
return X
}