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

Exo01 refacto #34

Open
wants to merge 3 commits into
base: exo01
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Update kata.ts
  • Loading branch information
username-Everam committed Sep 23, 2024
commit 504fd1c20a256f2389cebdaa752962a567fcdbba
85 changes: 49 additions & 36 deletions src/12_RefactoringGolf/hole1/kata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,92 +6,105 @@ export class Game {
private _toto: Board = new Board();

public Play(symbol: string, x: number, y: number): void {
//if first move
this.valideFirstMove(symbol);
this.valideNotRepeated(symbol);
this.validePlacePlayed(x, y);

this.updateGameState(symbol, x, y);
}

private valideFirstMove(symbol: string): void {
if (this._lastSymbol == ' ') {
//if player is X
if (symbol == 'O') {
throw new Error('Invalid first player');
}
}
//if not first move but player repeated
else if (symbol == this._lastSymbol) {
}

private valideNotRepeated(symbol: string): void {
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 != ' ') {
}

private validePlacePlayed(x : number, y : number): void {
if (this._toto.TileAt(x, y).Symbol != ' ') {
throw new Error('Invalid position');
}
}

// update game state
private updateGameState(symbol: string, x: number, y: number): void {
this._lastSymbol = symbol;
this._toto.AddTileAt(symbol, x, y);
}

public Winner(): string {
//if the positions in first row are taken
if (
this.firstRowFull()
) {
//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
) {
if (this.firstRowFull()) {
if (this.firstRowFullSameSymbol()) {
return this._toto.TileAt(0, 0)!.Symbol;
}
}

//if the positions in middle row are taken
if (
this.middleRowFull()
) {
//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
) {
if (this.middleRowFull()) {
if (this.middleRowFullSameSymbol()) {
return this._toto.TileAt(1, 0)!.Symbol;
}
}

//if the positions in bottom row are taken
if (
this.bottomRowFull()
) {
//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
) {
if (this.bottomRowFull()) {
if (this.bottomRowFullSameSymbol()) {
return this._toto.TileAt(2, 0)!.Symbol;
}
}

return ' ';
}

private firstRowFull(): boolean {
return (
this._toto.TileAt(0, 0)!.Symbol != ' ' &&
this._toto.TileAt(0, 1)!.Symbol != ' ' &&
this._toto.TileAt(0, 2)!.Symbol != ' '
);
}

private middleRowFull(): boolean {
return (
this._toto.TileAt(1, 0)!.Symbol != ' ' &&
this._toto.TileAt(1, 1)!.Symbol != ' ' &&
this._toto.TileAt(1, 2)!.Symbol != ' '
);
}

private bottomRowFull(): boolean {
return (
this._toto.TileAt(2, 0)!.Symbol != ' ' &&
this._toto.TileAt(2, 1)!.Symbol != ' ' &&
this._toto.TileAt(2, 2)!.Symbol != ' '
);
}


private firstRowFullSameSymbol(): boolean {
return (
this._toto.TileAt(0, 0)!.Symbol == this._toto.TileAt(0, 1)!.Symbol &&
this._toto.TileAt(0, 2)!.Symbol == this._toto.TileAt(0, 1)!.Symbol
);
}

private middleRowFullSameSymbol(): boolean {
return (
this._toto.TileAt(1, 0)!.Symbol == this._toto.TileAt(1, 1)!.Symbol &&
this._toto.TileAt(1, 2)!.Symbol == this._toto.TileAt(1, 1)!.Symbol
);
}

private bottomRowFullSameSymbol(): boolean {
return (
this._toto.TileAt(2, 0)!.Symbol == this._toto.TileAt(2, 1)!.Symbol &&
this._toto.TileAt(2, 2)!.Symbol == this._toto.TileAt(2, 1)!.Symbol
);
}


}

Expand Down