Commit 1b0bf0d9 authored by 9611046's avatar 9611046

adds Move class to avoid duplicative codes

parent 8dd280cc
import java.util.Random;
public class Move {
private int x;
private int y;
private Land moveLand;
public Move(int x,int y,Land land){
this.x = x;
this.y = y;
moveLand = land;
}
public void moveXY(){
Random rand = new Random();
int a = rand.nextInt(8);
switch (a) {
case 0:
//Right
if (y + 1 <moveLand.getLength()) {
y += 1;
break;
}
else moveXY();
case 1:
//Left
if (y > 0) {
y -= 1;
break;
}
else moveXY();
case 2:
//Up
if (x > 0) {
x -= 1;
break;
}
else moveXY();
case 3:
//Down
if (x + 1 < moveLand.getWidth()) {
x += 1;
break;
}
else moveXY();
case 4:
//North East
if ((x > 0) && (y + 1 < moveLand.getLength())) {
x -= 1;
y += 1;
break;
}
else moveXY();
case 5:
//North west
if (x > 0 && y>0) {
y -= 1;
x -= 1;
break;
}
else moveXY();
case 6:
//Southeast
if ((x + 1 < moveLand.getWidth()) && (y + 1 < moveLand.getLength())) {
y += 1;
x += 1;
break;
}
else moveXY();
case 7:
//Southwest
if ((x + 1 < moveLand.getWidth()) && (y > 0)) {
x += 1;
y -= 1;
break;
}
else moveXY();
}
}
public int getX(){
return x;
}
public int getY(){
return y;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment