aboutsummaryrefslogtreecommitdiff
path: root/monopoly.rb
diff options
context:
space:
mode:
authorNat Lasseter <Nat Lasseter nathan@bytemark.co.uk>2017-12-21 11:44:21 +0000
committerNat Lasseter <Nat Lasseter nathan@bytemark.co.uk>2017-12-21 11:44:21 +0000
commit0f3f30d41168209bfd94d51f8aa6c5a13d523476 (patch)
treee8d5e4c9f72c417a4ce77f0e0ef4701b641f2003 /monopoly.rb
Initial Commit
Diffstat (limited to 'monopoly.rb')
-rwxr-xr-xmonopoly.rb129
1 files changed, 129 insertions, 0 deletions
diff --git a/monopoly.rb b/monopoly.rb
new file mode 100755
index 0000000..db8f01b
--- /dev/null
+++ b/monopoly.rb
@@ -0,0 +1,129 @@
+#!/usr/bin/env ruby
+
+require 'pp'
+
+class Roll
+ attr_reader :d1, :d2
+
+ def initialize
+ @d1 = rand(6) + 1
+ @d2 = rand(6) + 1
+ end
+
+ def sum
+ return @d1 + @d2
+ end
+
+ def double?
+ return @d1 == @d2
+ end
+end
+
+class Deck
+ def initialize(cards, strategy)
+ @draw = cards.shuffle
+ @size = cards.length
+ @strategy = strategy
+ @discard = [] if strategy == :discard
+ end
+
+ def take
+ this = @draw.shift
+
+ case @strategy
+ when :bottom
+ @draw << this
+ when :discard
+ @discard << this
+ if @draw.empty? then
+ @draw = @discard.shuffle
+ @discard = []
+ end
+ end
+
+ return this
+ end
+end
+
+class Chance < Deck
+ def initialize(strategy)
+ super([0, 24, 11, [12,28], [5,15,25,35], nil, nil, -3, 10, nil, nil, 5, 39, nil, nil, nil], strategy)
+ end
+end
+
+class CommunityChest < Deck
+ def initialize(strategy)
+ super([0, nil, nil, nil, nil, 10, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil], strategy)
+ end
+end
+
+space = 0
+doubles = 0
+stats = (0..39).map{|i|[i, 0]}.to_h
+
+strat = (ARGV.shift || 'bottom').chomp.intern
+
+chance = Chance.new(strat)
+chest = CommunityChest.new(strat)
+
+(ARGV.shift || "10000000").chomp.to_i.times do
+ r = Roll.new
+
+ if r.double? then
+ doubles += 1
+ else
+ doubles = 0
+ end
+
+ if doubles == 3 then
+ space = 10
+ else
+ space = (space + r.sum) % 40
+ end
+
+ stats[space] += 1
+
+ if space == 30 then
+ space = 10
+ end
+
+ if [7,22,36].include?(space) then
+ c = chance.take
+ if c.is_a?(Integer) then
+ if c >= 0 then
+ space = 0
+ else
+ space = (space + c) % 40
+ end
+ elsif c.is_a?(Array) then
+ try = c.drop_while{|x|x <= space}[0]
+ if try.nil? then
+ space = 0
+ else
+ space = try
+ end
+ end
+ end
+
+ if [2,17,33].include?(space) then
+ c = chest.take
+ if c.is_a?(Integer) then
+ if c >= 0 then
+ space = 0
+ else
+ space = (space + c) % 40
+ end
+ elsif c.is_a?(Array) then
+ try = c.drop_while{|x|x <= space}[0]
+ if try.nil? then
+ space = 0
+ else
+ space = try
+ end
+ end
+ end
+end
+
+stats.each do |k, v|
+ puts "#{k}\t#{v}"
+end