I’m writing a chess application and all was going smoothly until I got to pawn promotion. When the pawn reaches the last rank a dialog pops up and the user chooses a piece type. I noticed that the game was still running under the dialog (player1’s move is over and it is player2’s turn). I want the program to ‘wait’ until a piece is chosen.
Currently I have a function makeMove() that is called when a piece is dropped. If the move is not in an array of valid moves, it is rejected. If the move is accepted an internal game array is updated and the pieces are moved around. At the end of makeMove, genMoves is called that generates all of the legal moves for player2. The problem is that the program generates player2’s moves as if there is a pawn on the back rank as opposed to the promoted piece.
I really don’t want to rewrite makeMove() but it seems that I might have to…
// inside makeMove()
if(game[from]%10 == 6){//pawn
if(Math.abs(from - to)==20){// --pawn 2 squares
if(epsq!=-1)// clear old ep values. if any
game[epsq]=0;
epsq = to+(color==1?10:-10);// new ep square
game[epsq]=8;// ep square on the board
}else{
if(to==epsq){// capture via ep
capture(to);
game[epsq+(color==1?10:-10)] = 0;
}
// ----------------------- promotion
if(int(to/10)==(color==1?0:7)){
promotePawn(pieceArray[from]); // STOP HERE!!!!!!!!
}
if(epsq!=-1)
game[epsq]=0;
epsq=-1;
}
}else{//not a pawn
if(epsq!=-1)
game[epsq]=0;
epsq=-1;
}
// ... internal game is updated and pieces are moved
// ... current player is changed
genMoves(); // generates all of the valid moves for current player
So I’m wracking my brain for ways to get the user input before leaving promotePawn().
Maybe I’m just tired. I can’t code when I’m tired.