Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
muktar1907 committed Apr 29, 2024
2 parents 1b42ed7 + 0cd475f commit 5f563d0
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 58 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);
}
}
}
}
44 changes: 13 additions & 31 deletions src/main/java/uta/cse3310/GridField.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ public class GridField {
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
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() {
Expand All @@ -36,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 @@ -55,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,12 +73,19 @@ public void displayGrid() {
}
}

// Method to randomly place words from the word list on the grid
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);
Expand All @@ -107,19 +100,8 @@ public void placeRandomWords() {

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;
}
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
Expand Down
48 changes: 22 additions & 26 deletions src/main/java/uta/cse3310/WordList.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package uta.cse3310;

import java.io.IOException;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -8,55 +9,50 @@
import java.nio.file.Path;
import java.nio.file.Paths;

public class WordList //extends Direction
{
public class WordList {
public static List<String> list;
//private [] words;

//method for accesing the word list from file
public static ArrayList<String> getWordList(String wordsFile)
{
public static ArrayList<String> getWordList(String wordsFile) {
ArrayList<String> wordList = new ArrayList<>();

try {
list = Files.readAllLines(Paths.get(wordsFile));
List<String> lines = Files.readAllLines(Paths.get(wordsFile));
if (lines.isEmpty()) {
System.out.println("The file is empty: " + wordsFile);
return wordList; // Return empty list if the file is empty
}

list = new ArrayList<>(lines);
shuffleWords();
wordList.addAll(list);
} catch (IOException e) {
System.out.println("not a valid file!");
System.out.println("Error reading the file: " + wordsFile);
e.printStackTrace(); // Print the stack trace for debugging
}

return wordList;
}

//method for word shuffling
public static void shuffleWords()
{
Collections.shuffle(list);
public static void shuffleWords() {
Collections.shuffle(list);
}

public static ArrayList<String> updatedWordList(ArrayList<String> wordBank)
{
public static ArrayList<String> updatedWordList(ArrayList<String> wordBank) {
int totalCharacters = 0;
int index = 0;
ArrayList<String> updatedList = new ArrayList<>();

while (totalCharacters < 500 && index < wordBank.size())
{
while (totalCharacters < 500 && index < wordBank.size()) {
String word = wordBank.get(index);
if (totalCharacters < 500)
{
if (totalCharacters + word.length() <= 500) {
updatedList.add(word);
totalCharacters += word.length();
} else
{
} else {
break;
}
index++;
}

return updatedList;
}

}

}

0 comments on commit 5f563d0

Please sign in to comment.