Skip to content

@stianeikeland /gamesim.coffee
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Tennis kata
{Game, Player, Advantage} = require "./main"
playGame = (gamedata) ->
game = new Game
for x in gamedata.split " "
game.setAdvantage Advantage.playerA if x is "advA"
game.setAdvantage Advantage.playerB if x is "advB"
game.pointA() if x is "pointA"
game.pointB() if x is "pointB"
console.log "#{game}"
if game.isFinished()
console.log "Game is finished"
return
playGame "advA pointA pointA pointB pointB pointA pointB pointA pointA"
# playGame "pointA pointA pointA pointA"
# playGame "pointA pointA pointA pointB pointB pointB pointB"
Advantage =
playerA: 0
playerB: 1
class Player
constructor: (@score) ->
toString: () ->
switch
when @score is 0 then "love"
when @score is 1 then "15"
when @score is 2 then "30"
when @score is 3 then "40"
when @score is 4 then "60 - game"
else throw new Exception "Invalid score"
class Game
constructor: (scoreA = 0, scoreB = 0, advantage = Advantage.playerA) ->
@playerA = new Player scoreA
@playerB = new Player scoreB
@setAdvantage advantage
pointA: () ->
if not (@isDeuce() and @advantage isnt Advantage.playerA)
@playerA.score++
@advantage = Advantage.playerA
pointB: () ->
if not (@isDeuce() and @advantage isnt Advantage.playerB)
@playerB.score++
@advantage = Advantage.playerB
isTied: () ->
@playerA.score is @playerB.score
isDeuce: () ->
@isTied() and @playerA.score is 3
isFinished: () ->
@playerA.score is 4 or @playerB.score is 4
setAdvantage: (@advantage) ->
toString: () ->
switch
when @isDeuce() then "Score is deuce."
when @isTied() then "Score is #{@playerA} - all."
when @playerA.score is 4 then "PlayerA won #{@playerA}."
when @playerB.score is 4 then "PlayerB won #{@playerB}."
else "Score is #{@playerA} - #{@playerB}."
module.exports =
Game: Game
Player: Player
Advantage: Advantage
should = require 'should'
{Game, Player, Advantage} = require "../main"
describe "Game", ->
describe "new game", ->
game = new Game
it "should have two players", ->
should.exist game.playerA
should.exist game.playerB
it "should have score 0 - 0", ->
game.playerA.score.should.equal 0
game.playerB.score.should.equal 0
it "should give advantage to player A", ->
game.advantage.should.equal Advantage.playerA
describe "point", ->
it "to player A increments player A score", ->
game = new Game
game.pointA()
game.playerA.score.should.equal 1
game.playerB.score.should.equal 0
describe "when at deuce", ->
it "should increment when player has advantage", ->
game = new Game 3, 3, Advantage.playerA
game.pointA()
game.playerA.score.should.equal 4
game.playerB.score.should.equal 3
it "should not increment when player do not have advantage", ->
game = new Game 3, 3, Advantage.playerB
game.pointA()
game.playerA.score.should.equal 3
game.playerB.score.should.equal 3
game.advantage.should.equal Advantage.playerA
describe "players", ->
it ".toString() should return tennis scoring call", ->
(new Player 0).toString().should.equal "love"
(new Player 1).toString().should.equal "15"
(new Player 2).toString().should.equal "30"
(new Player 3).toString().should.equal "40"
(new Player 4).toString().should.equal "60 - game"
describe "game", ->
it ".toString() should return tennis scoring call", ->
(new Game).toString().should.equal "Score is love - all."
it ".toString() should end game scoring call", ->
game = new Game 4, 0
game.toString().should.equal "PlayerA won 60 - game."
it '.isDeuce() should return true if score 3 - 3', ->
game = new Game 3, 3
game.isDeuce().should.be.ok
it '.isTied() should return true if score is tied', ->
game = new Game 2, 2
game.isTied().should.be.ok
it '.isFinished() should return if either player has score 4', ->
game = new Game 3, 4
game.isFinished().should.be.ok
it '.setAdvantage() sets advantage to given player', ->
game = new Game
game.setAdvantage Advantage.playerB
game.advantage.should.equal Advantage.playerB
it ".toString() should return deuce scoring call", ->
game = new Game 3, 3
game.toString().should.equal "Score is deuce."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.