Skip to content

Instantly share code, notes, and snippets.

@banderson623
Created May 31, 2013 13:59
Show Gist options
  • Save banderson623/5685178 to your computer and use it in GitHub Desktop.
Save banderson623/5685178 to your computer and use it in GitHub Desktop.
Original Bingo.rb script
require 'rubygems'
BASE_PATH = "/not/important/anymore/"
# Hash to store all the words found
OPTIMIZER = {}
# Can pass in a path to a file, if not it will use
# a file foud at BASE_PATH/bingo_words.txt
if ARGV[0].is_a?(String)
puts "Reading from #{ARGV[0]}"
NEW_WORDS = File.read(ARGV[0]).split("\n")
else
NEW_WORDS = File.read(BASE_PATH + "/bingo_words.txt").split("\n")
end
def get_value(key, default = nil)
if !OPTIMIZER.has_key?(key)
found = NEW_WORDS.find() {|line|
line.downcase.include?(key.downcase)
}
if found != nil
found = found.split(":")[1].gsub(/^\s+/,'')
OPTIMIZER[key] = found
else
found = default
end
else
#puts "optimized!"
found = OPTIMIZER[key]
end
return found
end
def pick_one(options)
if !options.is_a?(Array)
options = [options]
end
picked = options[(rand(options.size + 1 ) % options.size)]
#puts "Picked: #{picked}"
picked
end
def get_word_grid(id)
srand(id)
cells = []
# Copy the words into a new array, once a word is used the word is deleted.
words = NEW_WORDS.clone.delete_if{|word| word.include?(":") || word.size <= 1}
# 4x4 grid
(0..4).each do |col|
cells[col] = []
(0..4).each do |row|
word = words.delete_at(rand(words.size))
cells[col][row] = word.to_s.strip
end
end
# The middle is a free grid!
cells[2][2] = pick_one(get_value("Center","FREE!").split(','))
return cells
end
def build_table_from_words(cells)
o=""
o << "<table>"
cells.each do |row|
o << "<tr>"
row.each do |cell|
o << "<td>#{cell}</td>"
end
o << "</tr>"
end
o << "</table>"
return o
end
def write_html_card_with_seed(id)
file_name = BASE_PATH + "/sheets/bingo_#{id}"
groups = get_value('groups',"First Half, Second Half").split(',')
o = File.new("#{file_name}.html", "w")
o.<< "
<!DOCTYPE html>
<html lang=\"en\">
<head>
<style>
body {
text-align:center;
font-family:helvetica;
}
table {
vertical-align:center;
width:75%;
border-collapse:collapse;
margin:0 auto;
}
td {
height:3em;
width:15%;
padding:5px;
border:1px solid #000;
text-transform:uppercase;
}
.small {
font-weight:normal;
font-size:8pt;
}
</style>
</head>
<body>
<h1>" + get_value("title", "Bingo!") + "</h1>"
groups.each do |group_title|
o << "<h2>#{group_title} <span class=\"small\">#{id}</span></h2>"
cells = get_word_grid(id)
o << build_table_from_words(cells)
end
o << "</body></html>"
return file_name + ".html"
end
upto = get_value("number", 30).to_i
(0..upto).each do |i|
puts "Writing #{i}"
write_html_card_with_seed(i)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment