summaryrefslogtreecommitdiff
path: root/ScotlandYard.rb
blob: 5bb91a2492e32d003f7123f5f8d9f51ba4289a5a (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
#!/usr/bin/ruby

require 'yaml'

class Game

    def initialize
        # load YAML
        file = File.read 'yard.yml'
        y = YAML.load file

        @possible_locations = y['starting_locations']
        @previous_locations = Array.new
        @map = y['map']
    end

    def possible_locations turn = 0
        locations = Hash.new 0
        @possible_locations.each do |loc|
            locations[loc] += 1
        end

        chances = locations.values.inject :+

        locations.each { |location, chance| locations[location] = chance / chances.to_f }

        locations
    end

    def remove possibility
        @possible_locations.delete possibility
    end

    def at location
        @possible_locations = [location]
    end

    def take method
        new_locations = Array.new
        @possible_locations.each do |loc|
            unless @map[loc][method] == nil
                new_locations += @map[loc][method]
            end
        end
        @possible_locations = new_locations
    end
end

def print_locations locations
    puts 'Possible Locations:'
    locations = locations.sort_by { |loc, prob| -prob }
    locations.each { |loc, prob| printf "   %3d: %6.2f\%\n", loc, prob*100 }
end

g = Game.new
start = 196
g.at start
print_locations g.possible_locations
g.take 'taxi'
g.take 'taxi'
g.take 'taxi'
g.take 'underground'
g.take 'bus'
print_locations g.possible_locations