Class: WarSimulator::Game
- Inherits:
-
Object
- Object
- WarSimulator::Game
- Defined in:
- lib/war_simulator/game.rb
Constant Summary collapse
- WAR_CARDS =
3
Instance Attribute Summary collapse
-
#deck ⇒ Object
Returns the value of attribute deck.
-
#p1_hand ⇒ Object
Returns the value of attribute p1_hand.
-
#p2_hand ⇒ Object
Returns the value of attribute p2_hand.
-
#war_hold ⇒ Object
Returns the value of attribute war_hold.
Instance Method Summary collapse
- #deal ⇒ Object
- #flatten_hands ⇒ Object
-
#initialize ⇒ Game
constructor
A new instance of Game.
- #pick_and_compare ⇒ Object
- #player_has_no_cards? ⇒ Boolean
- #setup_war ⇒ Object
- #simulate ⇒ Object
Constructor Details
Instance Attribute Details
#deck ⇒ Object
Returns the value of attribute deck.
6 7 8 |
# File 'lib/war_simulator/game.rb', line 6 def deck @deck end |
#p1_hand ⇒ Object
Returns the value of attribute p1_hand.
6 7 8 |
# File 'lib/war_simulator/game.rb', line 6 def p1_hand @p1_hand end |
#p2_hand ⇒ Object
Returns the value of attribute p2_hand.
6 7 8 |
# File 'lib/war_simulator/game.rb', line 6 def p2_hand @p2_hand end |
#war_hold ⇒ Object
Returns the value of attribute war_hold.
6 7 8 |
# File 'lib/war_simulator/game.rb', line 6 def war_hold @war_hold end |
Instance Method Details
#deal ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/war_simulator/game.rb', line 18 def deal raise 'Cards already delt' if @deck.cards.nil? cut = @deck.cards.each_slice(@deck.cards.count / 2).to_a @deck.cards = nil @p1_hand = cut[0] @p2_hand = cut[1] end |
#flatten_hands ⇒ Object
96 97 98 99 100 |
# File 'lib/war_simulator/game.rb', line 96 def flatten_hands @p1_hand.flatten! @p2_hand.flatten! @war_hold.flatten! end |
#pick_and_compare ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/war_simulator/game.rb', line 66 def pick_and_compare p1_pick = @p1_war_cards.shuffle.first p2_pick = @p2_war_cards.shuffle.first @war_hold << @p1_war_cards @war_hold << @p2_war_cards @p1_war_cards = [] @p2_war_cards = [] flatten_hands if p1_pick.number == p2_pick.number setup_war elsif p1_pick.number > p2_pick.number @p1_hand << @war_hold @war_hold = [] else @p2_hand << @war_hold @war_hold = [] end end |
#player_has_no_cards? ⇒ Boolean
92 93 94 |
# File 'lib/war_simulator/game.rb', line 92 def player_has_no_cards? @p1_hand.count == 0 || @p2_hand.count == 0 end |
#setup_war ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/war_simulator/game.rb', line 54 def setup_war WAR_CARDS.times do @p1_war_cards << @p1_hand.shift unless @p1_hand.empty? and return @p2_war_cards << @p2_hand.shift unless @p2_hand.empty? and return end @p1_war_cards.flatten! @p2_war_cards.flatten! pick_and_compare end |
#simulate ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/war_simulator/game.rb', line 27 def simulate battles = 0 until player_has_no_cards? battles += 1 if @p1_hand.first.number == @p2_hand.first.number @war_hold << @p1_hand.shift @war_hold << @p2_hand.shift setup_war elsif @p1_hand.first.number > @p2_hand.first.number @p1_hand << @p2_hand.shift @p1_hand << @p1_hand.shift else @p2_hand << @p1_hand.shift @p2_hand << @p2_hand.shift end flatten_hands end battles end |