Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
54 lines (46 sloc) 1.52 KB
constants = require './constants.coffee'
exports.createTournament = (players) ->
numberOfPlayers: -> players.length
listOfGames: ->
arr = []
for left in [0..@numberOfPlayers()-2]
do(left) =>
for right in [left+1..@numberOfPlayers()-1]
do(right) ->
tuple = [left, right]
arr.push(tuple)
arr
startTournament: ->
finalScores = for p in [0..@numberOfPlayers()-1]
name: players[p].name
score: 0
wins: 0
games = @listOfGames()
results = for game in games
do(game) ->
g = new Game(
players[game[0]].createPlayer,
players[game[1]].createPlayer)
g.startGame()
for pos in [0..results.length-1]
finalScores[games[pos][0]].score = finalScores[games[pos][0]].score + results[pos][0]
finalScores[games[pos][1]].score = finalScores[games[pos][1]].score + results[pos][1]
finalScores
class Game
constructor:(@playerA,@playerB) ->
turns:20
scores:[0,0]
startGame: -> this.playTurn(0,null,null)
playTurn:(turn,moveAforB,moveBforA) =>
@keepScores(moveAforB,moveBforA)
if turn < @turns then @playTurn(turn+1,@playerA.nextTurn(moveBforA),@playerB.nextTurn(moveBforA))
else @scores
addToScore:(toAdd) ->
@scores = [@scores[0]+toAdd[0],@scores[1]+toAdd[1]]
keepScores:(moveAforB,moveBforA) ->
switch moveAforB + moveBforA
when constants.CC then @addToScore(constants.CC_SCORE)
when constants.CD then @addToScore(constants.CD_SCORE)
when constants.DC then @addToScore(constants.DC_SCORE)
when constants.DD then @addToScore(constants.DD_SCORE)
exports.Game = Game