Skip to content

Instantly share code, notes, and snippets.

@EricDykstra
Created December 22, 2015 08:06
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 EricDykstra/4e0fc2569f240a613775 to your computer and use it in GitHub Desktop.
Save EricDykstra/4e0fc2569f240a613775 to your computer and use it in GitHub Desktop.
class Battle
SPELLS = {
"Magic Missile" => {cost: 53, damage: 4},
"Drain" => {cost: 73, damage: 2, heal: 2},
"Shield" => {cost: 113, timer: 6, effect: "armor", amount: 7},
"Poison" => {cost: 173, timer: 6, effect: "poison", amount: 3},
"Recharge" => {cost: 229, timer: 5, effect: "recharge", amount: 101},
}
def initialize
@armor_effect_timer = 0
@poison_effect_timer = 0
@recharge_effect_timer = 0
@enemy_hp = 51
@enemy_dmg = 9
@hp = 50
@mana = 500
@mana_use = 0
@bad = false
end
def run
until @hp <= 0 || @enemy_hp <= 0 do
hero_turn
break if @hp <= 0 || @enemy_hp <= 0
enemy_turn
end
[@enemy_hp <= 0 && @bad == false, @mana_use]
end
def hero_turn
@hp -= 1
@bad = true if @hp <= 0
status_effects
this_spells = SPELLS
this_spells.delete("Shield") if @armor_effect_timer != 0
this_spells.delete("Poison") if @poison_effect_timer != 0
this_spells.delete("Recharge") if @recharge_effect_timer != 0
if @armor_effect_timer == 0 && (1..3).to_a.sample != 2
spell, vars = ["Shield", {cost: 113, timer: 6, effect: "armor", amount: 7},]
elsif @mana < 400 && @enemy_hp > 10 && (1..3).to_a.sample != 2
spell, vars = ["Recharge", {cost: 229, timer: 5, effect: "recharge", amount: 101},]
elsif @poison_effect_timer == 0 && (1..3).to_a.sample != 2
spell, vars = ["Poison", {cost: 173, timer: 6, effect: "poison", amount: 3},]
else
spell, vars = this_spells.select do |k,v|
v[:cost] < @mana
end.to_a.sample
end
if spell == nil
spell, vars = ["Recharge", {cost: 229, timer: 5, effect: "recharge", amount: 101},]
end
@mana -= vars[:cost]
@bad = true if @mana < 0
@mana_use += vars[:cost]
@enemy_hp -= vars[:damage].to_i
if vars[:timer]
@armor_effect_timer += vars[:timer] if spell == "Shield"
@poison_effect_timer += vars[:timer] if spell == "Poison"
@recharge_effect_timer += vars[:timer] if spell == "Recharge"
end
if spell == "Drain"
@hp += 2
end
end
def enemy_turn
status_effects
if @armor_effect_timer > 0
@hp -= (@enemy_dmg - 7)
else
@hp -= @enemy_dmg
end
end
def status_effects
if @armor_effect_timer > 0
@armor_effect_timer -= 1
end
if @poison_effect_timer > 0
@poison_effect_timer -= 1
@enemy_hp -= 3
end
if @recharge_effect_timer > 0
@recharge_effect_timer -= 1
@mana += 101
end
end
end
20.times {
these = 100000.times.map{ Battle.new.run }.select{|arr| arr[0] == true}
p these.count
answer = these.sort_by{|arr| arr[1]}
p answer.first
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment