Skip to content

Commit

Permalink
Implemented functionality to randomly place words on the grid in the …
Browse files Browse the repository at this point in the history
…GridField class
  • Loading branch information
2onefan2 committed Apr 28, 2024
1 parent 327c377 commit 28d2ced
Showing 1 changed file with 45 additions and 7 deletions.
52 changes: 45 additions & 7 deletions src/main/java/uta/cse3310/GridField.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@

public class GridField {
private char[][] grid;
private ArrayList<String> wordList=new ArrayList<String>();
private ArrayList<String> wordList;
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) <-no need -muktar
this.remainingWords = wordList.size();
generateGrid(5); // Initialize grid with default size (e.g., 5x5)
}

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() {
return grid;
}
Expand All @@ -36,7 +37,6 @@ 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,7 +74,6 @@ 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,4 +85,43 @@ public void displayGrid() {
System.out.println();
}
}

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;
}
return false;
}
}

0 comments on commit 28d2ced

Please sign in to comment.