Beispiel #1
0
        public void MakeMove(Move move)
        {
            if (IsValidMove(move))
            {
                _board.RemovePiece(move.Piece);
                int newRow = move.Piece.Row;
                int newCol = move.Piece.Col;
                foreach (MoveDirection direction in move.Direction)
                {
                    newRow += MoveUtil.GetRowMoveAmountByColor(move.Piece.Owner, direction);
                    newCol += MoveUtil.GetColMoveAmount(direction);

                    CheckerPiece pieceInPosition = _board.GetPiece(newRow, newCol);
                    if (pieceInPosition != null)
                    {
                        _board.RemovePiece(pieceInPosition);
                        newRow += MoveUtil.GetRowMoveAmountByColor(move.Piece.Owner, direction);
                        newCol += MoveUtil.GetColMoveAmount(direction);
                    }
                }

                var pieceAfterMove = new CheckerPiece(move.Piece);
                pieceAfterMove.Row = newRow;
                pieceAfterMove.Col = newCol;
                _board.AddPiece(pieceAfterMove);
            }
            else
            {
                throw new ArgumentException("Invalid Move");
            }
        }
Beispiel #2
0
 public BoardMoveGenerator(CheckerBoard board, int row, int col)
 {
     Board = board;
     if (Board == null || Board.GetPiece(row, col) == null)
     {
         throw new ArgumentException("Error: Move generator must have both a valid board and piece to generate moves.");
     }
     Piece = board.GetPiece(row, col);
 }
Beispiel #3
0
 public void UpdateDisplay(CheckerBoard board)
 {
     for (int row = 0; row < CheckerBoard.SIZE; row++)
     {
         for (int col = 0; col < CheckerBoard.SIZE; col++)
         {
             CheckerPiece tile    = board.GetPiece(row, col);
             string       tileRep = GetTileDisplayRepresentationWithPadding(tile);
             _display.DisplayText(tileRep);
         }
         _display.DisplayText($"{Environment.NewLine}");
     }
 }
Beispiel #4
0
        //public bool MakeMove(Move move)
        //{
        //PieceColor thisColor = move.Piece.Owner;
        //if (!GetLegalMoves(thisColor).Contains(move))
        //    return false;

        //var tempBoard = (move.Piece.Owner == PieceColor.White) ? FlipBoard() : boardState;
        //var opposingColor = Piece.GetOppositeColor(move.Piece.Owner);
        //int rowMove = GetRowMoveAmount(move.Direction);
        //int colMove = GetColMoveAmount(move.Direction);

        //if (TileCanBeJumped(move.Piece.Row + rowMove, move.Piece.Col + colMove, opposingColor, boardState, move.Direction))
        //{
        //    // move piece two times further
        //    // capture opposing piece
        //    // check that piece can't jump again
        //    if (PieceCanJump(move.Piece, opposingColor, boardState))
        //    {
        //        // get move from player to jump
        //    }
        //}
        //else
        //{
        //    // move piece
        //}
        //return true;
        //}

        public List <Move> GetLegalMoves(PieceColor player)
        {
            PieceColor   opposingColor = CheckerPiece.GetOppositeColor(player);
            CheckerBoard boardToCheck  = this;
            var          legalMoves    = new List <Move>();

            for (int i = 0; i < SIZE; i++)
            {
                for (int j = 0; j < SIZE; j++)
                {
                    var piece = boardToCheck.GetPiece(i, j);
                    if ((piece == null) || (piece.Owner == opposingColor))
                    {
                        continue;
                    }
                    var moves = new BoardMoveGenerator(boardToCheck, piece.Row, piece.Col).Moves;
                    legalMoves.AddRange(moves);
                }
            }
            return(legalMoves);
        }
Beispiel #5
0
        public static bool TileIsOpposingColor(this CheckerBoard board, int row, int col, PieceColor opposingColor)
        {
            CheckerPiece piece = board.GetPiece(row, col);

            return((piece != null) && (piece.Owner == opposingColor));
        }