Commit 2bedad5a authored by Omid Sayfun's avatar Omid Sayfun

Applied OOP Principles

parent 32eac881
import java.util.*; import java.util.*;
import java.util.concurrent.*;
import java.io.IOException; import java.io.IOException;
import lab.game.*;
public class Main{ public class Main{
static class Board{
public int n;
public int m;
public Thief thief;
public int thiefMoves;
public int policeMoves;
public ArrayList<Police> polices;
public Board(int n, int m){
this.n = n;
this.m = m;
this.thief = null;
this.polices = new ArrayList<Police>();
this.thiefMoves = 0;
this.policeMoves = 0;
}
public Boolean isTakenByThief(int x, int y){
if( this.thief != null && this.thief.x == x && this.thief.y == y ){
return true;
}
return false;
}
public Boolean isTakenByPolice(int x, int y){
for(Police t : this.polices){
if( t.x == x && t.y == y ){
return true;
}
}
return false;
}
public Boolean isFree(int x, int y){
if( !isTakenByThief(x, y) && !isTakenByPolice(x, y) ){
return true;
}else{
return false;
}
}
public Boolean isThiefInRange(int x, int y){
if( Math.abs(this.thief.x - x) <= 2 && Math.abs(this.thief.y - y) <= 2 ){
return true;
}
return false;
}
public Boolean timeMove(){
Random rand = new Random();
int prevX = this.thief.x;
int prevY = this.thief.y;
// Move Thief
while(true){
int xShift = rand.nextInt(3) - 1;
while(true){
if( this.thief.x + xShift >= 0 && this.thief.x + xShift < this.n ){
break;
}
xShift = rand.nextInt(3) - 1;
}
int yShift = rand.nextInt(3) - 1;
while(true){
if( this.thief.y + yShift >= 0 && this.thief.y + yShift < this.m ){
break;
}
yShift = rand.nextInt(3) - 1;
}
this.thiefMoves++;
// To Do: Check if thief went into police house
if( isTakenByPolice(this.thief.x + xShift, this.thief.y + yShift) ){
this.thief.x += xShift;
this.thief.y += yShift;
return false;
}else{
this.thief.x += xShift;
this.thief.y += yShift;
break;
}
}
// Move Polices
for(Police p : this.polices){
if( p.target == null){// Random Move
while(true){
int xShift = rand.nextInt(3) - 1;
while(true){
if( p.x + xShift >= 0 && p.x + xShift < this.n ){
break;
}
xShift = rand.nextInt(3) - 1;
}
int yShift = rand.nextInt(3) - 1;
while(true){
if( p.y + yShift >= 0 && p.y + yShift < this.m ){
break;
}
yShift = rand.nextInt(3) - 1;
}
if( !isTakenByPolice(p.x + xShift, p.y + yShift) ){
p.x += xShift;
p.y += yShift;
if( isThiefInRange(p.x, p.y) ){
for( Police police : this.polices ){
police.target = this.thief;
}
}
this.policeMoves++;
break;
}
}
}else{// Move based on thought :D
int xShiftThought = 0;
int yShiftThought = 0;
if( prevX - p.x != 0 ){
xShiftThought = (prevX - p.x) / Math.abs(prevX - p.x);
}
if( prevY - p.y != 0 ){
yShiftThought = (prevY - p.y) / Math.abs(prevY - p.y);
}
if( !isTakenByPolice(p.x + xShiftThought, p.y + yShiftThought) ){ // Diagonal
p.x += xShiftThought;
p.y += yShiftThought;
this.policeMoves++;
if( p.y == prevY && p.x == prevX ){
return false;
}
}else if( !isTakenByPolice(p.x, p.y + yShiftThought) ){ // Y Axis
p.y += yShiftThought;
this.policeMoves++;
if( p.y == prevY && p.x == prevX ){
return false;
}
}else if( !isTakenByPolice(p.x + xShiftThought, p.y) ){ // X Axis
p.x += xShiftThought;
this.policeMoves++;
if( p.y == prevY && p.x == prevX ){
return false;
}
}else{ // Random Move
while(true){
int xShift = rand.nextInt(3) - 1;
while(true){
if( p.x + xShift >= 0 && p.x + xShift < this.n ){
break;
}
xShift = rand.nextInt(3) - 1;
}
int yShift = rand.nextInt(3) - 1;
while(true){
if( p.y + yShift >= 0 && p.y + yShift < this.m ){
break;
}
yShift = rand.nextInt(3) - 1;
}
if( !isTakenByPolice(p.x + xShift, p.y + yShift) ){
p.x += xShift;
p.y += yShift;
this.policeMoves++;
if( p.y == prevY && p.x == prevX ){
return false;
}
break;
}
}
}
}
}
return true;
}
public void print(int t){
System.out.println("Time: " + t);
for(int i = 0; i < n; i++){// Check Bounds
for(int j = 0; j < m; j++){
if( isTakenByThief(i, j) ){
System.out.print("T ");
}else if( isTakenByPolice(i, j) ){
System.out.print("P ");
}else{
System.out.print("- ");
}
}
System.out.println("");
}
}
public void printFinal(){
System.out.println("\tThe Fucking Thief is caught!");
System.out.println();
System.out.println("Thief Moves: " + this.thiefMoves);
System.out.println("Police Moves: " + this.policeMoves);
}
}
static class Thief{
public int x;
public int y;
public Thief(int x, int y){
this.x = x;
this.y = y;
}
}
static class Police{
public int x;
public int y;
public Thief target;
public Police(int x, int y){
this.x = x;
this.y = y;
this.target = null;
}
}
public static void main(String[] args) throws IOException, InterruptedException{ public static void main(String[] args) throws IOException, InterruptedException{
Scanner sc = new Scanner(System.in); Scanner sc = new Scanner(System.in);
// Get Data // Get Data
...@@ -251,43 +13,15 @@ public class Main{ ...@@ -251,43 +13,15 @@ public class Main{
System.out.println("Enter number of polices: "); System.out.println("Enter number of polices: ");
int p = Integer.parseInt(sc.next()); int p = Integer.parseInt(sc.next());
// Initialize Board // Initialize Board
Board mainBoard = new Board(n, m); Board mainBoard = new Board(n, m, p);
Random rand = new Random(); mainBoard.initBoard();
// Create Thief // Start the game
boolean flag = true; try {
int x = 0, y = 0; mainBoard.startGame();
while(flag){
x = rand.nextInt(n);
y = rand.nextInt(m);
flag = !mainBoard.isFree(x, y);
}
Thief newThief = new Thief(x, y);
mainBoard.thief = newThief;
// Create Polices
for(int i = 0; i < p; i++){
flag = true;
x = 0;
y = 0;
while(flag){
x = rand.nextInt(n);
y = rand.nextInt(m);
flag = !(mainBoard.isFree(x, y) && !mainBoard.isThiefInRange(x, y));
}
Police newPolice = new Police(x, y);
mainBoard.polices.add(newPolice);
} }
// Print fucking board catch(IOException e) {
flag = true; e.printStackTrace();
int i = 0;
while(flag){
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
mainBoard.print(i);
flag = mainBoard.timeMove();
TimeUnit.SECONDS.sleep(2);
i++;
} }
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
mainBoard.printFinal();
sc.close(); sc.close();
} }
} }
package lab.game;
import java.util.*;
import java.util.concurrent.*;
import java.io.IOException;
public class Board{
private int n;
private int m;
private int c;
private Thief thief;
private int thiefMoves;
private int policeMoves;
private ArrayList<Police> polices;
public Board(int n, int m, int c){
this.n = n;
this.m = m;
this.c = c;
this.thief = null;
this.polices = new ArrayList<Police>();
this.thiefMoves = 0;
this.policeMoves = 0;
}
public void setThief(Thief t){
this.thief = t;
}
public void addPolice(Police p){
this.polices.add(p);
}
public void initBoard(){
Random rand = new Random();
// Create Thief
boolean flag = true;
int x = 0, y = 0;
while(flag){
x = rand.nextInt(this.n);
y = rand.nextInt(this.m);
flag = !isFree(x, y);
}
Thief newThief = new Thief(x, y);
this.thief = newThief;
// Create Polices
for(int i = 0; i < this.c; i++){
flag = true;
x = 0;
y = 0;
while(flag){
x = rand.nextInt(this.n);
y = rand.nextInt(this.m);
flag = !(isFree(x, y) && !isThiefInRange(x, y));
}
Police newPolice = new Police(x, y);
this.polices.add(newPolice);
}
}
public void startGame() throws IOException, InterruptedException{
boolean flag = true;
int i = 0;
while(flag){
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
this.print(i);
flag = this.timeMove();
TimeUnit.SECONDS.sleep(2);
i++;
}
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
this.printFinal();
}
public Boolean isTakenByThief(int x, int y){
if( this.thief != null && this.thief.getX() == x && this.thief.getY() == y ){
return true;
}
return false;
}
public Boolean isTakenByPolice(int x, int y){
for(Police t : this.polices){
if( t.getX() == x && t.getY() == y ){
return true;
}
}
return false;
}
public Boolean isFree(int x, int y){
if( !isTakenByThief(x, y) && !isTakenByPolice(x, y) ){
return true;
}else{
return false;
}
}
public Boolean isThiefInRange(int x, int y){
if( Math.abs(this.thief.getX() - x) <= 2 && Math.abs(this.thief.getY() - y) <= 2 ){
return true;
}
return false;
}
public Boolean timeMove(){
Random rand = new Random();
int prevX = this.thief.getX();
int prevY = this.thief.getY();
// Move Thief
while(true){
int xShift = rand.nextInt(3) - 1;
while(true){
if( this.thief.getX() + xShift >= 0 && this.thief.getX() + xShift < this.n ){
break;
}
xShift = rand.nextInt(3) - 1;
}
int yShift = rand.nextInt(3) - 1;
while(true){
if( this.thief.getY() + yShift >= 0 && this.thief.getY() + yShift < this.m ){
break;
}
yShift = rand.nextInt(3) - 1;
}
this.thiefMoves++;
this.thief.setCoordinates(this.thief.getX() + xShift, this.thief.getY() + yShift);
if( isTakenByPolice(this.thief.getX() + xShift, this.thief.getY() + yShift) ){
return false;
}else{
break;
}
}
// Move Polices
for(Police p : this.polices){
if( p.getTarget() == null){// Random Move
while(true){
int xShift = rand.nextInt(3) - 1;
while(true){
if( p.getX() + xShift >= 0 && p.getX() + xShift < this.n ){
break;
}
xShift = rand.nextInt(3) - 1;
}
int yShift = rand.nextInt(3) - 1;
while(true){
if( p.getY() + yShift >= 0 && p.getY() + yShift < this.m ){
break;
}
yShift = rand.nextInt(3) - 1;
}
if( !isTakenByPolice(p.getX() + xShift, p.getY() + yShift) ){
p.setCoordinates(p.getX() + xShift, p.getY() + yShift);
if( isThiefInRange(p.getX(), p.getY()) ){
for( Police police : this.polices ){
police.setTarget(this.thief);
}
}
this.policeMoves++;
break;
}
}
}else{// Move based on thought :D
int xShiftThought = 0;
int yShiftThought = 0;
if( prevX - p.getX() != 0 ){
xShiftThought = (prevX - p.getX()) / Math.abs(prevX - p.getX());
}
if( prevY - p.getY() != 0 ){
yShiftThought = (prevY - p.getY()) / Math.abs(prevY - p.getY());
}
if( !isTakenByPolice(p.getX() + xShiftThought, p.getY() + yShiftThought) ){ // Diagonal
p.setCoordinates(p.getX() + xShiftThought, p.getY() + yShiftThought);
this.policeMoves++;
if( p.getY() == prevY && p.getX() == prevX ){
return false;
}
}else if( !isTakenByPolice(p.getX(), p.getY() + yShiftThought) ){ // Y Axis
p.setCoordinates(p.getX(), p.getY() + yShiftThought);
this.policeMoves++;
if( p.getY() == prevY && p.getX() == prevX ){
return false;
}
}else if( !isTakenByPolice(p.getX() + xShiftThought, p.getY()) ){ // X Axis
p.setCoordinates(p.getX() + xShiftThought, p.getY());
this.policeMoves++;
if( p.getY() == prevY && p.getX() == prevX ){
return false;
}
}else{ // Random Move
while(true){
int xShift = rand.nextInt(3) - 1;
while(true){
if( p.getX() + xShift >= 0 && p.getX() + xShift < this.n ){
break;
}
xShift = rand.nextInt(3) - 1;
}
int yShift = rand.nextInt(3) - 1;
while(true){
if( p.getY() + yShift >= 0 && p.getY() + yShift < this.m ){
break;
}
yShift = rand.nextInt(3) - 1;
}
if( !isTakenByPolice(p.getX() + xShift, p.getY() + yShift) ){
p.setCoordinates(p.getX() + xShift, p.getY() + yShift);
this.policeMoves++;
if( p.getY() == prevY && p.getX() == prevX ){
return false;
}
break;
}
}
}
}
}
return true;
}
public void print(int t){
System.out.println("Time: " + t);
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if( isTakenByThief(i, j) ){
System.out.print("T ");
}else if( isTakenByPolice(i, j) ){
System.out.print("P ");
}else{
System.out.print("- ");
}
}
System.out.println("");
}
}
public void printFinal(){
System.out.println("\tThe Thief is caught!");
System.out.println();
System.out.println("Thief Moves: " + this.thiefMoves);
System.out.println("Police Moves: " + this.policeMoves);
}
}
package lab.game;
public class Police{
private int x;
private int y;
private Thief target;
public Police(int x, int y){
this.x = x;
this.y = y;
this.target = null;
}
public void setCoordinates(int x, int y){
this.x = x;
this.y = y;
}
public void setTarget(Thief t){
this.target = t;
}
public int getX(){
return this.x;
}
public int getY(){
return this.y;
}
public Thief getTarget(){
return this.target;
}
}
package lab.game;
public class Thief{
private int x;
private int y;
public Thief(int x, int y){
this.x = x;
this.y = y;
}
public void setCoordinates(int x, int y){
this.x = x;
this.y = y;
}
public int getX(){
return this.x;
}
public int getY(){
return this.y;
}
}
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