diff --git a/src/README.md b/src/12_RefactoringGolf/hole1/README.md similarity index 69% rename from src/README.md rename to src/12_RefactoringGolf/hole1/README.md index 4cfc9a0..87ea8b9 100644 --- a/src/README.md +++ b/src/12_RefactoringGolf/hole1/README.md @@ -1,14 +1,15 @@ # Hole 1 to Hole 2 -Change the code in hole 1 to be identical to the code in hole 2; implementation and tests can change. +# create a pull request ## Refactorings -- Tackle code comments, long method and large class - - Extract method +- Tackle all code comments, long method and large class + - Extract methods where there is a comment ## Tips +- Change the code in hole 1 to be identical to the code in hole 2; implementation and tests can change. - Use a diff tool to identify the code changes you need to perform - Check the code coverage is enough to detect any unintended behaviour changes diff --git a/src/12_RefactoringGolf/hole1/kata.ts b/src/12_RefactoringGolf/hole1/kata.ts index c750e5e..fde5008 100644 --- a/src/12_RefactoringGolf/hole1/kata.ts +++ b/src/12_RefactoringGolf/hole1/kata.ts @@ -1,74 +1,75 @@ /* eslint-disable */ +// améliore ce code export class Game { private _lastSymbol = ' '; - private _toto: Board = new Board(); + private _board: Board = new Board(); - public Play(symbol: string, x: number, y: number): void { - //if first move - if (this._lastSymbol == ' ') { - //if player is X - if (symbol == 'O') { - throw new Error('Invalid first player'); - } + public Play(symbol: string, x: number, y: number): void { + //if first move //if player is X + if (this._lastSymbol == ' ' && symbol == 'O') { + throw new Error('Invalid first player'); } //if not first move but player repeated - else if (symbol == this._lastSymbol) { + if (symbol == this._lastSymbol) { throw new Error('Invalid next player'); } //if not first move but play on an already played tile - else if (this._toto.TileAt(x, y).Symbol != ' ') { + if (this._board.TileAt(x, y).Symbol != ' ') { throw new Error('Invalid position'); } + + this.updateGameState(symbol, x, y); + } - // update game state + private updateGameState(symbol: string, x: number, y: number) { this._lastSymbol = symbol; - this._toto.AddTileAt(symbol, x, y); + this._board.AddTileAt(symbol, x, y); } public Winner(): string { //if the positions in first row are taken if ( - this._toto.TileAt(0, 0)!.Symbol != ' ' && - this._toto.TileAt(0, 1)!.Symbol != ' ' && - this._toto.TileAt(0, 2)!.Symbol != ' ' + this._board.TileAt(0, 0)!.Symbol != ' ' && + this._board.TileAt(0, 1)!.Symbol != ' ' && + this._board.TileAt(0, 2)!.Symbol != ' ' ) { //if first row is full with same symbol if ( - this._toto.TileAt(0, 0)!.Symbol == this._toto.TileAt(0, 1)!.Symbol && - this._toto.TileAt(0, 2)!.Symbol == this._toto.TileAt(0, 1)!.Symbol + this._board.TileAt(0, 0)!.Symbol == this._board.TileAt(0, 1)!.Symbol && + this._board.TileAt(0, 2)!.Symbol == this._board.TileAt(0, 1)!.Symbol ) { - return this._toto.TileAt(0, 0)!.Symbol; + return this._board.TileAt(0, 0)!.Symbol; } } //if the positions in first row are taken if ( - this._toto.TileAt(1, 0)!.Symbol != ' ' && - this._toto.TileAt(1, 1)!.Symbol != ' ' && - this._toto.TileAt(1, 2)!.Symbol != ' ' + this._board.TileAt(1, 0)!.Symbol != ' ' && + this._board.TileAt(1, 1)!.Symbol != ' ' && + this._board.TileAt(1, 2)!.Symbol != ' ' ) { //if middle row is full with same symbol if ( - this._toto.TileAt(1, 0)!.Symbol == this._toto.TileAt(1, 1)!.Symbol && - this._toto.TileAt(1, 2)!.Symbol == this._toto.TileAt(1, 1)!.Symbol + this._board.TileAt(1, 0)!.Symbol == this._board.TileAt(1, 1)!.Symbol && + this._board.TileAt(1, 2)!.Symbol == this._board.TileAt(1, 1)!.Symbol ) { - return this._toto.TileAt(1, 0)!.Symbol; + return this._board.TileAt(1, 0)!.Symbol; } } //if the positions in first row are taken if ( - this._toto.TileAt(2, 0)!.Symbol != ' ' && - this._toto.TileAt(2, 1)!.Symbol != ' ' && - this._toto.TileAt(2, 2)!.Symbol != ' ' + this._board.TileAt(2, 0)!.Symbol != ' ' && + this._board.TileAt(2, 1)!.Symbol != ' ' && + this._board.TileAt(2, 2)!.Symbol != ' ' ) { //if middle row is full with same symbol if ( - this._toto.TileAt(2, 0)!.Symbol == this._toto.TileAt(2, 1)!.Symbol && - this._toto.TileAt(2, 2)!.Symbol == this._toto.TileAt(2, 1)!.Symbol + this._board.TileAt(2, 0)!.Symbol == this._board.TileAt(2, 1)!.Symbol && + this._board.TileAt(2, 2)!.Symbol == this._board.TileAt(2, 1)!.Symbol ) { - return this._toto.TileAt(2, 0)!.Symbol; + return this._board.TileAt(2, 0)!.Symbol; } }