diff --git a/src/12_RefactoringGolf/hole1/kata.ts b/src/12_RefactoringGolf/hole1/kata.ts index 4e9ba71..f955f3d 100644 --- a/src/12_RefactoringGolf/hole1/kata.ts +++ b/src/12_RefactoringGolf/hole1/kata.ts @@ -4,7 +4,7 @@ export class Game { private _lastSymbol = ' '; private _board: Board = new Board(); - private readonly playerO = 'O'; + private readonly emptyPlay = ' '; private readonly firstRow = 0; @@ -24,8 +24,10 @@ export class Game { } private validateFirstMove(player: string) { + + const playerO = 'O'; if (this._lastSymbol == this.emptyPlay) { - if (player == this.playerO) { + if (player == playerO) { throw new Error('Invalid first player'); } } @@ -52,69 +54,35 @@ export class Game { } public Winner(): string { - if (this.isFirstRowFull() && this.isFirstRowFullWithSameSymbol()) { + if (this.isRowFull(0) && this.isRowFullWithSameSymbol(0)) { return this._board.TileAt(this.firstRow, this.firstColumn)!.Symbol; } - if (this.isSecondRowFull() && this.isSecondRowFullWithSameSymbol()) { + if (this.isRowFull(1) && this.isRowFullWithSameSymbol(1)) { return this._board.TileAt(this.secondRow, this.firstColumn)!.Symbol; } - if (this.isThirdRowFull() && this.isThirdRowFullWithSameSymbol()) { + if (this.isRowFull(2) && this.isRowFullWithSameSymbol(2)) { return this._board.TileAt(this.thirdRow, this.firstColumn)!.Symbol; } return this.emptyPlay; } - private isFirstRowFull() { - return ( - this._board.TileAt(this.firstRow, this.firstColumn)!.Symbol != this.emptyPlay && - this._board.TileAt(this.firstRow, this.secondColumn)!.Symbol != this.emptyPlay && - this._board.TileAt(this.firstRow, this.thirdColumn)!.Symbol != this.emptyPlay - ); - } - - private isFirstRowFullWithSameSymbol() { - return ( - this._board.TileAt(this.firstRow, this.firstColumn)!.Symbol == - this._board.TileAt(this.firstRow, this.secondColumn)!.Symbol && - this._board.TileAt(this.firstRow, this.thirdColumn)!.Symbol == - this._board.TileAt(this.firstRow, this.secondColumn)!.Symbol - ); - } - - private isSecondRowFull() { + + private isRowFull(row: number) { return ( - this._board.TileAt(this.secondRow, this.firstColumn)!.Symbol != this.emptyPlay && - this._board.TileAt(this.secondRow, this.secondColumn)!.Symbol != this.emptyPlay && - this._board.TileAt(this.secondRow, this.thirdColumn)!.Symbol != this.emptyPlay + this._board.TileAt(row, this.firstColumn)!.Symbol != this.emptyPlay && + this._board.TileAt(row, this.secondColumn)!.Symbol != this.emptyPlay && + this._board.TileAt(row, this.thirdColumn)!.Symbol != this.emptyPlay ); } - - private isSecondRowFullWithSameSymbol() { - return ( - this._board.TileAt(this.secondRow, this.firstColumn)!.Symbol == - this._board.TileAt(this.secondRow, this.secondColumn)!.Symbol && - this._board.TileAt(this.secondRow, this.thirdColumn)!.Symbol == - this._board.TileAt(this.secondRow, this.secondColumn)!.Symbol - ); - } - - private isThirdRowFull() { - return ( - this._board.TileAt(this.thirdRow, this.firstColumn)!.Symbol != this.emptyPlay && - this._board.TileAt(this.thirdRow, this.secondColumn)!.Symbol != this.emptyPlay && - this._board.TileAt(this.thirdRow, this.thirdColumn)!.Symbol != this.emptyPlay - ); - } - - private isThirdRowFullWithSameSymbol() { + private isRowFullWithSameSymbol(row: number) { return ( - this._board.TileAt(this.thirdRow, this.firstColumn)!.Symbol == - this._board.TileAt(this.thirdRow, this.secondColumn)!.Symbol && - this._board.TileAt(this.thirdRow, this.thirdColumn)!.Symbol == - this._board.TileAt(this.thirdRow, this.secondColumn)!.Symbol + this._board.TileAt(row, this.firstColumn)!.Symbol == + this._board.TileAt(row, this.secondColumn)!.Symbol && + this._board.TileAt(row, this.thirdColumn)!.Symbol == + this._board.TileAt(row, this.secondColumn)!.Symbol ); } }