Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Tennis kata

View gamesim.coffee
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
{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"
View gamesim.coffee
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
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
View gamesim.coffee
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
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.