https://github.com/turingschool-examples/sorting_cards
https://github.com/turingschool-examples/sorting_cards
be-1 project ruby
Last synced: 5 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/turingschool-examples/sorting_cards
- Owner: turingschool-examples
- Created: 2018-09-24T18:14:20.000Z (over 7 years ago)
- Default Branch: main
- Last Pushed: 2023-04-22T23:07:03.000Z (about 3 years ago)
- Last Synced: 2025-06-05T07:47:30.464Z (about 1 year ago)
- Topics: be-1, project, ruby
- Language: Ruby
- Homepage:
- Size: 4.88 KB
- Stars: 0
- Watchers: 10
- Forks: 44
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Sorting Cards
In this project, you will write a program to simulate a deck of cards. We will add functionality to guess what the card on the top of the deck is, and sort the deck.
In order to build good habits, we've broken the project into small classes to demonstrate objects that have a single responsibility.
## Setup
* Fork [this repository](https://github.com/turingschool-examples/sorting_cards)
* Clone YOUR fork to your computer
* Make sure you don't clone the turingschool-examples repository
* Complete the iterations below
* Remember to commit frequently!
## Submission
When you are finished, make sure your code is pushed up to your repository on Github. Submit a Pull Request from your repository to the turingschool-examples repository.
# Iteration 1
### Cards
A `Card` represents a single card in our deck. It stores a suit and a value.
Use the tests provided to drive the development of your `Card` class. From the root directory of your project, run the test like this:
```
rspec spec/card_spec.rb
```
If you haven't already, you will need to install RSpec:
```
gem install rspec
```
If your `Card` class is written properly, you should be able to open a pry session and interact with it like so:
```ruby
pry(main)> require './lib/card'
#=> true
pry(main)> card = Card.new("Ace", "Spades")
#=> #
pry(main)> card.value
#=> "Ace"
pry(main)> card.suit
#=> "Spades"
```
This interaction pattern assumes your `Card` class is in a file located at `./lib/card.rb`.
### Guesses
Create a `Guess` class and an accompanying test file with the following methods:
* `initialize(string, Card)` - A guess is initialized with two arguments. The first is a string representing a response to a card in the form of ` of `. The second argument is a `Card` object representing the card being guessed.
* `response` - This method returns the response
* `card` - This method returns the Card
* `correct?` - This method returns a boolean indicating if the response correctly guesses the value and suit of the Card
* `feedback` - This method either returns `"Correct!"` or `"Incorrect."` based on whether the guess was correct or not
The `Guess` class should respond to the following interaction pattern:
```ruby
pry(main)> require './lib/guess'
#=> true
pry(main)> require './lib/card'
#=> true
pry(main)> card = Card.new("10", "Hearts")
#=> #
pry(main)> guess = Guess.new("10 of Hearts", card)
#=> #, @guess="10 of Hearts">
pry(main)> guess.card
#=> #
pry(main)> guess.response
#=> "10 of Hearts"
pry(main)> guess.correct?
#=> true
pry(main)> guess.feedback
#=> "Correct!"
```
We also want to make sure that incorrect guesses are handled properly.
```ruby
pry(main)> require './lib/guess'
#=> true
pry(main)> require './lib/card'
#=> true
pry(main)> card = Card.new("Queen", "Clubs")
#=> #
pry(main)> guess = Guess.new("2 of Diamonds", card)
#=> #, @guess="2 of Diamonds">
pry(main)> guess.card
=> #
pry(main)> guess.response
=> "2 of Diamonds"
pry(main)> guess.correct?
=> false
pry(main)> guess.feedback
=> "Incorrect."
```
Remember, `#` means "A Guess Obeject" or "An Instance of the Guess Class".
# Iteration 2
### Storing Cards in a Deck
Create a `Deck` class with an accompanying test file. A `Deck` is initialized with an array of `Card` objects. The `Deck` class should respond to the following interaction pattern:
```ruby
pry(main)> require './lib/card'
#=> true
pry(main)> require './lib/deck'
#=> true
pry(main)> card_1 = Card.new("3","Hearts")
#=> #
pry(main)> card_2 = Card.new("4", "Clubs")
#=> #
pry(main)> card_3 = Card.new("5", "Diamonds")
#=> #
pry(main)> cards = [card_1, card_2, card_3]
pry(main)> deck = Deck.new(cards)
#=> #
pry(main)> deck.cards
#=> [#, #, #]
pry(main)> deck.count
#=> 3
```
### The Round
A `Round` will be the object that processes responses and records guesses. A `Round` is initialized with a `Deck`. The idea is that when we start a Round, the current card is the first in the deck (the first in the Deck's array of Cards). When we make a guess, the guess is recorded, and the current card becomes the next card in the deck.
The `record_guess` method is the crux of this problem. The `record_guess` method takes a hash representing the guess. It should create a new `Guess` object with the appropriate response and Card. It should store this new guess, as well as return it from the `record_guess` method. Also, when the `record_guess` method is called, the `Round` should move on to the next card in the deck.
A `Round` should respond to the following interaction pattern:
```ruby
pry(main)> require './lib/card'
#=> true
pry(main)> require './lib/deck'
#=> true
pry(main)> require './lib/round'
#=> true
pry(main)> card_1 = Card.new("3","Hearts")
#=> #
pry(main)> card_2 = Card.new("4", "Clubs")
#=> #
pry(main)> deck = Deck.new([card_1, card_2])
#=> #
pry(main)> round = Round.new(deck)
#=> #,
pry(main)> round.deck
#=> #
pry(main)> round.guesses
#=> []
pry(main)> round.current_card
#=> #
pry(main)> new_guess = round.record_guess({value: "3", suit: "Hearts"})
#=> #, @response="3 of Hearts">
pry(main)> new_guess.class
#=> Guess
pry(main)> new_guess.correct?
#=> true
pry(main)> round.guesses
#=> [#, @response="3 of Hearts">]
pry(main)> round.number_correct
#=> 1
pry(main)> round.current_card
#=> #
pry(main)> round.record_guess({value: "Jack", suit: "Diamonds"})
#=> #
pry(main)> round.guesses.count
#=> 2
pry(main)> round.guesses.last.feedback
#=> "Incorrect."
pry(main)> round.number_correct
#=> 1
pry(main)> round.percent_correct
#=> 50.0
```
# Iteration 3: Sorting the Deck
In this iteration, we will start to add some algorithmic complexity. We are going to add to the deck object the ability to sort the cards based on their value from lowest to highest. The order of values from lowest to highest is 2 through 10, Jack, Queen, King, Ace.
If two cards have the same value, the suit should be used to determine the order they are sorted. The order of suit from lowest to highest is Clubs, Diamonds, Hearts, Spades.
You are *NOT* allowed to use any built in sorting methods.
The interaction pattern will look like this:
```ruby
card_1 = Card.new("4","Hearts")
card_2 = Card.new("Ace", "Spades")
card_3 = Card.new("5", "Diamonds")
card_4 = Card.new("Jack", "Clubs")
card_5 = Card.new("Ace", "Diamonds")
deck = Deck.new([card_1, card_2, card_3, card_4, card_5])
deck.sort
=> [card_1, card_3, card_4, card_5, card_2]
```
# Iteration 4: Merge Sort
We're doing the same here, but with a different sorting algorithm, merge sort. As you implement this, think about why we might need different algorithms. How many swaps does your sort from iteration 3 make in the best case scenario? Worst case? How does this compare to Merge Sort?
The interaction pattern will look like this:
```ruby
card_1 = Card.new("4","Hearts")
card_2 = Card.new("Ace", "Spades")
card_3 = Card.new("5", "Diamonds")
card_4 = Card.new("Jack", "Clubs")
card_5 = Card.new("Ace", "Diamonds")
deck = Deck.new([card_1, card_2, card_3, card_4, card_5])
deck.merge_sort
=> [card_1, card_3, card_4, card_5, card_2]
```
### Merge Sort Resources
* [Youtube CS50](https://youtu.be/Pr2Jf83_kG0)
* [Merge Sort Visualization](https://www.youtube.com/watch?v=ZRPoEKHXTJg)
* [Folk Dance](https://www.youtube.com/watch?v=XaqR3G_NVoo)
# Evaluation Rubric
## Functionality
* Student completes through Iteration 3
## Mechanics
The student:
* appropriately uses Ruby datatypes to solve a problem (including Strings, Integers, Floats, Ranges, Symbols, Nils, Arrays, and/or Hashes)
* implements best-choice enumerable methods to iterate over collections
* uses boolean expressions and flow control structures to logically manage a program's flow
* uses methods, arguments, and return values to break code into logical components
* creates Classes that utilize instance variables, attribute accessors, and instance methods
## Design
The student:
* adheres to the Single Responsibility and DRY principles
* creates Objects and Classes that appropriately encompass state and behavior
* uses instance and local variables appropriately
* writes readable code with the following characteristics:
* Variable and method names are self explanatory
* Methods are under 7 lines
* Lines of code are under 80 characters
* Project directory structure adheres to convention (uses `lib` and `spec` folders)
* A linter (e.g. Rubocop) reports less than 5 errors
## Testing
The student:
* writes RSpec tests that describe the expected behavior of a program according to technical specifications
* names and orders tests so that a test file reads like documentation
* writes RSpec assertions (`expect` statements) that accurately test a piece of functionality
* writes a test before writing code that implements the behavior to make that test pass
* writes both integration and unit tests
## Version Control
The student:
* hosts their code on the master branch of their remote repository
* makes commits in small chunks of functionality
* submits and merges Pull Requests using the Github interface