aboutsummaryrefslogtreecommitdiff
path: root/monopoly.rb
blob: db8f01ba138b419440c96bc8227cb27c955f44f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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