-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Rohith Ravi
committed
Apr 21, 2011
0 parents
commit 3ec84f4
Showing
13 changed files
with
8,685 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ' ' | ||
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] |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.