Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Reed Law committed Apr 8, 2012
0 parents commit 5532280
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
107 changes: 107 additions & 0 deletions engine.rb
@@ -0,0 +1,107 @@
#!/usr/bin/env ruby
require 'debugger'

class Player
attr_accessor :proxy, :health

def initialize
@health = 100
@level = 0
@strength = 20
@defense = 10
@experience = 0
end

def fight(opponent)
@health = @health - 2
if opponent.class != Player
raise "Nice try, but no cheating!"
else
opponent.health = opponent.health - 5
end
end

def stats
{ health: @health, level: @level, strength: @strength, defense: @defense }
end
end

class PlayerProxy
def initialize(player)
@player = player
end

def method_missing(method, *args, &block)
if [:move, :fight, :to_s, :trade, :rest, :inspect, :stats].include? method
@player.send(method, *args, &block)
end
end
end

class Game
attr_reader :players, :proxies

class << self
attr_accessor :world
end

def initialize
@players = []
@proxies = []
end

def turn(rounds)
update_world
rounds.times do |i|
p "Round #{i}"
@players.each do |p|
m = p.proxy.move.first
o = p.proxy.move.last
object = @players.select {|p|p.proxy == o}.first
case m
when :fight
prep = "with"
p.fight(object)
when :travel
prep = "to"
else
prep = "with"
end
p "#{p} #{m}s #{prep} #{o}"
end
end
@players.each do |p|
puts "#{p}: #{p.stats}"
end
end

private

def update_world
self.class.world = { players: @proxies }
end
end

modules = []
ObjectSpace.each_object(Module) {|m| modules << m.name }

Dir["players/*.rb"].each do |file|
require File.join(File.dirname(__FILE__), file )
end

added_modules = []
ObjectSpace.each_object(Module) {|m| added_modules << m.name unless modules.include?(m.name) }

game = Game.new

added_modules.each do |m|
constant = Object
player = Player.new
game.players << player
p = PlayerProxy.new(player)
p.extend constant.const_get(m)
player.proxy = p
game.proxies << p
end

game.turn(3)
9 changes: 9 additions & 0 deletions players/random.rb
@@ -0,0 +1,9 @@
module RandomPlayer
def move
[[:trade, 2], [:travel, 3]][rand(2)]
end

def to_s
"Crazy Carl"
end
end
10 changes: 10 additions & 0 deletions players/tough.rb
@@ -0,0 +1,10 @@
module Tough
def move
opponent = Game.world[:players].select {|p| p != self.proxy }.first
[:fight, opponent]
end

def to_s
"Sir Samsonite"
end
end

0 comments on commit 5532280

Please sign in to comment.