Skip to content

Commit

Permalink
adding test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Whiton Nguyen authored and Whiton Nguyen committed May 1, 2024
2 parents cff7468 + c9153e6 commit 7be665e
Show file tree
Hide file tree
Showing 4 changed files with 264 additions and 185 deletions.
3 changes: 0 additions & 3 deletions src/main/java/com/cse3310/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,6 @@ public User findWinner(int GameId) {
totalWordCount += u.wordCount;
}
}

System.out.println("Total word count: " + totalWordCount);
System.out.println("Word bank size: " + g.wordBank.size());
if (totalWordCount == g.wordBank.size()) {
return winner;
}
Expand Down
68 changes: 33 additions & 35 deletions src/main/java/com/cse3310/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import java.util.ArrayList;
import java.util.Random;

public class Game
{
public class Game {

public int GameId;
public char[][] grid;
Expand All @@ -19,6 +18,17 @@ public class Game
public double gridTime;


public Game() {
this.wordBank = new ArrayList<String>();
this.startIds = new ArrayList<Integer>();
this.endIds = new ArrayList<Integer>();
this.playerNames = new ArrayList<String>();
this.ActiveButtons = new ArrayList<Integer>();
this.CompletedButtons = new ArrayList<Integer>();
this.AllCompletedButtons = new ArrayList<Integer>();
this.GameId = 0;
}

public Game(ArrayList<String> words, int GameId) {
this.wordBank = new ArrayList<String>();
this.startIds = new ArrayList<Integer>();
Expand All @@ -32,8 +42,7 @@ public Game(ArrayList<String> words, int GameId) {
this.grid = generateGrid(words, wordBank);
}

public int isEnd(int id)
{
public int isEnd(int id) {
for (int endId : endIds) {
if (id == endId) {
return 0;
Expand Down Expand Up @@ -110,14 +119,13 @@ public char[][] generateGrid(ArrayList<String> words, ArrayList<String> wordbank
int[][] directions = { { 1, 1 }, { -1, 1 }, { 0, 1 }, { -1, 0 }, { 1, 0 } };
double density = .67; // density of grid
int maxLength = 10;
float diagUp,diagDown,horizontals,vertUp,vertDown; // stats to see orientations
float diagUp, diagDown, horizontals, vertUp, vertDown; // stats to see orientations
diagUp = diagDown = horizontals = vertUp = vertDown = 0;
// Input words into the array
// Take random words from the word list to put inside a word bank
int index = 0;
while (((double) validWordsLetters / (length * width)) < density)
{
//Grab a word and add it to a word length
while (((double) validWordsLetters / (length * width)) < density) {
// Grab a word and add it to a word length
String word = words.get(rand.nextInt(words.size())).toUpperCase();
while (word.length() > maxLength) {
word = words.get(rand.nextInt(words.size())).toUpperCase();
Expand Down Expand Up @@ -158,8 +166,7 @@ public char[][] generateGrid(ArrayList<String> words, ArrayList<String> wordbank

} while (fits == false && tries < 100);

if (tries >= 100)
{
if (tries >= 100) {
fits = false;
wordBank.remove(index);
}
Expand All @@ -180,30 +187,24 @@ public char[][] generateGrid(ArrayList<String> words, ArrayList<String> wordbank

System.out.println("Start: " + startId + "\n" + "End: " + endId);


if(dY == 0)
{
if (dY == 0) {
horizontals++;
}
else if(dY == 1)
{
if(dX == 1)
diagUp++;
} else if (dY == 1) {
if (dX == 1)
diagUp++;
else
vertUp++;
}
else if(dY == -1)
{
if(dX == 1)
diagDown++;
vertUp++;
} else if (dY == -1) {
if (dX == 1)
diagDown++;
else
vertDown++;
vertDown++;
}
System.out.println(index + "." + wordBank.get(index));
validWordsLetters = validWordsLetters + wordBank.get(index).length();
index++;
}

}

System.out.println("Actual Density: " + (double)validWordsLetters / (length * width));
Expand All @@ -218,28 +219,26 @@ else if(dY == -1)
// Print grid to console for debugging
for (int i = 0; i < width; i++) {
for (int j = 0; j < length; j++) {
if (grid[i][j] == 0)
{
if (grid[i][j] == 0) {

grid[i][j] = alphabet.charAt(rand.nextInt(alphabet.length()));
}


System.out.printf("" + grid[i][j] + "|");



System.out.printf("" + grid[i][j] + "|");

}
System.out.println();
}
double endTime = System.currentTimeMillis();
System.out.println("Time to generate grid: " + (endTime - startTime) + " ms");
this.gridTime = endTime - startTime;
System.out.println("Word Bank: " + wordBank);
System.out.println();

return grid;

}


public void checkWin(User user) {

}
Expand All @@ -252,4 +251,3 @@ public void Tick() {

}
}

171 changes: 81 additions & 90 deletions src/test/java/com/cse3310/AppTest.java
Original file line number Diff line number Diff line change
@@ -1,121 +1,112 @@
package com.cse3310;
//import static org.junit.Assert.assertArrayEquals;

import static org.junit.Assert.assertArrayEquals;
import org.java_websocket.WebSocket;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Random;
import java.util.Vector;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
public class AppTest {
public WebSocket conn;
User user1 = new User(" ", conn);
User user2 = new User(" ", conn);
Vector<Game> ActiveGames = new Vector<Game>();
Vector<User> ActiveUsers = new Vector<User>();

public User findWinner(int GameId) {
int totalWordCount = 0;
Game g = ActiveGames.get(GameId);
int most = 0;
User winner = new User();

for (User u : ActiveUsers) {
if (GameId == u.GameId) {
if (u.wordCount > most) {
most = u.wordCount; // Track user with highest score
winner = u;
}

totalWordCount += u.wordCount;
}
}

/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
if (totalWordCount == g.wordBank.size()) {
return winner;
}

/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
return null;
}

public void testGame()
{
assertTrue( true );
}
@Test
public void testFindWinner() {
// Create game
Game g = new Game();
g.GameId = 0;
g.wordBank.add("A");
g.wordBank.add("B");

public void EndIdCase(Game G)
{
ArrayList<Integer> array = new ArrayList<>();
for (int i = 0; i < 47; i++) {
array.add(1);
}
assertEquals(2, g.wordBank.size());

G.endIds = array;
ActiveGames.add(g);

assertTrue(G.isEnd(1) == 0);
}
// Set stats
user1.username = "user1";
user1.GameId = 0;
user1.wordCount = 2;

public void testEndIdCase()
{
ArrayList<String> stringList = new ArrayList<>();
user2.username = "user2";
user2.GameId = 0;
user2.wordCount = 0;

// Add elements to the ArrayList
stringList.add("Apple");
stringList.add("Banana");
stringList.add("Orange");
ActiveUsers.add(user1);
ActiveUsers.add(user2);

Game B = new Game(stringList, 1);

EndIdCase(B);
// Test for winner of the game
assertEquals(user1, findWinner(g.GameId));
}

public void gameWon(User winner) {
ArrayList<User> disconnectUsers = new ArrayList<>();

public void getCompletedButtonsCase(Game G)
{
ArrayList<Integer> array = new ArrayList<>();
array.add(6);
array.add(7);
array.add(8);
array.add(9);
array.add(10);
array.add(11);

ArrayList<Integer> result = G.getCompletedButtons(6, 11);
int wonGameId = winner.GameId;

for (User u : ActiveUsers) {
if (u.GameId == wonGameId) {
Winner e = new Winner(winner);
disconnectUsers.add(u);
}
}

assertTrue(result.containsAll(array) == array.containsAll(result));
for (User u : disconnectUsers) {
ActiveUsers.remove(u);
}
}

public void testGetCompletedButtons()
{
ArrayList<String> stringList = new ArrayList<>();
stringList.add("Apple");
Game B = new Game(stringList, 1);

getCompletedButtonsCase(B);
}
@Test
public void testGameWon() {
ActiveUsers.clear();
ActiveGames.clear();

User winner = new User();
winner.GameId = 0;
user1.GameId = 0;
user2.GameId = 0;

public void GenerateGrid(Game G) {
// Create a Game object
ArrayList<String> stringList = new ArrayList<>();

// Add elements to the ArrayList
stringList.add("CAT");
char[][] result = G.generateGrid(stringList, new ArrayList<>());
assertEquals(20, result.length);
}

public void testGenerateGrid()
{
ArrayList<String> stringList = new ArrayList<>();
stringList.add("App");
Game B = new Game(stringList, 1);
GenerateGrid(B);
}
ActiveUsers.add(user1);
ActiveUsers.add(user2);
ActiveUsers.add(winner);

gameWon(winner);

// Active users in the game should be cleared out
assertEquals(0, ActiveUsers.size());
}
}
Loading

0 comments on commit 7be665e

Please sign in to comment.