Giter Club home page Giter Club logo

go-fish-card-game's Introduction

Go Fish - OO Ruby with Tests

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.

Setup

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"

Building the Game

Take a look inside go_fish.rb and you'll see some boilerplate code for our classes.

Class Interfaces

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 to_s to_s
face shuffle shuffle
to_s draw 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

Playing Cards

Your first goal will be to build your PlayingCard objects. Here's some raw data for you to copy paste (no jokers!):

RANKS = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
SUITS = ["C", "D", "H", "S"]
SORTED_CARDS = [
    "AC", "AD", "AH", "AS", "2C", "2D", "2H", "2S", "3C", "3D", "3H", "3S",
    "4C", "4D", "4H", "4S", "5C", "5D", "5H", "5S", "6C", "6D", "6H", "6S",
    "7C", "7D", "7H", "7S", "8C", "8D", "8H", "8S", "9C", "9D", "9H", "9S",
    "10C", "10D", "10H", "10S", "JC", "JD", "JH", "JS", "QC", "QD", "QH", "QS",
    "KC", "KD", "KH", "KS"
]

Follow the tests to get started!

Tips

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"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.