Skip to content

Instantly share code, notes, and snippets.

@mark
Created October 10, 2013 03:37
Show Gist options
  • Save mark/6912658 to your computer and use it in GitHub Desktop.
Save mark/6912658 to your computer and use it in GitHub Desktop.
A simulator of the pnp game Oil Rush: http://boardgamegeek.com/boardgame/19365/oil-rush
# encoding: UTF-8
ORTHO = [[0, 1], [0, -1], [1, 0], [-1, 0]]
DIAG = [[1, 1], [1, -1], [-1, 1], [-1, -1]]
BASE_REQUIREMENT = 9
MAP_SIZE = 8
def roll
rand(6) + rand(6) + 2
end
map = Array.new(MAP_SIZE) { Array.new(MAP_SIZE, :unknown) }
def print_map(map)
puts '+' + ('-' * map[0].length) + '+'
map.each do |row|
print '|'
row.each do |cell|
print cell == :oil ? '•' : ' '
end
puts '|'
end
puts '+' + ('-' * map[0].length) + '+'
end
def map_get(map, row, col)
if row >= 0 && row < map.length && col >= 0 && col < map[0].length
map[row][col]
else
nil
end
end
def map_val(index, map)
row = index/map.length
col = index%map.length
map[row][col]
end
def set_map_val(index, map, val)
row = index/map.length
col = index%map.length
map[row][col] = val
end
def oil_neighbors(index, map)
row = index/map.length
col = index%map.length
adj_oil = 0
cor_oil = 0
ORTHO.each do |dr, dc|
if map_get(map, row+dr, col+dc) == :oil
adj_oil += 1
end
end
DIAG.each do |dr, dc|
if map_get(map, row+dr, col+dc) == :oil
cor_oil += 1
end
end
required = BASE_REQUIREMENT - adj_oil - cor_oil/2
# print_nbhd(map, row, col)
# puts "Requirement = #{ required }\n"
# puts "adjacent = #{ adj_oil }, corner = #{ cor_oil } (-#{ cor_oil/2})"
# puts
return roll >= required ? :oil : :empty
end
def print_nbhd(map, row, col)
puts '+---+'
(-1..1).each do |dr|
print '|'
(-1..1).each do |dc|
case map_get(map, row+dr, col+dc)
when :oil then print '•'
when :empty then print ' '
when :unknown then print '?'
when nil then print 'X'
end
end
puts '|'
end
puts '+---+'
end
indexes = (0...(map.length * map[0].length)).to_a.sort_by { rand }
indexes.each do |idx|
set_map_val(idx, map, oil_neighbors(idx, map))
end
print_map(map)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment