Skip to content

platypusIntermediate

platypus edited this page Jan 22, 2012 · 2 revisions
class Player
  def initialize
	@bound = []
	@health = 20
	@detonate = 0
	@roped = 0
	@directions = [:forward, :right, :backward, :left]
	@bombs = 0
  end

  def play_turn(warrior)
	if stop_ticking(warrior) and warrior.feel(stop_ticking(warrior)).captive?
		warrior.rescue!(stop_ticking(warrior))
	elsif stop_ticking(warrior) != nil and warrior.feel(stop_ticking(warrior)).empty?
		if @bound.length != 0
			@roped = @bound.length
			@bound = []
		end
		@bombs = 0
		warrior.walk!(stop_ticking(warrior))
	elsif bind_enemy(warrior) != nil
		@bound.push(bind_enemy(warrior))
		warrior.bind!(bind_enemy(warrior))
	elsif get_health(warrior)
		warrior.rest!
	elsif detonate_bomb(warrior)
		warrior.detonate!
		@detonate = 2
		@bombs += 1
	elsif attack_enemy(warrior) != nil
		warrior.attack!(attack_enemy(warrior))
		@bound.delete(attack_enemy(warrior))
	elsif captive_rescue(warrior)
		warrior.rescue!(captive_rescue(warrior))
	else
		if @bound.length != 0
			@roped = @bound.length
			@bound = []
		end
	@bombs = 0
	warrior.walk!(walk_where(warrior))
	end
	@health = warrior.health
	if @detonate != 0
		@detonate -= 1
	else
	end
  end
  
  def walk_where(warrior)
	captive = Array.new
	empty = Array.new
	warrior.listen.each_index do |x|
		if warrior.listen[x].captive?
			captive.push(x)
		end
	end
	@directions.each do |x|
		if warrior.feel(x).empty? and !warrior.feel(x).stairs?
			empty.push(x)
		end
	end
	if captive[0] != nil and !warrior.feel(warrior.direction_of(warrior.listen[captive[0]])).stairs?
		return warrior.direction_of(warrior.listen[captive[0]])
	elsif warrior.listen[0] and !warrior.feel(warrior.direction_of(warrior.listen[0])).stairs?
		return warrior.direction_of(warrior.listen[0])
	elsif warrior.listen[0] 
		return empty[0]
	else
		return warrior.direction_of_stairs
	end
  end
  
  def attack_enemy(warrior)
	enemy = nil
	captive = nil
	@directions.each do |x|
		if warrior.feel(x).enemy?
			enemy = x
		elsif warrior.feel(x).captive?
			captive = x
		end		
	end
	if enemy != nil
		return enemy
	elsif captive != nil and warrior.feel(captive).unit.character != "C"
		return captive
	else
		return @bound[0]
	end
  end
  
  def bind_enemy(warrior)
	enemy = Array.new
	@directions.each do |x|
		if warrior.feel(x).enemy?
			enemy.push(x)
		else
		end
	end
	if enemy.length > 1 and enemy[0] == stop_ticking(warrior)
		return enemy[1]
	elsif enemy.length > 1
		return enemy [0]
	else
		return nil
	end
  end

  def get_health(warrior)
	health = []
	warrior.listen.each_index do |x|
		if warrior.listen[x].enemy?
			health.push(x)
		else
		end
	end
	if health.length == 0 and @bound.length == 0 and !attack_captive(warrior)
		return false
	elsif !warrior.listen[0]
		return false
	elsif warrior.health <= minimum_health(warrior) and warrior.health >= @health 
		return true
	elsif warrior.health <= minimum_health(warrior) and @detonate == 1 and warrior.health >= @health - 4
		return true
	end
  end

  def stop_ticking(warrior)
	ticking = Array.new
	warrior.listen.each_index do |x|
		if warrior.listen[x].ticking?
			ticking.push(x)
		else
		end
	end
	if ticking[0] and @bombs != 2 and (warrior.health > 10 or warrior.health == @health) 
			return warrior.direction_of(warrior.listen[ticking[0]])
	end
  end

  def detonate_bomb(warrior)
	unsafe = 0
	warrior.listen.each_index do |x|
		if warrior.listen[x].captive? and warrior.distance_of(warrior.listen[x]) > unsafe
			unsafe = warrior.distance_of(warrior.listen[x])
		end
	end
	if warrior.look[0].enemy? and warrior.look[1].enemy? and unsafe > 2
		return true
	elsif @bombs == 2 and unsafe > 2
		return true
	else
		return false
	end
  end
 
  def minimum_health(warrior)
	health = 1
	sludge = 0
	thick = 0
	warrior.listen.each do |x|
		if x.unit.character == "s"
			health += 6
			sludge += 1
		elsif x.unit.character == "S"
			health += 11
			thick += 1
		end
		if attack_captive(warrior) and sludge + thick != 0
			health += 1
		end
		if health > 18
			health = 18
		end
	end
	return health
  end

  def captive_rescue(warrior)
	captive = Array.new
	@directions.each do |x|
		if warrior.feel(x).captive?
			captive.push(x)
		else
		end
	end
	return captive[0]
  end

  def attack_captive(warrior)
	check = 0
	warrior.listen.each_index do |x|
		if warrior.listen[x].captive?  
			check += 1
		end
	end
	if @roped >= check
		return true
	else
		return false
	end
  end
end