Class: WeewarAI::Faction

Inherits:
Object show all
Defined in:
lib/weewar-ai/faction.rb

Overview

Instances of the Faction class correspond to factions in a Game. They provide some utility methods about factions, such as whether or not a faction is currently playing in a Game, or whether it can afford to buy a unit type.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(game, xml, ordinal) ⇒ Faction

You should not need to instantiate any Faction s on your own; they are created for you by Game instances.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/weewar-ai/faction.rb', line 12

def initialize( game, xml, ordinal )
  @game, @ordinal = game, ordinal
  @credits = xml[ 'credits' ].to_i
  @current = ( xml[ 'current' ] == 'true' )
  @player_id = xml[ 'playerId' ].to_i
  @player_name = xml[ 'playerName' ]
  @state = xml[ 'state' ]
rescue Exception => e
  $stderr.puts "Input XML: " + xml.inspect
  raise e
end

Instance Attribute Details

#creditsObject (readonly)

Returns the value of attribute credits.



8
9
10
# File 'lib/weewar-ai/faction.rb', line 8

def credits
  @credits
end

#player_idObject (readonly) Also known as: playerId

Returns the value of attribute player_id.



8
9
10
# File 'lib/weewar-ai/faction.rb', line 8

def player_id
  @player_id
end

#player_nameObject (readonly) Also known as: playerName

Returns the value of attribute player_name.



8
9
10
# File 'lib/weewar-ai/faction.rb', line 8

def player_name
  @player_name
end

#stateObject (readonly)

Returns the value of attribute state.



8
9
10
# File 'lib/weewar-ai/faction.rb', line 8

def state
  @state
end

Instance Method Details

#can_afford?(type) ⇒ Boolean

True iff the faction has enough credits to purchase a unit of the given type.

i = me = my = game.my_faction
if i.can_afford? :hart
  my_base.build :hart
end

Returns:

  • (Boolean)


42
43
44
# File 'lib/weewar-ai/faction.rb', line 42

def can_afford?( type )
  @credits >= WeewarAI::Unit::UNIT_COSTS[ type ]
end

#current?Boolean

Whether or not this Faction is the one whose turn it is in the Game.

i = me = my = game.my_faction
is_my_turn = i.current?

Returns:

  • (Boolean)


29
30
31
# File 'lib/weewar-ai/faction.rb', line 29

def current?
  @current
end

#playing?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/weewar-ai/faction.rb', line 33

def playing?
  @state == 'playing'
end

#to_sObject



53
54
55
# File 'lib/weewar-ai/faction.rb', line 53

def to_s
  @player_name
end

#unitsObject

An Array of the Unit s in the Game belonging to this Faction.

i = me = my = game.my_faction
my_units = my.units


49
50
51
# File 'lib/weewar-ai/faction.rb', line 49

def units
  @game.units.find_all { |u| u.faction == self }
end