Commit 6797a5f1 authored by 9731077's avatar 9731077

fix bug + finished Robber class

parent a0959836
import java.util.*;
public class Robber {
private int x, y, moveNum;
private int x;
private int y;
private int moveNum;
public void setY(int y) {
this.y = y;
......@@ -15,6 +17,7 @@ public class Robber {
this.moveNum = moveNum;
}
public int getY() {
return y;
}
......@@ -27,37 +30,37 @@ public class Robber {
return moveNum;
}
public void moveRandomly(int length, int width) {
public void moveRandomly(int m, int n) {
Random randomGenerator = new Random();
moveNum++;
if (x != 0 && x != length - 1 && y != 0 && y != width - 1) {
if (x != 0 && x != m - 1 && y != 0 && y != n - 1) {
int rx = randomGenerator.nextInt(3) - 1;
int ry = randomGenerator.nextInt(3) - 1;
x = x + rx;
y = y + ry;
}
if (x == 0 && y != 0 && y != width - 1) {
if (x == 0 && y != 0 && y != n - 1) {
int rx = randomGenerator.nextInt(2);
int ry = randomGenerator.nextInt(3) - 1;
x = x + rx;
y = y + ry;
}
if (y == 0 && x != 0 && x != length - 1) {
if (y == 0 && x != 0 && x != m - 1) {
int rx = randomGenerator.nextInt(3) - 1;
int ry = randomGenerator.nextInt(2);
x = x + rx;
y = y + ry;
}
if (x == length - 1 && y != 0 && y != width - 1) {
if (x == m - 1 && y != 0 && y != n - 1) {
int rx = randomGenerator.nextInt(2) - 1;
int ry = randomGenerator.nextInt(3) - 1;
x = x + rx;
y = y + ry;
}
if (y == width - 1 && x != 0 && x != length - 1) {
if (y == n - 1 && x != 0 && x != m - 1) {
int rx = randomGenerator.nextInt(3) - 1;
int ry = randomGenerator.nextInt(2) - 1;
x = x + rx;
......@@ -69,19 +72,19 @@ public class Robber {
x = x + rx;
y = y + ry;
}
if (x == length - 1 && y == 0) {
if (x == m - 1 && y == 0) {
int rx = randomGenerator.nextInt(2) - 1;
int ry = randomGenerator.nextInt(2);
x = x + rx;
y = y + ry;
}
if (x == 0 && y == width - 1) {
if (x == 0 && y == n - 1) {
int rx = randomGenerator.nextInt(2);
int ry = randomGenerator.nextInt(2) - 1;
x = x + rx;
y = y + ry;
}
if (x == length - 1 && y == width - 1) {
if (x == m - 1 && y == n - 1) {
int rx = randomGenerator.nextInt(2) - 1;
int ry = randomGenerator.nextInt(2) - 1;
x = x + rx;
......
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