From 2460d0a1ab54f687d7f16daa87ebe207066f4b0d Mon Sep 17 00:00:00 2001 From: 2onefan2 <159247397+2onefan2@users.noreply.github.com> Date: Sun, 28 Apr 2024 21:54:09 -0500 Subject: [PATCH] Updated --- src/main/java/uta/cse3310/Direction.java | 36 ++++++++++- src/main/java/uta/cse3310/GridField.java | 81 ++++++++---------------- 2 files changed, 62 insertions(+), 55 deletions(-) diff --git a/src/main/java/uta/cse3310/Direction.java b/src/main/java/uta/cse3310/Direction.java index 6c28e82..10ed9e1 100644 --- a/src/main/java/uta/cse3310/Direction.java +++ b/src/main/java/uta/cse3310/Direction.java @@ -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); + } + } } } diff --git a/src/main/java/uta/cse3310/GridField.java b/src/main/java/uta/cse3310/GridField.java index 1f83f2a..451f7fc 100644 --- a/src/main/java/uta/cse3310/GridField.java +++ b/src/main/java/uta/cse3310/GridField.java @@ -12,13 +12,11 @@ public GridField(ArrayList 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() { @@ -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) { @@ -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++; } @@ -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; } -} \ No newline at end of file