Skip to content

Instantly share code, notes, and snippets.

@nzifnab
Created June 28, 2011 17:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nzifnab/1051714 to your computer and use it in GitHub Desktop.
Save nzifnab/1051714 to your computer and use it in GitHub Desktop.
helping stuff
# <-- This is a comment
# Array of cards:
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# and the player's current hand:
my_hand = []
# In ruby it can also be written like this:
# arr = [1..10]
# Now let's get a random value from the array (deck of cards):
# my_card = arr[rand(arr.size() - 1)]
# Let's break that down so we understand what's happening...
# This will give us the size of the array is one greater than the max index
# arr[9] is the value '10' (because arr[0] is the value '1'), but arr.size()
# returns 10 because there's 10 total values:
arr_size = arr.size() - 1
#arr_size is now '9'
# This gives us a random number from 0 - 9
ran_num = rand(arr_size)
# ran_num is now a random index value, now let's grab that value from our array of 10 cards:
my_card = arr[ran_num]
# Now let's add it to our player's hand of cards:
# the push() method appends a value to the end of an existing array.
my_hand.push(my_card)
# my_hand now looks like this:
# [4] (assuming our random card was '4')
full_deck = []
["2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"].each do |number|
["c", "s", "d", "h"].each do |card_type|
# We are looping through each possible number & shape combination for the cards, and
# adding them to the deck. It will end up looking like this:
# ["2c", "2s", "2d", "2h", "3c", "3s", "3d", "3h"..."13d", "13h"]
full_deck.push!(number + card_type)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment