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

Exo04 refacto #36

Open
wants to merge 2 commits into
base: exo04
Choose a base branch
from
Open
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
66 changes: 17 additions & 49 deletions src/12_RefactoringGolf/hole1/kata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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');
}
}
Expand All @@ -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
);
}
}
Expand Down