Skip to content

Commit

Permalink
initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
Rohith Ravi committed Apr 21, 2011
0 parents commit 3ec84f4
Show file tree
Hide file tree
Showing 13 changed files with 8,685 additions and 0 deletions.
50 changes: 50 additions & 0 deletions app.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
express = require 'express'
Game = require './models/game'

app = module.exports = express.createServer()

# register a new mime type for client-side coffeescript
mime = require "mime"
mime.define
'text/coffeescript': ['coffee']

DEFAULT_BOARD_SIZE = 6

createNewGame = ->
g = new Game DEFAULT_BOARD_SIZE
g.on 'completeSquare', ->
console.log "Square Completed!!"
g

# Configuration
app.configure ->
app.set 'views', __dirname + '/views'
app.set 'view engine', 'jade'
# app.use express.logger()
app.use express.bodyParser()
app.use express.methodOverride()
app.use app.router
app.use express.static __dirname + '/public'

all_games = {}

app.get '/games', (req, res) ->
res.local 'all_games', all_games
res.render 'games/index'

app.get '/g/:game_id', (req, res) ->
gid = req.params.game_id
all_games[gid] ||= createNewGame()
res.local 'game_id', gid
res.local 'game', all_games[gid]
res.render 'games/show'

app.get '/g/:game_id/set/:edge_num', (req, res) ->
gid = req.params.game_id
all_games[gid].fillEdge parseInt req.params.edge_num
res.redirect "/g/#{gid}"

# Only listen on $ node app.js
unless module.parent
app.listen 3000
console.log "Express server listening on port %d", app.address().port
61 changes: 61 additions & 0 deletions models/game.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
EventEmitter = require('events').EventEmitter

module.exports = class Game extends EventEmitter
constructor: (@size) ->
@resetBoard()

resetBoard: ->
@alpha = 2 * @size - 1
@num_edges = 2 * @size * (@size - 1)
@board = new Array @num_edges

fillEdge: (edgeNum) ->
return false unless 0 <= edgeNum < @num_edges

@board[edgeNum] = true

if @isVerticalEdge edgeNum
@checkSquare 'left', edgeNum unless @isOnPerimeter 'left', edgeNum
@checkSquare 'right', edgeNum unless @isOnPerimeter 'right', edgeNum
else
@checkSquare 'top', edgeNum unless @isOnPerimeter 'top', edgeNum
@checkSquare 'bottom', edgeNum unless @isOnPerimeter 'bottom', edgeNum

true

isVerticalEdge: (edgeNum) ->
(edgeNum % @alpha) >= Math.floor(@alpha / 2)

checkSquare: (direction, edgeNum) ->

coordinates = switch direction
when 'left'
[edgeNum - 1, edgeNum - @size, edgeNum + (@size - 1)]
when 'right'
[edgeNum + 1, edgeNum + @size, edgeNum - (@size - 1)]
when 'top'
[edgeNum - @alpha, edgeNum - @size, edgeNum - (@size - 1)]
when 'bottom'
[edgeNum + @alpha, edgeNum + @size, edgeNum + (@size - 1)]

for edge in coordinates
# don't need to check out-of-bounds coordinates
continue unless 0 <= edge < @num_edges

# if one of the neighbor lines are not set, return false
return false unless @board[edge]

# if execution has gotten this far, all the neighbors are set
@emit 'completeSquare'
true

isOnPerimeter: (direction, edgeNum) ->
switch direction
when 'left'
edgeNum % @alpha == @size - 1
when 'right'
edgeNum % @alpha == @alpha - 1
when 'top'
edgeNum in [0...@size]
when 'bottom'
edgeNum in [(@num_edges - @size + 1)..@num_edges]
Binary file added public/favicon.ico
Binary file not shown.
42 changes: 42 additions & 0 deletions public/javascripts/client.game.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
Client ?= {}

class Client.Game
constructor: (@container)->
@container.click (e) =>
target = $(e.target)
return unless target.is 'a'

gameId = @container.attr 'id'
target.attr "href", "/g/#{gameId}/set/#{target.data('edgeNum')}"
return true

console.log "Client game construction detected"

render: ->
console.log "render triggered on client game"
for edge, edgeNum in @board
link = $("<a></a>").html '&nbsp'
currentOrientation = @isVerticalEdge edgeNum
switching = currentOrientation != @isVerticalEdge(edgeNum - 1)
if currentOrientation
link.addClass 'vert'
else
link.addClass 'horiz'
@container.append link
link.css 'clear', 'left' if switching
link.addClass 'filled' if @board[edgeNum]
link.data 'edgeNum', edgeNum

isVerticalEdge: (edgeNum) ->
(edgeNum % @alpha) >= Math.floor(@alpha / 2)

isOnPerimeter: (direction, edgeNum) ->
switch direction
when 'left'
edgeNum % @alpha == @size - 1
when 'right'
edgeNum % @alpha == @alpha - 1
when 'top'
edgeNum in [0...@size]
when 'bottom'
edgeNum in [(@num_edges - @size + 1)..@num_edges]
76 changes: 76 additions & 0 deletions public/javascripts/client.game.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions public/javascripts/coffee-script.js

Large diffs are not rendered by default.

Loading

0 comments on commit 3ec84f4

Please sign in to comment.