Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. package ex;
  2.  
  3. import java.awt.Point;
  4. import java.util.Objects;
  5. import java.util.Random;
  6.  
  7. public class StashTab
  8. {
  9. public Item[][] items;
  10. public Point lastChaosPosition = new Point( 0, 0 );
  11.  
  12. public String itemNames[] = { "item", "item", "item", "item", "item" };
  13.  
  14. public String randomName()
  15. {
  16. String r = "";
  17. Random rd = new Random();
  18.  
  19. int randomInRange = rd.nextInt( itemNames.length ) ;
  20. r = itemNames[ randomInRange ];
  21.  
  22. return r;
  23. }
  24.  
  25. public StashTab()
  26. {
  27. super();
  28. items = new Item[ 12 ][ 12 ];
  29.  
  30. for( int y = 0; y < 12; y++ )
  31. {
  32. for( int x = 0; x < 12; x++ )
  33. {
  34. ItemType randomType = ItemType.values()[(int)(Math.random()*ItemType.values().length)];
  35. String name = "";
  36.  
  37. if( Objects.equals( randomType, ItemType.chaos ) )
  38. name = "chaos";
  39. else
  40. name = randomName();
  41.  
  42. name = name + " : " + String.valueOf( y ) + ":" + String.valueOf( x );
  43. items[ y ][ x ] = new Item( name, randomType );
  44. System.out.println( items[ y ][ x ].name );
  45. }
  46. }
  47. }
  48.  
  49. public Point findNextChaos()
  50. {
  51. Point pos = new Point( -1, 0 );
  52.  
  53. boolean found = false;
  54.  
  55.  
  56. for( int y = lastChaosPosition.y; y < 12; y++ )
  57. {
  58. for( int x = lastChaosPosition.x; x < 12; x++ )
  59. {
  60. lastChaosPosition.x = 0;
  61. if( Objects.equals( items[ y ][ x ].type, ItemType.chaos ) )
  62. {
  63. found = true;
  64. pos.x = x;
  65. pos.y = y;
  66.  
  67. lastChaosPosition.y = y;
  68.  
  69. if( x < 11 )
  70. lastChaosPosition.x = x +1;
  71. else
  72. {
  73. lastChaosPosition.x = 0;
  74. lastChaosPosition.y = y +1;
  75. }
  76.  
  77.  
  78. System.out.println( String.valueOf( y ) + " : " + String.valueOf( x ) );
  79. break;
  80. }
  81.  
  82. }
  83. if( found )
  84. break;
  85. }
  86.  
  87. return pos;
  88. }
  89.  
  90.  
  91.  
  92.  
  93.  
  94. public void sortIt()
  95. {
  96. for( int y = 0; y < 12; y++ )
  97. {
  98. for( int x = 0; x < 12; x++ )
  99. {
  100. if( Objects.equals( items[ y ][ x ].type, ItemType.chaos ) == false )
  101. {
  102. Point nextChaos = findNextChaos();
  103. if( nextChaos.x != -1 )
  104. {
  105. Item objNotChaos = items[ y ][ x ];
  106. items[ y ][ x ] = items[ nextChaos.y ][ nextChaos.x ];
  107. items[ nextChaos.y ][ nextChaos.x ] = objNotChaos;
  108. }
  109. }
  110. }
  111. }
  112.  
  113. }
  114.  
  115.  
  116. public void showTheCurrentTabContainings()
  117. {
  118. for( int y = 0; y < 12; y++ )
  119. {
  120. for( int x = 0; x < 12; x++ )
  121. {
  122. System.out.println( items[ y ][ x ].name );
  123. }
  124. }
  125. }
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement