Skip to content

Instantly share code, notes, and snippets.

@brokenmass
Last active April 15, 2019 14:53
Show Gist options
  • Save brokenmass/aa9787e8b0ab45e91249e07594777c99 to your computer and use it in GitHub Desktop.
Save brokenmass/aa9787e8b0ab45e91249e07594777c99 to your computer and use it in GitHub Desktop.
TENNIS
const TennisGame = require('./TennisGame');
const Player = require('./Player');
const myGame = new TennisGame([new Player('p1'), new Player('p2')]);
myGame.play();
const TENNIS_POINTS = [0, 15, 30, 40];
class Player {
constructor (name) {
this.name = name;
this.score = 0;
}
scorePoint () {
this.score++;
}
getHRScore () {
return TENNIS_POINTS[this.score];
}
}
module.exports = Player;
const Player = require('./Player');
describe('Player class', () => {
it('should exist', () => {
expect(Player).toBeTruthy();
});
it('should allow to define a playher name', () => {
const player = new Player('NAME SURNAME');
expect(player.name).toBe('NAME SURNAME');
});
it('should have an initial score of 0', () => {
const player = new Player('NAME SURNAME');
expect(player.score).toBe(0);
});
it('should allow to increment score', () => {
const player = new Player('NAME SURNAME');
player.scorePoint();
expect(player.score).toBe(1);
player.scorePoint();
expect(player.score).toBe(2);
player.scorePoint();
expect(player.score).toBe(3);
player.scorePoint();
expect(player.score).toBe(4);
});
it('should return a human readable scroe', () => {
const player = new Player('NAME SURNAME');
expect(player.getHRScore()).toBe(0);
player.scorePoint();
expect(player.getHRScore()).toBe(15);
player.scorePoint();
expect(player.getHRScore()).toBe(30);
player.scorePoint();
expect(player.getHRScore()).toBe(40);
player.scorePoint();
});
});
class TennisGame {
constructor (players) {
this.status = TennisGame.STATUS.GAME;
this.players = players;
}
logScore () {
// eslint-disable-next-line no-console
console.log(this.getScore());
}
getScore () {
switch (this.status) {
case TennisGame.STATUS.GAME:
return `${this.players[0].name} ${this.players[0].getHRScore()} - ${this.players[1].getHRScore()} ${this.players[1].name}`;
case TennisGame.STATUS.DEUCE:
return 'DEUCE';
case TennisGame.STATUS.ADVANTAGE:
return `${this.lastScorer.name} ADVANTAGE`;
case TennisGame.STATUS.GAME_OVER:
return `${this.lastScorer.name} WINS`;
default:
throw new Error('INVALID STATE');
}
}
assignPoint () {
this.lastScorer = this.players[Math.round(Math.random())];
this.lastScorer.scorePoint();
// eslint-disable-next-line no-console
console.log(`${this.lastScorer.name} scored \n`);
}
updateStatus () {
// the following extract the scores from the players using a map functions
// and then spread the array as parameters of the Math.max utility
const maxScore = Math.max(...this.players.map((player) => player.score));
const deltaScore = Math.abs(this.players[0].score - this.players[1].score);
if (maxScore < 3 || maxScore === 3 && deltaScore !== 0) {
this.status = TennisGame.STATUS.GAME;
} else if (maxScore >= 3 && deltaScore === 0) {
this.status = TennisGame.STATUS.DEUCE;
} else if (deltaScore === 1) {
this.status = TennisGame.STATUS.ADVANTAGE;
} else {
this.status = TennisGame.STATUS.GAME_OVER;
}
return this.status;
}
play () {
while (this.status !== TennisGame.STATUS.GAME_OVER) {
this.logScore();
this.assignPoint();
this.updateStatus();
}
this.logScore();
}
}
TennisGame.STATUS = {
GAME: 'GAME',
GAME_OVER: 'GAME_OVER',
DEUCE: 'DEUCE',
ADVANTAGE: 'ADVANTAGE'
};
module.exports = TennisGame;
const Player = require('./Player');
const TennisGame = require('./TennisGame');
describe('TennisGame class', () => {
it('should exist', () => {
expect(TennisGame).toBeTruthy();
});
describe('getScore', () => {
it('should return the current score if the status is GAME', () => {
const players = [new Player('p1'), new Player('p2')];
const game = new TennisGame(players);
players[1].score = 3;
expect(game.getScore()).toBe('p1 0 - 40 p2');
});
it('should return the current score if the status is DEUCE', () => {
const players = [new Player('p1'), new Player('p2')];
const game = new TennisGame(players);
game.status = TennisGame.STATUS.DEUCE;
expect(game.getScore()).toBe('DEUCE');
});
it('should return the current score if the status is ADVANTAGE', () => {
const players = [new Player('p1'), new Player('p2')];
const game = new TennisGame(players);
game.status = TennisGame.STATUS.ADVANTAGE;
game.lastScorer = players[0];
expect(game.getScore()).toBe('p1 ADVANTAGE');
game.lastScorer = players[1];
expect(game.getScore()).toBe('p2 ADVANTAGE');
});
it('should return the current score if the status is GAME_OVER', () => {
const players = [new Player('p1'), new Player('p2')];
const game = new TennisGame(players);
game.status = TennisGame.STATUS.GAME_OVER;
game.lastScorer = players[0];
expect(game.getScore()).toBe('p1 WINS');
game.lastScorer = players[1];
expect(game.getScore()).toBe('p2 WINS');
});
});
describe('updateStatus', () => {
it('should correctly identify wehn game is DEUCE', () => {
const players = [new Player('p1'), new Player('p2')];
const game = new TennisGame(players);
expect(game.updateStatus()).not.toBe(TennisGame.STATUS.DEUCE);
players[0].score = players[1].score = 3;
expect(game.updateStatus()).toBe(TennisGame.STATUS.DEUCE);
players[0].score = players[1].score = 4;
expect(game.updateStatus()).toBe(TennisGame.STATUS.DEUCE);
players[0].score = players[1].score = 5;
expect(game.updateStatus()).toBe(TennisGame.STATUS.DEUCE);
});
it('should correctly identify wehn game is ADVANTAGE', () => {
const players = [new Player('p1'), new Player('p2')];
const game = new TennisGame(players);
expect(game.updateStatus()).not.toBe(TennisGame.STATUS.ADVANTAGE);
players[0].score = 3;
players[1].score = 4;
expect(game.updateStatus()).toBe(TennisGame.STATUS.ADVANTAGE);
players[0].score = 77;
players[1].score = 78;
expect(game.updateStatus()).toBe(TennisGame.STATUS.ADVANTAGE);
});
it('should correctly identify wehn game is GAME_OVER', () => {
const players = [new Player('p1'), new Player('p2')];
const game = new TennisGame(players);
expect(game.updateStatus()).not.toBe(TennisGame.STATUS.GAME_OVER);
players[0].score = 3;
players[1].score = 0;
expect(game.updateStatus()).not.toBe(TennisGame.STATUS.GAME_OVER);
players[0].score = 4;
players[1].score = 0;
expect(game.updateStatus()).toBe(TennisGame.STATUS.GAME_OVER);
players[0].score = 77;
players[1].score = 79;
expect(game.updateStatus()).toBe(TennisGame.STATUS.GAME_OVER);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment