Permalink
Browse files

Say Hello to Mr. Norris

  • Loading branch information...
1 parent 085f657 commit 763a3fc39bbe458eefddbf0152332dd638bfa1c7 @Omnipresent Omnipresent committed Apr 12, 2012
Showing with 82 additions and 17 deletions.
  1. +7 −17 engine.rb
  2. +75 −0 players/omnipresent.rb
View
@@ -1,45 +1,37 @@
#!/usr/bin/env ruby
require 'debugger'
-#This creates the require_dir method that is used for requiring the engine, players, and monsters directories below
def require_dir(dir)
Dir["#{dir}/*.rb"].each do |file|
require File.join(File.dirname(__FILE__), file )
end
end
-#This requires the engine directory
require_dir("engine")
modules = []
-ObjectSpace.each_object(Module) {|m| modules << m.name } #Traverses all objects that are modules or subclasses of modules
- #And adds them to the modules array
-
+ObjectSpace.each_object(Module) {|m| modules << m.name }
require_dir("players")
player_modules = []
ObjectSpace.each_object(Module) do |m|
player_modules << m.name unless modules.include?(m.name)
end
-#IThis imports the monsters directory
require_dir("monsters")
-#This creates a new Game object
game = Game.new
player_modules.each do |m|
constant = Object
player = Player.new
- game.players << player #adds each player to the game's player array
-
- p = PlayerProxy.new(player) #I'm not sure what the player proxy is
+ game.players << player
+ p = PlayerProxy.new(player)
p.extend constant.const_get(m)
player.proxy = p
game.proxies << p
end
-#This generates new monsters
-20.times do
+5.times do
monster = Monster.new
game.players << monster
r = PlayerProxy.new(monster)
@@ -48,13 +40,11 @@ def require_dir(dir)
game.proxies << r
end
-#Rreceive the round count, and does something with a regex for any other command line arguments
if ARGV.size > 1 and ARGV[0] == "-r" and ARGV[1] =~ /^[1-9]\d*$/
ARGV.shift
- round_count = ARGV.shift.to_i
+ rount_count = ARGV.shift.to_i
else
- round_count = 10
+ rount_count = 10
end
-#This iterates the rounds
-game.round(round_count)
+game.round(rount_count)
View
@@ -0,0 +1,75 @@
+module Omnipresent
+ RISKY_HEALTH_LEVEL = 30
+ DONT_GET_GANGED_UP = 60
+ GOOD_HEALTH = 70
+ PREV_WORST_HEALTH = 100
+ def move
+ return [:attack, confirm_dead_after_attack[0]] unless confirm_dead_after_attack[0].nil?
+
+ return play_aggressive if (stats[:health] > GOOD_HEALTH && current_spot != 0)
+
+ return show_fight if (stats[:health] <= DONT_GET_GANGED_UP && current_spot == 0)
+
+ [:rest]
+ end
+
+ def to_s
+ "Chuck Norris"
+ end
+
+ private
+
+ def show_fight
+ [:attack, sort_by_health[0]]
+ end
+
+ def play_aggressive
+ @attack = false
+ prev_worst_health= PREV_WORST_HEALTH
+ others.each do |other|
+ worst_health_of_player = other.stats[:health] - opponent_health_after_my_attack(other)
+ my_loss = health_i_would_lose(other)
+ if (worst_health_of_player < prev_worst_health && more_exp_than_me?(other) )
+ prev_worst_health = worst_health_of_player
+ @opponent = other
+ @attack = true
+ end
+ end
+ if @attack && !risky_health_level?
+ [:attack, @opponent]
+ else
+ [:rest]
+ end
+ end
+
+ def more_exp_than_me?(other)
+ other.stats[:experience] >= stats[:experience]
+ end
+ def current_spot
+ sort_by_experience.index{|other| other.to_s =~ /Chuck Norris/}
+ end
+ def sort_by_experience
+ Game.world[:players].sort{|a,b| b.stats[:experience]<=>a.stats[:experience]}
+ end
+ def sort_by_health
+ others.sort{|a,b| a.stats[:health]<=>b.stats[:health]}
+ end
+ def risky_health_level?
+ stats[:health] <= RISKY_HEALTH_LEVEL
+ end
+ def others
+ Game.world[:players].select { |player| player != self }
+ end
+ def points_if_attack(other)
+ stats[:strength] - other.stats[:defense]/2
+ end
+ def confirm_dead_after_attack
+ others.select{|other| opponent_health_after_my_attack(other) <= 0}
+ end
+ def opponent_health_after_my_attack(other)
+ other.stats[:health] - points_if_attack(other)
+ end
+ def health_i_would_lose (other)
+ stats[:health] - points_if_attack(other)
+ end
+end

0 comments on commit 763a3fc

Please sign in to comment.