diff --git a/README.md b/README.md index bee7dc7..e461717 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ TODO - Remove PlayToBeat model and table. -- Test `play_hand` of run including face cards +- Implement run including face cards - BUG TODO: First player / lowest card holder should not be able to pass first play diff --git a/app/models/play.rb b/app/models/play.rb index f02c059..59632e8 100644 --- a/app/models/play.rb +++ b/app/models/play.rb @@ -33,6 +33,7 @@ class Play < ActiveRecord::Base # Validation methods def hand_valid? + return false if self.hand_type == nil # Gather the truths about the hand @truths = [] @@ -42,6 +43,11 @@ class Play < ActiveRecord::Base if game.try(:hand_to_beat) # current player is not the player to beat if (game.player_to_beat != player) + # valid bomb? + if (self.hand_type == 'bomb' && game.play_to_beat.try("play").try("hand_type") == 'single' && game.play_to_beat.try("play").try("player_cards")[0].try('rank') == '2') + @truths << true + return @truths + end # hand_type match? @truths << (self.hand_type == game.play_to_beat.try("play").try("hand_type")) if game.play_to_beat.try("play").try(:hand_type) # beats? @@ -86,6 +92,8 @@ class Play < ActiveRecord::Base # TODO: Recognize face cards def hand_type case self.player_cards.length + when 0 + @hand_type = 'empty' when 1 @hand_type = 'single' when 2 diff --git a/spec/models/play_spec.rb b/spec/models/play_spec.rb index 1d91ca8..2ec4143 100644 --- a/spec/models/play_spec.rb +++ b/spec/models/play_spec.rb @@ -287,15 +287,68 @@ RSpec.describe Play, type: :model do end context 'when four cards are played' do - it 'valid bomb' do - @spade3 = PlayerCard.new(rank: "3", suit: "Spade") - @club3 = PlayerCard.new(rank: "3", suit: "Club") - @diamond3 = PlayerCard.new(rank: "3", suit: "Diamond") - @heart3 = PlayerCard.new(rank: "3", suit: "Heart") + it 'valid bomb against a 2' do + @heart2 = PlayerCard.new(rank: '2', suit: 'Heart', value: 52, player_id: @game.next_player_id) + @play_to_beat = Play.new(game_id: @game.id, player_id: @game.controlling_player_id, player_cards: [@heart2]) + @play_to_beat.save + @game.play_to_beat_id = @play_to_beat.id + @spade3 = PlayerCard.new(value: 1, rank: "3", suit: "Spade") + @club3 = PlayerCard.new(value: 2, rank: "3", suit: "Club") + @diamond3 = PlayerCard.new(value: 3, rank: "3", suit: "Diamond") + @heart3 = PlayerCard.new(value: 4, rank: "3", suit: "Heart") @cards_to_play = [@spade3, @club3, @diamond3, @heart3] @play.player_cards << @cards_to_play expect(@play.hand_type).to eq('bomb') + expect(@play.valid?).to eq(true) end + it 'valid bomb against a lesser bomb' do + # Setup the play to beat + @spade3 = PlayerCard.new(rank: "3", suit: "Spade", value: 1) + @club3 = PlayerCard.new(rank: "3", suit: "Club", value: 2) + @diamond3 = PlayerCard.new(rank: "3", suit: "Diamond", value: 3) + @heart3 = PlayerCard.new(rank: "3", suit: "Heart", value: 4) + @cards_to_beat = [@spade3, @club3, @diamond3, @heart3] + @play_to_beat = Play.new(game_id: @game.id, player_id: @game.controlling_player_id, player_cards: @cards_to_beat) + @play_to_beat.save + @game.play_to_beat_id = @play_to_beat.id + + # Setup the play + @spade4 = PlayerCard.new(rank: "4", suit: "Spade", value: 5) + @club4 = PlayerCard.new(rank: "4", suit: "Club", value: 6) + @diamond4 = PlayerCard.new(rank: "4", suit: "Diamond", value: 7) + @heart4 = PlayerCard.new(rank: "4", suit: "Heart", value: 8) + @cards_to_play = [@spade4, @club4, @diamond4, @heart4] + @play.player_cards = @cards_to_play + + expect(@play.hand_type).to eq('bomb') + expect(@play.beats? @play_to_beat).to eq(true) + expect(@play.valid?).to eq(true) + end + + it 'invalid bomb against a greater bomb' do + # Setup the play to beat + @spade4 = PlayerCard.new(rank: "4", suit: "Spade", value: 5) + @club4 = PlayerCard.new(rank: "4", suit: "Club", value: 6) + @diamond4 = PlayerCard.new(rank: "4", suit: "Diamond", value: 7) + @heart4 = PlayerCard.new(rank: "4", suit: "Heart", value: 8) + @cards_to_beat = [@spade4, @club4, @diamond4, @heart4] + @play_to_beat = Play.new(game_id: @game.id, player_id: @game.next_player_id, player_cards: @cards_to_beat) + @play_to_beat.save + @game.play_to_beat_id = @play_to_beat.id + + # Setup the play + @spade3 = PlayerCard.new(rank: "3", suit: "Spade", value: 1) + @club3 = PlayerCard.new(rank: "3", suit: "Club", value: 2) + @diamond3 = PlayerCard.new(rank: "3", suit: "Diamond", value: 3) + @heart3 = PlayerCard.new(rank: "3", suit: "Heart", value: 4) + @cards_to_play = [@spade3, @club3, @diamond3, @heart3] + @play.player_cards = @cards_to_play + + expect(@play.hand_type).to eq('bomb') + expect(@play.beats? @play_to_beat).to eq(false) + expect(@play.valid?).to eq(false) + end + it 'valid run of 4' do @spade3 = PlayerCard.new(rank: "3", suit: "Spade") @club4 = PlayerCard.new(rank: "4", suit: "Club") @@ -338,6 +391,7 @@ RSpec.describe Play, type: :model do expect(@play.hand_type).to eq('run of 5') expect(@play.errors[:player_cards]).to eq(['Invalid run of 5']) end + it 'valid run of 5 suited' do @spade3 = PlayerCard.new(rank: "3", suit: "Spade") @club4 = PlayerCard.new(rank: "4", suit: "Spade")