-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBattle.rb
52 lines (45 loc) · 1.44 KB
/
Battle.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
class Battle
attr_accessor :turn, :notTurn, :trainer1, :trainer2, :finished, :level, :pokemon_count, :ready
def initialize(trainer1, trainer2, level=nil, pokemon_count=1)
@trainer1 = trainer1
@trainer2 = trainer2
@turn = @trainer1
@notTurn = @trainer2
@level = level
@pokemon_count = pokemon_count
@finished = false
@ready = false
end
def set_pokemon(trainer, pokemon_name, pokemon_level=@level)
pokemon = Pokemon.new(pokemon_name, pokemon_level)
trainer.add_pokemon(pokemon)
@ready = (@trainer1.pokemon_list == pokemon_count && @trainer2.pokemon_list == pokemon_count)
end
def get_trainer(nick)
return @trainer1 if @trainer1.nick == nick
return @trainer2 if @trainer2.nick == nick
end
def get_pokemon(nick)
return get_trainer(nick).active_pokemon
end
def get_moves(nick=nil)
return get_pokemon(nick).list_moves if nick
return @turn.active_pokemon.list_moves
end
def fight(moveNumber, nick=nil)
attacker = @turn.active_pokemon
defender = @notTurn.active_pokemon
message = attacker.fight(moveNumber, defender)
if defender.hp <= 0
message += "\nThe pokemon has fainted"
@notTurn.active_pokemon = @notTurn.alive_pokemon.first
message += "\n#{@notTurn.nick} has sent out #{@notTurn.active_pokemon.name}" if @notTurn.active_pokemon
end
if @notTurn.alive_pokemon.count <= 0
@finished = true
message += "\n#{@turn.nick} has won."
end
@turn, @notTurn = @notTurn, @turn
return message
end
end