Skip to content

Guardia's Beginner Code

Guardia edited this page May 29, 2011 · 2 revisions
class Player

	def initialize
		@max_health = 20
		@low_health = 13
		@critical_health = 7
		@bestiary_of_the_ranged = {
			"Captive"=>false,
			"Sludge"=>false,
			"Thick Sludge"=>false,
			"Archer"=>true,
			"Wizard"=>true
		}
		
		@previous_health = @max_health
		
		@enemy_ahead = false
		@ranged_enemy = false
		@backtrack = false
	end
	
	def play_turn(warrior)	
		start_of_turn_checks(warrior)
		action(warrior)
		end_of_turn_checks(warrior)
	end
	
	def start_of_turn_checks(warrior)
		@took_damage = warrior.health < @previous_health
		
		@injured = warrior.health < @low_health
		
		@dieing = warrior.health < @critical_health
		
		@enemy_ahead = false
		@ranged_enemy = false
		@backtrack = false
	end
	
	def end_of_turn_checks(warrior)
		@previous_health = warrior.health
	end
	
	def survey_area(warrior)
		surroundings = warrior.look
			
		surroundings.each do |creature|
			if not creature.empty? and not creature.wall?
				puts "I see a #{creature} ahead!"
				@ranged_enemy = @bestiary_of_the_ranged[creature.to_s()]
				@enemy_ahead = creature.enemy?
				return
			end
		end
		
		surroundings = warrior.look(:backward)
		
		surroundings.each do |creature|
			if not creature.empty? and not creature.wall?
				puts "I see a #{creature} behind me!"
				@ranged_enemy = @bestiary_of_the_ranged[creature.to_s()]
				@enemy_ahead = creature.enemy?
				@backtrack = true
				return
			end
		end
	end
	
	def action(warrior)
		if warrior.feel.wall?
			warrior.pivot!
		elsif warrior.feel.captive?
			warrior.rescue!
		elsif warrior.feel.empty?
			survey_area(warrior)
			
			if @ranged_enemy
				warrior.shoot!
			elsif @injured and not @took_damage and (@enemy_ahead or @dieing)
				warrior.rest!
			elsif @backtrack
				warrior.pivot!
			else
				warrior.walk!
			end
		else
			warrior.attack!
		end
	end
end