matthewking
Programmer
Hi,
I've written this for a simple board game applet, its supposed to role the dice, which it does correctly, then move the players token along each grid piece, one at a time until it reaches its destination, its usually fine the first time around the board, then it irregularly jumps, for example grid 3 to 5, missing 4 etc. Any ideas?
public void run()
{
while (true)
{
/**
* Handles Animating the dice
*/
if (rolling)
{
int i = 0;
// animates dices moving
while (i < 20)
{
i++;
Dice1 =(int) (Math.random()*5)+1;
Dice2 =(int) (Math.random()*5)+1;
try { task.sleep(100); }
catch (Exception e) { }
repaint();
}
rolling = false;
/**
* Procedure for Moving players Token
* This is the problem part.
*/
// set curPos to the players current grid ref
int curPos = player[1].getPlayerPosition();
// total grid refs to move
int Move = Dice1 + Dice2 + 1;
while (Move != 0)
{
// correct game position if going off board
if (curPos == 40) { curPos = 0; }
// set new position
player[1].setPlayerPosition(curPos);
curPos++;
// when the moves run out, we stop.
Move--;
// pause thread try { task.sleep(250); }
catch (Exception e) { }
repaint();
}
} // roll dice
}
} // run
Anyone have any ideas?
Thanks in advance,
Matt.
I've written this for a simple board game applet, its supposed to role the dice, which it does correctly, then move the players token along each grid piece, one at a time until it reaches its destination, its usually fine the first time around the board, then it irregularly jumps, for example grid 3 to 5, missing 4 etc. Any ideas?
public void run()
{
while (true)
{
/**
* Handles Animating the dice
*/
if (rolling)
{
int i = 0;
// animates dices moving
while (i < 20)
{
i++;
Dice1 =(int) (Math.random()*5)+1;
Dice2 =(int) (Math.random()*5)+1;
try { task.sleep(100); }
catch (Exception e) { }
repaint();
}
rolling = false;
/**
* Procedure for Moving players Token
* This is the problem part.
*/
// set curPos to the players current grid ref
int curPos = player[1].getPlayerPosition();
// total grid refs to move
int Move = Dice1 + Dice2 + 1;
while (Move != 0)
{
// correct game position if going off board
if (curPos == 40) { curPos = 0; }
// set new position
player[1].setPlayerPosition(curPos);
curPos++;
// when the moves run out, we stop.
Move--;
// pause thread try { task.sleep(250); }
catch (Exception e) { }
repaint();
}
} // roll dice
}
} // run
Anyone have any ideas?
Thanks in advance,
Matt.