From 49f4e2c9a43f67edaa654099bfbdbd5cc8e1b770 Mon Sep 17 00:00:00 2001 From: Ben Burwell Date: Tue, 21 Oct 2014 23:58:52 -0400 Subject: Initialize --- ScotlandYard.rb | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100755 ScotlandYard.rb (limited to 'ScotlandYard.rb') diff --git a/ScotlandYard.rb b/ScotlandYard.rb new file mode 100755 index 0000000..5bb91a2 --- /dev/null +++ b/ScotlandYard.rb @@ -0,0 +1,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 -- cgit v1.2.3