Ruby Quiz <james / grayproductions.net> writes: > by Brian Candler > > A version of Bingo played in the UK and some other countries is called "Housie". > Players buy "books" of 6 tickets. Each ticket shows exactly 15 numbers, and in > each book every number from 1 to 90 is used exactly once. > 1. Write a Ruby program which generates random individual tickets > or > 2. Write a Ruby program which generates random complete books of tickets When I first read the description, I thought "I already did that". And in fact, I did. Back in 2002, after only a few weeks using Ruby. Have a look at the timestamp: -rw-r--r-- 1 chris chris 1472 Jan 13 2002 bingo.rb (Incidentally, that's my sisters birthday and I remember the guests liked it.) Anyway, my bingo solution from 2002 generates plain TeX output suitable for printing. The macro package is appended. You'd call it like ruby bingo.rb > entries.tex && pdftex bingo.tex. Remember that this code has been untouched for 5 years, I wouldn't necessarily code it that way today.
#!/usr/bin/env ruby # -*- ruby -*- Num = 14 class Bingo def initialize @width = 9 @height = 3 @field = [[0,0,0],[0,0,0],[0,0,0], [0,0,0],[0,0,0],[0,0,0], [0,0,0],[0,0,0],[0,0,0]] end def insert_numbers 0.upto (@width-1) { |x| arr = ((x*10)..((x*10)+10)-1).to_a # Special cases: don't insert a 0, add 90 to the row with 80 arr.delete(0) if arr.index(89) != nil arr = arr + [90] end 0.upto (@height-1) { |y| r = arr[rand (arr.length)] arr.delete(r) @field[x][y] = r } @field[x].sort! } end def remove_some 0.upto(@height-1) { |x| cnt = 0 while cnt < 4 cnt+=1 r = rand @field.length # The following crap eludes `three in a row' if @field[r][x] != nil && !((@field[r][0] == nil && @field[r][1] == nil) || (@field[r][1] == nil && @field[r][2] == nil) || (@field[r][2] == nil && @field[r][0] == nil)) @field[r][x] = nil else cnt-=1 end end 0.upto (@width-1) { |y| } } puts end def dump 0.upto(@height-1) { |x| 0.upto(@width-1) { |y| value = @field[y][x] if (0..9).to_a.index(value) != nil value = '?' + value.to_s end if value == nil value = '\X' end print "&& ", value, " " } if x != @height-1 print "&\\nl\n" else print "&\\newsheet\n" end } end end Num.times { b = Bingo.new b.insert_numbers b.remove_some b.dump }
% Print ``bingo'' sheets. \font\mainfont=cmbx14 \mainfont \newbox\strutbox \setbox\strutbox=\hbox{\vrule height18.5pt depth13.5pt width0pt} \def\strut{\relax\ifmmode\copy\strutbox\else\unhcopy\strutbox\fi} \newdimen\onedigitwidth \newdimen\digitwidth \setbox0=\hbox{\mainfont0} \onedigitwidth=\wd0 \setbox0=\hbox{\mainfont00} \digitwidth=\wd0 \catcode`?=\active \def?{\hbox to \onedigitwidth{}} \def\X{\hbox to \digitwidth{\hfil$\bigcirc$\hfil}} \begingroup \tabskip=0pt \offinterlineskip \def\nl{\cr\noalign{\hrule}} \def\newsheet{\cr\noalign{\hrule\vskip1cm\hrule}} \halign{\strut#& \vrule#\tabskip=0.50em& \hfil#\hfil& \vrule#&\hfil#\hfil& \vrule#&\hfil#\hfil& \vrule#& \hfil#\hfil& \vrule#&\hfil#\hfil& \vrule#&\hfil#\hfil& \vrule#& \hfil#\hfil& \vrule#&\hfil#\hfil& \vrule#&\hfil#\hfil& \vrule#\tabskip=0pt\nl \input entries.tex \cr} \endgroup \bye
Enjoy, -- Christian Neukirchen <chneukirchen / gmail.com> http://chneukirchen.org