Skip to content

Instantly share code, notes, and snippets.

@tarolandia
Created August 2, 2013 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tarolandia/6143117 to your computer and use it in GitHub Desktop.
Save tarolandia/6143117 to your computer and use it in GitHub Desktop.
class Player
@health = 20
@bottom = false
@pivot = false
@last_mode = nil
@last_move = nil
@mode = :walk
@move = :backward
@fullrecover = false
def play_turn(warrior)
if !@bottom
@bottom = @last_move == :backward && warrior.feel(:backward).wall?
end
@fullrecover = false if warrior.health == 20
@mode, @move = decide(warrior)
case @mode
when :rescue
warrior.rescue!(@move)
when :attack
warrior.attack!(@move)
when :heal
warrior.rest!
when :pivot
warrior.pivot!
else
warrior.walk!(@move)
end
check_situation(warrior)
end
private
def check_situation(warrior)
@last_move = @move
@last_mode = @mode
@health = warrior.health
end
def under_attack?(warrior)
@health > warrior.health
end
def can_fight?(warrior)
warrior.health > 10
end
def decide(warrior)
safe_move = false
if !@bottom
@move = :backward
else
if under_attack?(warrior) && !can_fight?(warrior)
@move = :backward
safe_move = true
else
@move = :forward
end
end
if safe_move
@mode = :walk
@fullrecover = true
else
@mode = :attack if warrior.feel(@move).enemy?
@mode = :rescue if warrior.feel(@move).captive?
@mode = :walk if warrior.feel(@move).empty?
@mode = :pivot if !@bottom
@mode = :heal if @fullrecover
end
return @mode, @move
end
end
@tarolandia
Copy link
Author

class Player
  @health = 20
  @bottom = false
  @pivot  = false
  @last_mode = nil
  @last_move = nil
  @mode = :walk
  @move = :backward
  @fullrecover = false
  @path = nil
  @look = true
  @shoot_arrow = false

  def initialize
    @health ||= 20
    @look = true
    super
  end

  def play_turn(warrior)
    if !@bottom
      @bottom = warrior.feel(:backward).wall? || warrior.feel(:forward).wall?
    end
    @fullrecover = false if warrior.health == 20

    @mode, @move = decide(warrior)

    case @mode
    when :rescue
      warrior.rescue!(@move)
    when :attack
      warrior.attack!(@move)
    when :heal
      warrior.rest!
    when :pivot
      warrior.pivot!
    when :look
      @path = warrior.look(@move)
    when :shoot
      warrior.shoot!
    else
      warrior.walk!(@move)
    end

    check_situation(warrior)
  end

  private

  def check_situation(warrior)
    @last_move = @move
    @last_mode = @mode
    @health = warrior.health
    @look = [:pivot, :shoot].include?(@last_move) ? true : !@look
    @shoot_arrow = !@path.nil? && @path[2].enemy? && !@path[0].enemy? && !@path[1].enemy?
  end

  def under_attack?(warrior)
    @health > warrior.health
  end

  def can_fight?(warrior)
    warrior.health > 10
  end

  def decide(warrior)
    safe_move = false
    if !@bottom
      @move = :backward
    else
      if under_attack?(warrior) && !can_fight?(warrior)
        @move = :backward
        safe_move = true
      else
        @move = :forward
      end
    end
    if safe_move
      @mode = :walk
      @fullrecover = true
    else
      @mode = :attack if warrior.feel(@move).enemy?
      @mode = :rescue if warrior.feel(@move).captive?
      @mode = :walk   if warrior.feel(@move).empty?
      @mode = :pivot  if !@bottom
      @mode = :look   if @look
      @mode = :shoot  if @shoot_arrow
      @mode = :heal   if @fullrecover
    end

    return @mode, @move
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment