Commit 0ac16802 authored by Omid Sayfun's avatar Omid Sayfun

Added Thoughtful:D Movement

parent ef5bb69c
Pipeline #485 failed with stages
...@@ -59,29 +59,123 @@ public class Main{ ...@@ -59,29 +59,123 @@ public class Main{
Random rand = new Random(); Random rand = new Random();
// Move Polices // Move Polices
for(Police p : this.polices){ for(Police p : this.polices){
while(true){ if( p.target == null){// Random Move
int xShift = rand.nextInt(3) - 1;
while(true){ while(true){
if( p.x + xShift >= 0 && p.x + xShift < this.n ){ 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( isFree(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; break;
} }
xShift = rand.nextInt(3) - 1;
} }
int yShift = rand.nextInt(3) - 1; }else{// Move based on thought :D
while(true){ int xShiftThought = 0;
if( p.y + yShift >= 0 && p.y + yShift < this.m ){ int yShiftThought = 0;
if( p.target.x - p.x != 0 ){
break; xShiftThought = (p.target.x - p.x) / Math.abs(p.target.x - p.x);
}
if( p.target.y - p.y != 0 ){
yShiftThought = (p.target.y - p.y) / Math.abs(p.target.y - p.y);
}
if( isFree(p.x + xShiftThought, p.y + yShiftThought) ){ // Diagonal
p.x += xShiftThought;
p.y += yShiftThought;
this.policeMoves++;
}else if( isFree(p.x, p.y + yShiftThought) ){ // Y Axis
p.y += yShiftThought;
this.policeMoves++;
}else if( isFree(p.x + xShiftThought, p.y) ){ // X Axis
p.x += xShiftThought;
this.policeMoves++;
}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( isFree(p.x + xShift, p.y + yShift) ){
p.x += xShift;
p.y += yShift;
if( p.target == null && isThiefInRange(p.x, p.y) ){
for( Police police : this.polices ){
police.target = this.thief;
}
}
this.policeMoves++;
break;
}
} }
yShift = rand.nextInt(3) - 1;
} }
if( isFree(p.x + xShift, p.y + yShift) ){ }
}
// 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 ){
p.x += xShift;
p.y += yShift;
break; break;
} }
yShift = rand.nextInt(3) - 1;
}
// To Do: Check if thief went into police house
if( isFree(this.thief.x + xShift, this.thief.y + yShift) ){
this.thief.x += xShift;
this.thief.y += yShift;
this.thiefMoves++;
break;
} }
} }
} }
......
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