Skip to content

Commit

Permalink
fix test fails
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenil committed Apr 23, 2024
1 parent a9b9647 commit 3c06703
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 38 deletions.
19 changes: 7 additions & 12 deletions src/main/java/uta/cse3310/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.security.PublicKey;

class Game {
private int MAX = 50;
private Random random = new Random();
private static final Random random = new Random();
private int gameID;
private char[][] wordMatrix = new char[MAX][MAX];
private List<String> wordsList = new ArrayList<>();
Expand All @@ -15,7 +16,7 @@ class Game {


public Game(String filePath, int numOfWords,int gridSize) {
MAX=gridSize;
this.MAX=gridSize;
this.gameID = random.nextInt(1000);
initializeMatrix();
try {
Expand All @@ -28,16 +29,9 @@ public Game(String filePath, int numOfWords,int gridSize) {


}
public Game(String filePath, int numOfWords, int gridSize, Random random) { //for testing only
this.random = random;
this.MAX = gridSize;
this.gameID = this.random.nextInt(1000);
public void initializwithdots(){
initializeMatrix();
try {
readWordsFromFile(filePath, numOfWords);
} catch (IOException e) {
System.err.println("Error reading file: " + e.getMessage());
}

}
public void fillWithwords (){
placeWords();
Expand All @@ -49,11 +43,12 @@ public char[][] getMatrix(){
return wordMatrix;
}

private void initializeMatrix() {
public void initializeMatrix() {
for (int i = 0; i < MAX; i++) {
Arrays.fill(wordMatrix[i], '.');
}
}


private void readWordsFromFile(String filePath, int numOfWords) throws IOException {
List<String> allWords = new ArrayList<>();
Expand Down
40 changes: 14 additions & 26 deletions src/test/java/uta/cse3310/GameTest.java
Original file line number Diff line number Diff line change
@@ -1,47 +1,35 @@
package uta.cse3310;
import junit.framework.TestCase;

import java.io.IOException;
import java.util.Arrays;
import java.util.Random;

public class GameTest extends TestCase {
private Game game;

public Game game;
private int max=50;
public void setUp() {
Random fixedRandom = new Random(42);
game = new Game("testWords.txt" ,5, 10, fixedRandom);
game.fillWithwords();

game = new Game("src/test/java/uta/cse3310/testWords.txt", 5, max);
}


public void testMatrixInitialization() {
game.initializwithdots();
char[][] matrix = game.getMatrix();
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
assertEquals("Matrix should be initialized with dots.", '.', matrix[i][j]);
for (int i = 0; i < max; i++) {
for (int j = 0; j < max; j++) {

assertEquals('.',matrix[i][j]);
}
}
}

}

public void testWordPlacement() {

game.fillWithwords();
assertTrue("Expected at least one word to be placed", game.getNumofWords() > 0);
}

public void testFillRandom() {
game.fillWithrandom();
char[][] matrix = game.getMatrix();
boolean hasRandomLetters = false;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
if (matrix[i][j] != '.') {
hasRandomLetters = true;
assertTrue("Character must be a letter", Character.isLetter(matrix[i][j]));
}
}
}
assertTrue("Matrix should contain random letters", hasRandomLetters);
}



}

0 comments on commit 3c06703

Please sign in to comment.