Commit 22d731ec authored by 9731077's avatar 9731077

finished

parent 95728c84
import java.util.ArrayList; import java.util.*;
import java.util.Random; import java.sql.SQLOutput;
//string to
public class Field { public class Field {
private int m; private final int m;
private int n; private final int n;
private ArrayList<Police> polices = new ArrayList<Police>();
private Robber Robber;
public String[][] fieldShape; public String[][] fieldShape;
private Robber robber;
private ArrayList<Police> polices = new ArrayList<Police>();
public Field(int m, int n) { public Field(int m, int n) {
this.m = m; this.m = m;
...@@ -18,98 +18,107 @@ public class Field { ...@@ -18,98 +18,107 @@ public class Field {
fieldShape[i][j] = "-"; fieldShape[i][j] = "-";
} }
public int getM() { public void changeField() {
return m; for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
fieldShape[i][j] = "-";
fieldShape[robber.getX()][robber.getY()] = "D";
boolean b = false;
for (Police police : polices) {
if (fieldShape[police.getX()][police.getY()].equals("D")) {
b = true;
fieldShape[police.getX()][police.getY()] = "*";
}
if (fieldShape[police.getPreviosX()][police.getPreviosY()].equals("D")) {
b = true;
fieldShape[police.getX()][police.getY()] = "*";
}
if (!fieldShape[police.getX()][police.getY()].equals("*"))
fieldShape[police.getX()][police.getY()] = "P";
}
if (b == true) {
showFieldShape();
System.exit(0);
}
}
public void setRobber(Robber robber) {
this.robber = robber;
} }
public void setM(int m) { public Robber getRobber() {
this.m = m; return robber;
} }
public int getN() { public String[][] getCourt() {
return n; return fieldShape;
} }
public void setN(int n) { public ArrayList<Police> getPolices() {
this.n = n; return polices;
} }
public void setFirstPlaceOfPolice(int policeNum) { public void randomPolicePlace(int policeNum) {
int tmpX, tmpY, cnt = 0; Random random = new Random();
Random randomGenerator = new Random();
for (int i = 0; i < policeNum; i++) { for (int i = 0; i < policeNum; i++) {
tmpX = randomGenerator.nextInt(m); int x = random.nextInt(m);
tmpY = randomGenerator.nextInt(n); int y = random.nextInt(n);
if (polices.size() == 0) { boolean bl = true;
Police p = new Police(tmpX, tmpY); if (polices.size() != 0) {
fieldShape[tmpX][tmpY] = "P";
polices.add(p);
System.out.println("First police created");
} else {
for (Police police : polices) { for (Police police : polices) {
if (police.getX() == tmpX && police.getY() == tmpY) { if (police.getX() == x && police.getY() == y) {
i--; i--;
cnt++; bl = false;
break; break;
} }
} }
if (cnt == 0) {
Police p = new Police(tmpX, tmpY);
fieldShape[tmpX][tmpY] = "P";
polices.add(p);
}
} }
if (bl == true) {
fieldShape[x][y] = "P";
Police p = new Police(x, y);
polices.add(p);
}
} }
}
public void setFirstPlaceOfRobber() { }
int tmpX, tmpY, cnt = 0;
public void randomRobberPlce() {
Random random = new Random();
while (true) { while (true) {
Random randomGenerator = new Random(); int x = random.nextInt(m);
tmpX = randomGenerator.nextInt(m); int y = random.nextInt(n);
tmpY = randomGenerator.nextInt(n); boolean b = true;
for (Police police : polices) { for (Police police : polices) {
if (police.getX() == tmpX && police.getY() == tmpY) { if (police.getX() == x && police.getY() == y)
cnt++; b = false;
break;
}
} }
if (cnt == 0) { if (b == true) {
Robber Robber = new Robber(); robber = new Robber(x, y);
fieldShape[tmpX][tmpY] = "-"; fieldShape[x][y] = "D";
return; break;
} }
} }
} }
public void changeField() { public void showFieldShape() {
for (int i = 0; i < m; i++) for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) for (int j = 0; j < n; j++)
fieldShape[i][j] = "-"; System.out.print(fieldShape[i][j] + " | ");
System.out.println();
fieldShape[Robber.getX()][Robber.getY()] = "D";
boolean b = false;
for (Police police : polices) {
if (fieldShape[police.getX()][police.getY()].equals("D")) {
b = true;
fieldShape[police.getX()][police.getY()] = "*";
}
if (fieldShape[police.getPreviosX()][police.getPreviosY()].equals("D")) {
b = true;
fieldShape[police.getX()][police.getY()] = "*";
}
if (!fieldShape[police.getX()][police.getY()].equals("*"))
fieldShape[police.getX()][police.getY()] = "P";
}
if (b == true) {
//show field
System.exit(0);
} }
} }
} }
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