public void CloneTest_WhenCloning_ThenReturnsCopy() { BoardState target = new BoardState(null); target.SetPiece(0, 0, Piece.X); var copiedBoard = target.Clone(); Assert.AreEqual(copiedBoard.GetPiece(0, 0), Piece.X); }
public Gameplay(PlayStrategy ai) { _ai = ai; _ai.SetMark(BoardMark.O); _whoGoesNow = BoardMark.X; _board = new BoardState(); }
public TicTacToeGame(TicTacToeObjects ticTacToeObjects) { _inputReader = ticTacToeObjects.InputReader; _boardState = ticTacToeObjects.BoardState; _humanPlayer = ticTacToeObjects.HumanPlayer; _computerPlayer = ticTacToeObjects.ComputerPlayer; _pieceManager = ticTacToeObjects.PieceManager; }
public void CloneTest_WhenCloning_ThenReturnsCopyButDifferentInstance() { BoardState target = new BoardState(null); target.SetPiece(0, 0, Piece.X); var copiedBoard = target.Clone(); Assert.AreEqual(copiedBoard.GetPiece(0, 0), Piece.X); target.SetPiece(0, 1, Piece.X); Assert.AreEqual(target.GetPiece(0, 1), Piece.X); Assert.AreEqual(copiedBoard.GetPiece(0, 1), Piece.Empty); }
public void WhenHas3OInRow_ThenOWins() { BoardState target = new BoardState(null); var board = new Piece[3, 3]; board[0, 0] = Piece.O; board[1, 0] = Piece.Empty; board[2, 0] = Piece.X; board[0, 1] = Piece.X; board[1, 1] = Piece.O; board[2, 1] = Piece.Empty; board[0, 2] = Piece.X; board[1, 2] = Piece.Empty; board[2, 2] = Piece.O; target.Board = board; bool isXWinner = target.IsWinner(Piece.O); Assert.AreEqual(true, isXWinner); }
public void WhenAllPositionAreFullAndNoneWins_ThenGameIsDraw() { BoardState target = new BoardState(null); var board = new Piece[3, 3]; board[0, 0] = Piece.O; board[1, 0] = Piece.X; board[2, 0] = Piece.O; board[0, 1] = Piece.O; board[1, 1] = Piece.X; board[2, 1] = Piece.X; board[0, 2] = Piece.X; board[1, 2] = Piece.O; board[2, 2] = Piece.O; target.Board = board; bool isDraw = target.IsDraw(); Assert.AreEqual(true, isDraw); }
public void WhenAllButOnePositionsAreFull_ThenDoMoveUsesLastPosition() { ComputerPlayer target = new ComputerPlayer(); var board = new Piece[3, 3]; board[0, 0] = Piece.O; board[1, 0] = Piece.X; board[2, 0] = Piece.O; board[0, 1] = Piece.O; board[1, 1] = Piece.O; board[2, 1] = Piece.X; board[0, 2] = Piece.X; board[1, 2] = Piece.X; board[2, 2] = Piece.Empty; var boardState = new BoardState(null); boardState.Board = board; target.DoMove(Piece.O, boardState); Assert.AreEqual(Piece.O, boardState.Board[2, 2]); }
public void WhenComputerPlayerHasPossibilityToWinInNextMove_ThenDoMoveMustMustSelectWinningMove() { ComputerPlayer target = new ComputerPlayer(); var board = new Piece[3, 3]; board[0, 0] = Piece.X; board[1, 0] = Piece.O; board[2, 0] = Piece.X; board[0, 1] = Piece.X; board[1, 1] = Piece.O; board[2, 1] = Piece.Empty; board[0, 2] = Piece.Empty; board[1, 2] = Piece.Empty; board[2, 2] = Piece.Empty; var boardState = new BoardState(null); boardState.Board = board; target.DoMove(Piece.O, boardState); Assert.AreEqual(Piece.O, boardState.Board[1, 2]); }
public void WhenFirstPlayerStartsInCorner_ThenDoMoveMustPutPieceInCenter() { ComputerPlayer target = new ComputerPlayer(); var board = new Piece[3, 3]; board[0, 0] = Piece.X; board[1, 0] = Piece.Empty; board[2, 0] = Piece.Empty; board[0, 1] = Piece.Empty; board[1, 1] = Piece.Empty; board[2, 1] = Piece.Empty; board[0, 2] = Piece.Empty; board[1, 2] = Piece.Empty; board[2, 2] = Piece.Empty; var boardState = new BoardState(null); boardState.Board = board; target.DoMove(Piece.O, boardState); Assert.AreEqual(Piece.O, boardState.Board[1, 1]); }
public void GetPieceTest_WhenFieldIsX_ThenGetPieceReturnsX() { BoardState target = new BoardState(null); var board = new Piece[3, 3]; board[0, 0] = Piece.Empty; board[1, 0] = Piece.X; board[2, 0] = Piece.Empty; board[0, 1] = Piece.Empty; board[1, 1] = Piece.Empty; board[2, 1] = Piece.Empty; board[0, 2] = Piece.Empty; board[1, 2] = Piece.Empty; board[2, 2] = Piece.Empty; target.Board = board; Piece actualPiece = target.GetPiece(1, 0); Assert.AreEqual(Piece.X, actualPiece); }
public override int GetNextPosition(BoardState board) { var stopwatch = new Stopwatch(); stopwatch.Start(); var decisionsRoot = new BobDecisionTree(board, OwnMark); var position = GetBestDecision(decisionsRoot).Position; stopwatch.Stop(); Devinfo("Tree Size {0} built in {1} ms", decisionsRoot.TreeSize, stopwatch.ElapsedMilliseconds); return position; }
private static IEnumerable<BobDecisionTree> AllDecisionsFromHere(BoardState board, BoardMark whosTurn, int decisionsCount) { IEnumerable<int> positions2Go = new BoardSymmetry(board).GetFreeUniquePositions(); if (CanOptimizeTreeBuilding(decisionsCount, positions2Go)) { positions2Go = positions2Go.AsParallel(); } return positions2Go .Select(p => new { NewBoard = board.Set(p, whosTurn), Position = p }) .Select(p => new BobDecisionTree(p.NewBoard, whosTurn.OpponentsMark(), p.Position, decisionsCount + 1)) .ToArray(); }
public BoardState Clone() { var newBoardState = new BoardState(_outputWriter); for (int x = 0; x < 3; x++) { for (int y = 0; y < 3; y++) { newBoardState.Board[x, y] = this.Board[x, y]; } } return newBoardState; }
private BobDecisionTree(BoardState board, BoardMark whosTurn, int position, int decisionsCount) { _board = board; _position = position; _decisionsCount = decisionsCount; _children = board.HasCompleteLine() ? Enumerable.Empty<BobDecisionTree>() : AllDecisionsFromHere(board, whosTurn, decisionsCount); _endsFromHere = IsEndDecision ? new[] {this} : CollectEndDecisions(); _treeSize = _children.Select(ch => ch.TreeSize).Sum() + 1; }
public List<Position> GetEmptyFields(BoardState board) { var emptyPositions = new List<Position>(); for (int x = 0; x < 3; x++) { for (int y = 0; y < 3; y++) { if (board.GetPiece(x, y) == Piece.Empty) { emptyPositions.Add(new Position { X = x, Y = y }); } } } return emptyPositions; }
/// <summary> /// Does a computer move using minimax algorithm. /// http://neverstopbuilding.com/minimax /// </summary> public int DoMove(Piece piece, BoardState board) { if (board.IsWinner(Piece.X)) { return 10; } else if (board.IsWinner(Piece.O)) { return -10; } else if (board.IsDraw()) { return 0; } else { var boardStatesForNextMove = new List<BoardState>(); var emptyPositions = GetEmptyFields(board); var scores = new List<int>(); foreach (var move in emptyPositions) { BoardState boardForThisMove = board.Clone(); boardForThisMove.SetPiece(move.X, move.Y, piece); boardStatesForNextMove.Add(boardForThisMove); Piece nextPiece = piece == Piece.X ? Piece.O : Piece.X; scores.Add(DoMove(nextPiece, boardForThisMove)); } int max = scores.Max(); int maxIndex = scores.IndexOf(max); int min = scores.Min(); int minIndex = scores.IndexOf(min); if (piece == Piece.X) { var selectedMove = emptyPositions[maxIndex]; board.SetPiece(selectedMove.X, selectedMove.Y, piece); return max; } else { var selectedMove = emptyPositions[minIndex]; board.SetPiece(selectedMove.X, selectedMove.Y, piece); return min; } } }
public void DoMoveTest_WhenAnIllegalMoveIsEntered_ThenUserMustEnterALegalMove() { var reader = new InputReaderFake(); reader.SetUserInput("a2"); reader.SetUserInput("a2"); reader.SetUserInput("a1"); var target = new HumanPlayer(reader); var boardState = new BoardState(null); var board = new Piece[3, 3]; board[0, 0] = Piece.Empty; board[1, 0] = Piece.X; board[2, 0] = Piece.Empty; board[0, 1] = Piece.Empty; board[1, 1] = Piece.Empty; board[2, 1] = Piece.Empty; board[0, 2] = Piece.Empty; board[1, 2] = Piece.Empty; board[2, 2] = Piece.Empty; boardState.Board = board; int retValue = target.DoMove(Piece.O, boardState); Assert.AreEqual(0, retValue); Assert.AreEqual(Piece.O, boardState.GetPiece(0, 0)); }
public int DoMove(Piece piece, BoardState boardState) { GetUserInput(); do { if (IsFinished()) { return -100; } if (IsInputValid() && boardState.GetPiece(MoveCoordinatesX, MoveCoordinatesY) == Piece.Empty) { boardState.SetPiece(MoveCoordinatesX, MoveCoordinatesY, piece); return 0; } GetUserInput(); } while (true); }
public override int GetNextPosition(BoardState board) { _board = board; return EmptyPosition .Try(IfICanWin, GoToWinPosition) .Try(IfOpponentCanWin, BlockWinPosition) .Try(IfICanFork, GoToForkPosition) .Try(IfOpponentCanFork, BlockForkPosition) .Try(IfCenterFree, () => BoardState.Center) .Try(IfAnyCornerFree, GetBestCorner) .Try(() => board.FreePositions.Count() > 0, () => board.FreePositions.First()) .Return(); }
public void GetEmptyFieldsTest_WhenTwoFieldsAreEmpty_ThenReturnThoseEmptyFields() { ComputerPlayer target = new ComputerPlayer(); var board = new Piece[3, 3]; board[0, 0] = Piece.O; board[1, 0] = Piece.X; board[2, 0] = Piece.O; board[0, 1] = Piece.O; board[1, 1] = Piece.O; board[2, 1] = Piece.Empty; board[0, 2] = Piece.X; board[1, 2] = Piece.X; board[2, 2] = Piece.Empty; var boardState = new BoardState(null); boardState.Board = board; List<Position> emptyFields = target.GetEmptyFields(boardState); List<Position> expectedEmptys = new List<Position>() { new Position {X = 2, Y = 1}, new Position {X = 2, Y = 2} }; Assert.AreEqual(emptyFields.Count == expectedEmptys.Count, true); int i = 0; foreach (var position in expectedEmptys) { Assert.AreEqual(true, position.IsEqual(emptyFields[i])); i++; } }
public void Reset() { _whoGoesNow = BoardMark.X; _board = new BoardState(); OnChanged(); }
public void WhenSettingXIntoField_ThenThatFieldIsOccupiedByX() { BoardState target = new BoardState(null); target.SetPiece(0, 0, Piece.X); Assert.AreEqual(Piece.X, target.Board[0, 0]); }
public void WhenSettingYIntoField_ThenThatFieldIsOccupiedByY() { BoardState target = new BoardState(null); target.SetPiece(1, 2, Piece.O); Assert.AreEqual(Piece.O, target.Board[1, 2]); }
public void WhenXHas3XInRow_ThenXWins() { BoardState target = new BoardState(null); target.SetPiece(0, 1, Piece.X); target.SetPiece(1, 1, Piece.X); target.SetPiece(2, 1, Piece.X); bool isXWinner = target.IsWinner(Piece.X); Assert.AreEqual(true, isXWinner); }
public void WhenResettingTheBoard_ThenTheFieldsAreEmpty() { BoardState target = new BoardState(null); target.Board[1, 2] = Piece.X; target.Reset(); Assert.AreEqual(Piece.Empty, target.GetPiece(1, 2)); }
public void WhenXHasNot3XInRow_ThenXDoesNotWin() { BoardState target = new BoardState(null); target.SetPiece(0, 0, Piece.X); target.SetPiece(1, 1, Piece.X); target.SetPiece(2, 1, Piece.X); bool isXWinner = target.IsWinner(Piece.X); Assert.AreEqual(false, isXWinner); }
public void WhenNotAllPositionsFull_ThenGameIsNotDraw() { BoardState target = new BoardState(null); var board = new Piece[3, 3]; board[0, 0] = Piece.O; board[1, 0] = Piece.X; board[2, 0] = Piece.O; board[0, 1] = Piece.X; board[1, 1] = Piece.X; board[2, 1] = Piece.X; board[0, 2] = Piece.X; board[1, 2] = Piece.Empty; board[2, 2] = Piece.X; target.Board = board; bool isDraw = target.IsDraw(); Assert.AreEqual(false, isDraw); }
public BobDecisionTree(BoardState board, BoardMark whosTurn) : this(board, whosTurn, -1, 0) { }
private void SetAMark(int position) { if (!Board.FreePositions.Any(p => p == position)) { throw new InvalidOperationException(String.Format("Cannot go to position {0}!", position)); } else { _board = _board.Set(position, WhoGoesNow); _whoGoesNow = WhoGoesNow.OpponentsMark(); } }
public BoardSymmetry(BoardState boardState) { _boardState = boardState; }