Commit 89fb11cc authored by hosein's avatar hosein

Thief -> randomMove added

parent 7d2a06ab
package pack; package pack;
import java.util.Random;
public class Thief { public class Thief {
private int x, y; private int x, y;
private boolean see; private boolean see;
...@@ -10,5 +12,250 @@ public class Thief { ...@@ -10,5 +12,250 @@ public class Thief {
see = false; see = false;
} }
public void randomMove(int n, int m, PlayGround playGround) {
boolean i, j, I, J;
i = j = I = J = false;
if (x != 0) i = true;
if (x != n - 1) I = true;
if (y != 0) j = true;
if (y != m - 1) J = true;
Random random = new Random();
boolean confirm = false;
int X = 0, Y = 0;
do {
switch (random.nextInt(8)) {
case 0:
if (i && j) {
X = x - 1;
Y = y - 1;
confirm = playGround.isEmpty(X, Y);
}
break;
case 1:
if (i) {
X = x - 1;
Y = y;
confirm = playGround.isEmpty(X, Y);
}
break;
case 2:
if (i && J) {
X = x - 1;
Y = y + 1;
confirm = playGround.isEmpty(X, Y);
}
break;
case 3:
if (j) {
X = x;
Y = y - 1;
confirm = playGround.isEmpty(X, Y);
}
break;
case 4:
if (J) {
X = x;
Y = y + 1;
confirm = playGround.isEmpty(X, Y);
}
break;
case 5:
if (j && I) {
X = x + 1;
Y = y - 1;
confirm = playGround.isEmpty(X, Y);
}
break;
case 6:
if (I) {
X = x + 1;
Y = y;
confirm = playGround.isEmpty(X, Y);
}
break;
case 7:
if (I && J) {
X = x + 1;
Y = y + 1;
confirm = playGround.isEmpty(X, Y);
}
}
} while (!confirm);
playGround.changeSituation(x, y, X, Y, -1);
x = X;
y = Y;
}
public void targetedMove(PlayGround playGround) {
boolean[] isPoliceThere = new boolean[8];
int i, j, I, J;
if (x > 1) i = x - 2;
else i = 0;
if (x < playGround.getNM()[0][0] - 2) I = x + 3;
else I = playGround.getNM()[0][0];
if (y > 1) j = y - 2;
else j = 0;
if (y < playGround.getNM()[0][1] - 2) J = y + 3;
else J = playGround.getNM()[0][1];
int q, w;
for (isPoliceThere[0] = false, q = i; q < x; q++) // up, left
for (w = j; w < y; w++)
if (playGround.getGround()[q][w] == 1) {
isPoliceThere[0] = true;
}
for (isPoliceThere[1] = false, q = i; q < x; q++) // up
for (w = y; w < y + 1; w++)
if (playGround.getGround()[q][w] == 1) {
isPoliceThere[1] = true;
}
for (isPoliceThere[2] = false, q = i; q < x; q++) // up, right
for (w = j + 1; w < J; w++)
if (playGround.getGround()[q][w] == 1) {
isPoliceThere[2] = true;
}
for (isPoliceThere[3] = false, q = x; q < x + 1; q++) // left
for (w = j; w < y; w++)
if (playGround.getGround()[q][w] == 1) {
isPoliceThere[3] = true;
}
for (isPoliceThere[4] = false, q = x; q < x + 1; q++) // right
for (w = y + 1; w < J; w++)
if (playGround.getGround()[q][w] == 1) {
isPoliceThere[4] = true;
}
for (isPoliceThere[5] = false, q = x + 1; q < I; q++) // down, left
for (w = j; w < y; w++)
if (playGround.getGround()[q][w] == 1) {
isPoliceThere[5] = true;
}
for (isPoliceThere[6] = false, q = x + 1; q < I; q++) // down
for (w = y; w < y + 1; w++)
if (playGround.getGround()[q][w] == 1) {
isPoliceThere[6] = true;
}
for (isPoliceThere[7] = false, q = x + 1; q < I; q++) // down, right
for (w = y + 1; w < J; w++)
if (playGround.getGround()[q][w] == 1) {
isPoliceThere[7] = true;
}
int[][] newSituation = almostRandomMove(isPoliceThere);
playGround.changeSituation(x, y, newSituation[0][0], newSituation[0][1], -1);
x = newSituation[0][0];
y = newSituation[0][1];
}
private int[][] almostRandomMove(boolean[] isPoliceThere) {
int[][] destination = new int[1][2];
if (isPoliceThere[0] && isPoliceThere[2]) isPoliceThere[1] = true;
if (isPoliceThere[0] && isPoliceThere[5]) isPoliceThere[3] = true;
if (isPoliceThere[7] && isPoliceThere[2]) isPoliceThere[4] = true;
if (isPoliceThere[5] && isPoliceThere[7]) isPoliceThere[6] = true;
if (isPoliceThere[1] && isPoliceThere[3]) isPoliceThere[0] = true;
if (isPoliceThere[1] && isPoliceThere[4]) isPoliceThere[2] = true;
if (isPoliceThere[6] && isPoliceThere[3]) isPoliceThere[5] = true;
if (isPoliceThere[4] && isPoliceThere[6]) isPoliceThere[7] = true;
if (!isPoliceThere[0] || !isPoliceThere[1] || !isPoliceThere[2] || !isPoliceThere[3] || !isPoliceThere[4] || !isPoliceThere[5] || !isPoliceThere[6] || !isPoliceThere[7]) {
Random r = new Random();
do {
switch (r.nextInt(8)) {
case 0:
if (!isPoliceThere[0]) {
destination[0][0] = x - 1;
destination[0][1] = y - 1;
return destination;
}
break;
case 1:
if (!isPoliceThere[1]) {
destination[0][0] = x - 1;
destination[0][1] = y;
return destination;
}
break;
case 2:
if (!isPoliceThere[2]) {
destination[0][0] = x + 1;
destination[0][1] = y - 1;
return destination;
}
break;
case 3:
if (!isPoliceThere[3]) {
destination[0][0] = x;
destination[0][1] = y - 1;
return destination;
}
break;
case 4:
if (!isPoliceThere[4]) {
destination[0][0] = x;
destination[0][1] = y + 1;
return destination;
}
break;
case 5:
if (!isPoliceThere[5]) {
destination[0][0] = x + 1;
destination[0][1] = y - 1;
return destination;
}
break;
case 6:
if (!isPoliceThere[6]) {
destination[0][0] = x + 1;
destination[0][1] = y;
return destination;
}
break;
case 7:
if (!isPoliceThere[7]) {
destination[0][0] = x + 1;
destination[0][1] = y + 1;
return destination;
}
}
} while (true);
} else {
destination[0][0] = x;
destination[0][1] = y;
return destination;
}
}
} }
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