Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author Grzegorz
- */
- import robocode.Robot;
- import robocode.ScannedRobotEvent;
- import java.awt.geom.Point2D;
- import java.awt.*;
- import java.awt.event.MouseEvent;
- import java.util.Random;
- import java.util.ArrayList;
- class pole {
- public int x;
- public int y;
- public boolean zajete = false;
- public int G = 99999;
- public int F = 99999;
- public pole poprzednik = null;
- pole(int x, int y) {
- this.x = x;
- this.y = y;
- }
- int getDist(pole pozycja) {
- return Math.abs(pozycja.x - this.x) + Math.abs(pozycja.y - this.y);
- }
- }
- public class robocik extends Robot {
- double moveAmount; // How much to move
- private Random rand = new Random();
- private Point mouse_target = null;
- private double angle_to_target;
- private double dist;
- private ArrayList<Point> przeszkody, lista;
- private pole[][] siatka;
- private int licznik_trasy = 0;
- private boolean rysuje = false;
- private pole start = null;
- private pole obecny = null;
- private pole cel = null;
- private int siatka_w = 40;
- private int siatka_h = 40;
- private ArrayList<pole> zamkniete = new ArrayList();
- private ArrayList<pole> otwarte = new ArrayList();
- private ArrayList<pole> trasa = new ArrayList();
- public void run() {
- int borderRange = getSentryBorderSize() - 20;
- przeszkody = new ArrayList();
- lista = new ArrayList();
- siatka = new pole[(int) (getBattleFieldWidth() / siatka_w)][(int) (getBattleFieldHeight() / siatka_h)];
- for (int x = 0; x < (int) (getBattleFieldWidth() / siatka_w); x++) {
- for (int y = 0; y < (int) (getBattleFieldHeight() / siatka_w); y++) {
- siatka[x][y] = new pole(x, y);
- }
- }
- setAdjustRadarForRobotTurn(true);
- for (int i = 0; i < 36; i++) {
- turnRadarRight(10);
- }
- // ruch robota
- while (true) {
- if (!rysuje) {
- if (mouse_target == null) {
- if (licznik_trasy < trasa.size()) {
- mouse_target = new Point(trasa.get(trasa.size() - licznik_trasy - 1).x * siatka_w + (int) siatka_w / 2, trasa.get(trasa.size() - licznik_trasy - 1).y * siatka_h + (int) siatka_h / 2);
- licznik_trasy++;
- } else {
- this.doNothing();
- }
- } // if(rand.nextDouble()>0.9){
- // turnRight(rand.nextDouble()*180-90);
- // }
- // if ((getY() > getBattleFieldHeight() - borderRange && (getHeading() < 90 || getHeading() > 270)) || (getY() < borderRange && (getHeading() > 90 && getHeading() < 270))) {
- // if (rand.nextBoolean()) {
- // turnRight(rand.nextInt(90) + 90);
- // } else {
- // turnLeft(rand.nextInt(90) + 90);
- // }
- // ahead(10);
- // } else if ((getX() < borderRange && (getHeading() >= 180 && getHeading() <= 360)) || (getX() > getBattleFieldWidth() - borderRange && (getHeading() >= 0 && getHeading() <= 180))) {
- // if (rand.nextBoolean()) {
- // turnRight(rand.nextInt(90) + 90);
- // } else {
- // turnLeft(rand.nextInt(90) + 90);
- // }
- // ahead(10);
- // } else {
- // ahead(10);
- // }
- // }
- else {
- angle_to_target = -Math.toDegrees(Math.atan2(mouse_target.y - getY(), mouse_target.x - getX()));
- angle_to_target += 90;
- if (angle_to_target < 0) {
- angle_to_target += 360;
- }
- angle_to_target -= getHeading();
- if (angle_to_target > 180) {
- angle_to_target -= 360;
- } else if (angle_to_target < -180) {
- angle_to_target += 360;
- }
- dist = Math.sqrt((mouse_target.x - getX()) * (mouse_target.x - getX()) + (mouse_target.y - getY()) * (mouse_target.y - getY()));
- if (Math.abs(angle_to_target) > 1) {
- turnRight(angle_to_target);
- }
- if (dist > 10 && Math.abs(angle_to_target) <= 1) {
- ahead(10);
- } else if (Math.abs(angle_to_target) < 1) {
- mouse_target = null;
- }
- }
- } else if (!otwarte.isEmpty()) {
- obecny = null;
- for (pole p : otwarte) {
- if (obecny == null || obecny.F > p.F) {
- obecny = p;
- }
- }
- if (obecny.x == cel.x && obecny.y == cel.y) {
- trasa.add(obecny);
- while (obecny.poprzednik != null) {
- obecny = obecny.poprzednik;
- trasa.add(obecny);
- }
- rysuje=false;
- }
- otwarte.remove(obecny);
- zamkniete.add(obecny);
- int delta = 1;
- for (int dx = -delta; dx <= delta; dx += delta) {
- for (int dy = -delta; dy <= delta; dy += delta) {
- if (dy != 0 && dx != 0) {
- continue;
- }
- if (zamkniete.contains(siatka[obecny.x + dx][obecny.y + dy]) || siatka[obecny.x + dx][obecny.y + dy].zajete) {
- continue;
- }
- int tempG = obecny.G + 1;
- if (!otwarte.contains(siatka[obecny.x + dx][obecny.y + dy])) {
- otwarte.add(siatka[obecny.x + dx][obecny.y + dy]);
- } else if (siatka[obecny.x + dx][obecny.y + dy].G < tempG) {
- continue;
- }
- siatka[obecny.x + dx][obecny.y + dy].poprzednik = obecny;
- siatka[obecny.x + dx][obecny.y + dy].G = tempG;
- siatka[obecny.x + dx][obecny.y + dy].F = tempG + siatka[obecny.x + dx][obecny.y + dy].getDist(cel);
- }
- }
- ahead(0);
- }
- }
- }
- private double dist(Point p1,Point p2){
- return Math.sqrt((p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y));
- }
- public void onScannedRobot(ScannedRobotEvent e) { // wyniki z radaru
- double s_dist = e.getDistance();
- double s_radian = Math.toRadians((getHeading() + e.getBearing()) % 360);
- int delta = 22;
- int x = (int) (getX() + Math.sin(s_radian)*s_dist);
- int y = (int) (getY() + Math.cos(s_radian)*s_dist);
- boolean istnieje=false;
- for(int i=0;i<lista.size();i++){
- if(dist(lista.get(i),new Point(x,y))<delta ){
- istnieje=true;
- }
- }
- if(!istnieje){
- lista.add(new Point(x,y));
- for(int dx = -delta; dx <= delta; dx+=delta/4){
- for(int dy = -delta; dy <= delta; dy+=delta/4){
- siatka[(int)Math.floor((x+dx)/siatka_w)][(int)Math.floor((y+dy)/siatka_h)].zajete=true;
- }
- }
- }
- }
- public void onMouseClicked(MouseEvent e) {
- // mouse_target = new Point(e.getX(), e.getY());
- mouse_target=null;
- licznik_trasy=0;
- cel = new pole((int)Math.floor(e.getX()/siatka_w),(int)Math.floor(e.getY()/siatka_h));
- start = new pole((int)(getX()/siatka_w),(int)(getY()/siatka_h));
- obecny=null;
- zamkniete= new ArrayList();;
- otwarte = new ArrayList();
- trasa = new ArrayList();
- start.G=0;
- start.F=start.getDist(cel);
- otwarte.add(start);
- rysuje=true;
- }
- public void onPaint(Graphics2D g) {
- g.setColor(Color.RED);
- g.drawString("Współrzędne:", 20, 400);
- g.drawString("X: " + (int) getX() + " Y: " + (int) getY(), 20, 380);
- if(mouse_target!=null)
- g.drawString("m_X: " + (int) mouse_target.getX() + " m_Y: " + (int) mouse_target.getY(), 20, 370);
- g.drawString("Liczba przeszkód: "+przeszkody.size(),20,360);
- // ###########SIATKA
- g.setColor(Color.GREEN);
- for(int w=0; w<getBattleFieldWidth(); w+=siatka_w){
- g.drawLine(w, 0, w, (int)getBattleFieldHeight());
- }
- for(int h=0; h<getBattleFieldHeight(); h+=siatka_h){
- g.drawLine(0, h, (int)getBattleFieldWidth(), h);
- }
- g.setColor(Color.RED);
- for(pole p:trasa){
- if(p.poprzednik!=null)
- g.drawLine((int)(p.poprzednik.x*siatka_w+0.5*siatka_w), (int)(p.poprzednik.y*siatka_h+0.5*siatka_h), (int)(p.x*siatka_w+0.5*siatka_w), (int)(p.y*siatka_h+0.5*siatka_w));
- }
- if(cel!=null)
- g.fillRect(cel.x*(siatka_w), cel.y*(siatka_h), siatka_w, siatka_h);
- g.setColor(Color.RED);
- int p_w=10;
- for(Point punkt: lista)
- g.fillRect(punkt.x-p_w/2, punkt.y-p_w/2, p_w, p_w);
- g.setColor(new Color(120, 230, 171, 120));
- for(int x=0;x<(int)(getBattleFieldWidth()/siatka_w);x++){
- for(int y=0;y<(int)(getBattleFieldHeight()/siatka_h);y++){
- if(siatka[x][y].zajete){
- g.fillRect(x*(siatka_w), y*(siatka_h), siatka_w, siatka_h);
- }
- }
- }
- g.setColor(new Color(200, 0, 0, 120));
- for(pole o: otwarte){
- g.fillRect(o.x*(siatka_w), o.y*(siatka_h), siatka_w, siatka_h);
- }
- g.setColor(new Color(0, 0, 200, 120));
- for(pole o: zamkniete){
- g.fillRect(o.x*(siatka_w), o.y*(siatka_h), siatka_w, siatka_h);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement