public ChessPieceViewModel(ChessPiece i_ChessPiece, PieceColor i_PieceColor, PieceSide i_PieceSide, int i_File, int i_Rank) { ChessCoord tempChessCoord = new ChessCoord(); tempChessCoord.File = i_File; tempChessCoord.Rank = i_Rank; m_ChessPieceModel = new ChessPieceModel(i_ChessPiece, i_PieceColor, i_PieceSide, tempChessCoord); }
public static bool LegalCheck(ChessCoord coordinates) { if (LetterCheck(coordinates.coord) && (coordinates.coord.Length == 2)) { return(true); } return(false); }
// Knight Probability in Chessboard // On an NxN chessboard, a knight starts at the r-th row and c-th column and attempts to make exactly K moves. // Each time the knight is to move, it chooses one of eight possible moves uniformly at random // (even if the piece would go off the chessboard) and moves there. // The knight continues moving until it has made exactly K moves or has moved off the chessboard. // Return the probability that the knight remains on the board after it has stopped moving. // https://leetcode.com/problems/knight-probability-in-chessboard/ public static double CalcKnightProbability(int boardSize, int movesAmount, int startRow, int startColumn) { ChessCoord[] moveOptions = { new ChessCoord(1, 2), new ChessCoord(-1, 2), new ChessCoord(2, 1), new ChessCoord(2, -1), new ChessCoord(-1, -2), new ChessCoord(1, -2), new ChessCoord(-2, -1), new ChessCoord(-2, 1) }; // the snapshots of probabilities for each cell for the current and the next moves double[][,] boardSnapshots = { new double[boardSize, boardSize], new double[boardSize, boardSize] }; int curIdx = 0; // the switchable "pointers" to the appropriate boards int nextIdx = 1; // before the 1st move knight is on the starting cell with 100% probability boardSnapshots[curIdx][startRow, startColumn] = 1; for (int moveIdx = 0; moveIdx < movesAmount; moveIdx++) { forEachCell(boardSize, (row, col) => { double sourceValue = boardSnapshots[curIdx][row, col]; if (sourceValue == 0) { return; } boardSnapshots[curIdx][row, col] = 0; foreach (ChessCoord moveOption in moveOptions) { ChessCoord resPos = new ChessCoord(row, col) + moveOption; if (resPos.IsInsideTheBoard(boardSize)) { boardSnapshots[nextIdx][resPos.Row, resPos.Column] += sourceValue / 8; // 8 possible moves from each pos } } }); (curIdx, nextIdx) = (nextIdx, curIdx); } double totalProbability = 0; forEachCell(boardSize, (row, col) => totalProbability += boardSnapshots[curIdx][row, col]); return(totalProbability); }
public ChessPieceModel(ChessPiece i_ChessPiece, PieceColor i_PieceColor, PieceSide i_PieceSide, ChessCoord i_ChessCoord) { ChessPiece = i_ChessPiece; PieceColor = i_PieceColor; PieceSide = i_PieceSide; ChessCoord = i_ChessCoord; string PieceColorStr; if (i_PieceColor == PieceColor.White) { PieceColorStr = "White"; } else { PieceColorStr = "Black"; } switch (i_ChessPiece) { case ChessPiece.King: ResourceURI = @"/ChessAI;component/Resources/Pieces/" + PieceColorStr + "_King.png"; break; case ChessPiece.Queen: ResourceURI = @"/ChessAI;component/Resources/Pieces/" + PieceColorStr + "_Queen.png"; break; case ChessPiece.Bishop: ResourceURI = @"/ChessAI;component/Resources/Pieces/" + PieceColorStr + "_Bishop.png"; break; case ChessPiece.Knight: ResourceURI = @"/ChessAI;component/Resources/Pieces/" + PieceColorStr + "_Knight.png"; break; case ChessPiece.Rook: ResourceURI = @"/ChessAI;component/Resources/Pieces/" + PieceColorStr + "_Rook.png"; break; case ChessPiece.Pawn: ResourceURI = @"/ChessAI;component/Resources/Pieces/" + PieceColorStr + "_Pawn.png"; break; } }
public static Vector3 ChessToVector(ChessCoord coordinates) { int horizontalScale = 0; int verticalScale = (int)coordinates.coord[1]; switch (coordinates.coord[0]) { case 'a': horizontalScale = 1; break; case 'b': horizontalScale = 2; break; case 'c': horizontalScale = 3; break; case 'd': horizontalScale = 4; break; case 'e': horizontalScale = 5; break; case 'f': horizontalScale = 6; break; case 'g': horizontalScale = 7; break; case 'h': horizontalScale = 8; break; } Vector3 createdVector = new Vector3(horizontalScale, 0f, verticalScale); return(createdVector); }
void Spawn(Transform prefab, Color color, ChessCoord position, FigureType type) { Transform instance = Instantiate(prefab, CoordinatesConverter.ChessToVector(position), Quaternion.identity, this.transform); instance.localScale = scaleVector; instance.gameObject.GetComponent <MeshRenderer>().material.color = color; Figure addedF = instance.gameObject.AddComponent <Figure>(); addedF.AssignTypeBehaviour(type); //Add to BoardManager BoardManager.board[(int)CoordinatesConverter.ChessToVector(position).x, (int)CoordinatesConverter.ChessToVector(position).z].figure = addedF; if (color == Color.black) { instance.localRotation = Quaternion.Euler(0f, 180f, 0f); BoardManager.blackFigures.Add(addedF); } else { BoardManager.whiteFigures.Add(addedF); } }