Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.74 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6.  
  7. /**
  8. *
  9. * @author Grzegorz
  10. */
  11. import robocode.Robot;
  12. import robocode.ScannedRobotEvent;
  13.  
  14. import java.awt.geom.Point2D;
  15. import java.awt.*;
  16. import java.awt.event.MouseEvent;
  17. import java.util.Random;
  18. import java.util.ArrayList;
  19.  
  20. class pole {
  21.  
  22. public int x;
  23. public int y;
  24. public boolean zajete = false;
  25. public int G = 99999;
  26. public int F = 99999;
  27. public pole poprzednik = null;
  28.  
  29. pole(int x, int y) {
  30. this.x = x;
  31. this.y = y;
  32. }
  33.  
  34. int getDist(pole pozycja) {
  35. return Math.abs(pozycja.x - this.x) + Math.abs(pozycja.y - this.y);
  36. }
  37.  
  38. }
  39.  
  40. public class robocik extends Robot {
  41.  
  42. double moveAmount; // How much to move
  43. private Random rand = new Random();
  44. private Point mouse_target = null;
  45. private double angle_to_target;
  46. private double dist;
  47. private ArrayList<Point> przeszkody, lista;
  48. private pole[][] siatka;
  49.  
  50. private int licznik_trasy = 0;
  51. private boolean rysuje = false;
  52. private pole start = null;
  53. private pole obecny = null;
  54. private pole cel = null;
  55.  
  56. private int siatka_w = 40;
  57. private int siatka_h = 40;
  58.  
  59. private ArrayList<pole> zamkniete = new ArrayList();
  60. private ArrayList<pole> otwarte = new ArrayList();
  61. private ArrayList<pole> trasa = new ArrayList();
  62.  
  63. public void run() {
  64.  
  65. int borderRange = getSentryBorderSize() - 20;
  66. przeszkody = new ArrayList();
  67. lista = new ArrayList();
  68. siatka = new pole[(int) (getBattleFieldWidth() / siatka_w)][(int) (getBattleFieldHeight() / siatka_h)];
  69. for (int x = 0; x < (int) (getBattleFieldWidth() / siatka_w); x++) {
  70. for (int y = 0; y < (int) (getBattleFieldHeight() / siatka_w); y++) {
  71. siatka[x][y] = new pole(x, y);
  72.  
  73. }
  74. }
  75.  
  76. setAdjustRadarForRobotTurn(true);
  77. for (int i = 0; i < 36; i++) {
  78. turnRadarRight(10);
  79. }
  80.  
  81. // ruch robota
  82. while (true) {
  83. if (!rysuje) {
  84. if (mouse_target == null) {
  85. if (licznik_trasy < trasa.size()) {
  86. 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);
  87. licznik_trasy++;
  88. } else {
  89.  
  90. this.doNothing();
  91. }
  92. } // if(rand.nextDouble()>0.9){
  93. // turnRight(rand.nextDouble()*180-90);
  94. // }
  95. // if ((getY() > getBattleFieldHeight() - borderRange && (getHeading() < 90 || getHeading() > 270)) || (getY() < borderRange && (getHeading() > 90 && getHeading() < 270))) {
  96. // if (rand.nextBoolean()) {
  97. // turnRight(rand.nextInt(90) + 90);
  98. // } else {
  99. // turnLeft(rand.nextInt(90) + 90);
  100. // }
  101. // ahead(10);
  102. // } else if ((getX() < borderRange && (getHeading() >= 180 && getHeading() <= 360)) || (getX() > getBattleFieldWidth() - borderRange && (getHeading() >= 0 && getHeading() <= 180))) {
  103. // if (rand.nextBoolean()) {
  104. // turnRight(rand.nextInt(90) + 90);
  105. // } else {
  106. // turnLeft(rand.nextInt(90) + 90);
  107. // }
  108. // ahead(10);
  109. // } else {
  110. // ahead(10);
  111. // }
  112. // }
  113. else {
  114. angle_to_target = -Math.toDegrees(Math.atan2(mouse_target.y - getY(), mouse_target.x - getX()));
  115. angle_to_target += 90;
  116. if (angle_to_target < 0) {
  117. angle_to_target += 360;
  118. }
  119. angle_to_target -= getHeading();
  120. if (angle_to_target > 180) {
  121. angle_to_target -= 360;
  122. } else if (angle_to_target < -180) {
  123. angle_to_target += 360;
  124. }
  125.  
  126. dist = Math.sqrt((mouse_target.x - getX()) * (mouse_target.x - getX()) + (mouse_target.y - getY()) * (mouse_target.y - getY()));
  127. if (Math.abs(angle_to_target) > 1) {
  128. turnRight(angle_to_target);
  129. }
  130.  
  131. if (dist > 10 && Math.abs(angle_to_target) <= 1) {
  132. ahead(10);
  133. } else if (Math.abs(angle_to_target) < 1) {
  134. mouse_target = null;
  135. }
  136.  
  137. }
  138. } else if (!otwarte.isEmpty()) {
  139.  
  140. obecny = null;
  141. for (pole p : otwarte) {
  142. if (obecny == null || obecny.F > p.F) {
  143. obecny = p;
  144. }
  145. }
  146. if (obecny.x == cel.x && obecny.y == cel.y) {
  147. trasa.add(obecny);
  148. while (obecny.poprzednik != null) {
  149. obecny = obecny.poprzednik;
  150. trasa.add(obecny);
  151.  
  152. }
  153.  
  154. rysuje=false;
  155. }
  156. otwarte.remove(obecny);
  157. zamkniete.add(obecny);
  158. int delta = 1;
  159. for (int dx = -delta; dx <= delta; dx += delta) {
  160. for (int dy = -delta; dy <= delta; dy += delta) {
  161. if (dy != 0 && dx != 0) {
  162. continue;
  163. }
  164. if (zamkniete.contains(siatka[obecny.x + dx][obecny.y + dy]) || siatka[obecny.x + dx][obecny.y + dy].zajete) {
  165. continue;
  166.  
  167. }
  168. int tempG = obecny.G + 1;
  169.  
  170. if (!otwarte.contains(siatka[obecny.x + dx][obecny.y + dy])) {
  171. otwarte.add(siatka[obecny.x + dx][obecny.y + dy]);
  172. } else if (siatka[obecny.x + dx][obecny.y + dy].G < tempG) {
  173. continue;
  174. }
  175. siatka[obecny.x + dx][obecny.y + dy].poprzednik = obecny;
  176. siatka[obecny.x + dx][obecny.y + dy].G = tempG;
  177. siatka[obecny.x + dx][obecny.y + dy].F = tempG + siatka[obecny.x + dx][obecny.y + dy].getDist(cel);
  178. }
  179.  
  180. }
  181. ahead(0);
  182.  
  183. }
  184.  
  185. }
  186.  
  187.  
  188. }
  189. private double dist(Point p1,Point p2){
  190. return Math.sqrt((p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y));
  191. }
  192.  
  193. public void onScannedRobot(ScannedRobotEvent e) { // wyniki z radaru
  194. double s_dist = e.getDistance();
  195. double s_radian = Math.toRadians((getHeading() + e.getBearing()) % 360);
  196. int delta = 22;
  197. int x = (int) (getX() + Math.sin(s_radian)*s_dist);
  198. int y = (int) (getY() + Math.cos(s_radian)*s_dist);
  199. boolean istnieje=false;
  200. for(int i=0;i<lista.size();i++){
  201. if(dist(lista.get(i),new Point(x,y))<delta ){
  202. istnieje=true;
  203. }
  204. }
  205. if(!istnieje){
  206. lista.add(new Point(x,y));
  207.  
  208. for(int dx = -delta; dx <= delta; dx+=delta/4){
  209. for(int dy = -delta; dy <= delta; dy+=delta/4){
  210. siatka[(int)Math.floor((x+dx)/siatka_w)][(int)Math.floor((y+dy)/siatka_h)].zajete=true;
  211. }
  212. }
  213. }
  214.  
  215. }
  216.  
  217. public void onMouseClicked(MouseEvent e) {
  218. // mouse_target = new Point(e.getX(), e.getY());
  219. mouse_target=null;
  220. licznik_trasy=0;
  221. cel = new pole((int)Math.floor(e.getX()/siatka_w),(int)Math.floor(e.getY()/siatka_h));
  222. start = new pole((int)(getX()/siatka_w),(int)(getY()/siatka_h));
  223. obecny=null;
  224. zamkniete= new ArrayList();;
  225. otwarte = new ArrayList();
  226. trasa = new ArrayList();
  227.  
  228. start.G=0;
  229. start.F=start.getDist(cel);
  230. otwarte.add(start);
  231. rysuje=true;
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240. }
  241.  
  242. public void onPaint(Graphics2D g) {
  243.  
  244. g.setColor(Color.RED);
  245. g.drawString("Współrzędne:", 20, 400);
  246. g.drawString("X: " + (int) getX() + " Y: " + (int) getY(), 20, 380);
  247. if(mouse_target!=null)
  248. g.drawString("m_X: " + (int) mouse_target.getX() + " m_Y: " + (int) mouse_target.getY(), 20, 370);
  249. g.drawString("Liczba przeszkód: "+przeszkody.size(),20,360);
  250.  
  251. // ###########SIATKA
  252. g.setColor(Color.GREEN);
  253. for(int w=0; w<getBattleFieldWidth(); w+=siatka_w){
  254. g.drawLine(w, 0, w, (int)getBattleFieldHeight());
  255. }
  256. for(int h=0; h<getBattleFieldHeight(); h+=siatka_h){
  257. g.drawLine(0, h, (int)getBattleFieldWidth(), h);
  258. }
  259.  
  260. g.setColor(Color.RED);
  261. for(pole p:trasa){
  262. if(p.poprzednik!=null)
  263. 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));
  264.  
  265. }
  266. if(cel!=null)
  267. g.fillRect(cel.x*(siatka_w), cel.y*(siatka_h), siatka_w, siatka_h);
  268.  
  269.  
  270.  
  271. g.setColor(Color.RED);
  272. int p_w=10;
  273. for(Point punkt: lista)
  274. g.fillRect(punkt.x-p_w/2, punkt.y-p_w/2, p_w, p_w);
  275.  
  276.  
  277. g.setColor(new Color(120, 230, 171, 120));
  278. for(int x=0;x<(int)(getBattleFieldWidth()/siatka_w);x++){
  279. for(int y=0;y<(int)(getBattleFieldHeight()/siatka_h);y++){
  280. if(siatka[x][y].zajete){
  281. g.fillRect(x*(siatka_w), y*(siatka_h), siatka_w, siatka_h);
  282. }
  283. }
  284. }
  285. g.setColor(new Color(200, 0, 0, 120));
  286. for(pole o: otwarte){
  287. g.fillRect(o.x*(siatka_w), o.y*(siatka_h), siatka_w, siatka_h);
  288. }
  289. g.setColor(new Color(0, 0, 200, 120));
  290. for(pole o: zamkniete){
  291. g.fillRect(o.x*(siatka_w), o.y*(siatka_h), siatka_w, siatka_h);
  292. }
  293. }
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement