public void OnEndDrag(PointerEventData eventData) { if (!moveManager.CanPieceMove(piece.GetColor())) { return; } board.DeselectAllTiles(); board.SetRayBlockingForAllPieces(true); if (board.IsPositionOnBoard(eventData.position)) { Tile newTile = board.GetCurrentlyPointedTile(); List <ChessMove> availableMoves = moveManager.AvailableMovesForPiece(piece); bool validMove = false; for (int i = 0; i < availableMoves.Count; i++) { if (availableMoves[i].toTile == newTile) { moveManager.DoMove(availableMoves[i]); validMove = true; break; } } if (!validMove) { MovePiece(currentTileUI); } } else { MovePiece(currentTileUI); } }
public void AddMove(string algebraicMove, PieceColor player) { ChessMove move = AlgebraicNotation.ToChessMove(algebraicMove, moveManager, player); moveManager.DoMove(move, false, false); }
public static ChessMove ToChessMove(string algebraicMove, MoveManager moveManager, PieceColor color) { PieceColor oppColor; if (color == PieceColor.White) { oppColor = PieceColor.Black; } else { oppColor = PieceColor.White; } string modAlgMove = algebraicMove; //Strip not checked /*int curChar = 0; * if (modAlgMove[curChar] == oppKingInCheck) * { * //curChar++; * modAlgMove = modAlgMove.Substring(1); * }*/ bool promotionMove = false; PieceType promotionType = PieceType.Queen; if (algebraicMove[algebraicMove.Length - 2] == promotion) { modAlgMove = algebraicMove.Substring(0, algebraicMove.Length - 1); promotionMove = true; promotionType = GetPieceName(algebraicMove[algebraicMove.Length - 1].ToString()); } List <ChessMove> allMoves = moveManager.AllAvailableMoves(color); foreach (ChessMove move in allMoves) { ChessMove fullMove = moveManager.DoMove(move, true, false); fullMove.checksOpponent = moveManager.IsKingInCheck(oppColor); moveManager.UndoMove(fullMove, true, false); string genMove = ToAlgebraic(fullMove, moveManager); if (genMove == modAlgMove) { if (promotionMove) { fullMove.isPromotionMove = true; fullMove.isCompletePromotionMove = true; fullMove.promotionType = promotionType; } return(fullMove); } } throw new Exception($"Move {algebraicMove} cannot be performed"); /*ChessMove chessMove = new ChessMove(); * if (string.Equals(algebraicMove, queenSideCastle)) * { * return moveManager.GetCastlingMove(color, true); * } else if (string.Equals(algebraicMove, kingSideCastle)) * { * return moveManager.GetCastlingMove(color, false); * } * //Not a castle move * PieceColor oppColor = (color == PieceColor.White) ? PieceColor.Black : PieceColor.White; * //Is opponents king in check * int curChar = 0; * if (algebraicMove[curChar] == oppKingInCheck) * { * chessMove.checksOpponent = true; * curChar++; * } else * { * chessMove.checksOpponent = false; * } * * PieceType type = GetPieceName(algebraicMove[curChar].ToString()); * if (type != PieceType.Pawn) * { * curChar++; * } * * * Column srcCol, dstCol; * Row srcRow, dstRow; * bool srcColPresent = false, srcRowPresent = false; * //If there's ambiguity by rank * if (srcColPresent = Enum.TryParse(algebraicMove[curChar].ToString(), out srcCol)) * { * curChar++; * } * //If there's ambiguity by file * if (srcRowPresent = Enum.TryParse(algebraicMove[curChar].ToString(), out srcRow)) * { * curChar++; * } * //If there's captured piece * if (String.Equals(algebraicMove[curChar], capturedPiece)) * { * curChar++; * } * //Destination * dstCol = (Column)Enum.Parse(typeof(Column), algebraicMove[curChar].ToString()); * curChar++; * dstRow = (Row)Enum.Parse(typeof(Row), algebraicMove[curChar].ToString()); * curChar++; * //Get all moves * List<ChessMove> allMoves = moveManager.AllAvailableMoves(color); * foreach (ChessMove move in allMoves) * { * if (move.toTile.col == dstCol && move.toTile.row == dstRow * && (!srcColPresent || move.fromTile.col == srcCol) * && (!srcRowPresent || move.fromTile.row == srcRow)) * { * chessMove = move; * break; * } * } * //Promotion * if (String.Equals(algebraicMove[curChar], promotion)) * { * curChar++; * * chessMove.isCompletePromotionMove = true; * chessMove.isPromotionMove = true; * chessMove.promotionType = GetPieceName(algebraicMove[curChar].ToString()); * curChar++; * } * return chessMove;*/ }