Implement runs including face cards

This commit is contained in:
Jesse C. Fisher 2015-11-10 03:53:10 -08:00
parent 48b7b66f69
commit 4d230f755d
5 changed files with 19 additions and 13 deletions

View File

@ -23,7 +23,8 @@ var Inventory = React.createClass({
type="checkbox"
name="player_card_ids[]"
value={card.id} />
{card.rank} {card.suit}
{/* TODO: Don't hard code face cards here */}
{card.rank.replace('11','J').replace('12','Q').replace('13','K').replace('14','A').replace('15','2')} {card.suit}
</label>
</li>;
})}

View File

@ -1,5 +1,5 @@
class Card
RANKS = %w(3 4 5 6 7 8 9 10 J Q K A 2)
RANKS = %w(3 4 5 6 7 8 9 10 11 12 13 14 15)
SUITS = %w(Spade Club Diamond Heart)
attr_accessor :value, :rank, :suit

View File

@ -138,19 +138,18 @@ class Play < ActiveRecord::Base
when 5..13
@hand_type = "run of #{self.player_cards.length}"
@ranks = Array.new << self.player_cards.map {|pc| pc.rank}
@ranks.flatten!
@suits = Array.new << self.player_cards.map {|pc| pc.suit}
@suits.flatten!
@ranks = [self.player_cards.map {|pc| pc.rank}].flatten!.sort_by(&:to_i)
@suits = [self.player_cards.map {|pc| pc.suit}].flatten!
# double? && length?
if (@ranks.uniq.length == (@ranks.length/2)) && ([6,8,10].include? @ranks.length)
@hand_type = "double " + @hand_type
# run?
# iterate over each unique rank; except for the last rank
@ranks.uniq.sort[0...-1].each.with_index do |rank, i|
@ranks.uniq.sort_by(&:to_i)[0...-1].each.with_index do |rank, i|
# each rank one away from each other?
if (rank.to_i + 1) == @ranks.uniq.sort[i+1].to_i
if (rank.to_i + 1) == @ranks.uniq.sort_by(&:to_i)[i+1].to_i
# valid do nothing
else
@hand_type = "Invalid " + @hand_type
errors.add(:player_cards, "Invalid double run of #{@ranks.length}")
@ -159,10 +158,10 @@ class Play < ActiveRecord::Base
else
# normal run?
# For each card except for the last card
@ranks.sort[0...-1].each_with_index do |r, i|
@ranks.sort_by(&:to_i)[0...-1].each_with_index do |rank, i|
# If cards are not a single run
if (r.to_i + 1) != @ranks[i+1].to_i
# And if cards are not a double run
if (rank.to_i + 1) != @ranks[i+1].to_i
# And if cards are not a run
errors.add(:player_cards, "Invalid run of #{@ranks.length}")
end
end

View File

@ -3,6 +3,12 @@ class PlayerCard < ActiveRecord::Base
belongs_to :play
def to_s
[rank, suit].join ' '
# TODO: Shouldn't hard code face cards here
[rank, suit].join(' ')
.gsub('11','J')
.gsub('12','Q')
.gsub('13','K')
.gsub('14','A')
.gsub('15','2')
end
end

View File

@ -1,5 +1,5 @@
json.extract! @game, :id, :title, :created_at, :updated_at, :controlling_player_id
if @game.controlling_player == @game.try(:player_to_beat)
if @game.controlling_player_id == @game.player_to_beat.try(:id)
json.play_to_beat_string "None"
else
json.play_to_beat_string @game.play_to_beat.try(:play).try(:to_s)