Skip to content

Guardia's Beginner Code

Guardia edited this page May 29, 2011 · 2 revisions
class Player
	@@max_health = 20
	@@low_health = 13
	@@critical_health = 7
	@@bestiary_of_the_ranged = {
		"Captive"=>false,
		"Sludge"=>false,
		"Thick Sludge"=>false,
		"Archer"=>true,
		"Wizard"=>true
	}
	
	def play_turn(warrior)	
		start_of_turn_checks(warrior)
		action(warrior)
		end_of_turn_checks(warrior)
	end
	
	def start_of_turn_checks(warrior)
		if @min_health == nil
			@min_health = @@low_health
		end
		
		if @previous_health == nil
			@previous_health = @@max_health
		end
		
		@took_damage = warrior.health < @previous_health
		
		@injured = warrior.health < @min_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