Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #56 from davidk01/master

more revisions
  • Loading branch information...
commit 54e34093b1ab1ac383261839418abef0e9f81c8a 2 parents e21aa98 + 348aa4b
Reed G. Law authored
Showing with 15 additions and 52 deletions.
  1. +15 −52 players/davidk.rb
67 players/davidk.rb
View
@@ -1,5 +1,5 @@
module DavidK
- # move recursion call mitigation
+ # `move` recursion call mitigation
def self.extended(base)
base.instance_variable_set :@move_call_depth, 0
base.instance_variable_set :@move_callee, nil
@@ -14,45 +14,29 @@ def to_s
"david karapetyan"
end
- # if there are no checks then if another player tries to check our moves this will lead to
- # an infinite loop because killable_opponent will call move on another player which will call
- # move on us and so on and so forth
def move
- @move_call_depth += 1
- if @move_call_depth > 1 # this means somebody is trying to figure out what we are doing so we lie
+ action = [:rest] # rest is the default
+ if (@move_call_depth += 1) > 1 # this means somebody is trying to figure out what we are doing so we lie
@move_call_depth -= 1
- return [:rest]
+ return action
end
- action = [:rest]
- opponent, full_speed = killable_opponent
- if (stats[:health] >= 50 || full_speed) && !opponent.nil?
+ opponent = pick_opponent
+ if stats[:health] >= 90
action = [:attack, opponent]
end
@move_call_depth -= 1
- return action
+ action
end
private
-
- # find somebody we can kill by relaxing the number of hits
- def n_hit_killables(opponents)
- n = 1
- while (possible_opponents = opponents.select {|o| can_kill_in_n_hits?(o, n)}).empty?
- n += 1
- end
- possible_opponents
- end
- # go through all players other than self and build up some hash maps that can be used
- # for picking an opponent to attack
def gang_score_and_aggro(opponents)
g_score = Hash.new(0)
predator_prey_relation = Hash.new([])
opponents.each do |p|
if p.respond_to?(:move) # monsters don't respond to move
@move_callee, player_move = p, p.move
- if player_move.length == 2 then # see if the player is attacking somebody
- attackee = player_move[1] # who is the player attacking
+ if (attackee = player_move[1]) # see if the player is attacking somebody
# increment gang score and track the attacker
g_score[attackee] += 1
predator_prey_relation[attackee] += [p]
@@ -63,37 +47,16 @@ def gang_score_and_aggro(opponents)
end
# find somebody we can potentially attack or return nil in case we should be resting
- def killable_opponent
+ def pick_opponent
all_opponents = Game.world[:players].select {|p| p.to_s != "david karapetyan"}
- # special logic when only one player is left
- if all_opponents.length == 1
- return all_opponents[0], :yes
- end
- # find people we could potentially kill
- possible_opponents = n_hit_killables(all_opponents)
# compute some metrics and relations to be used in our strategy
gang_score, aggro = gang_score_and_aggro(all_opponents)
- # see if only one perseon is attacking us and our health is less than 80
- # and rest if that's true. the reason is that we can mitigate health loss
- # in this case at the expense of more experience points. we last longer but
- # potentially drop down the ladder
- if stats[:health] < 80 && gang_score[self] < 2 then
- return nil, nil
+ # swartzian transform
+ health_cache = Hash.new
+ sorted_opponents = all_opponents.sort do |a,b|
+ [gang_score[b], health_cache[b] ||= b.stats[:health]] <=> [gang_score[a], health_cache[a] ||= a.stats[:health]]
end
- # order possible opponents according to gang score. lower score is better because
- # we get more experience in that case
- sorted_opponents = possible_opponents.sort {|a,b| gang_score[a] <=> gang_score[b]}
- # see if there are any monsters we can attack because they are easy to kill. if that's
- # not the case then attack the opponent with the lowest gang score because a kill in that
- # case is more beneficial in terms of experience.
- return sorted_opponents.select {|p| !p.respond_to?(:move)}[0] || sorted_opponents[0], nil
- end
-
- # figure out whether at our current strength level we can kill an opponent in n hits
- # according to how the game engine calculates health loss
- def can_kill_in_n_hits?(player, n)
- enemy_stats = player.stats
- points = stats[:strength] - (enemy_stats[:defense] / 2)
- enemy_stats[:health] <= n * points
+ choices = sorted_opponents[0..2]
+ choices[rand(choices.length)]
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.