Consistent quotes. I prefer doublequotes as Coffeescript assigns
them a consistent meaning. Changed the 'BINGO' algorithm to use just numeric coordinates, converting exactly once from the BINGO to 0..4. Changed the test-if-won algorithm to create scoped tests with consistent signatures. This let me catch an error I missed with the first pass.
This commit is contained in:
parent
173727ef39
commit
17c8a86bb8
64
bingo.coffee
64
bingo.coffee
|
@ -1,62 +1,60 @@
|
|||
#!/usr/bin/coffee
|
||||
|
||||
io = require 'socket.io-client'
|
||||
_ = require 'underscore'
|
||||
io = require "socket.io-client"
|
||||
_ = require "underscore"
|
||||
|
||||
bingo = ['B', 'I', 'N', 'G', 'O']
|
||||
state = 'lost'
|
||||
bingo = ["B", "I", "N", "G", "O"]
|
||||
coors = [0..4]
|
||||
state = "lost"
|
||||
|
||||
snum = (n) -> if n < 10 then " #{n}" else "#{n}"
|
||||
fnum = (j) -> if j.found then "[" + snum(j.n) + "]" else " " + snum(j.n) + " "
|
||||
fnum = (j) -> if j.found then "[#{fnum(j.n)}]" else " #{fnum(j.n)} "
|
||||
|
||||
socket = io.connect 'ws://yahoobingo.herokuapp.com'
|
||||
socket.on 'connect', ->
|
||||
socket = io.connect "ws://yahoobingo.herokuapp.com"
|
||||
socket.on "connect", ->
|
||||
|
||||
card = {}
|
||||
|
||||
socket.on 'card', (data) ->
|
||||
for i in bingo
|
||||
card[i] = ({n: j, found: false} for j in data.slots[i])
|
||||
socket.on "card", (data) ->
|
||||
card = for i in bingo
|
||||
({n: j, found: false} for j in data.slots[i])
|
||||
|
||||
socket.on 'number', (ball) ->
|
||||
socket.on "number", (ball) ->
|
||||
m = ball.match /([BINGO])(\d+)/
|
||||
card[m[1]] = for j in card[m[1]]
|
||||
row = bingo.indexOf(m[1])
|
||||
card[row] = for j in card[row]
|
||||
if j.found or j.n != +m[2] then j else {n: j.n, found: true}
|
||||
|
||||
may = (m, cond) -> if cond.found then m+1 else m
|
||||
|
||||
f = (->
|
||||
for i in bingo
|
||||
if 5 == _.reduce card[m[1]], ((m, i) -> may m, i), 0
|
||||
for i in coors
|
||||
for test in [((m, j) -> may m, card[i][j]), ((m, j) -> may m, card[j][i])]
|
||||
if 5 == _.reduce coors, test, 0
|
||||
return true
|
||||
|
||||
|
||||
for test in [((m, j) -> may m, card[j][j]), ((m, j) -> may m, card[j][4 - j])]
|
||||
if 5 == _.reduce coors, test, 0
|
||||
return true
|
||||
|
||||
for i in [0..4]
|
||||
if 5 == _.reduce bingo, ((m, j) -> may m, card[j][i]), 0
|
||||
return true
|
||||
|
||||
if 5 == _.reduce [0..4], ((m, j) -> may m, card[bingo[j]][j]), 0
|
||||
return true
|
||||
|
||||
if 5 == _.reduce [0..4], ((m, j) -> may m, card[bingo[j]][4 - j]), 0
|
||||
return true
|
||||
|
||||
false)()
|
||||
|
||||
console.log "\n\nBALL: #{ball}\n"
|
||||
for i in bingo
|
||||
for i in coors
|
||||
console.log(card[i].map(fnum).join(" "))
|
||||
if f
|
||||
console.log "Looks like you won."
|
||||
socket.emit 'bingo'
|
||||
socket.emit "bingo"
|
||||
|
||||
socket.on 'win', -> state = 'won'
|
||||
socket.on 'lose', -> state = 'lost'
|
||||
socket.on "win", -> state = "won"
|
||||
socket.on "lose", -> state = "lost"
|
||||
|
||||
socket.on 'disconnect', ->
|
||||
socket.on "disconnect", ->
|
||||
console.log "You appear to have #{state}."
|
||||
process.exit()
|
||||
|
||||
socket.emit 'register',
|
||||
name: 'Elf M. Sternberg'
|
||||
email: 'elf.sternberg@gmail.com'
|
||||
url: 'https://github.com/elfsternberg/yahoobingo'
|
||||
socket.emit "register",
|
||||
name: "Elf M. Sternberg"
|
||||
email: "elf.sternberg@gmail.com"
|
||||
url: "https://github.com/elfsternberg/yahoobingo"
|
||||
|
|
Loading…
Reference in New Issue