Commit 7eb920d2 authored by hosein's avatar hosein

new package created! name: 'pack' & classes added

parent 684aca4e
<root>
<item name='java.util.concurrent.TimeUnit void sleep(long)'>
<annotation name='java.lang.Deprecated'/>
</item>
</root>
\ No newline at end of file
package pack;
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
System.out.print("Enter n: ");
int n = input.nextInt();
System.out.print("Enter m: ");
int m = input.nextInt();
System.out.print("Enter police numbers: ");
int pn = input.nextInt();
PlayGround playGround = new PlayGround(n, m, pn);
int[][] positions = playGround.putRandom();
Thief thief = new Thief(positions[pn][0], positions[pn][1]);
Police[] polices = new Police[pn];
for (n = 0; n < pn; n++)
polices[n] = new Police(positions[n][0], positions[n][1]);
//--------------------------------------------------------------------------------
int i, I = 1;
int[][] thiefSituation = new int[1][2];
int c = 0;
for (int j, z = 0; z < playGround.getNM()[0][0]; z++)
for (j = 0; j < playGround.getNM()[0][1]; j++)
if (playGround.getGround()[z][j] == 1)
c++;
System.out.println("c ============== " + c);
playGround.show(5000);
do {
thiefSituation = playGround.canPoliceSeeThief();
if (thiefSituation == null) {
System.out.println("randomMove");
for (i = 0; i < pn; i++)
polices[i].randomMove(n, m, playGround);
} else {
System.out.println("thief : x = "+playGround.getThiefSituation()[0][0]+" y = "+playGround.getThiefSituation()[0][1]+" \ntargetedMove\ni = " + I);
I++;
for (i = 0; i < pn; i++)
polices[i].targetedMove(thiefSituation, playGround);
}
playGround.show(3250);
System.out.print("\n\n\n\n");
} while (!playGround.catchThief());
}
}
package pack;
import java.util.Random;
public class PlayGround {
private int[][] ground, thiefSituation;
private int n, m, policeNumbers;
PlayGround(int n, int m, int policeNumbers) {
this.n = n;
this.m = m;
this.policeNumbers = policeNumbers;
ground = new int[n][m];
thiefSituation = new int[1][2];
int i, j;
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
ground[i][j] = 0;
}
public void show(int time) {
int i, j;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (ground[i][j] == 0)
System.out.print("-");
else if (ground[i][j] == 1) // police
System.out.print("X");
else // Dozd
System.out.print("*");
}
System.out.println();
}
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public int[][] putRandom() {
int[][] positions = new int[policeNumbers + 1][2];
int i = 0, j = 0, k;
Random rand = new Random();
if (n * m < policeNumbers) System.out.println("Error!! NOT ENOUGH SPACE FOR POLICES AND THIEF IN GROUND");
else {
for (k = 0; k < policeNumbers; k++) { // police
i = rand.nextInt(n);
j = rand.nextInt(m);
System.out.println("i = " + i + " j = " + j); ///////////////////////////////////////////////////////////////// TODO clean
if (ground[i][j] == 0) {
ground[i][j] = 1;
positions[k][0] = i; // x
positions[k][1] = j; // y
} else k--;
}
for (k = 0; k < 1; k++) { // thief
i = rand.nextInt(n);
j = rand.nextInt(m);
System.out.println("i = " + i + " j = " + j);///////////////////////////////////////////////////////////////// TODO clean
if (ground[i][j] == 0) {
ground[i][j] = -1;
positions[policeNumbers][0] = i; // x
positions[policeNumbers][1] = j; // y
thiefSituation[0][0] = i; // x
thiefSituation[0][1] = j; // y
} else k--;
}
}
return positions;
}
public void change(int[][] newPolicesSituation, int xThief, int yThief) {
int i, j;
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
ground[i][j] = 0;
for (i = 0; i < policeNumbers; i++)
ground[newPolicesSituation[i][0]][newPolicesSituation[i][1]] = 1;
ground[xThief][yThief] = -1;
}
public void changeSituation(int xPrevious, int yPrevious, int xNew, int yNew, int k) {
if ((xPrevious != xNew || yPrevious!=yNew) && ground[xNew][yNew] == 1) System.out.println("RIIIIIIIIIIIIIIIIIIIIIIIIIIIDI");
if (ground[xPrevious][yPrevious] == 0) System.out.println("RIIIIIIIIIIIIIIIIIIIIIIIIIIIDI 2");
int c = 0;
for (int j, z = 0; z < n; z++)
for (j = 0; j < m; j++)
if (ground[z][j] == 1)
c++;
System.out.println("c1 ===== " + c);
ground[xPrevious][yPrevious] = 0;
c = 0;
for (int j, z = 0; z < n; z++)
for (j = 0; j < m; j++)
if (ground[z][j] == 1)
c++;
System.out.println("c2 ===== " + c+" k == "+k);
ground[xNew][yNew] = k;
c = 0;
for (int j, z = 0; z < n; z++)
for (j = 0; j < m; j++)
if (ground[z][j] == 1)
c++;
System.out.println("c3 ===== " + c);
}
public int[][] getGround() {
return ground;
}
public int[][] canPoliceSeeThief() {
int i, j, I, J;
if (thiefSituation[0][0] > 1) i = thiefSituation[0][0] - 2;
else i = 0;
if (thiefSituation[0][0] < n - 2) I = thiefSituation[0][0] + 3;
else I = n;
if (thiefSituation[0][1] > 1) j = thiefSituation[0][1] - 2;
else j = 0;
if (thiefSituation[0][1] < m - 2) J = thiefSituation[0][1] + 3;
else J = m;
for (int jj; i < I; i++)
for (jj = j; jj < J; jj++) {
if (ground[i][jj] == 1)
return thiefSituation;
}
return null;
}
public boolean catchThief() {
int i, j;
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
if (ground[i][j] == 100)
return true;
return false;
}
public boolean isEmpty(int x, int y) {
return ground[x][y] == 0;
}
public int[][] getNM() {
int[][] ret = new int[1][2];
ret[0][0] = n;
ret[0][1] = m;
return ret;
}
public int[][] getThiefSituation() {
int[][] ret = new int[1][2];
ret[0][0] = thiefSituation[0][0];
ret[0][1] = thiefSituation[0][1];
return ret;
}
}
package pack;
import java.util.Random;
public class Police {
private int x, y;
private static boolean see;
Police(int x, int y) {
this.x = x;
this.y = y;
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;
if (!i && !I && !j && !J) System.out.println("truuuuuuuuuuuuuuuuuue nistan // police -> random move");
Random random = new Random();
boolean confirm = false;
int X = 0, Y = 0;
System.out.println("x = "+x+" y = "+y+"\n i "+i+"\n I "+I+"\n j "+j+"\n J "+J);
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(int[][] thiefSituation, PlayGround playGround) {
int X = thiefSituation[0][0], Y = thiefSituation[0][1];
int[][] newSituation;
int c = 0;
for (int j, z = 0; z < playGround.getNM()[0][0]; z++)
for (j = 0; j < playGround.getNM()[0][1]; j++)
if (playGround.getGround()[z][j] == 1)
c++;
System.out.println("\nc1 ============== " + c);
if (x > X) {
if (y > Y) // down, right
newSituation = confirmTargetMove(x, y, 1, playGround);
else if (y < Y) // down, left
newSituation = confirmTargetMove(x, y, 3, playGround);
else // down
newSituation = confirmTargetMove(x, y, 2, playGround);
} else if (x < X) {
if (y > Y) // up, right
newSituation = confirmTargetMove(x, y, 6, playGround);
else if (y < Y) // up, left
newSituation = confirmTargetMove(x, y, 8, playGround);
else // up
newSituation = confirmTargetMove(x, y, 7, playGround);
} else {
if (y < Y) // left
newSituation = confirmTargetMove(x, y, 5, playGround);
else // right
newSituation = confirmTargetMove(x, y, 4, playGround);
}
c = 0;
for (int j, z = 0; z < playGround.getNM()[0][0]; z++)
for (j = 0; j < playGround.getNM()[0][1]; j++)
if (playGround.getGround()[z][j] == 1)
c++;
System.out.println("c1.5 ============ " + c);
playGround.changeSituation(x, y, newSituation[0][0], newSituation[0][1], 1);
x = newSituation[0][0];
y = newSituation[0][1];
c = 0;
for (int j, z = 0; z < playGround.getNM()[0][0]; z++)
for (j = 0; j < playGround.getNM()[0][1]; j++)
if (playGround.getGround()[z][j] == 1)
c++;
System.out.println("c2 ============== " + c + "\n");
}
public void targetedMove2(int[][] thiefSituation, PlayGround playGround) {
int X = thiefSituation[0][0], Y = thiefSituation[0][1];
int[][] newSituation = new int[1][2];
switch ((int) (1000 * (Math.sqrt(Math.pow(X - x, 2) + Math.pow(Y - y, 2)) - Math.sqrt(Math.pow(X - 0.3 - x, 2) + Math.pow(Y - 0.4 - getY(), 2))))) {
case 493: // up, left
case 433:
case 489:
case 492:
newSituation = confirmTargetMove(x, y, 8, playGround);
break;
case 193: // up
case 253:
newSituation = confirmTargetMove(x, y, 7, playGround);
break;
case 33:
case -112:
case -151:
case -263: //up, right
newSituation = confirmTargetMove(x, y, 6, playGround);
break;
case 372:
case 329: //left
newSituation = confirmTargetMove(x, y, 5, playGround);
break;
case -431:
case -418: //right
newSituation = confirmTargetMove(x, y, 4, playGround);
break;
case 174:
case -17:
case -140:
case 26: //down, left
newSituation = confirmTargetMove(x, y, 3, playGround);
break;
case -360:
case -334: //down
newSituation = confirmTargetMove(x, y, 2, playGround);
break;
case -496:
case -493:
case -456:
case -495: //down, right
newSituation = confirmTargetMove(x, y, 1, playGround);
}
if (newSituation[0][0] != x || newSituation[0][1] != y)
playGround.changeSituation(x, y, newSituation[0][0], newSituation[0][1], 1);
}
public int[][] confirmTargetMove(int x, int y, int destination, PlayGround p) {
int[][] newSituation = new int[1][2];
Random r = new Random();
switch (destination) {
case 1:
if (p.isEmpty(x - 1, y - 1)) { // go up & left
newSituation[0][0] = x - 1;
newSituation[0][1] = y - 1;
return newSituation;
} else {
switch (r.nextInt(2)) {
case 0:
if (p.isEmpty(x - 1, y)) { // go up
newSituation[0][0] = x - 1;
newSituation[0][1] = y;
return newSituation;
}
case 1:
if (p.isEmpty(x, y - 1)) { // go left
newSituation[0][0] = x;
newSituation[0][1] = y - 1;
return newSituation;
}
}
newSituation[0][0] = x; // stay in previous situation
newSituation[0][1] = y;
return newSituation;
}
case 2:
if (p.isEmpty(x - 1, y)) { // go up
newSituation[0][0] = x - 1;
newSituation[0][1] = y;
return newSituation;
} else {
switch (r.nextInt(2)) {
case 0:
if (y != p.getNM()[0][1] - 1 && p.isEmpty(x - 1, y + 1)) { // go up, right
newSituation[0][0] = x - 1;
newSituation[0][1] = y + 1;
return newSituation;
}
case 1:
if (y != 0 && p.isEmpty(x - 1, y - 1)) { // go up, left
newSituation[0][0] = x - 1;
newSituation[0][1] = y - 1;
return newSituation;
}
}
newSituation[0][0] = x; // stay in previous situation
newSituation[0][1] = y;
return newSituation;
}
case 3:
if (p.isEmpty(x - 1, y + 1)) { // go up & right
newSituation[0][0] = x - 1;
newSituation[0][1] = y + 1;
return newSituation;
} else {
switch (r.nextInt(2)) {
case 0:
if (p.isEmpty(x, y + 1)) { // go right
newSituation[0][0] = x;
newSituation[0][1] = y + 1;
return newSituation;
}
case 1:
if (p.isEmpty(x - 1, y)) { // go up
newSituation[0][0] = x - 1;
newSituation[0][1] = y;
return newSituation;
}
}
newSituation[0][0] = x; // stay in previous situation
newSituation[0][1] = y;
return newSituation;
}
case 4:
if (p.isEmpty(x, y - 1)) { // go left
newSituation[0][0] = x;
newSituation[0][1] = y - 1;
return newSituation;
} else {
switch (r.nextInt(2)) {
case 0:
if (x != 0 && p.isEmpty(x - 1, y - 1)) { // go up , left
newSituation[0][0] = x - 1;
newSituation[0][1] = y - 1;
return newSituation;
}
case 1:
if (x != p.getNM()[0][0] && p.isEmpty(x + 1, y - 1)) { // go down, left
newSituation[0][0] = x + 1;
newSituation[0][1] = y - 1;
return newSituation;
}
}
newSituation[0][0] = x; // stay in previous situation
newSituation[0][1] = y;
return newSituation;
}
case 5:
if (p.isEmpty(x, y + 1)) { // go right
newSituation[0][0] = x;
newSituation[0][1] = y + 1;
return newSituation;
} else {
switch (r.nextInt(2)) {
case 0:
if (x != p.getNM()[0][0] && p.isEmpty(x + 1, y + 1)) { // go down, right
newSituation[0][0] = x + 1;
newSituation[0][1] = y + 1;
return newSituation;
}
case 1:
if (x != 0 && p.isEmpty(x - 1, y + 1)) { // go up, right
newSituation[0][0] = x - 1;
newSituation[0][1] = y + 1;
return newSituation;
}
}
newSituation[0][0] = x; // stay in previous situation
newSituation[0][1] = y;
return newSituation;
}
case 6:
if (p.isEmpty(x + 1, y - 1)) { // go down & left
newSituation[0][0] = x + 1;
newSituation[0][1] = y - 1;
return newSituation;
} else {
switch (r.nextInt(2)) {
case 0:
if (p.isEmpty(x, y - 1)) { // go left
newSituation[0][0] = x;
newSituation[0][1] = y - 1;
return newSituation;
}
case 1:
if (p.isEmpty(x + 1, y)) { // go down
newSituation[0][0] = x - 1;
newSituation[0][1] = y;
return newSituation;
}
}
newSituation[0][0] = x; // stay in previous situation
newSituation[0][1] = y;
return newSituation;
}
case 7:
if (p.isEmpty(x + 1, y)) { // go down
newSituation[0][0] = x + 1;
newSituation[0][1] = y;
return newSituation;
} else {
switch (r.nextInt(2)) {
case 0:
if (y != 0 && p.isEmpty(x + 1, y - 1)) { // go down, left
newSituation[0][0] = x + 1;
newSituation[0][1] = y - 1;
return newSituation;
}
case 1:
if (y != p.getNM()[0][1] && p.isEmpty(x + 1, y + 1)) { // go down, right
newSituation[0][0] = x + 1;
newSituation[0][1] = y + 1;
return newSituation;
}
}
newSituation[0][0] = x; // stay in previous situation
newSituation[0][1] = y;
return newSituation;
}
case 8:
if (p.isEmpty(x + 1, y + 1)) { // go down, right
newSituation[0][0] = x + 1;
newSituation[0][1] = y + 1;
return newSituation;
} else {
switch (r.nextInt(2)) {
case 0:
if (p.isEmpty(x + 1, y)) { // go down
newSituation[0][0] = x + 1;
newSituation[0][1] = y;
return newSituation;
}
case 1:
if (p.isEmpty(x, y + 1)) { // go right
newSituation[0][0] = x;
newSituation[0][1] = y + 1;
return newSituation;
}
}
newSituation[0][0] = x; // stay in previous situation
newSituation[0][1] = y;
return newSituation;
}
}
return newSituation;
}
public int[][] getSituation() {
int[][] Return = new int[1][1];
Return[0][0] = x;
Return[0][1] = y;
return Return;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
\ No newline at end of file
package pack;
public class Thief {
private int x, y;
private boolean see;
Thief(int x, int y) {
this.x = x;
this.y = y;
see = false;
}
}
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