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;
}
}
This diff is collapsed.
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