Commit f23ef9a7 authored by 9611046's avatar 9611046

Completes the project

parent d4850a88
public class Land { public class Land {
private int length; private int length;
private int width; private int width;
private int[][] landMatrix = new int[length][width]; private char[][] landMatrix;
public Land(int length, int width) { public Land(int length, int width) {
this.length = length; this.length = length;
this.width = width; this.width = width;
landMatrix = new char[length][width];
} }
public int getLength() { public int getLength() {
...@@ -16,8 +18,14 @@ public class Land { ...@@ -16,8 +18,14 @@ public class Land {
return width; return width;
} }
public int[][] getLandMatrix() { public void setLandMatrix(int x, int y, char c){
return landMatrix; if(x>=0 && x<width && y>=0 && y<length) {
landMatrix[x][y] = c;
}
}
public char getLandMatrix(int x,int y) {
return landMatrix[x][y];
} }
} }
...@@ -8,8 +8,10 @@ public class Main { ...@@ -8,8 +8,10 @@ public class Main {
Scanner scanner = new Scanner(System.in); Scanner scanner = new Scanner(System.in);
int length = scanner.nextInt(); int length = scanner.nextInt();
int width = scanner.nextInt(); int width = scanner.nextInt();
int arrestX =56; int arrestX =0;
int arrestY =56 ; int arrestY =0;
int policeMovment = 0;
int thiefMovement = 0;
Land myLand = new Land(length, width); Land myLand = new Land(length, width);
Random rand = new Random(); Random rand = new Random();
int policeNumbers = scanner.nextInt(); int policeNumbers = scanner.nextInt();
...@@ -78,8 +80,10 @@ public class Main { ...@@ -78,8 +80,10 @@ public class Main {
thief.move(); thief.move();
thiefMovement++;
for (Police police : polices) { for (Police police : polices) {
police.move(); police.move();
policeMovment++;
} }
...@@ -127,8 +131,10 @@ System.out.println( ...@@ -127,8 +131,10 @@ System.out.println(
thief.smartMove(polices.get(0).getX(), polices.get(0).getY()); thief.smartMove(polices.get(0).getX(), polices.get(0).getY());
thiefMovement++;
for (Police police : polices) { for (Police police : polices) {
police.smartMove(thief.getX(), thief.getY()); police.smartMove(thief.getX(), thief.getY());
policeMovment++;
} }
...@@ -162,6 +168,10 @@ System.out.println( ...@@ -162,6 +168,10 @@ System.out.println(
System.out.println(); System.out.println();
} }
System.out.println("The number of police movements: "+policeMovment+"\n"+
"The number of thieves' moves: "+thiefMovement);
} }
} }
import java.util.Random;
public class Police { public class Police {
Random rand = new Random();
Land policeLand; private Land policeLand;
private int x; private int x;
private int y; private int y;
public void setFirstxy() {
x = rand.nextInt(policeLand.getLength()); private boolean notify = false;
y = rand.nextInt(policeLand.getLength()); private boolean thiefCatch = false;
public void setFirstxy(int x, int y) {
this.x = x;
this.y = y;
} }
public void move() { public void move() {
int a = rand.nextInt(8); Move move1 = new Move(x,y,policeLand);
switch (a) { move1.moveXY();
case 0: this.x = move1.getX();
//Right this.y = move1.getY();
if (x + 1 <= policeLand.getLength()) { }
x += 1; public void smartMove(int w,int z) {
break; // this.w = x;
} // this.z = z;
case 1: Move move1 = new Move(x,y,w,z,policeLand);
//Left move1.smartMove();
if (x > 0) { this.x = move1.getX();
x -= 1; this.y = move1.getY();
break;
}
case 2:
//Up
if (y > 0) {
y -= 1;
break;
}
case 3:
//Down
if (y + 1 < policeLand.getWidth()) {
y += 1;
}
case 4:
//North East
if (y > 0) {
if (x + 1 <= policeLand.getLength()) {
y += 1;
x += 1;
}
}
case 5:
//North west
if (y > 0) {
if (x > 0) {
y += 1;
x -= 1;
}
}
case 6:
//Southeast
if (y + 1 < policeLand.getWidth()) {
if (x + 1 <= policeLand.getLength()) {
y += 1;
x += 1;
break;
}
}
case 7:
//Southwest
if (y + 1 < policeLand.getWidth()) {
if (x > 0) {
y += 1;
x -= 1;
break;
}
}
}
} }
public void setLand(Land myLand) { public void setLand(Land myLand) {
policeLand = myLand; policeLand = myLand;
} }
...@@ -97,4 +45,19 @@ public class Police { ...@@ -97,4 +45,19 @@ public class Police {
return y; return y;
} }
public boolean getNotify() {
return notify;
}
public void setNotify(boolean notify) {
this.notify = notify;
}
public void SetThiefCatch(boolean thiefCatch){
this.thiefCatch = thiefCatch;
}
public boolean getThiefCatch(){
return thiefCatch;
}
} }
import java.util.Random; import java.util.Random;
public class Thief { public class Thief {
Land thiefLand; private Land thiefLand;
Random rand = new Random();
private int firstx = rand.nextInt(thiefLand.getLength()); private int x ;
private int firsty = rand.nextInt(thiefLand.getLength()); private int y ;
private int x = firstx;
private int y = firsty;
public int getX(){ public int getX(){
...@@ -19,4 +17,29 @@ public class Thief { ...@@ -19,4 +17,29 @@ public class Thief {
public void setLand(Land myLand) { public void setLand(Land myLand) {
thiefLand = myLand; thiefLand = myLand;
} }
public void setFirstxy(int x,int y) {
this.x = x;
this.y = y;
// this.x = rand.nextInt(thiefLand.getLength());
// y = rand.nextInt(thiefLand.getLength());
}
public void move() {
Move move1 = new Move(x,y,thiefLand);
move1.moveXY();
this.x = move1.getX();
this.y = move1.getY();
}
public void smartMove(int w,int z) {
// this.w = x;
// this.z = z;
Move move1 = new Move(w,z,x,y,thiefLand);
move1.smartMove();
this.x = move1.getW();
this.y = move1.getZ();
}
} }
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