Class: StackWars::Territory

Inherits:
Object
  • Object
show all
Defined in:
lib/stack_wars/territory.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_strengthObject (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

#occupantObject (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

Returns:

  • (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

#columnObject



71
72
73
# File 'lib/stack_wars/territory.rb', line 71

def column
  @position[0]
end

#controlled_by?(player) ⇒ Boolean

Returns:

  • (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

Returns:

  • (Boolean)


44
45
46
# File 'lib/stack_wars/territory.rb', line 44

def occupied?
  !!@occupant
end

#occupied_by?(player) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/stack_wars/territory.rb', line 52

def occupied_by?(player)
  @occupant == player.color
end

#remove_one_armyObject



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

#rowObject



67
68
69
# File 'lib/stack_wars/territory.rb', line 67

def row
  @position[1]
end

#unoccupied?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/stack_wars/territory.rb', line 48

def unoccupied?
  !occupied?
end