Skip to main content
grammar format and punctuation; rejected initial edit due to incorrect and insignificant format; removed redundancy and clarified title
Source Link
Gnemlock
  • 5.3k
  • 5
  • 30
  • 60

Random aiAI movement in java help?Java

I am making a maze game in javaJava, where you have to escape the maze without being caught by the random moving airandomly-moving AI. 

The code iI am using to decide which direction the enemy moves is shown below but thisin causes the character to change direction way too fast. It currently chooses a random number and depending on, which number it chooses corresponds to whichthe direction in which the aiAI moves. 

How can iI edit this method so it only chooses a random number every 3 seconds?

if(new Random().nextInt(4) == 0){ xMove = speed; } if(new Random().nextInt(4) == 1){ xMove = -speed; } if(new Random().nextInt(4) == 2){ yMove = speed; } if(new Random().nextInt(4) == 3){ yMove = -speed; }

if(new Random().nextInt(4) == 0){
    xMove = speed;
}
if(new Random().nextInt(4) == 1){
    xMove = -speed;
}
if(new Random().nextInt(4) == 2){
    yMove = speed;
}
if(new Random().nextInt(4) == 3){
    yMove = -speed;
}

Random ai movement in java help?

I am making a maze game in java where you have to escape the maze without being caught by the random moving ai. The code i am using to decide which direction the enemy moves is shown below but this causes the character to change direction way too fast. It currently chooses a random number and depending on which number it chooses corresponds to which direction the ai moves. How can i edit this method so it only chooses a random number every 3 seconds?

if(new Random().nextInt(4) == 0){ xMove = speed; } if(new Random().nextInt(4) == 1){ xMove = -speed; } if(new Random().nextInt(4) == 2){ yMove = speed; } if(new Random().nextInt(4) == 3){ yMove = -speed; }

Random AI movement in Java

I am making a maze game in Java, where you have to escape the maze without being caught by the randomly-moving AI. 

The code I am using to decide which direction the enemy moves in causes the character to change direction way too fast. It currently chooses a random number, which corresponds to the direction in which the AI moves. 

How can I edit this method so it only chooses a random number every 3 seconds?

if(new Random().nextInt(4) == 0){
    xMove = speed;
}
if(new Random().nextInt(4) == 1){
    xMove = -speed;
}
if(new Random().nextInt(4) == 2){
    yMove = speed;
}
if(new Random().nextInt(4) == 3){
    yMove = -speed;
}
Source Link

Random ai movement in java help?

I am making a maze game in java where you have to escape the maze without being caught by the random moving ai. The code i am using to decide which direction the enemy moves is shown below but this causes the character to change direction way too fast. It currently chooses a random number and depending on which number it chooses corresponds to which direction the ai moves. How can i edit this method so it only chooses a random number every 3 seconds?

if(new Random().nextInt(4) == 0){ xMove = speed; } if(new Random().nextInt(4) == 1){ xMove = -speed; } if(new Random().nextInt(4) == 2){ yMove = speed; } if(new Random().nextInt(4) == 3){ yMove = -speed; }