Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the N times M foursquare example
- Loading branch information
0 parents
commit 1ca7f92
Showing
3 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
class Player | ||
include Dramatis::Actor | ||
|
||
attr_reader :name | ||
def initialize(name) | ||
@name = name | ||
@rounds = [] | ||
@round_names = [] | ||
@opponents = [] | ||
end | ||
|
||
def join(round, round_name) | ||
@rounds << round | ||
@round_names << round_name | ||
@opponents << [] | ||
end | ||
|
||
def add(round, opponent) | ||
opponents_for(round) << opponent | ||
end | ||
|
||
def serve(round) | ||
# We ignore the possiblity | ||
# of service faults | ||
opponent = choose_for(round) | ||
release(opponent).volley(round, 1) | ||
end | ||
|
||
def volley(round, volleys) | ||
# We ignore serve do-overs | ||
# We ignore out of bound hits | ||
if made_save | ||
# we ignore bad hits | ||
opponent = choose_for(round) | ||
release(opponent).volley(round, volleys + 1) | ||
else | ||
round.failed(self, volleys) | ||
end | ||
end | ||
|
||
private | ||
def made_save | ||
rand < 0.99999 | ||
end | ||
|
||
def index_for(round) | ||
@rounds.index(round) | ||
end | ||
|
||
def round_name_for(round) | ||
@round_names[index_for(round)] | ||
end | ||
|
||
def opponents_for(round) | ||
@opponents[index_for(round)] | ||
end | ||
|
||
def choose_for(round) | ||
o = opponents_for(round) | ||
o[rand(o.size)] | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
class Round | ||
include Dramatis::Actor | ||
|
||
def initialize(name, players) | ||
actor.refuse :finished? | ||
@start_time = Time.now | ||
@name = name | ||
@players = players | ||
puts "START: #{@name} with #{@players.size} players" | ||
@players.each do |player| | ||
player.join(self, @name) | ||
@players.each do |opponent| | ||
next if opponent == player | ||
player.add(self, opponent) | ||
end | ||
end | ||
end | ||
|
||
def play | ||
release(@players[-1]).serve(self) | ||
end | ||
|
||
def failed(loser, volleys) | ||
@end_time = Time.now | ||
@loser = loser | ||
@volleys = volleys | ||
puts "END: #{@name} in #{@end_time - @start_time} seconds" | ||
actor.accept :finished? | ||
end | ||
|
||
def finished? | ||
!!@loser | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#!/usr/bin/env ruby | ||
|
||
$:.unshift File.dirname(__FILE__) + '/../dramatis/lib' | ||
require 'dramatis' | ||
require 'dramatis/actor' | ||
require 'set' | ||
|
||
$:.unshift File.dirname(__FILE__) + '/lib' | ||
require 'round' | ||
require 'player' | ||
|
||
players_count = ARGV.shift.to_i | ||
rounds_count = ARGV.shift.to_i | ||
puts "Running with #{players_count} players, #{rounds_count} rounds" | ||
|
||
players = [] | ||
players_count.times do |i| | ||
players << Player.new("player-#{i}") | ||
end | ||
puts | ||
|
||
rounds = [] | ||
rounds_count.times do |i| | ||
set = Set.new | ||
(rand(30).to_i + 5).times do | ||
set << players[rand(players.size)] | ||
end | ||
rounds << Round.new("round-#{i}", set.to_a) | ||
end | ||
|
||
rounds.each do |r| | ||
r.play | ||
end | ||
|
||
until rounds.all? {|r| r.finished?} | ||
puts "Waiting" | ||
sleep 1 | ||
end |