Skip to content

Commit

Permalink
IT WORKS, Added checking for Stale and Chekmate
Browse files Browse the repository at this point in the history
  • Loading branch information
MrNtex committed Oct 14, 2023
1 parent 2aa9a96 commit 8f1cc75
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions App.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ const ChessBoard = () => {
['p', 'p', 'p', 'p'],
['n', 'q', 'k', 'r'],
]);
useEffect(() => {
console.log("Chess Board Updated!");
if (!CheckChessBoard(currentTurn)) {
if(SelectPiece(0,0,false,true)){
console.log("Checkmate!");
return;
}
console.log("Stalemate");
}
}, [currentChessBoard, currentTurn]);
const [currentChessBoardToTest, setChessBoardToTest] = useState([
['N', 'Q', 'K', 'R'],
['P', 'P', 'P', 'P'],
Expand Down Expand Up @@ -202,8 +212,16 @@ const ChessBoard = () => {
return null;
};

const SelectPiece = (rowIndex,colIndex, testForStalemate = false) => {

const SelectPiece = (rowIndex,colIndex, testForStalemate = false, testForCheckmate = false) => {
if(testForCheckmate){

if(CheckForLegality(currentChessBoard)){
return true;
}
else{
return false;
}
}
if(highLightsArray[rowIndex-1][colIndex] && !testForStalemate){

// MOVE PIECE
Expand Down Expand Up @@ -248,11 +266,6 @@ const ChessBoard = () => {
});
setHighLights(DEFAULT_HIGHLIGHTS);

if(!CheckChessBoard(newTurn)){
console.log("Stale/Checkmate");
}


//Check for stalemate / chackmate

} else if(currentChessBoard[rowIndex-1][colIndex]){
Expand All @@ -275,12 +288,7 @@ const ChessBoard = () => {
let pieceColorWhite = (chessPieceSelected == chessPieceSelected.toUpperCase()) ? true : false;

let newState = Array(8).fill(null).map(() => Array(4).fill(false));
let tempKing = {
whiteKingRow: king.whiteKingRow,
whiteKingCol: king.whiteKingCol,
blackKingRow: king.blackKingRow,
blackKingCol: king.blackKingCol
};

function CheckMoves(state, boardCopy, lastPiece, returnMove = false){
let highlightsState = state;
for(let i = 0; i < state.length; i++){
Expand Down Expand Up @@ -322,6 +330,14 @@ const ChessBoard = () => {
return highlightsState;
}
function CheckForLegality(testArray, kingM){
if(tempKing === null){
let tempKing = {
whiteKingRow: king.whiteKingRow,
whiteKingCol: king.whiteKingCol,
blackKingRow: king.blackKingRow,
blackKingCol: king.blackKingCol
};
}
let rowIdx = currentTurn ? tempKing.whiteKingRow : tempKing.blackKingRow;
let colIdx = currentTurn ? tempKing.whiteKingCol : tempKing.blackKingCol;
//First check for diagonals (queen)
Expand Down

0 comments on commit 8f1cc75

Please sign in to comment.