patterncsharpMinor
TicTactics GameBoard Logic
Viewed 0 times
tictacticsgameboardlogic
Problem
This is my take at the current code-challenge, Ultimate Tic-Tac-Toe.
It all started with a
"Wait", I hear you say - "an enum for board positions?" - Absolutely! That allowed me to define a
Inspired by WPF and
BoardBase[TCell]
Very early in the design process I realized most of the "bigger board" functionality was also needed in the "smaller boards", so I wrote a generic abstract class where the type parameter determines the type of cell:
```
public abstract class BoardBase : IBoardCell
where TCell : IBoardCell
{
private readonly IDictionary _cells;
private readonly BoardEvaluator _evaluator;
protected BoardBase(BoardEvaluator evaluator, ICellFactory cellFactory)
{
_evaluator = evaluator;
_cells = Enum.GetValues(typeof(BoardPosition))
.Cast()
.ToDictionary(position => position, position => cellFactory.Create(position));
RegisterCellE
It all started with a
CellValue and a BoardPosition:///
/// Identifies the players, or the possible values a cell can take.
///
public enum CellValue
{
X,
O
}
///
/// Identifies the possible board positions.
///
public enum BoardPosition
{
TopLeft,
Top,
TopRight,
Left,
Center,
Right,
BottomLeft,
Bottom,
BottomRight
}"Wait", I hear you say - "an enum for board positions?" - Absolutely! That allowed me to define a
BoardCell interface:public interface IBoardCell
{
BoardPosition Position { get; }
CellValue? Value { get; set; }
event EventHandler CellValueChanged;
}Inspired by WPF and
INotifyPropertyChanged, I'm using events to communicate a change of a cell's value to whoever might be interested (that's the parent board):public class CellValueChangedEventArgs : EventArgs
{
private BoardPosition _position;
BoardPosition Position { get { return _position; } }
private CellValue? _value;
CellValue? Value { get { return _value; } }
public CellValueChangedEventArgs(BoardPosition position, CellValue? value)
{
_position = position;
_value = value;
}
}BoardBase[TCell]
Very early in the design process I realized most of the "bigger board" functionality was also needed in the "smaller boards", so I wrote a generic abstract class where the type parameter determines the type of cell:
```
public abstract class BoardBase : IBoardCell
where TCell : IBoardCell
{
private readonly IDictionary _cells;
private readonly BoardEvaluator _evaluator;
protected BoardBase(BoardEvaluator evaluator, ICellFactory cellFactory)
{
_evaluator = evaluator;
_cells = Enum.GetValues(typeof(BoardPosition))
.Cast()
.ToDictionary(position => position, position => cellFactory.Create(position));
RegisterCellE
Solution
I like it,
especially the enums.
Although the Tuple evaluator is super hard to parse.
Definitely think it would be better to wrap that in a
besides if memory serves you can perform flag concatenation for a cleaner compare.
and make a list of them:
or something....
especially the enums.
Although the Tuple evaluator is super hard to parse.
Definitely think it would be better to wrap that in a
WinCondition interface or something which contains the relevant enum flag.besides if memory serves you can perform flag concatenation for a cleaner compare.
BoardPosition winPosition = BoardPosition.TopLeft | BoardPosition.Top | BoardPosition.TopRight;and make a list of them:
_winPositions.Any(position => position == currentPosition)or something....
Code Snippets
BoardPosition winPosition = BoardPosition.TopLeft | BoardPosition.Top | BoardPosition.TopRight;_winPositions.Any(position => position == currentPosition)Context
StackExchange Code Review Q#41029, answer score: 4
Revisions (0)
No revisions yet.