Skip to content

Commit

Permalink
Add functionality to randomly place words on the grid
Browse files Browse the repository at this point in the history
  • Loading branch information
2onefan2 committed Apr 28, 2024
1 parent 30e722d commit ff14eba
Showing 1 changed file with 11 additions and 37 deletions.
48 changes: 11 additions & 37 deletions src/main/java/uta/cse3310/GridField.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@

public class GridField {
private char[][] grid;
private ArrayList<String> wordList;
private ArrayList<String> wordList = new ArrayList<>();
private int remainingWords;

public GridField(ArrayList<String> wordList) {
this.wordList = wordList;
this.remainingWords = wordList.size();
generateGrid(5); // Initialize grid with default size (e.g., 5x5)
generateGrid(5); // Initialize grid with default size (e.g., 5x5) <-no need -muktar
}

public GridField() {
this.wordList = WordList.getWordList("Data/words");
this.wordList = WordList.updatedWordList(wordList);
this.remainingWords = wordList.size();
generateGrid(5); // Initialize grid with default size (e.g., 5x5)

}

public char[][] getGrid() {
Expand All @@ -37,6 +36,7 @@ public void generateGrid(int gridSize) {
grid[i][j] = (char) ('A' + random.nextInt(26)); // Randomly fill grid with alphabets
}
}
//this.addWord("group",0,0,Direction.Directions.HORIZONTAL); was testing selection function
}

public boolean checkWord(String word) {
Expand Down Expand Up @@ -74,6 +74,7 @@ public void addWord(String word, int row, int column, Direction.Directions direc
column += dc;
}
wordList.add(word);
//remainingWords starts off as the size of the updated wordlist
remainingWords++;
}

Expand All @@ -86,42 +87,15 @@ public void displayGrid() {
}
}

// Method to randomly place words from the word list on the grid
public void placeRandomWords() {
Random random = new Random();
for (String word : wordList) {
boolean added = false;
while (!added) {
int row = random.nextInt(grid.length);
int col = random.nextInt(grid[0].length);
Direction.Directions dir = Direction.Directions.values()[random.nextInt(Direction.Directions.values().length)];
if (canPlaceWord(word, row, col, dir)) {
addWord(word, row, col, dir);
added = true;
}
}
}
}

private boolean canPlaceWord(String word, int row, int col, Direction.Directions direction) {
int len = word.length();
int dr = 0, dc = 0;
switch (direction) {
case HORIZONTAL:
dc = 1;
break;
case VERTICAL:
dr = 1;
break;
case DIAGONAL:
dr = 1;
dc = 1;
break;
}
int endRow = row + dr * (len - 1);
int endCol = col + dc * (len - 1);
if (endRow >= 0 && endRow < grid.length && endCol >= 0 && endCol < grid[0].length) {
return true;
int len = word.length();
int row = random.nextInt(grid.length);
int col = random.nextInt(grid[0].length);
Direction.Directions direction = Direction.Directions.values()[random.nextInt(Direction.Directions.values().length)];
addWord(word, row, col, direction);
}
return false;
}
}

0 comments on commit ff14eba

Please sign in to comment.