Skip to content

Commit

Permalink
Updated
Browse files Browse the repository at this point in the history
  • Loading branch information
2onefan2 committed Apr 29, 2024
1 parent 46ad0af commit a4eb795
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
15 changes: 11 additions & 4 deletions src/main/java/uta/cse3310/GridField.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package uta.cse3310;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;

public class GridField {
Expand Down Expand Up @@ -46,8 +47,7 @@ public int getRemainingWords() {
}

public void revealWord(String word) {
if (wordList.contains(word)) {
wordList.remove(word);
if (wordList.remove(word)) {
remainingWords--;
}
}
Expand Down Expand Up @@ -76,9 +76,12 @@ public void displayGrid() {

public void placeRandomWords() {
Random random = new Random();
for (String word : wordList) {
ArrayList<String> remainingWords = new ArrayList<>(wordList); // Create a copy of the wordList to track remaining words
for (int i = 0; i < wordList.size(); i++) {
String word = remainingWords.get(random.nextInt(remainingWords.size())); // Select a random word from the remaining words
boolean wordPlaced = false;
while (!wordPlaced) {
int attempt = 0;
while (!wordPlaced && attempt < 100) { // Limit the number of attempts to prevent infinite loops
int len = word.length();
int row = random.nextInt(grid.length);
int col = random.nextInt(grid[0].length);
Expand All @@ -87,6 +90,10 @@ public void placeRandomWords() {
addWord(word, row, col, direction);
wordPlaced = true;
}
attempt++;
}
if (wordPlaced) {
remainingWords.remove(word); // Remove the word from remainingWords if successfully placed
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/test/java/uta/cse3310/GridFieldTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,20 @@ public void testPlaceRandomWords() {

// Create a grid field
GridField gridField = new GridField(wordList);
System.out.println("Finished");

// Place random words on the grid
gridField.placeRandomWords();
System.out.println("Finished\n"+gridField.getGrid());

// Get the grid from the grid field
char[][] grid = gridField.getGrid();

// Check if the grid is not null
assertNotNull(grid);

// Check if the grid size is within a reasonable range
assertTrue(grid.length >= 5 && grid.length <= 10);
assertTrue(grid[0].length >= 5 && grid[0].length <= 10);

// Check if all words from the word list are placed on the grid
for (String word : wordList) {
assertTrue(gridField.checkWord(word));
Expand Down

0 comments on commit a4eb795

Please sign in to comment.