Skip to content

Instantly share code, notes, and snippets.

@hoangddt
Created December 1, 2015 06:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoangddt/d82a40d6c744b628d280 to your computer and use it in GitHub Desktop.
Save hoangddt/d82a40d6c744b628d280 to your computer and use it in GitHub Desktop.
just a java code snipet
package RaceAI.AI;
import java.awt.Point;
import java.util.Random;
import java.util.Vector;
import RaceAI.MyLogger.MyLogger;
import RaceAI.MyLogger.FILE_LOG;
import RaceAI.RaceClient.Car;
import RaceAI.RaceClient.Race;
public class MainAI
{
Race race;
Vector<Car> All_cars;
Car Mycar;
public String key = "0000"; // Go-Back-Left-Right (Up - Down - Left - Right)
private MyLogger _log = MyLogger.getInstance()
.addNewLogger(this.getClass().getName(), FILE_LOG.MainAI);
public MainAI(Race race, Vector<Car> cars, Car Mycar){
this.race = race;
this.Mycar = Mycar;
this.All_cars = cars;
this.writeLogMsg("Constructor of MainAI is called");
}
private void writeLogMsg(String msg)
{
_log.writeLogMsg(FILE_LOG.MainAI, msg);
}
/// Write your AI here ...
// your variants
Point last, now, next, pivot;
Random rand = new Random();
int[] ix = {0, 1, 0, -1};
int[] iy = {1, 0, -1, 0};
//last position
double last_x = 0,
last_y = 0,
now_x = 0,
now_y = 0,
pivot_x = 0,
pivot_y = 0;
// number of the equalation
int equalFreq = 0;
boolean stuck = false;
// last speed
double speed = 0;
// your AI
public void AI()
{
int blockSize = this.race.BlockSize();
last_x = now_x;
last_y = now_y;
now_x = this.Mycar.getx();
now_y = this.Mycar.gety();
int map_x = (int) (this.Mycar.getx() / blockSize);
int map_y = (int) (this.Mycar.gety() / blockSize);
if (last_x == now_x && last_y == now_y && (map_x != 1 || map_y != 1))
{
this.writeLogMsg("Equal frequence is: " + equalFreq);
if (equalFreq > 400)
{
this.stuck = true;
}
else equalFreq ++;
}
if (this.stuck)
{
if (pivot_x == 0.0 && pivot_y == 0.0)
{
this.pivot_x = this.now_x;
this.pivot_y = this.now_y;
}
double distancex = this.now_x - this.pivot_x;
distancex = distancex < 0 ? -distancex : distancex;
double distancey = this.now_y - this.pivot_y;
distancey = distancey < 0 ? -distancey : distancey;
double distance = distancex > distancey ? distancex : distancey;
this.writeLogMsg("Distance x, y: " + distancex + ", " + distancey);
this.writeLogMsg("Distance is: " + distance + "so sanh voi: " + (0.3 * blockSize));
if (distance < 0.5 * blockSize)
{
this.writeLogMsg("Key =======> " + this.key);
this.key = "0100";
return;
}
else
{
equalFreq = 0;
this.pivot_x = 0.0;
this.pivot_y = 0.0;
this.stuck = false;
}
}
this.writeLogMsg("Now in: " + map_x + ", " + map_y);
this.last = this.now;
this.now = new Point(map_x, map_y);
// khoi tao cho pivot lan dau tien voi gia tri map
if (this.pivot == null)
{
this.pivot = new Point(map_x, map_y);
}
// neu khoang cach 2 bock nho hon 3 thi di thang
if ((map_x - this.pivot.x) <= 3 && (map_y - this.pivot.y) <= 3)
{
this.key = "1000"; // di thang
this.writeLogMsg("Key =======> " + this.key);
return;
}
else
{
// neu lon hon 3, re trai toi khi alpha = 90
// xong reset
if (this.Mycar.getalpha() < 90.0)
{
this.key = "0001";
this.writeLogMsg("Key =======> " + this.key);
return;
}
this.writeLogMsg("Old pivot value is: " + this.pivot.x + ", " + this.pivot.y);
this.pivot.x = map_x;
this.pivot.y = map_y;
this.writeLogMsg("New pivot value is: " + this.pivot.x + ", " + this.pivot.y);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment