Skip to content

Instantly share code, notes, and snippets.

@NerOcrO
Created April 8, 2021 07:40
Show Gist options
  • Save NerOcrO/bcb20be53cdadea696c10689fe1387c4 to your computer and use it in GitHub Desktop.
Save NerOcrO/bcb20be53cdadea696c10689fe1387c4 to your computer and use it in GitHub Desktop.
solution kata tennis refacto
import { TennisGame } from './TennisGame';
export class TennisGame1 implements TennisGame {
private scoreForPlayer1: number = 0;
private scoreForPlayer2: number = 0;
private player1Name: string;
private player2Name: string;
private score: string = '';
private normalPoints: string[] = [
'Love',
'Fifteen',
'Thirty',
'Forty',
];
constructor(player1Name: string, player2Name: string) {
this.player1Name = new Player(player1Name).getName();
this.player2Name = new Player(player2Name).getName();
}
wonPoint(playerName: string): void {
if (playerName === 'player1') {
this.scoreForPlayer1 += 1;
}
else {
this.scoreForPlayer2 += 1;
}
}
getScore(): string {
const isSameScore: boolean = this.scoreForPlayer1 === this.scoreForPlayer2;
const isLateGame: boolean = this.scoreForPlayer1 >= 4 || this.scoreForPlayer2 >= 4;
if (isSameScore) {
this.sameScore();
}
else if (isLateGame) {
this.scoreForLateGame();
}
else {
this.scoreForEarlyGame();
}
return this.score;
}
scoreForLateGame(): void {
const scoreDifference: number = this.scoreForPlayer1 - this.scoreForPlayer2;
const scoreDifferenceToWin: number = 2;
if (scoreDifference === 1) {
this.score = 'Advantage player1';
}
else if (scoreDifference === -1) {
this.score = 'Advantage player2';
}
else if (scoreDifference >= scoreDifferenceToWin) {
this.score = 'Win for player1';
}
else {
this.score = 'Win for player2';
}
}
scoreForEarlyGame(): void {
this.score = `${this.normalPoints[this.scoreForPlayer1]}-${this.normalPoints[this.scoreForPlayer2]}`;
}
sameScore(): void {
const isOverOrEqualToThirdPoint: boolean = this.scoreForPlayer1 >= 3;
if (isOverOrEqualToThirdPoint) {
// Deuce = 40-40
this.score = 'Deuce';
}
else {
this.score = `${this.normalPoints[this.scoreForPlayer1]}-All`;
}
}
}
class Player {
private name: string;
constructor(name) {
this.name = name;
}
getName() {
return this.name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment