Class: StackWars::Territory
- Inherits:
-
Object
- Object
- StackWars::Territory
- Defined in:
- lib/stack_wars/territory.rb
Instance Attribute Summary collapse
-
#army_strength ⇒ Object
readonly
Returns the value of attribute army_strength.
-
#occupant ⇒ Object
readonly
Returns the value of attribute occupant.
Instance Method Summary collapse
- #add_one_army(player) ⇒ Object
- #baseline_for?(player) ⇒ Boolean
- #column ⇒ Object
- #controlled_by?(player) ⇒ Boolean
- #fortify(player) ⇒ Object
-
#initialize(position, params = {}) ⇒ Territory
constructor
A new instance of Territory.
- #occupied? ⇒ Boolean
- #occupied_by?(player) ⇒ Boolean
- #remove_one_army ⇒ Object
- #row ⇒ Object
- #unoccupied? ⇒ Boolean
Constructor Details
#initialize(position, params = {}) ⇒ Territory
Returns a new instance of Territory.
3 4 5 6 7 8 9 10 11 12 |
# File 'lib/stack_wars/territory.rb', line 3 def initialize(position, params={}) @position = position @occupant = params.fetch(:occupant, nil) if occupied? @army_strength = params.fetch(:army_strength) else @army_strength = 0 end end |
Instance Attribute Details
#army_strength ⇒ Object (readonly)
Returns the value of attribute army_strength.
14 15 16 |
# File 'lib/stack_wars/territory.rb', line 14 def army_strength @army_strength end |
#occupant ⇒ Object (readonly)
Returns the value of attribute occupant.
14 15 16 |
# File 'lib/stack_wars/territory.rb', line 14 def occupant @occupant end |
Instance Method Details
#add_one_army(player) ⇒ Object
20 21 22 23 24 |
# File 'lib/stack_wars/territory.rb', line 20 def add_one_army(player) @army_strength += 1 @occupant ||= player.color end |
#baseline_for?(player) ⇒ Boolean
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/stack_wars/territory.rb', line 56 def baseline_for?(player) case player.color when "white" row == Battlefield::WHITE_BASELINE when "black" row == Battlefield::BLACK_BASELINE else raise ArgumentError, "#{color} is not a valid color" end end |
#column ⇒ Object
71 72 73 |
# File 'lib/stack_wars/territory.rb', line 71 def column @position[0] end |
#controlled_by?(player) ⇒ Boolean
16 17 18 |
# File 'lib/stack_wars/territory.rb', line 16 def controlled_by?(player) baseline_for?(player) || occupied_by?(player) end |
#fortify(player) ⇒ Object
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/stack_wars/territory.rb', line 33 def fortify(player) if controlled_by?(player) player.deploy_army add_one_army(player) @occupant ||= player.color else raise Errors::IllegalMove end end |
#occupied? ⇒ Boolean
44 45 46 |
# File 'lib/stack_wars/territory.rb', line 44 def occupied? !!@occupant end |
#occupied_by?(player) ⇒ Boolean
52 53 54 |
# File 'lib/stack_wars/territory.rb', line 52 def occupied_by?(player) @occupant == player.color end |
#remove_one_army ⇒ Object
26 27 28 29 30 31 |
# File 'lib/stack_wars/territory.rb', line 26 def remove_one_army raise Errors::IllegalMove unless @army_strength > 0 @army_strength -= 1 @occupant = nil if @army_strength == 0 end |
#row ⇒ Object
67 68 69 |
# File 'lib/stack_wars/territory.rb', line 67 def row @position[1] end |
#unoccupied? ⇒ Boolean
48 49 50 |
# File 'lib/stack_wars/territory.rb', line 48 def unoccupied? !occupied? end |