Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. package com.jpo.demo;
  2. import com.jpo.demo.dataClasses.GeneratorConstatnts;
  3.  
  4. import java.lang.reflect.Array;
  5. import java.util.ArrayList;
  6. import java.util.Random;
  7.  
  8. public class Tools {
  9. public static int max_height = GeneratorConstatnts.MAX_NUMBER_OF_HEIGH;
  10. public static int max_width = GeneratorConstatnts.MAX_NUMBER_OF_WIDTH;
  11. public static int max_platforms = GeneratorConstatnts.MAX_NUMBER_OF_PLATFORMS;
  12. public static int min_x_dist = GeneratorConstatnts.PLATFORM_GEN_MIN_X_DIST;
  13. public static int min_y_dist = GeneratorConstatnts.PLATFORM_GEN_MIN_Y_DIST;
  14.  
  15.  
  16. public static String generatePlatforms(int platforms_count){
  17. ArrayList<ArrayList> platforms = new ArrayList<ArrayList>();
  18.  
  19. for(int i = 0; i < platforms_count; i++){
  20. ArrayList tmp_platform = randomPlatform();
  21. int rep = -1;
  22. while((rep = platforms.indexOf(tmp_platform))!= -1){ //if generated the same platform as before try again
  23. tmp_platform = randomPlatform(platforms.get(rep));
  24. };
  25. platforms.set(i, tmp_platform);
  26. }
  27.  
  28. int[] invalid_platform = null;
  29. while ((invalid_platform = checkPlatforms(platforms))!= null){
  30. int[] tmp = randomPlatform(invalid_platform);
  31. int invalid_platform_idx = findArrayInArray(platforms, invalid_platform);
  32. platforms[invalid_platform_idx] = tmp;
  33. }
  34.  
  35. return platformsToGrzesiekFrontNotion(platforms);
  36. }
  37.  
  38. public static ArrayList randomPlatform(ArrayList exclude){
  39. ArrayList platform = new ArrayList();
  40. Random rd = new Random();
  41.  
  42. do{
  43. platform.set(0, 1 + rd.nextInt(max_width));
  44. platform.set(1, 1 + rd.nextInt(max_height));
  45. }while (platform.equals(exclude));
  46.  
  47. return platform;
  48.  
  49. } //refac
  50.  
  51. public static ArrayList randomPlatform(){
  52. ArrayList platform = new ArrayList();
  53. Random rd = new Random();
  54. platform.set(0, 1 + rd.nextInt(max_width));
  55. platform.set(1, 1 + rd.nextInt(max_height));
  56.  
  57. return platform;
  58.  
  59. } //refac
  60.  
  61. public static int[] checkPlatforms(int[][] platforms){
  62. ArrayList flawed_platform = findPlatformDistandFromPlatform(platforms, min_x_dist, min_y_dist);
  63. if(flawed_platform != null)
  64. return flawed_platform;
  65.  
  66. return null;
  67. }
  68.  
  69. public static int[] isArrayInArray(int[][] search_array, int[] pattern){
  70. for (int[] cur_array:search_array) {
  71. if(java.util.Arrays.equals(cur_array, pattern))
  72. return pattern;
  73. }
  74. return null;
  75. }
  76.  
  77. public static int findArrayInArray(int[][] search_array, int[] pattern){
  78. for (int i = 0; i < search_array.length; i++) {
  79. if(java.util.Arrays.equals(search_array[i], pattern))
  80. return i;
  81. }
  82. return -1;
  83. }
  84.  
  85. public static int[] findPlatformDistandFromPlatform(ArrayList<ArrayList<Integer>> platforms, int x_distance, int y_distance) {
  86. /*Look for any platform which is closer than {x OR y}_distance [Grzegorz front-end block unit]
  87. from another platform
  88. @return: int[] found platform OR null if not found
  89. */
  90.  
  91. for (ArrayList<Integer> platform:platforms) {
  92. for (ArrayList<Integer> rem_platform:platforms) {
  93. if(platform == rem_platform)
  94. continue; //skip if looks for itself
  95. if(Math.abs((platform.get(0) - rem_platform.get(0)) < x_distance)
  96. return rem_platform;
  97. if(Math.abs(platform[1] - rem_platform[1]) < y_distance)
  98. return rem_platform;
  99. }
  100. }
  101. return null;
  102. }
  103.  
  104. public static String platformsToGrzesiekFrontNotion(int[][] platforms){
  105. StringBuilder g_notion = new StringBuilder("");
  106.  
  107. for (int[] platform:platforms) {
  108. g_notion.append(platform[0]);
  109. g_notion.append(",");
  110. g_notion.append(platform[1]);
  111. g_notion.append("|");
  112. }
  113.  
  114. g_notion.deleteCharAt(g_notion.lastIndexOf("|"));
  115. return g_notion.toString();
  116. }push
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement