Skip to content

Instantly share code, notes, and snippets.

@mishin
Created July 28, 2013 23:59
Show Gist options
  • Save mishin/6101366 to your computer and use it in GitHub Desktop.
Save mishin/6101366 to your computer and use it in GitHub Desktop.
class Player
#########################
# Player Initialization #
#########################
# Initialize instance variables.
def initialize
@prv_health = 20 # What was my previous health?
@cur_health = 20 # What is my current health?
end
# List of possible warrior actions.
Actions = [:check_health,
:attack_monster,
:move_warrior]
###########################
# Pre and Post Processing #
###########################
# Understand environment prior to taking action.
def pre_sense
# Reset warrior action from previous turn.
@action_taken = false
# Determine health and surroundings.
@space_empty = @warrior.feel.empty?
@cur_health = @warrior.health
# Determine combat state.
@in_combat = in_combat?
end
# Perform state analysis for net turn.
def post_sense
@prv_health = @warrior.health
end
##################
# Action Methods #
##################
def check_health
if (@cur_health < 15) && (@in_combat == :no)
@warrior.rest!
@action_taken = true
end
end
def attack_monster
case @in_combat
when :near # Enemy is adjacent.
@warrior.attack!
@action_taken = true
when :far # Walk to enemy.
@warrior.walk!
@action_taken = true
end
end
def move_warrior
@warrior.walk!
end
##################
# Helper Methods #
##################
# Did I lose health from last turn to this one?
def lost_health?
(@cur_health - @prv_health) < 0 ? true : false
end
# Am I in combat? If so, what "kind" of combat?
def in_combat?
combat_state = :no
combat_state = :near if (@space_empty == false)
combat_state = :far if (@space_empty == true) && (lost_health? == true)
combat_state
end
#####################
# Logic for Actions #
#####################
def perform_action
Actions.each do |action|
send action
break if @action_taken == true
end
end
####################
# Play Turn Method #
####################
def take_turn
pre_sense # Gather information on environment.
perform_action # Perform action based on known items.
post_sense # Store state of warrior for next turn.
end
def play_turn(warrior)
# Allow warrior to be accessed in all sections.
@warrior = warrior
# Take turn for the warrior.
take_turn
end
end
---------------
class Player
#########################
# Player Initialization #
#########################
def initialize
@action_taken = false # Did I take my action?
@in_combat = false # Am I in combat?
end
##################
# Helper Methods #
##################
def sense_environment(warrior)
@action_taken = false # Reset environment.
# No longer in combat.
@in_combat = false if (warrior.feel.empty? == true)
end
def check_health(warrior)
if (warrior.health < 15) && (@in_combat == false)
warrior.rest!
@action_taken = true
end
end
def attack_monster(warrior)
if (warrior.feel.empty? == false)
warrior.attack!
@action_taken = true
@in_combat = true
else
@in_combat = false
end
end
def move_warrior(warrior)
warrior.walk! if @action_taken == false
@action_taken = true
end
####################
# Play Turn Method #
####################
def play_turn(warrior)
# Sense environment for clues.
sense_environment(warrior)
# Check health of the warrior.
check_health(warrior)
# Attack monster if present.
attack_monster(warrior)
# Move warrior in a specific direction.
move_warrior(warrior)
end
end
------------------------------
class Player
def play_turn(warrior)
if being_attacked?(warrior,@health)
charge(warrior)
elsif (warrior.health < 10) then
warrior.rest!
end
@health=warrior.health
end
end
def charge(warrior)
if warrior.feel.empty?
warrior.walk!
else
warrior.attack!
end
end
def being_attacked?(warrior,@health)
if (@health == nil) or (warrior.health >= to @health)
@health = warrior.health
return false
else
@health = warrior.health
return true
end
end
-------------------------
class Player
def play_turn(warrior)
if (warrior.feel.empty?) then
warrior.walk!
elsif (@health==nil) (warrior.health < 10) then
warrior.rest!
elsif(warrior.feel.empty? == false) then
warrior.attack!
elsif (warrior.health < 10) then
warrior.rest!
elsif ((@health>warrior.health)==false) then
warrior.rest!
end
@health=warrior.health
end
end
elsif (warrior.health < 10) then
warrior.rest!
def being_attacked?(warrior)
if @health == nil OR warrior.health >= to @health
@health = warrior.health
return false
else
@health = warrior.health
return true
end
end
def play_turn(warrior)
if being_attacked?(warrior)
if warrior.feel.empty?
warrior.walk!
else
warrior.attack!
end
else
# ... other code
end
end
def charge(warrior)
if warrior.feel.empty?
warrior.walk!
else
warrior.attack!
end
end
(@health==nil) or
puts " Not being attacked, health: #{warrior.health}"
elsif (warrior.feel.empty?) then
warrior.walk!
elsif (warrior.health < 10) and not(@health>warrior.health) then
warrior.rest!
def play_turn(warrior)
if being_attacked?(warrior)
charge(warrior)
else
# ... other code
end
end
def charge(warrior)
if warrior.feel.empty?
warrior.walk!
else
warrior.attack!
end
end
# http://tutorials.jumpstartlab.com/codenow/ruby_warrior.html
def play_turn(warrior)
if being_attacked?(warrior)
if warrior.feel.empty?
warrior.walk!
else
warrior.attack!
end
else
# ... other code
end
end
def being_attacked?(warrior)
if @health is nil OR warrior health is greater than or equal to @health
update @health with the current health
return false
else
update @health with the current health
return true
end
end
puts " Not being attacked, health: #{warrior.health}"
if I have more than 10 HPs left
if there''s an empty spot in front of me
move forward
else
attack that sludge!
end
else
rest up
end
if (warrior.health < 20) then
warrior.rest!
end
walk/rest/attack
When there is no enemy ahead of you call warrior.rest!
until health is full before walking forward.
warrior.walk!
if warrior.health<20
then warrior.rest!
end
if space.enemy?
then
warrior.walk!
else
warrior.attack!
end
if warrior.health<20
then warrior.rest!
end
until warrior.health == 20
warrior.rest!
end
if warrior.feel.empty?
then
warrior.walk!
else
if space.enemy?
then
warrior.attack!
warrior.rest!
end
end
full_health=warrior.health
if warrior.feel.empty?
then warrior.walk!
else warrior.attack!
end
if warrior.health < full_health
loop do
warrior.rest!
break if warrior.health==full_health
end
end
full_health
i+=1
print "#{i}"
break if warrior.health == 20
end
break if warrior.health == 20
end
full_health=warrior.health
loop do
warrior.rest!
break if warrior.health==full_health
end
if warrior.health < 20
warrior.rest! while i warrior.health < 20
end
if space.empty? then
warrior.walk!
else if space.enemy? then
warrior.attack!
end
warrior.rest!
if space.empty?
then
if warrior.health < 20
warrior.rest! while i warrior.health < 20
end
if space.enemy? then
warrior.attack!
else
end
end
warrior.rest!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment