Skip to content

Commit

Permalink
refactor: remove _prefix from instance vars
Browse files Browse the repository at this point in the history
  • Loading branch information
pedromsantos committed Jan 10, 2024
1 parent b24d4d7 commit bbac510
Show file tree
Hide file tree
Showing 14 changed files with 219 additions and 219 deletions.
72 changes: 36 additions & 36 deletions 13_RefactoringGolf/hole1/kata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,18 @@ public class Tile

public class Board
{
private readonly List<Tile> _plays = new();
private readonly List<Tile> plays = new();

public Board()
{
for (var i = 0; i < 3; i++)
for (var j = 0; j < 3; j++)
_plays.Add(new Tile { X = i, Y = j, Symbol = ' ' });
for (var j = 0; j < 3; j++)
plays.Add(new Tile { X = i, Y = j, Symbol = ' ' });
}

public Tile TileAt(int x, int y)
{
return _plays.Single(tile => tile.X == x && tile.Y == y);
return plays.Single(tile => tile.X == x && tile.Y == y);
}

public void AddTileAt(char symbol, int x, int y)
Expand All @@ -169,73 +169,73 @@ public void AddTileAt(char symbol, int x, int y)
Symbol = symbol
};

_plays.Single(tile => tile.X == x && tile.Y == y).Symbol = symbol;
plays.Single(tile => tile.X == x && tile.Y == y).Symbol = symbol;
}
}

public class Game
{
private readonly Board _board = new();
private char _lastSymbol = ' ';
private readonly Board board = new();
private char lastSymbol = ' ';

public void Play(char symbol, int x, int y)
{
//if first move
if (_lastSymbol == ' ')
if (lastSymbol == ' ')
{
//if player is X
if (symbol == 'O') throw new Exception("Invalid first player");
}
//if not first move but player repeated
else if (symbol == _lastSymbol)
else if (symbol == lastSymbol)
{
throw new Exception("Invalid next player");
}
//if not first move but play on an already played tile
else if (_board.TileAt(x, y).Symbol != ' ')
else if (board.TileAt(x, y).Symbol != ' ')
{
throw new Exception("Invalid position");
}

// update game state
_lastSymbol = symbol;
_board.AddTileAt(symbol, x, y);
lastSymbol = symbol;
board.AddTileAt(symbol, x, y);
}

public char Winner()
{
//if the positions in first row are taken
if (_board.TileAt(0, 0).Symbol != ' ' &&
_board.TileAt(0, 1).Symbol != ' ' &&
_board.TileAt(0, 2).Symbol != ' ')
if (board.TileAt(0, 0).Symbol != ' ' &&
board.TileAt(0, 1).Symbol != ' ' &&
board.TileAt(0, 2).Symbol != ' ')
//if first row is full with same symbol
if (_board.TileAt(0, 0).Symbol ==
_board.TileAt(0, 1).Symbol &&
_board.TileAt(0, 2).Symbol ==
_board.TileAt(0, 1).Symbol)
return _board.TileAt(0, 0).Symbol;
if (board.TileAt(0, 0).Symbol ==
board.TileAt(0, 1).Symbol &&
board.TileAt(0, 2).Symbol ==
board.TileAt(0, 1).Symbol)
return board.TileAt(0, 0).Symbol;

//if the positions in first row are taken
if (_board.TileAt(1, 0).Symbol != ' ' &&
_board.TileAt(1, 1).Symbol != ' ' &&
_board.TileAt(1, 2).Symbol != ' ')
if (board.TileAt(1, 0).Symbol != ' ' &&
board.TileAt(1, 1).Symbol != ' ' &&
board.TileAt(1, 2).Symbol != ' ')
//if middle row is full with same symbol
if (_board.TileAt(1, 0).Symbol ==
_board.TileAt(1, 1).Symbol &&
_board.TileAt(1, 2).Symbol ==
_board.TileAt(1, 1).Symbol)
return _board.TileAt(1, 0).Symbol;
if (board.TileAt(1, 0).Symbol ==
board.TileAt(1, 1).Symbol &&
board.TileAt(1, 2).Symbol ==
board.TileAt(1, 1).Symbol)
return board.TileAt(1, 0).Symbol;

//if the positions in first row are taken
if (_board.TileAt(2, 0).Symbol != ' ' &&
_board.TileAt(2, 1).Symbol != ' ' &&
_board.TileAt(2, 2).Symbol != ' ')
if (board.TileAt(2, 0).Symbol != ' ' &&
board.TileAt(2, 1).Symbol != ' ' &&
board.TileAt(2, 2).Symbol != ' ')
//if middle row is full with same symbol
if (_board.TileAt(2, 0).Symbol ==
_board.TileAt(2, 1).Symbol &&
_board.TileAt(2, 2).Symbol ==
_board.TileAt(2, 1).Symbol)
return _board.TileAt(2, 0).Symbol;
if (board.TileAt(2, 0).Symbol ==
board.TileAt(2, 1).Symbol &&
board.TileAt(2, 2).Symbol ==
board.TileAt(2, 1).Symbol)
return board.TileAt(2, 0).Symbol;

return ' ';
}
Expand Down
24 changes: 12 additions & 12 deletions 13_RefactoringGolf/hole10/kata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,18 +240,18 @@ public bool HasSamePosition(Tile other)

public class Board
{
private readonly List<Tile> _plays = new();
private readonly List<Tile> plays = new();

public Board()
{
for (var row = 0; row < 3; row++)
for (var column = 0; column < 3; column++)
_plays.Add(new Tile(row, column));
for (var column = 0; column < 3; column++)
plays.Add(new Tile(row, column));
}

public Tile TileAt(int x, int y)
{
return _plays.Single(tile => tile.HasSamePosition(new Tile(x, y)));
return plays.Single(tile => tile.HasSamePosition(new Tile(x, y)));
}

public void AddTileAt(int x, int y, Symbol symbol)
Expand Down Expand Up @@ -292,8 +292,8 @@ private bool IsRowTaken(int rowIndex)

public class Game
{
private readonly Board _board = new();
private Symbol _lastSymbol = Symbol.Empty;
private readonly Board board = new();
private Symbol lastSymbol = Symbol.Empty;

public void Play(int x, int y, Symbol newSymbol)
{
Expand All @@ -307,33 +307,33 @@ public void Play(int x, int y, Symbol newSymbol)

private void UpdateBoard(int x, int y, Symbol symbol)
{
_board.AddTileAt(x, y, symbol);
board.AddTileAt(x, y, symbol);
}

private void UpdateLastPlayer(Symbol symbol)
{
_lastSymbol = symbol;
lastSymbol = symbol;
}

private void ValidatePositionIsEmpty(int x, int y)
{
if (_board.TileAt(x, y).IsTaken()) throw new Exception("Invalid position");
if (board.TileAt(x, y).IsTaken()) throw new Exception("Invalid position");
}

private void ValidatePlayer(Symbol symbol)
{
if (symbol == _lastSymbol) throw new Exception("Invalid next player");
if (symbol == lastSymbol) throw new Exception("Invalid next player");
}

private void ValidateFirstMove(Symbol symbol)
{
if (_lastSymbol == Symbol.Empty)
if (lastSymbol == Symbol.Empty)
if (symbol == Symbol.O)
throw new Exception("Invalid first player");
}

public Symbol Winner()
{
return _board.FindSymbolWhoTookARow();
return board.FindSymbolWhoTookARow();
}
}
24 changes: 12 additions & 12 deletions 13_RefactoringGolf/hole11/kata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,18 +264,18 @@ public void UpdateSymbol(Symbol newSymbol)

public class Board
{
private readonly List<Tile> _plays = new();
private readonly List<Tile> plays = new();

public Board()
{
for (var row = 0; row < 3; row++)
for (var column = 0; column < 3; column++)
_plays.Add(new Tile(new Coordinate(row, column)));
for (var column = 0; column < 3; column++)
plays.Add(new Tile(new Coordinate(row, column)));
}

public Tile TileAt(Tile other)
{
return _plays.Single(tile => tile.HasSamePosition(other));
return plays.Single(tile => tile.HasSamePosition(other));
}

public void AddTileAt(Tile newTile)
Expand Down Expand Up @@ -316,8 +316,8 @@ private bool IsRowTaken(int rowIndex)

public class Game
{
private readonly Board _board = new();
private Symbol _lastSymbol = Symbol.Empty;
private readonly Board board = new();
private Symbol lastSymbol = Symbol.Empty;

public void Play(Tile newTile)
{
Expand All @@ -331,33 +331,33 @@ public void Play(Tile newTile)

private void UpdateBoard(Tile newTile)
{
_board.AddTileAt(newTile);
board.AddTileAt(newTile);
}

private void UpdateLastPlayer(Symbol symbol)
{
_lastSymbol = symbol;
lastSymbol = symbol;
}

private void ValidatePositionIsEmpty(Tile other)
{
if (_board.TileAt(other).IsTaken()) throw new Exception("Invalid position");
if (board.TileAt(other).IsTaken()) throw new Exception("Invalid position");
}

private void ValidatePlayer(Symbol symbol)
{
if (symbol == _lastSymbol) throw new Exception("Invalid next player");
if (symbol == lastSymbol) throw new Exception("Invalid next player");
}

private void ValidateFirstMove(Symbol symbol)
{
if (_lastSymbol == Symbol.Empty)
if (lastSymbol == Symbol.Empty)
if (symbol == Symbol.O)
throw new Exception("Invalid first player");
}

public Symbol Winner()
{
return _board.FindSymbolWhoTookARow();
return board.FindSymbolWhoTookARow();
}
}
24 changes: 12 additions & 12 deletions 13_RefactoringGolf/hole12/kata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,18 +240,18 @@ public void UpdateSymbol(Symbol newSymbol)

public class Board
{
private readonly List<Tile> _plays = new();
private readonly List<Tile> plays = new();

public Board()
{
for (var row = 0; row < 3; row++)
for (var column = 0; column < 3; column++)
_plays.Add(new Tile(new Coordinate(row, column)));
for (var column = 0; column < 3; column++)
plays.Add(new Tile(new Coordinate(row, column)));
}

public Tile TileAt(Tile other)
{
return _plays.Single(tile => tile.HasSamePosition(other));
return plays.Single(tile => tile.HasSamePosition(other));
}

public void AddTileAt(Tile newTile)
Expand Down Expand Up @@ -292,8 +292,8 @@ private bool IsRowTaken(int rowIndex)

public class Game
{
private readonly Board _board = new();
private Symbol _lastSymbol = Symbol.Empty;
private readonly Board board = new();
private Symbol lastSymbol = Symbol.Empty;

public void Play(Tile newTile)
{
Expand All @@ -307,31 +307,31 @@ public void Play(Tile newTile)

private void UpdateBoard(Tile newTile)
{
_board.AddTileAt(newTile);
board.AddTileAt(newTile);
}

private void UpdateLastPlayer(Symbol symbol)
{
_lastSymbol = symbol;
lastSymbol = symbol;
}

private void ValidatePositionIsEmpty(Tile other)
{
if (_board.TileAt(other).IsTaken()) throw new Exception("Invalid position");
if (board.TileAt(other).IsTaken()) throw new Exception("Invalid position");
}

private void ValidatePlayer(Symbol symbol)
{
if (symbol == _lastSymbol) throw new Exception("Invalid next player");
if (symbol == lastSymbol) throw new Exception("Invalid next player");
}

private void ValidateFirstMove(Symbol symbol)
{
if (_lastSymbol == Symbol.Empty && symbol == Symbol.O) throw new Exception("Invalid first player");
if (lastSymbol == Symbol.Empty && symbol == Symbol.O) throw new Exception("Invalid first player");
}

public Symbol Winner()
{
return _board.FindSymbolWhoTookARow();
return board.FindSymbolWhoTookARow();
}
}
Loading

0 comments on commit bbac510

Please sign in to comment.