Last active
April 6, 2017 23:55
-
-
Save fogonthedowns/26427907e3b845b9d0d02400b57ff5f5 to your computer and use it in GitHub Desktop.
Card game
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module ScoreRound | |
| SCORES = {"2"=>2, "3"=>3, "4"=>4, "5"=>5, "6"=>6, "7"=>7, "8"=>8, "9"=>9, "10"=>10, "J"=>11, "Q"=>12, "K"=>13, "A"=>14} | |
| def high_card(hand) | |
| hand.max_by do |card| | |
| p card.rank | |
| SCORES[card.rank] | |
| end | |
| end | |
| end | |
| class Card | |
| attr_accessor :rank, :suit | |
| def initialize(rank, suit) | |
| @rank = rank | |
| @suit = suit | |
| end | |
| end | |
| class Deck | |
| attr_accessor :cards | |
| def initialize | |
| @cards = %w(2 3 4 5 6 7 8 9 10 J Q K A).product(%w(Spade Heart Club Diamond)).shuffle.map{|card|Card.new(card[0], card[1])} | |
| end | |
| end | |
| class Game | |
| attr_accessor :deck, :players, :high_card, :number_cards_each | |
| def initialize(player_count, number_cards_each) | |
| @players = player_count | |
| @number_cards_each = number_cards_each | |
| @deck = Deck.new | |
| end | |
| end | |
| class Hand | |
| attr_accessor :hands | |
| include ScoreRound | |
| def initialize(game) | |
| @hands = {} | |
| self.deal(game) | |
| end | |
| def deal(game) | |
| game.deck.cards.sample(game.players * game.number_cards_each).each_slice(game.number_cards_each).each_with_index{|hand, index| @hands[index] = hand} | |
| end | |
| end | |
| @game = Game.new(2, 5) | |
| h = Hand.new(@game) | |
| p h | |
| p h.high_card(h.hands[0]) | |
| p h.high_card(h.hands[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment