In this lab you will be building a card game using Ruby Classes and Rspec Tests. Our game will include classes for: PlayingCard
, CardDeck
, HandOfCards
, and CardPlayer
. By the end of this lab you should be able to simulate a multi-player game of go_fish.
First, make sure you've installed the rspec
gem.
From the command line (inside the cloned repo directory) run:
gem install rspec # individually install the rspec gem
# or
bundle install # install all the gems listed in the Gemfile
Next, check that the rspec tests are working (they should be failing!):
rspec
# or
rspec spec/1_playing_card_spec.rb
# or, run only test that match your search
rspec spec/2_card_deck_spec.rb -e "shuffle"
Take a look inside go_fish.rb
and you'll see some boilerplate code for our classes.
Your classes will have the following interfaces (take the time to study these closely):
PlayingCard |
CardDeck |
HandOfCards |
CardPlayer |
---|---|---|---|
initialize |
initialize |
initialize |
initialize |
rank |
cards |
cards |
hand |
suit |
generate_cards |
to_s |
|
face |
shuffle |
draw |
|
to_s |
draw |
draw_one |
|
draw_one |
push |
||
push |
any? |
||
take! |
Together, these classes and methods will allow us to simulate game play, using the following "Driver Code":
# GAME SETUP
deck = CardDeck.new
deck.shuffle
card = deck.draw_one
two_cards = deck.draw(2)
cards1 = deck.draw(5)
h1 = HandOfCards.new(cards1)
p1 = CardPlayer.new(hand: h1)
cards2 = deck.draw(5)
h2 = HandOfCards.new(cards2)
p2 = CardPlayer.new(hand: h2 )
# GAME PLAY
wanted_rank = "3"
puts "p1, do you have any... #{wanted_rank}'s?"
if p1.hand.any?(rank: wanted_rank)
cards = p1.hand.take!(rank: wanted_rank)
p2.hand.push(*cards)
else
puts "Go Fish"
drawn_card = deck.draw
p2.hand.push(drawn_card)
end
Your first goal will be to build your PlayingCard
objects.
When you build your CardDeck
object, be sure to copy and paste the code below into your generate_cards
function (no jokers!):
ranks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
suits = ["C", "D", "H", "S"]
Follow the tests to get started!
It's easy to get lost in test output / lose sight of the "big picture". Make sure you understand what your goal is before you dive in!
You can run your game's Driver Code from the command line by typing:
ruby go_fish.rb
Hint: Take a look at the "Drive Code" at the bottom of
go_fish.rb
!
You can also load your code into Pry to test your assumptions:
pry
> load 'go_fish.rb'
> card = Card.new({...})
> card.face # "AC"