Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package WSGenerator;
- import java.util.List;
- import java.util.Random;
- public class SopaLetrasGenerator {
- private List<String> palavras;
- private char[][] grelha;
- private int size;
- public SopaLetrasGenerator(List<String> palavras) {
- this.palavras = palavras;
- }
- public void criarGrelha() {
- calcSize();
- criarGrelhaVazia();
- palavras.forEach(e -> adicionarPalavra(e));
- encherGrelha();
- imprimirGrelha();
- }
- //vamos mudar isto
- private void calcSize() {
- int length = palavras.get(0).length();
- for(int i = 1; i < palavras.size(); i++) {
- if(palavras.get(i).length() > length)
- length = palavras.get(i).length();
- }
- size = length * 2;
- }
- private void criarGrelhaVazia() {
- grelha = new char[size][size];
- for(int i = 0; i < size; i++) {
- for(int j = 0; j < size; j++) {
- grelha[i][j] = ' ';
- }
- }
- }
- private boolean adicionarPalavra(String word) {
- word = word.toUpperCase();
- char[][] original = new char[size][size];
- for(int i=0; i<size; i++)
- for(int j=0; j<size; j++)
- original[i][j] = grelha[i][j];
- Random r = new Random();
- for(int tries=0; tries<100; tries++) {
- Direction direction = Direction.randomDirection();
- int wordLength = word.length();
- int linha = 0, col = 0;
- if(direction.equals(Direction.down) || direction.equals(Direction.down) || direction.equals(Direction.downright)) {
- linha = r.nextInt(size - wordLength);
- col = r.nextInt(size - wordLength);
- }
- if(direction.equals(Direction.up) || direction.equals(Direction.left) || direction.equals(Direction.upleft)) {
- linha = r.nextInt(size - wordLength) + wordLength;
- col = r.nextInt(size - wordLength) + wordLength;
- }
- if(direction.equals(Direction.downleft)) {
- linha = r.nextInt(size - wordLength);
- col = r.nextInt(size - wordLength) + wordLength;
- }
- if(direction.equals(Direction.upright)) {
- linha = r.nextInt(size - wordLength) + wordLength;
- col = r.nextInt(size - wordLength);
- }
- int i;
- for(i=0; i<word.length(); i++) {
- if(grelha[linha][col] == ' ' || grelha[linha][col] == word.charAt(i)) {
- grelha[linha][col] = word.charAt(i);
- if(direction.equals(Direction.up)) linha--;
- if(direction.equals(Direction.down)) linha++;
- if(direction.equals(Direction.right)) col++;
- if(direction.equals(Direction.left)) col--;
- if(direction.equals(Direction.downleft)) { linha++; col--; }
- if(direction.equals(Direction.downright)) { linha++; col++; }
- if(direction.equals(Direction.upleft)) { linha--; col--; }
- if(direction.equals(Direction.upright)) { linha--; col++; }
- } else {
- for(int j=i; j>0; j--) {
- if(direction.equals(Direction.up)) linha++;
- if(direction.equals(Direction.down)) linha--;
- if(direction.equals(Direction.right)) col--;
- if(direction.equals(Direction.left)) col++;
- if(direction.equals(Direction.downleft)) { linha--; col++; }
- if(direction.equals(Direction.downright)) { linha--; col--; }
- if(direction.equals(Direction.upleft)) { linha++; col++; }
- if(direction.equals(Direction.upright)) { linha++; col--; }
- grelha[linha][col] = original[linha][col];
- }
- break;
- }
- }
- if(--i > 0) return true;
- }
- return false;
- }
- private void encherGrelha() {
- RandLetter l = new RandLetter();
- for(int i=0; i<size; i++) {
- for(int j=0; j<size; j++) {
- if(grelha[i][j] == ' ') {
- grelha[i][j] = l.nextLetter();
- }
- }
- }
- }
- public void imprimirGrelha() {
- for(int i = 0; i < size; i++) {
- for(int j = 0; j < size; j++) {
- System.out.print(grelha[i][j]);
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement