You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was trying to implement this board to my site. But sometimes, even tho my move was valid even though the board registered my move (didn't put my piece back into its original position), the "move" variable's value still null. I changed the value to "123", and made many invalid moves on purpose. And logged the "move"s value. And what do i see? It's sometimes 123 and sometimes null. I'm a newbie developer and this issue put me two days behind because no errors, no nothing and sometimes gives errors, and sometimes doesn't...
const[game,setGame]=useState(newChess());functionsafeGameMutate(modify){setGame((g)=>{<----ASYNCFUNCTION,sothiskindamakesthesafeGameMutatefunctionanasyncfunctionconstupdate={ ...g};modify(update);returnupdate;});}functiononDrop(sourceSquare,targetSquare){letmove=null;safeGameMutate((game)=>{<--------Callinganasyncfunctiontosetthemovevariable.andinstantlyafter,checkingit's value.
move = game.move({
from: sourceSquare,
to: targetSquare,
promotion: "q", // always promote to a queen for example simplicity
});
});
if (move === null) return false; // illegal move
setTimeout(makeRandomMove, 200);
return true;
}
My solution for my instance was to just use game.move() before hands, if it didn't return null, i just updated the board with the safeGameMutate function.
The text was updated successfully, but these errors were encountered:
You raise a very good point, this was written back when I had less of an idea about state update rules and I hadn't thought to look back at it, so good catch.
The good news is that there is a simple solution, which I think maybe is the one you've done but I'm not sure from the description.
The solution is actually already implemented in another example, so I must have found this issue at some point but not updated other examples:
functiononDrop(sourceSquare,targetSquare){constgameCopy={ ...game};constmove=gameCopy.move({from: sourceSquare,to: targetSquare,promotion: 'q'// always promote to a queen for example simplicity});setGame(gameCopy);// illegal moveif(move===null)returnfalse;// store timeout so it can be cleared on undo/reset so computer doesn't execute moveconstnewTimeout=setTimeout(makeRandomMove,200);setCurrentTimeout(newTimeout);returntrue;}
Now that taking a look at your solution, even though my solution worked, it was not safe for other reasons, and yours is much better, thanks! xd
Also, as a newbie programmer "catching" this issue was a long and painful process for me, believe me xd i thought about abandoning my project even. I trusted you more than myself, thought that i was using it wrong rather than it being wrong.
Anyways, thanks a lot. This is a very nice package/library or whatever its called. Have a nice one <3
I was trying to implement this board to my site. But sometimes, even tho my move was valid even though the board registered my move (didn't put my piece back into its original position), the "move" variable's value still null. I changed the value to "123", and made many invalid moves on purpose. And logged the "move"s value. And what do i see? It's sometimes 123 and sometimes null. I'm a newbie developer and this issue put me two days behind because no errors, no nothing and sometimes gives errors, and sometimes doesn't...
My solution for my instance was to just use game.move() before hands, if it didn't return null, i just updated the board with the safeGameMutate function.
The text was updated successfully, but these errors were encountered: