My Stuff Ruby

Everybody loves the SuperBowl commercials! Last year, I hacked up a script to randomly generate items to create your own bingo cards. This year, I improved the script to actually generate the cards as a PDF file based on an excel sheet of bingo items.

Here it is:

puts "How many players?"
players = gets.chomp.to_i
puts "How many items?"
items = gets.chomp.to_i
(1..players).each do |i|
puts "card #{i}"
card = Array.new
(1..5).each do |row|
print " "
(1..5).each do |col|
if(row == 3 && col == 3)
print " F "
else
begin
next_num = rand(items) + 1
end while !card.index(next_num).nil?
card << next_num
print " " if next_num < 10
print next_num
print " "
end
end
print "\n"
end
print "\n"
end
view raw bingo.rb hosted with ❤ by GitHub
equire 'prawn'
require 'spreadsheet'
items = []
Spreadsheet.open('bingo_items.xls').worksheet(0).each do |row|
items << row[0] unless row[0].nil?
end
#items.each { |i| puts i }
puts "How many players?"
players = gets.chomp.to_i
Prawn::Document.generate 'bingo_cards.pdf' do
(1..players).each do |current_player|
font_size 22
text "Commercial Bingo", :align => :center
font_size 18
text "Super Bown XLVI", :align => :center
move_down 15
font_size 16
text "Player #{current_player}"
font_size 14
move_down 30
card = [[ "B", "I", "N", "G", "O"]]
(1..5).each do |row|
row_items = []
(1..5).each do |col|
if(row == 3 && col == 3)
row_items << "FREE SPACE"
else
begin
next_num = rand(items.length)
end while card.flatten.include?(items[ next_num ])
row_items << items[next_num]
end
end
card << row_items
end
move_down 50
table card do
width = 500
cells.valign = :center
cells.align = :center
row(0).font_style = :bold
row(0).size = 16
#free space
row(3).column(2).border_width = 2
row(3).column(2).font_style = :bold
end
start_new_page unless current_player == players
end
end
view raw v2.rb hosted with ❤ by GitHub

Check out the second comment for more info.

– Chris

Image

Christopher R Marshall

@codegoalie

Enjoys programming web applications; especially in Go and Ruby. Also enjoys playing ice hockey as a goalie and playing the guitar.

Categories