Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hole001 to Hole 02 #14

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/README.md → src/12_RefactoringGolf/hole1/README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
61 changes: 31 additions & 30 deletions src/12_RefactoringGolf/hole1/kata.ts
Original file line number Diff line number Diff line change
@@ -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
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ce commentaire ne sert à rien

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
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ici aussi

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;
}
}

Expand Down
Loading