S
SamuelXiao
Hi all, I am writing a simple monopoly board game, there're only 2
tokens, one is controlled by human, another by PC. I am trying to
make it turn based and move around the map step by step. Then when I
use TimerTask to trigger the step-forward movement, it's ok for the
token controlled by human, but for the PC one. It doesn't move as
expected. Below is part of the codes for Dice Roll button & Done
button.
// btnRoll() function
public void btnRoll() {
Timer timer = new Timer();
final int index = turn - 1;
boolean snakeEyes = false;
Dice1 = (int)(Math.random() * 6 + 1);
Dice2 = (int)(Math.random() * 6 + 1);
if(Dice1 == Dice2) {
snakeEyes = true;
rolled = false;
}
else
rolled = true;
if(snakeEyes == true){
tempFlagPlayer = true;
}else{
tempFlagPlayer = false;
}
timer.schedule(new TimerTask(){
private int temp = Dice1 + Dice2;
public void run(){
if (temp > 0){
SystemLogHelper.debug("btnRoll()'s players.get(index): " + index);
movePlayer(players.get(index), tempFlagPlayer); // move player one
space each time
temp --;
}else{
checkPlayerMovedStatus(players,tempFlagPlayer);
cancel();
}
repaint();
}
}, 100L,100L);
repaint();
}
// PC turn
public void AIturn(int tempNumOfPlayers){
btnRoll();
SystemLogHelper.info("players.get(turn-1): " +
players.get(turn-1).getName());
SystemLogHelper.info("players.get(turn-1).getPosition(): " +
players.get(turn-1).getPosition());
if(propertymanager.Properties[players.get(turn-1).getPosition()]
[0] == 0){
SystemLogHelper.info("enter btnBuy()");
btnBuy();
}
if(rolled) btnDone(tempNumOfPlayers);
}
// done button, next player's turn
public void btnDone(int tempNumOfPlayers){
rolled = false;
if(this.getTurn() == tempNumOfPlayers){
this.setTurn(1);
}
else{
turn += 1;
// SystemLogHelper.info("AI's this.getTurn(): " + turn);
if((AutoPlayer)players.get(turn - 1) instanceof AutoPlayer){
AIturn(tempNumOfPlayers);
}
}
repaint();
}
The main problem is, I found that
SystemLogHelper.info("players.get(turn-1): " +
players.get(turn-1).getName());
and SystemLogHelper.info("players.get(turn-1).getPosition(): " +
players.get(turn-1).getPosition());
will run before the btnRoll() function.
how could I make sure that btnRoll() is done then go to the next
code?
Any help would be appreciated.
tokens, one is controlled by human, another by PC. I am trying to
make it turn based and move around the map step by step. Then when I
use TimerTask to trigger the step-forward movement, it's ok for the
token controlled by human, but for the PC one. It doesn't move as
expected. Below is part of the codes for Dice Roll button & Done
button.
// btnRoll() function
public void btnRoll() {
Timer timer = new Timer();
final int index = turn - 1;
boolean snakeEyes = false;
Dice1 = (int)(Math.random() * 6 + 1);
Dice2 = (int)(Math.random() * 6 + 1);
if(Dice1 == Dice2) {
snakeEyes = true;
rolled = false;
}
else
rolled = true;
if(snakeEyes == true){
tempFlagPlayer = true;
}else{
tempFlagPlayer = false;
}
timer.schedule(new TimerTask(){
private int temp = Dice1 + Dice2;
public void run(){
if (temp > 0){
SystemLogHelper.debug("btnRoll()'s players.get(index): " + index);
movePlayer(players.get(index), tempFlagPlayer); // move player one
space each time
temp --;
}else{
checkPlayerMovedStatus(players,tempFlagPlayer);
cancel();
}
repaint();
}
}, 100L,100L);
repaint();
}
// PC turn
public void AIturn(int tempNumOfPlayers){
btnRoll();
SystemLogHelper.info("players.get(turn-1): " +
players.get(turn-1).getName());
SystemLogHelper.info("players.get(turn-1).getPosition(): " +
players.get(turn-1).getPosition());
if(propertymanager.Properties[players.get(turn-1).getPosition()]
[0] == 0){
SystemLogHelper.info("enter btnBuy()");
btnBuy();
}
if(rolled) btnDone(tempNumOfPlayers);
}
// done button, next player's turn
public void btnDone(int tempNumOfPlayers){
rolled = false;
if(this.getTurn() == tempNumOfPlayers){
this.setTurn(1);
}
else{
turn += 1;
// SystemLogHelper.info("AI's this.getTurn(): " + turn);
if((AutoPlayer)players.get(turn - 1) instanceof AutoPlayer){
AIturn(tempNumOfPlayers);
}
}
repaint();
}
The main problem is, I found that
SystemLogHelper.info("players.get(turn-1): " +
players.get(turn-1).getName());
and SystemLogHelper.info("players.get(turn-1).getPosition(): " +
players.get(turn-1).getPosition());
will run before the btnRoll() function.
how could I make sure that btnRoll() is done then go to the next
code?
Any help would be appreciated.