Created
March 1, 2013 18:27
-
-
Save Problematic/5066685 to your computer and use it in GitHub Desktop.
rubywarrior
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Player | |
| def initialize | |
| @directions = [:forward, :right, :backward, :left] | |
| @initialized = false | |
| @state = :exploring | |
| @explored = Set.new | |
| @unexplored = Set.new | |
| end | |
| def damaged_last_turn? | |
| @warrior.health < @health | |
| end | |
| def needs_rest? | |
| return @warrior.health <= 3 | |
| end | |
| def safe_to_rest? | |
| @directions.each{|direction| | |
| if @warrior.feel(direction).enemy? | |
| return false | |
| elsif bow_target? direction and clear_shot? direction | |
| return false | |
| end | |
| } | |
| return true | |
| end | |
| def clear_shot? direction=:forward | |
| @directions.each{|direction| | |
| if @warrior.feel(direction).enemy? | |
| return false | |
| end | |
| } | |
| if not direction == :backward | |
| backspaces = @warrior.look(:backward).to_s | |
| if backspaces.include? 'Wizard' or backspaces.include? 'Archer' | |
| return false | |
| end | |
| end | |
| spaces = @warrior.look direction | |
| if spaces[0].captive? | |
| return false | |
| elsif spaces[1].captive? and spaces[2].enemy? | |
| return false | |
| elsif spaces[2].captive? | |
| return false | |
| else | |
| return true | |
| end | |
| end | |
| def bow_target? direction=:forward | |
| spaces = @warrior.look direction | |
| spaces.each{|space| | |
| if space.enemy? | |
| return true | |
| end | |
| } | |
| return false | |
| end | |
| def current_location? | |
| x1,y1 = @warrior.feel.location | |
| x2,y2 = @warrior.feel(:backward).location | |
| [(x1+x2)/2,(y1+y2)/2] | |
| end | |
| def observe | |
| @explored.add current_location? | |
| @unexplored -= [current_location?] | |
| @directions.each{|direction| | |
| spaces = @warrior.look direction | |
| spaces.each {|space| | |
| if not @explored.include? space.location and not space.wall? | |
| @unexplored.add space.location | |
| end | |
| if not space.empty? and not space.stairs? | |
| end | |
| } | |
| } | |
| end | |
| def sally_forth! | |
| if needs_rest? and safe_to_rest? | |
| if damaged_last_turn? | |
| @state = :retreating | |
| else | |
| @state = :resting | |
| end | |
| end | |
| case @state | |
| when :resting | |
| if @warrior.health < (@max_health / 2) | |
| @warrior.rest! | |
| else | |
| @state = :exploring | |
| sally_forth! | |
| end | |
| when :attacking | |
| if not @warrior.feel.enemy? | |
| if bow_target? and clear_shot? | |
| @warrior.shoot! | |
| else | |
| @state = :exploring | |
| sally_forth! | |
| end | |
| else | |
| @warrior.attack! | |
| end | |
| when :retreating | |
| @warrior.walk! :backward | |
| when :exploring | |
| if @warrior.feel.enemy? or (bow_target? and clear_shot?) | |
| @state = :attacking | |
| sally_forth! | |
| elsif @warrior.feel.captive? | |
| @warrior.rescue! | |
| elsif @warrior.feel.wall? | |
| @warrior.pivot! | |
| elsif @warrior.feel.stairs? | |
| if @unexplored.size > 1 # the stairs tile | |
| @warrior.pivot! | |
| else | |
| @warrior.walk! | |
| end | |
| else | |
| @warrior.walk! | |
| end | |
| end | |
| end | |
| def play_turn(warrior) | |
| if not @initialized | |
| @max_health = warrior.health | |
| @health = warrior.health | |
| @initialized = true | |
| end | |
| @warrior = warrior | |
| observe | |
| sally_forth! | |
| @health = @warrior.health | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment