Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. package WSGenerator;
  2.  
  3. import java.util.List;
  4. import java.util.Random;
  5.  
  6. public class SopaLetrasGenerator {
  7. private List<String> palavras;
  8. private char[][] grelha;
  9. private int size;
  10.  
  11. public SopaLetrasGenerator(List<String> palavras) {
  12. this.palavras = palavras;
  13. }
  14.  
  15. public void criarGrelha() {
  16. calcSize();
  17. criarGrelhaVazia();
  18. palavras.forEach(e -> adicionarPalavra(e));
  19. encherGrelha();
  20. imprimirGrelha();
  21. }
  22.  
  23. //vamos mudar isto
  24. private void calcSize() {
  25. int length = palavras.get(0).length();
  26.  
  27. for(int i = 1; i < palavras.size(); i++) {
  28. if(palavras.get(i).length() > length)
  29. length = palavras.get(i).length();
  30. }
  31.  
  32. size = length * 2;
  33. }
  34.  
  35. private void criarGrelhaVazia() {
  36. grelha = new char[size][size];
  37.  
  38. for(int i = 0; i < size; i++) {
  39. for(int j = 0; j < size; j++) {
  40. grelha[i][j] = ' ';
  41. }
  42. }
  43. }
  44.  
  45. private boolean adicionarPalavra(String word) {
  46. word = word.toUpperCase();
  47.  
  48. char[][] original = new char[size][size];
  49. for(int i=0; i<size; i++)
  50. for(int j=0; j<size; j++)
  51. original[i][j] = grelha[i][j];
  52.  
  53. Random r = new Random();
  54.  
  55. for(int tries=0; tries<100; tries++) {
  56. Direction direction = Direction.randomDirection();
  57. int wordLength = word.length();
  58.  
  59. int linha = 0, col = 0;
  60.  
  61. if(direction.equals(Direction.down) || direction.equals(Direction.down) || direction.equals(Direction.downright)) {
  62. linha = r.nextInt(size - wordLength);
  63. col = r.nextInt(size - wordLength);
  64. }
  65.  
  66. if(direction.equals(Direction.up) || direction.equals(Direction.left) || direction.equals(Direction.upleft)) {
  67. linha = r.nextInt(size - wordLength) + wordLength;
  68. col = r.nextInt(size - wordLength) + wordLength;
  69. }
  70.  
  71. if(direction.equals(Direction.downleft)) {
  72. linha = r.nextInt(size - wordLength);
  73. col = r.nextInt(size - wordLength) + wordLength;
  74. }
  75.  
  76. if(direction.equals(Direction.upright)) {
  77. linha = r.nextInt(size - wordLength) + wordLength;
  78. col = r.nextInt(size - wordLength);
  79. }
  80.  
  81. int i;
  82. for(i=0; i<word.length(); i++) {
  83. if(grelha[linha][col] == ' ' || grelha[linha][col] == word.charAt(i)) {
  84. grelha[linha][col] = word.charAt(i);
  85.  
  86. if(direction.equals(Direction.up)) linha--;
  87. if(direction.equals(Direction.down)) linha++;
  88. if(direction.equals(Direction.right)) col++;
  89. if(direction.equals(Direction.left)) col--;
  90. if(direction.equals(Direction.downleft)) { linha++; col--; }
  91. if(direction.equals(Direction.downright)) { linha++; col++; }
  92. if(direction.equals(Direction.upleft)) { linha--; col--; }
  93. if(direction.equals(Direction.upright)) { linha--; col++; }
  94. } else {
  95. for(int j=i; j>0; j--) {
  96. if(direction.equals(Direction.up)) linha++;
  97. if(direction.equals(Direction.down)) linha--;
  98. if(direction.equals(Direction.right)) col--;
  99. if(direction.equals(Direction.left)) col++;
  100. if(direction.equals(Direction.downleft)) { linha--; col++; }
  101. if(direction.equals(Direction.downright)) { linha--; col--; }
  102. if(direction.equals(Direction.upleft)) { linha++; col++; }
  103. if(direction.equals(Direction.upright)) { linha++; col--; }
  104.  
  105. grelha[linha][col] = original[linha][col];
  106. }
  107. break;
  108. }
  109. }
  110. if(--i > 0) return true;
  111. }
  112. return false;
  113. }
  114.  
  115. private void encherGrelha() {
  116. RandLetter l = new RandLetter();
  117.  
  118. for(int i=0; i<size; i++) {
  119. for(int j=0; j<size; j++) {
  120. if(grelha[i][j] == ' ') {
  121. grelha[i][j] = l.nextLetter();
  122. }
  123. }
  124. }
  125. }
  126.  
  127. public void imprimirGrelha() {
  128. for(int i = 0; i < size; i++) {
  129. for(int j = 0; j < size; j++) {
  130. System.out.print(grelha[i][j]);
  131. }
  132. System.out.println();
  133. }
  134. }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement