Skip to content

Instantly share code, notes, and snippets.

@knjname
Created August 11, 2012 08:38
Show Gist options
  • Save knjname/3322532 to your computer and use it in GitHub Desktop.
Save knjname/3322532 to your computer and use it in GitHub Desktop.
bingoMaxNumber = 75
firstNumber = (taken) ->
unless taken[num = 1 + Math.floor( Math.random() * (bingoMaxNumber - 1) )]
taken[num] = true
num
else
firstNumber taken
class BingoCell
constructor : (@num) ->
@pushed = false
push : (num = @num) ->
@pushed = true if num is @num
isPushed : () -> @pushed
toString : () -> "[#{if @isPushed() then 'O' else ' '}:#{(''+@num).replace(/^(.)$/, '0$1')}]"
class BingoSheet
constructor : ->
made = {}
@cells = for row in [0...5]
for col in [0...5]
new BingoCell(firstNumber made)
@cells[2][2].push()
@won = false
push :(num) ->
@cells[row][col].push(num) for col in [0...5] for row in [0...5]
@bingo()
bingo : () ->
isPushed = (r,c) => @cells[r][c].isPushed()
check = (rcList) =>
return false for e in rcList when not isPushed(e[0], e[1])
true
@won or= (check ([row, col] for col in [0...5])) for row in [0...5]
@won or= (check ([row, col] for row in [0...5])) for col in [0...5]
@won or= check ([cross,cross] for cross in [0...5])
@won or= check ([cross,4-cross] for cross in [0...5])
isWon : () -> @won
toString : ->
(for r in [0...5]
("#{@cells[r][c]}" for c in [0...5]).join(",\t")).join("\n")
class BingoSession
constructor : (peoples) ->
@bingoes = (new BingoSheet() for n in [1..peoples])
@taken = {}
@at = 0
# returns first bingo peoples count
takeNumber : () ->
@at++
num = firstNumber @taken
# console.log 'Number is :', num
(console.log(b.toString()) for b in @bingoes when (not b.isWon()) and (b.push(num))).length
peoples = 1000
test = () ->
sess = new BingoSession(peoples)
sum = 0
for i in [1...bingoMaxNumber]
sum += sess.takeNumber()
# console.log sum
if sum >= 1
break
i
testcount = 1
sum = 0
for i in [0...testcount]
peoples = 20
result = test()
console.log result
sum += result
console.log 'result:', sum / testcount
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment