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 d822692 commit de570c9
Showing 1 changed file with 114 additions and 0 deletions.
114 changes: 114 additions & 0 deletions src/test/java/uta/cse3310/GridField.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package uta.cse3310;

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

public class GridField {
private char[][] grid;
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)
}

public GridField() {
this.wordList = WordList.getWordList("Data/words");
this.wordList = WordList.updatedWordList(wordList);
}

public char[][] getGrid() {
return grid;
}

public ArrayList<String> getWordList() {
return wordList;
}

public void generateGrid(int gridSize) {
grid = new char[gridSize][gridSize];
Random random = new Random();
for (int i = 0; i < gridSize; i++) {
for (int j = 0; j < gridSize; j++) {
grid[i][j] = (char) ('A' + random.nextInt(26)); // Randomly fill grid with alphabets
}
}
}

public boolean checkWord(String word) {
return wordList.contains(word);
}

public int getRemainingWords() {
return remainingWords;
}

public void revealWord(String word) {
if (wordList.remove(word)) {
remainingWords--;
}
}

public void addWord(String word, int row, int column, Direction.Directions direction) {
int len = word.length();
int dr = direction.getRowIncrement();
int dc = direction.getColumnIncrement();
for (int i = 0; i < len; i++) {
grid[row][column] = word.charAt(i);
row += dr;
column += dc;
}
wordList.add(word);
remainingWords++;
}

public void displayGrid() {
for (char[] row : grid) {
for (char cell : row) {
System.out.print(cell + " ");
}
System.out.println();
}
}

public void placeRandomWords() {
Random random = new Random();
for (String word : wordList) {
boolean wordPlaced = false;
int attemptCount = 0; // Track the number of attempts to place the word
while (!wordPlaced) {
attemptCount++;
if (attemptCount > 100) {
// Add a safeguard to prevent infinite loops; abort if attempts exceed a certain threshold
System.out.println("Failed to place word: " + word);
break;
}

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)];
if (canPlaceWord(word, row, col, direction)) {
addWord(word, row, col, direction);
wordPlaced = true;
}
}
}
}

private boolean canPlaceWord(String word, int row, int column, Direction.Directions direction) {
int len = word.length();
int dr = direction.getRowIncrement();
int dc = direction.getColumnIncrement();
for (int i = 0; i < len; i++) {
if (row < 0 || row >= grid.length || column < 0 || column >= grid[0].length || grid[row][column] != 0) {
return false; // Check if the word goes out of bounds or overlaps with existing letters
}
row += dr;
column += dc;
}
return true;
}
}

0 comments on commit de570c9

Please sign in to comment.