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 99c0ed2 commit 2460d0a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 55 deletions.
36 changes: 35 additions & 1 deletion src/main/java/uta/cse3310/Direction.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,40 @@ public class Direction {
enum Directions {
HORIZONTAL, // Represents horizontal direction
VERTICAL, // Represents vertical direction
DIAGONAL // Represents diagonal direction
DIAGONAL; // Represents diagonal direction

/**
* Method to get the row increment based on the direction.
* @return The row increment.
*/
public int getRowIncrement() {
switch (this) {
case HORIZONTAL:
return 0;
case VERTICAL:
return 1;
case DIAGONAL:
return 1;
default:
throw new IllegalStateException("Unexpected value: " + this);
}
}

/**
* Method to get the column increment based on the direction.
* @return The column increment.
*/
public int getColumnIncrement() {
switch (this) {
case HORIZONTAL:
return 1;
case VERTICAL:
return 0;
case DIAGONAL:
return 1;
default:
throw new IllegalStateException("Unexpected value: " + this);
}
}
}
}
81 changes: 27 additions & 54 deletions src/main/java/uta/cse3310/GridField.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ public GridField(ArrayList<String> wordList) {
this.wordList = wordList;
this.remainingWords = wordList.size();
generateGrid(5); // Initialize grid with default size (e.g., 5x5)
placeWords(); // Place words on the grid
}

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

}

public char[][] getGrid() {
Expand All @@ -37,7 +35,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 All @@ -56,26 +53,14 @@ public void revealWord(String word) {

public void addWord(String word, int row, int column, 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 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 starts off as the size of the updated wordlist
remainingWords++;
}

Expand All @@ -87,47 +72,35 @@ public void displayGrid() {
System.out.println();
}
}
// Method to place words from the word list on the grid
private void placeWords() {
for (String word : wordList) {
boolean wordPlaced = false;
while (!wordPlaced) {
int len = word.length();
Random random = new Random();
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;

public void placeRandomWords() {
Random random = new Random();
for (String word : wordList) {
boolean wordPlaced = false;
while (!wordPlaced) {
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;
}
}
}
}
}

// Method to check if a word can be placed on the grid
private boolean canPlaceWord(String word, int row, int column, 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;
}
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
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;
}
row += dr;
column += dc;
return true;
}
return true;
}
}

0 comments on commit 2460d0a

Please sign in to comment.