I managed to implement the background counting of movesleft. I've used the setinterval function todo this. And now we can keep on playing immediatly after a move was done and it will update the movesleft value once it has done calculating them. I just call the GameLoop one more time to verify if it is a winning game and draw the results basically my code looks like this now :
//procedure that starts the movesLeftIter function using an interval to loop over
//all pegs
function movesLeft() {
if(movesLeftTimer)
clearInterval(movesLeftTimer);
movesLeftCount = 0;
pegsLeft = 0;
movesLeftX = 0;
movesLeftY = 0;
movesLeftTimer = setInterval(movesLeftIter, 0);
}
// procedure that calculates how many moves are possible in the current board state for 1 Peg
// we can simply do this by checking all parts and see if they can move to all directions
// the canmoveto method in CPegs is does all the checking
function movesLeftIter() {
//print (BoardParts.Items[Y][X]);
// if there is a boardpart on that X,Y Coordinate
// check all direction if we can move to that if so increases the movesleft
if (BoardParts.Items[movesLeftY][movesLeftX]) {
if (BoardParts.Items[movesLeftY][movesLeftX].AnimPhase < 2) {
pegsLeft++;
movesLeftCount += CPeg_CanMoveTo(BoardParts.Items[movesLeftY][movesLeftX], movesLeftX + 2, movesLeftY, false);
movesLeftCount += CPeg_CanMoveTo(BoardParts.Items[movesLeftY][movesLeftX], movesLeftX - 2, movesLeftY, false);
movesLeftCount += CPeg_CanMoveTo(BoardParts.Items[movesLeftY][movesLeftX], movesLeftX, movesLeftY - 2, false);
movesLeftCount += CPeg_CanMoveTo(BoardParts.Items[movesLeftY][movesLeftX], movesLeftX, movesLeftY + 2, false);
movesLeftCount += CPeg_CanMoveTo(BoardParts.Items[movesLeftY][movesLeftX], movesLeftX + 2, movesLeftY - 2, false);
movesLeftCount += CPeg_CanMoveTo(BoardParts.Items[movesLeftY][movesLeftX], movesLeftX + 2, movesLeftY + 2, false);
movesLeftCount += CPeg_CanMoveTo(BoardParts.Items[movesLeftY][movesLeftX], movesLeftX - 2, movesLeftY + 2, false);
movesLeftCount += CPeg_CanMoveTo(BoardParts.Items[movesLeftY][movesLeftX], movesLeftX - 2, movesLeftY - 2, false);
}
}
movesLeftX++;
if(movesLeftX == NROFCOLS)
{
movesLeftY++;
if((movesLeftY == NROFROWS) && (movesLeftX == NROFCOLS))
{
clearInterval(movesLeftTimer);
prevMovesLeftCount = movesLeftCount;
prevPegsLeft = pegsLeft;
if(GameState == GSGAME)
loop();
return;
}
movesLeftX = 0;
}
}
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Hey gordon,
I managed to implement the background counting of movesleft. I've used the setinterval function todo this. And now we can keep on playing immediatly after a move was done and it will update the movesleft value once it has done calculating them. I just call the GameLoop one more time to verify if it is a winning game and draw the results basically my code looks like this now :
and that seems to work fine