Functionally everything works on the chess board except the chess check during castle tester in the vector. It is like it doesn’t write the rule into pseudoLegalMove. Can a vector work for this type of thing? I am new C# and i’m learning and have spent days trying to fix this problem. My thoughts are I need to instead do this check in a bool or something so it writes the illegal move into pseudoLegalMove. Also everything else is working correctly…like enpassant, doublepawnpush ect. Any help from someone who has coded a chess board would be fantastic!
This is where i think the problem is..
/// <param name="movedPiece"></param>
/// <param name="board"></param>
/// <returns></returns>
public Vector2[] GetAllLegalMoves(Piece movedPiece, Board board) //STACK OVERFLOW this is where i think the problem is?
{
Vector2[] pseudoLegalMoves = GetAllPseudoLegalMoves(movedPiece, board);
Vector2 piecePos = movedPiece.GetVec();
Piece king = movedPiece.white ? board.whiteKing : board.blackKing;
if (king != null)
{
Piece[] currentGrid = board.grid; //gets board grid
Piece[] testGrid = new Piece[currentGrid.Length]; //gets king piece
Vector2 kingPos = king.GetVec(); // gets king piece
for (int i = 0; i < pseudoLegalMoves.Length; i++)
{
Vector2 pseudoLegalMove = pseudoLegalMoves[i]; //its like this doesn't write the illegal king move into the rules here or something
if (pseudoLegalMove == legalMovesEndMarker) { break; }
else
{
Array.Copy(currentGrid, testGrid, currentGrid.Length);
board.MoveGridPieceVec(piecePos, pseudoLegalMove, testGrid);
Piece PawnThatDidADoublePush = null;
if (movedPiece.type == "pawn" && (Mathf.Abs(movedPiece.x - (int)pseudoLegalMove.x) > 1))
{
PawnThatDidADoublePush = movedPiece;
}
Vector2 threatenedPos = movedPiece.type != "king" ? kingPos : pseudoLegalMove;
if (isKingInCheck(threatenedPos, testGrid, board, PawnThatDidADoublePush, movedPiece.white)) { pseudoLegalMoves[i] = legalMovesIgnoreMarker; }
}
}
}
return pseudoLegalMoves;
}
// if (!isKingInCheck(threatenedPos.x, threatenedPos.y, opponentPiece.type, opponentPiece.x, opponentPseudoLegalMove.x, opponentPseudoLegalMove.y, movedPiece.white))
this is where it checks pseudolegalmove is checked to allow the castle to happen
public int MoveLegalCheck(Piece movedPiece, int x, int y, Board board, Vector2[] legalMoves)
{
int result = 0;
Piece targetPiece = board.GetPiece(x, y);
Vector2 move = new Vector2(x, y);
bool legal = false;
foreach (Vector2 legalMove in legalMoves)
{
if (legalMove != legalMovesIgnoreMarker)
{
if (legalMove == legalMovesEndMarker) break;
if (move == legalMove) { legal = true; break; }
}
}
if (legal)
{
if (targetPiece != null) { targetPiece._Capture(); result = 2; } else { result = 1; }
if (movedPiece.type == "pawn" && movedPiece.x != x && board.GetPiece(x, y) == null)//EN PASSANT
{
board.PawnThatDidADoublePushLastRound._Capture();
result = 2;
}
if (movedPiece.type == "king" && Mathf.Abs(x - movedPiece.x) == 2) //CASTLING
{
int dir = Mathf.Min(x, movedPiece.x) == x ? -1 : 1;
Piece rookCastle = dir == 1 ? board.GetPiece(7, y) : board.GetPiece(0, y);
rookCastle._SetPosition(x + (dir * -1), y);
}
if (movedPiece.type == "pawn" && Mathf.Abs(y - movedPiece.y) == 2)
{
board.PawnThatDidADoublePushLastRound = movedPiece; //NOTICE, to have it be synced I also do the same check in the Piece refresh function (won't be synced for those who join right after this move, but it's fine)
}
else
{
board.PawnThatDidADoublePushLastRound = null;
}
movedPiece.hasMoved = true;
movedPiece._SetPosition(x, y);
return result;
}
else
{
movedPiece._SetPosition(movedPiece.x, movedPiece.y);
return 0;
}
}
i can post more if needed..didnt want to spam or something….the whole board is like 6 scripts so
chessboard problem
I’ve tried multiple changes little changes…adding if then infront of the castle command just no luck. i dont know a lot about int and vectors…but something tells me i need to do the castle check in a bool in order to write into the pseudolegal moves….or am i wrong….any guidance in the right direction would help 100% and thanks in advance!
CharlesRyann is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.