Advertisement
MateuszJasek

Untitled

Feb 22nd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.38 KB | None | 0 0
  1. package ncdc.bow;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7.  
  8. import org.apache.commons.io.FileUtils;
  9. import org.newdawn.slick.util.pathfinding.AStarPathFinder;
  10. import org.newdawn.slick.util.pathfinding.Path;
  11. import org.springframework.boot.SpringApplication;
  12. import org.springframework.boot.autoconfigure.SpringBootApplication;
  13. import org.springframework.web.client.RestTemplate;
  14.  
  15. @SpringBootApplication
  16. public class BowApplication {
  17.  
  18.     public static void main(String[] args) {
  19.         SpringApplication.run(BowApplication.class, args);
  20.        
  21.         RestTemplate restTemplate = new RestTemplate();
  22.         Integer[][] rawMap = restTemplate.getForObject("http://bow.westeurope.cloudapp.azure.com:8080/getMap", Integer[][].class);
  23.        
  24.         Map map = new Map(rawMap);
  25.         map.printMap();
  26.        
  27.         AStarPathFinder asPathFinder = new AStarPathFinder(map, 1000, false);
  28.         Path path = asPathFinder.findPath(null, 0, 0, 7, 3);
  29.        
  30.         for (int i = 0; i < path.getLength(); i++) {
  31.             System.out.println(path.getStep(i).getX() + ", " + path.getStep(i).getY());
  32.         }
  33.        
  34. //      map.printMapWithPath(path);
  35.         map.printMapWithPathByWiktus(path);
  36.        
  37.         System.out.println(getMovesListFromPath(path));
  38.        
  39.         createHTMLFile(map);
  40.        
  41.     }
  42.    
  43.     public static List<String> getMovesListFromPath(Path path) {
  44.         ArrayList<String> movesList = new ArrayList<String>();
  45.        
  46.         int lastX = path.getX(0),
  47.             lastY = path.getY(0),
  48.             currentX = path.getX(0),
  49.             currentY = path.getY(0);
  50.        
  51.         for(int i = 0; i < path.getLength(); i++) {
  52.             currentX = path.getX(i);
  53.             currentY = path.getY(i);
  54.            
  55.             if(currentX - lastX == 1) {
  56.                 movesList.add("R");
  57.  
  58.             } else if(currentX - lastX == -1) {
  59.                 movesList.add("L");
  60.                
  61.             } else if(currentY - lastY == 1) {
  62.                 movesList.add("U");
  63.                
  64.             } else if(currentY - lastY == -1) {
  65.                 movesList.add("D");
  66.             }
  67.            
  68.             lastX = currentX;
  69.             lastY = currentY;
  70.         }
  71.        
  72.         return movesList;
  73.     }
  74.    
  75.     public static void createHTMLFile(Map map) {
  76.         map.printMap();
  77.         Integer[][] htmlMap = map.map;
  78.         htmlMap = map.getRotatedMap(htmlMap);
  79.        
  80.         for(int y = 0; y < htmlMap.length; y++ ) {
  81.             for(int x = 0; x < htmlMap[y].length; x++) {
  82.                 System.out.print(htmlMap[y][x] + " ");
  83.             }
  84.             System.out.println();
  85.         }
  86.         File htmlTemplateFile = new File("html/template.html");
  87.         File newHtmlFile = new File("html/new.html");
  88.         String htmlString = "";
  89.        
  90.         try {
  91.             htmlString = FileUtils.readFileToString(htmlTemplateFile);
  92.         } catch (IOException ex) {
  93.             ex.printStackTrace();
  94.         }
  95.        
  96.         StringBuilder strBuilder = new StringBuilder("");
  97.        
  98.         for(int y = 0; y < htmlMap.length; y++ ) {
  99.            
  100.             strBuilder.append("\n\t\t<tr>");
  101.            
  102.             for(int x = 0; x < htmlMap[y].length; x++) {
  103.                 strBuilder.append("\n\t\t\t<td><img src=\"");
  104.                
  105.                 switch(htmlMap[y][x]) {
  106.                 case 0: strBuilder.append("0"); break;
  107.                 case 1: strBuilder.append("1"); break;
  108.                 case 2: strBuilder.append("2"); break;
  109.                 case 3: strBuilder.append("3"); break;
  110.                 case 4: strBuilder.append("4"); break;
  111.                 case 5: strBuilder.append("5"); break;
  112.                 }
  113.                
  114.                 strBuilder.append(".png\"></td>");
  115.             }
  116.             strBuilder.append("\n\t\t</tr>\n\t");
  117.         }
  118.        
  119.         htmlString = htmlString.replace("$table", strBuilder.toString());
  120.        
  121.         try {
  122.             FileUtils.writeStringToFile(newHtmlFile, htmlString);
  123.         } catch(IOException ex) {
  124.             ex.printStackTrace();
  125.         }
  126.    
  127.        
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement