Skip to content

Instantly share code, notes, and snippets.

@knjname
Created August 11, 2012 08:15
Show Gist options
  • Save knjname/3322461 to your computer and use it in GitHub Desktop.
Save knjname/3322461 to your computer and use it in GitHub Desktop.
firstNumber = (taken) ->
unless taken[num = Math.floor( Math.random() * 100 )]
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 : () -> "[#{@num}:#{@isPushed()}]"
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
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
(null 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...100]
sum += sess.takeNumber()
# console.log sum
if sum >= (peoples / 2)
break
i
testcount = 1000
sum = 0
for i in [0...testcount]
peoples = i + 1
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