Exemple #1
0
        public GameBoard AfterMove(Square square)
        {
            Debug.Assert(_legalMoves.Contains(square));

            GameBoard nextPosition = new GameBoard();
            nextPosition._moveIndex = _moveIndex + 1;

            nextPosition._pieces = new PieceColor[NumberOfSquares];
            Array.Copy(_pieces, nextPosition._pieces, NumberOfSquares);
            List<Square> capturedPieces = FlankedSquares(square).ToList();
            foreach (Square capturedPiece in capturedPieces)
                nextPosition._pieces[capturedPiece.Index] = _toMove;
            nextPosition._pieces[square.Index] = _toMove;

            nextPosition._toMove = GetOpposite();
            nextPosition.UpdateLegalMoves();

            return nextPosition;
        }
Exemple #2
0
        private IEnumerable<Square> FlankedSquaresInDirection(Square square, int deltaRow, int deltaColumn)
        {
            Square step = NextSquare(square, deltaRow, deltaColumn);
            PieceColor opposite = GetOpposite();

            if (step.IsOnBoard && _pieces[step.Index] == opposite)
            {
                List<Square> flankedPieces = new List<Square>();
                flankedPieces.Add(step);
                step = NextSquare(step, deltaRow, deltaColumn);

                while (step.IsOnBoard)
                {
                    if (_pieces[step.Index] == _toMove)
                        return flankedPieces;
                    if (_pieces[step.Index] == PieceColor.Empty)
                        return Enumerable.Empty<Square>();
                    flankedPieces.Add(step);
                    step = NextSquare(step, deltaRow, deltaColumn);
                }
                return Enumerable.Empty<Square>();
            }

            return Enumerable.Empty<Square>();
        }
Exemple #3
0
 private bool IsLegalMove(Square square)
 {
     return FlankedSquares(square).Any();
 }
Exemple #4
0
        public void MakeMove(Square square)
        {
            _depGameBoard.OnGet();
            if (MyTurn && _gameBoard.LegalMoves.Contains(square))
            {
                _player.MakeMove(_gameBoard.MoveIndex, square.Index);

                _depGameBoard.OnGet();
                if (_gameBoard.ToMove == PieceColor.Empty)
                {
                    int blackCount = _gameBoard.BlackCount;
                    int whiteCount = _gameBoard.WhiteCount;
                    if (blackCount > whiteCount)
                        _player.Game.DeclareWinner(_player.Game.Players.Single(p => p.Index == 0), false);
                    else if (blackCount < whiteCount)
                        _player.Game.DeclareWinner(_player.Game.Players.Single(p => p.Index == 1), false);
                    else
                        _player.Game.DeclareWinner(null, false);
                }
            }
        }
Exemple #5
0
 private static Square NextSquare(Square from, int deltaRow, int deltaColumn)
 {
     return Square.FromCoordinates(from.Row + deltaRow, from.Column + deltaColumn);
 }
 public bool IsPreviewCede(Square square)
 {
     return PreviewBoard.LegalMoves.Contains(square);
 }
 public void MakeMove(Square square)
 {
     _mainNavigation.PreviewMove = null;
     _depGameBoard.OnGet();
     if (_mainNavigation.PendingMove == null && MyTurn && _gameBoard.LegalMoves.Contains(square))
     {
         _mainNavigation.PendingMoveIndex = _gameBoard.MoveIndex;
         _mainNavigation.PendingMove = square;
     }
 }
 public PieceColor PieceAt(Square square)
 {
     GameBoard gameBoard = GetGameBoard();
     return gameBoard == null ? PieceColor.Empty : gameBoard.PieceAt(square);
 }
 public SquareViewModel(GameState gameState, Square square)
 {
     _gameState = gameState;
     _square = square;
 }
 public bool IsPriorMove(Square square)
 {
     GameBoard gameBoard = GetGameBoard();
     GameBoard priorBoard = GetPriorBoard();
     return
         _mainNavigation.PreviewMove == null &&
         priorBoard.PieceAt(square) == PieceColor.Empty &&
         gameBoard.PieceAt(square) != PieceColor.Empty;
 }
 public void MakeMove(Square square)
 {
     _mainNavigation.PreviewMove = null;
     GameBoard gameBoard = GetGameBoard();
     if (_mainNavigation.PendingMove == null && gameBoard.LegalMoves.Contains(square))
     {
         _mainNavigation.PendingMoveIndex = gameBoard.MoveIndex;
         _mainNavigation.PendingMove = square;
     }
 }
 public bool IsPreviewCede(Square square)
 {
     GameBoard previewBoard = GetPreviewBoard();
     return
         previewBoard.LegalMoves.Contains(square);
 }
 public bool IsPreviewCapture(Square square)
 {
     GameBoard gameBoard = GetGameBoard();
     GameBoard previewBoard = GetPreviewBoard();
     return
         _mainNavigation.PreviewMove != square &&
         gameBoard.PieceAt(square) != previewBoard.PieceAt(square);
 }
 public LocalSquareViewModel(LocalGameState gameState, Square square)
 {
     _gameState = gameState;
     _square = square;
 }
Exemple #15
0
 public PieceColor PieceAt(Square square)
 {
     return _pieces[square.Index];
 }
Exemple #16
0
        public override string ToString()
        {
            StringBuilder boardPosition = new StringBuilder();
            for (int row = 0; row < Square.NumberOfRows; row++)
            {
                for (int column = 0; column < Square.NumberOfColumns; column++)
                {
                    Square square = new Square(row, column);
                    var piece = _pieces[square.Index];
                    boardPosition.Append(
                        piece == PieceColor.Black ? "B" :
                        piece == PieceColor.White ? "W" : ".");
                }
                boardPosition.AppendLine();
            }

            return boardPosition.ToString();
        }
 public bool IsPreviewCapture(Square square)
 {
     return
         _mainNavigation.PreviewMove != square &&
         _gameBoard.PieceAt(square) != PreviewBoard.PieceAt(square);
 }
Exemple #18
0
 private static Square NextSquare(Square from, int deltaRow, int deltaColumn)
 {
     return new Square(from.Row + deltaRow, from.Column + deltaColumn);
 }
 public bool IsPriorMove(Square square)
 {
     _depGameBoard.OnGet();
     return
         _mainNavigation.PreviewMove == null &&
         _priorBoard.PieceAt(square) == PieceColor.Empty &&
         _gameBoard.PieceAt(square) != PieceColor.Empty;
 }
Exemple #20
0
 private IEnumerable<Square> EvaluateLegalMoves()
 {
     for (int row = 0; row < Square.NumberOfRows; row++)
     {
         for (int column = 0; column < Square.NumberOfColumns; column++)
         {
             Square square = new Square(row, column);
             if (IsLegalMove(square))
                 yield return square;
         }
     }
 }
 public bool SetPreviewMove(Square square)
 {
     _depGameBoard.OnGet();
     if (square != null && !_gameBoard.LegalMoves.Contains(square))
         square = null;
     if (!Object.Equals(_mainNavigation.PreviewMove, square))
     {
         _mainNavigation.PreviewMove = square;
         return square != null;
     }
     return false;
 }
Exemple #22
0
 private IEnumerable<Square> FlankedSquares(Square center)
 {
     if (_pieces[center.Index] == PieceColor.Empty)
     {
         for (int direction = 0; direction < 9; direction++)
         {
             int deltaRow = (direction / 3) - 1;
             int deltaColumn = (direction % 3) - 1;
             if (deltaRow != 0 || deltaColumn != 0)
                 foreach (Square square in FlankedSquaresInDirection(center, deltaRow, deltaColumn))
                     yield return square;
         }
     }
 }
Exemple #23
0
 public PieceColor PieceAt(Square square)
 {
     _depGameBoard.OnGet();
     return _gameBoard == null ? PieceColor.Empty : _gameBoard.PieceAt(square);
 }
 public RemoteSquareViewModel(RemoteGameState gameState, Square square)
 {
     _gameState = gameState;
     _square = square;
 }