Skip to content
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
103 changes: 103 additions & 0 deletions src/Game.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
export class Game {
private _lastSymbol: string = ' ';
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");
}
}
//if not first move but player repeated
else if (symbol == this._lastSymbol) {
throw new Error("Invalid next player");
}
//if not first move but play on an already played tile
else if (this._board.TileAt(x, y).Symbol != ' ') {
Copy link

Choose a reason for hiding this comment

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

this._board.TileAt(x, y).Symbol content coupling

throw new Error("Invalid position");
}

// update game state
this._lastSymbol = symbol;
this._board.AddTileAt(symbol, x, y);
}
Copy link

Choose a reason for hiding this comment

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

This function is breaking rule one of the SOLID Princple (single responsibility)

Copy link
Author

Choose a reason for hiding this comment

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

You can list the responsibilities with a name, you can never go bad with an apporach like that, I've came with this two, surely there can be better ones.

Responsibilites in this class:
A) decide winner
B) keep track of the turn


public Winner() : string {
//if the positions in first row are taken
if (this._board.TileAt(0, 0)!.Symbol != ' ' &&
Copy link

Choose a reason for hiding this comment

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

content coupling here ^

Copy link
Author

Choose a reason for hiding this comment

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

The internal content is exposed so it can be modified by external classes

We not only can access to the internals of the board but also we have to learn a protocol to interpret it. The convention ' ' means empty tile could really be any convention '_'.

this._board.TileAt(0, 1)!.Symbol != ' ' &&
this._board.TileAt(0, 2)!.Symbol != ' ') {
//if first row is full with same symbol
if (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._board.TileAt(0, 0)!.Symbol;
}
}

//if the positions in first row are taken
if (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._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._board.TileAt(1, 0)!.Symbol;
}
}

//if the positions in first row are taken
if (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._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._board.TileAt(2, 0)!.Symbol;
}
}

return ' ';
}
}

interface Tile
{
X: number;
Y: number;
Symbol: string;
}

class Board
{
private _plays : Tile[] = [];

constructor()
{
for (let i = 0; i < 3; i++)
{
for (let j = 0; j < 3; j++)
{
const tile : Tile = {X :i, Y:j, Symbol:" "};
this._plays.push(tile);
}
}
}

public TileAt(x: number, y: number): Tile {
return this._plays.find((t:Tile) => t.X == x && t.Y == y)!
}

public AddTileAt(symbol: string, x: number, y: number) : void
{
const tile : Tile = {X :x, Y:y, Symbol:symbol};

this._plays.find((t:Tile) => t.X == x && t.Y == y)!.Symbol = symbol;
}
}