Skip to content

Commit

Permalink
game test
Browse files Browse the repository at this point in the history
test files
  • Loading branch information
Guerrero96 committed Apr 16, 2024
1 parent c8618e8 commit e99381f
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 7 deletions.
101 changes: 99 additions & 2 deletions src/main/java/uta/cse3310/GameLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class GameLogic {

public GameLogic(PlayerType players) {
this.players = players;
this.wordGrid = new char[50][50];
this.wordGrid = new char[50][50];
}

public int getGameId() {
Expand All @@ -39,6 +39,7 @@ public char[][] getWordGrid() {
return wordGrid;
}

// Method to set words from a file hosted on a URL
public void setWordsFromFile(String url) {
List<String> wordsList = new ArrayList<>();
try {
Expand All @@ -58,7 +59,6 @@ public void setWordsFromFile(String url) {
randomWords = wordsList.toArray(new String[0]);
}


// Method to generate random words
public void generateRandomWords(int wordCount) {
randomWords = new String[wordCount];
Expand Down Expand Up @@ -106,4 +106,101 @@ public double calculateFillerPercentage() {
public boolean isValidWord(String word) {
return word.startsWith(players.getColor());
}

// Method to check if the specified word exists in the rows
public boolean checkRows(String word) {
for (int i = 0; i < wordGrid.length; i++) {
String row = new String(wordGrid[i]);
if (row.contains(word)) {
return true;
}
}
return false;
}

// Method to check if the specified word exists in the columns
public boolean checkColumns(String word) {
for (int i = 0; i < wordGrid[0].length; i++) {
StringBuilder column = new StringBuilder();
for (int j = 0; j < wordGrid.length; j++) {
column.append(wordGrid[j][i]);
}
if (column.toString().contains(word)) {
return true;
}
}
return false;
}

// Method to check if the specified word exists in the diagonals
public boolean checkDiagonals(String word) {
// Check main diagonal (top-left to bottom-right)
StringBuilder mainDiagonal = new StringBuilder();
for (int i = 0; i < wordGrid.length; i++) {
mainDiagonal.append(wordGrid[i][i]);
}
if (mainDiagonal.toString().contains(word)) {
return true;
}

// Check secondary diagonal (top-right to bottom-left)
StringBuilder secondaryDiagonal = new StringBuilder();
for (int i = 0; i < wordGrid.length; i++) {
secondaryDiagonal.append(wordGrid[i][wordGrid.length - 1 - i]);
}
if (secondaryDiagonal.toString().contains(word)) {
return true;
}

return false;
}


// Calculate points based on word length
public int calculatePoints(String word) {
int length = word.length();
switch (length) {
case 3:
return 50;
case 4:
return 60;
case 5:
return 70;
case 6:
return 80;
case 7:
return 90;
case 8:
return 100;
case 9:
return 110;
case 10:
return 120;
case 11:
return 130;
case 12:
return 140;
case 13:
return 150;
case 14:
return 160;
case 15:
return 170;
default:
return 0; // Invalid word length
}
}


// Method to check if the highlighted word matches any word in the word list
public int checkWord(String word) {
for (String randomWord : randomWords) {
if (word.equalsIgnoreCase(randomWord)) {
// Calculate points based on word length
int points = calculatePoints(word);
return points;
}
}
return 0;
}
}
32 changes: 27 additions & 5 deletions src/test/java/uta/cse3310/GameLogicTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@ public void setUp() {

@Test
public void testSetWordsFromURL() {
PlayerType player = new PlayerType("PlayerName", "PlayerColor", PlayerType.Status.Waiting);
GameLogic gameLogic = new GameLogic(player);
gameLogic.setWordsFromFile("https://raw.githubusercontent.com/utastudents/cse3310_sp24_group_8/main/src/main/java/uta/cse3310/words.txt");
String[] randomWords = gameLogic.getRandomWords();
assertNotNull(randomWords);
assertTrue(randomWords.length > 0);
}


@Test
public void testGenerateRandomWords() {
gameLogic.generateRandomWords(10);
Expand Down Expand Up @@ -58,9 +55,34 @@ public void testCalculateFillerPercentage() {

@Test
public void testIsValidWord() {
PlayerType player = new PlayerType("Player1", "Red", PlayerType.Status.Waiting);
gameLogic = new GameLogic(player);
assertTrue(gameLogic.isValidWord("RedWord"));
assertFalse(gameLogic.isValidWord("BlueWord"));
}

@Test
public void testCheckColumns() {
gameLogic.gridGenerator(); // Generate a random word grid
assertFalse(gameLogic.checkColumns("XYZ")); // Assuming "XYZ" does not exist in any column
}

@Test
public void testCheckDiagonals() {
gameLogic.gridGenerator(); // Generate a random word grid
assertFalse(gameLogic.checkDiagonals("XYZ")); // Assuming "XYZ" does not exist in any diagonal
}

@Test
public void testCalculatePoints() {
assertEquals(50, gameLogic.calculatePoints("THE")); // 3-letter word
assertEquals(60, gameLogic.calculatePoints("Four")); // 4-letter word
assertEquals(170, gameLogic.calculatePoints("Acclimatization")); // 15-letter word
assertEquals(0, gameLogic.calculatePoints("A")); // Invalid word length
}

@Test
public void testCheckWord() {
gameLogic.setWordsFromFile("https://raw.githubusercontent.com/utastudents/cse3310_sp24_group_8/main/src/main/java/uta/cse3310/words.txt");
assertEquals(70, gameLogic.checkWord("APPLE")); // Assuming "APPLE" is in the word list
assertEquals(0, gameLogic.checkWord("XYZ")); // Assuming "XYZ" is not in the word list
}
}
Binary file modified target/classes/uta/cse3310/GameLogic.class
Binary file not shown.
Binary file modified target/test-classes/uta/cse3310/GameLogicTest.class
Binary file not shown.

0 comments on commit e99381f

Please sign in to comment.