AutoHotkey Community

It is currently May 27th, 2012, 6:25 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 9 posts ] 
Author Message
PostPosted: March 1st, 2011, 4:25 am 
Offline

Joined: April 4th, 2008, 8:15 pm
Posts: 538
Location: Canada
I've recently found that I can't keep myself dedicated to singular projects in AHK mostly because of time constraints, so I'm hoping to occasionally update this thread with simple programs that will be commented. I am hoping that someone will find these examples useful. While I will not use the most efficient methods, the goal is to help someone who has just begun programming to see the basic steps to problem solving.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 1st, 2011, 4:26 am 
Offline

Joined: April 4th, 2008, 8:15 pm
Posts: 538
Location: Canada
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 Cards
Code:
;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
}


Last edited by purloinedheart on March 2nd, 2011, 3:30 am, edited 3 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 1st, 2011, 1:21 pm 
Offline

Joined: July 31st, 2008, 10:27 pm
Posts: 336
A short description of what each script is trying to accomplish would be nice.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 1st, 2011, 9:21 pm 
Offline

Joined: April 4th, 2008, 8:15 pm
Posts: 538
Location: Canada
Added a title, the first one is simply creating a deck of cards, and I added the same script in Java. I'm not sure if my explanation is very clear though, might try rewriting it again


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 1st, 2011, 11:25 pm 
Offline

Joined: July 25th, 2006, 9:06 am
Posts: 51
A nice example for those of us who are trying to learn this new fangled object stuff. :P

Add a few more and I might finally use them without getting the examples out each time.
Thanks
Peterm


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 1st, 2011, 11:27 pm 
No idea why I did this... Was very bored. Lol. But:
Code:
Loop
 MsgBox, % "Your card is: " Random_Card() "."
Return

Random_Card() {
 S := "Hearts|Diamonds|Spades|Clubs"
 N := "Ace|2|3|4|5|6|7|8|9|10|Jack|Queen|King"
 StringSplit, S, S, |
 StringSplit, N, N, |
 Random, C, 1, 52
 CN := (Mod(C,13)!=0) ? Mod(C,13):13, N := N%CN%
 CS := (Mod(C,4)!=0) ? Mod(C,4):4, S := S%CS%
 Return N " of " S
}

Div(X, Y) {
   Return Floor(X/Y)   
}


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 1st, 2011, 11:54 pm 
I already had an edit script open, so I just wrote it in that... That's where that Div() function came from that doesn't have any use in the card thing. :P.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 2nd, 2011, 3:17 am 
Offline

Joined: April 4th, 2008, 8:15 pm
Posts: 538
Location: Canada
Code:
;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
}


I thought that choosing an example where you could relate it to real life would be better than simply randomizing the position of the array. This method also has the advantage of being able to show the change in order of the cards at each step.

Edit: Also moved it to the top post

Edit2: Is it possible to Object.Insert an array?

ie.
Code:
Object := Object()
Object2 := Object()

Object.Insert(0,Object2)

Msgbox % Object[n] ; This will return Object2[n]


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 2nd, 2011, 4:21 am 
Offline

Joined: April 8th, 2009, 8:23 pm
Posts: 3036
Location: Rio de Janeiro - RJ - Brasil
Related: Cards.dll wrapper

_________________
"Read the manual. Read it again. Search the forum.
Try something before asking. Show what you've tried.
"
Image
Antonio França
My stuff: Google Profile


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Apollo, Bing [Bot], JamixZol, rbrtryn, Stigg, Yahoo [Bot] and 27 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group