diff --git a/src/main/java/com/cse3310/App.java b/src/main/java/com/cse3310/App.java index 73b7e0c..026c0e1 100644 --- a/src/main/java/com/cse3310/App.java +++ b/src/main/java/com/cse3310/App.java @@ -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; } diff --git a/src/main/java/com/cse3310/Game.java b/src/main/java/com/cse3310/Game.java index 4a2e715..34efce5 100644 --- a/src/main/java/com/cse3310/Game.java +++ b/src/main/java/com/cse3310/Game.java @@ -3,8 +3,7 @@ import java.util.ArrayList; import java.util.Random; -public class Game -{ +public class Game { public int GameId; public char[][] grid; @@ -19,6 +18,17 @@ public class Game public double gridTime; + public Game() { + this.wordBank = new ArrayList(); + this.startIds = new ArrayList(); + this.endIds = new ArrayList(); + this.playerNames = new ArrayList(); + this.ActiveButtons = new ArrayList(); + this.CompletedButtons = new ArrayList(); + this.AllCompletedButtons = new ArrayList(); + this.GameId = 0; + } + public Game(ArrayList words, int GameId) { this.wordBank = new ArrayList(); this.startIds = new ArrayList(); @@ -32,8 +42,7 @@ public Game(ArrayList 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; @@ -110,14 +119,13 @@ public char[][] generateGrid(ArrayList words, ArrayList 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(); @@ -158,8 +166,7 @@ public char[][] generateGrid(ArrayList words, ArrayList wordbank } while (fits == false && tries < 100); - if (tries >= 100) - { + if (tries >= 100) { fits = false; wordBank.remove(index); } @@ -180,30 +187,24 @@ public char[][] generateGrid(ArrayList words, ArrayList 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)); @@ -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) { } @@ -252,4 +251,3 @@ public void Tick() { } } - diff --git a/src/test/java/com/cse3310/AppTest.java b/src/test/java/com/cse3310/AppTest.java index a7e29f0..738e424 100644 --- a/src/test/java/com/cse3310/AppTest.java +++ b/src/test/java/com/cse3310/AppTest.java @@ -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 ActiveGames = new Vector(); + Vector ActiveUsers = new Vector(); + + 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 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 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 disconnectUsers = new ArrayList<>(); - public void getCompletedButtonsCase(Game G) - { - ArrayList array = new ArrayList<>(); - array.add(6); - array.add(7); - array.add(8); - array.add(9); - array.add(10); - array.add(11); - - ArrayList 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 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 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 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()); + } } diff --git a/src/test/java/com/cse3310/GameTest.java b/src/test/java/com/cse3310/GameTest.java index f869005..8914319 100644 --- a/src/test/java/com/cse3310/GameTest.java +++ b/src/test/java/com/cse3310/GameTest.java @@ -1,57 +1,150 @@ -package com.cse3310; -//import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertTrue; - -import java.util.ArrayList; - -import org.junit.Test; - -import java.io.BufferedReader; -import java.io.FileReader; -import java.io.IOException; - -public class GameTest { - - @Test - public void densityIsWithinRange(){ - - String filename = "words.txt"; - ArrayList wordList = new ArrayList<>(); - - try (BufferedReader br = new BufferedReader(new FileReader(filename))) { - String line; - while ((line = br.readLine()) != null) { - wordList.add(line.trim()); - } - } catch (IOException e) { - System.err.println("Error reading file:" + e.getMessage()); - } - - Game g = new Game(wordList, 0); - - System.out.println("DENSITY IS " + g.gridDensity); - assertTrue(g.gridDensity >= 0.67); - } - - @Test - public void timeIsWithinRange(){ - - String filename = "words.txt"; - ArrayList wordList = new ArrayList<>(); - - try (BufferedReader br = new BufferedReader(new FileReader(filename))) { - String line; - while ((line = br.readLine()) != null) { - wordList.add(line.trim()); - } - } catch (IOException e) { - System.err.println("Error reading file:" + e.getMessage()); - } - - Game g = new Game(wordList, 0); - - System.out.println("TIME IS " + g.gridTime); - assertTrue(g.gridTime <= 1000); - } - -} +package com.cse3310; +//import static org.junit.Assert.assertArrayEquals; + +import static org.junit.Assert.assertArrayEquals; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Random; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +public class GameTest + extends TestCase { + /** + * Create the test case + * + * @param testName name of the test case + */ + public GameTest(String testName) { + super(testName); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() { + return new TestSuite(GameTest.class); + } + + /** + * Rigourous Test :-) + */ + public void testApp() { + assertTrue(true); + } + + public void testGame() { + assertTrue(true); + } + + public void EndIdCase(Game G) { + ArrayList array = new ArrayList<>(); + for (int i = 0; i < 47; i++) { + array.add(1); + } + + G.endIds = array; + + assertTrue(G.isEnd(1) == 0); + } + + public void testEndIdCase() { + ArrayList stringList = new ArrayList<>(); + + // Add elements to the ArrayList + stringList.add("Apple"); + stringList.add("Banana"); + stringList.add("Orange"); + + Game B = new Game(stringList, 1); + + EndIdCase(B); + } + + public void getCompletedButtonsCase(Game G) { + ArrayList array = new ArrayList<>(); + array.add(6); + array.add(7); + array.add(8); + array.add(9); + array.add(10); + array.add(11); + + ArrayList result = G.getCompletedButtons(6, 11); + + assertTrue(result.containsAll(array) == array.containsAll(result)); + } + + public void testGetCompletedButtons() { + ArrayList stringList = new ArrayList<>(); + stringList.add("Apple"); + Game B = new Game(stringList, 1); + + getCompletedButtonsCase(B); + } + + public void GenerateGrid(Game G) { + // Create a Game object + ArrayList 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 stringList = new ArrayList<>(); + stringList.add("App"); + Game B = new Game(stringList, 1); + GenerateGrid(B); + } + + + public void densityIsWithinRange(){ + + String filename = "words.txt"; + ArrayList wordList = new ArrayList<>(); + + try (BufferedReader br = new BufferedReader(new FileReader(filename))) { + String line; + while ((line = br.readLine()) != null) { + wordList.add(line.trim()); + } + } catch (IOException e) { + System.err.println("Error reading file:" + e.getMessage()); + } + + Game g = new Game(wordList, 0); + + System.out.println("DENSITY IS " + g.gridDensity); + assertTrue(g.gridDensity >= 0.67); + } + + + public void timeIsWithinRange(){ + + String filename = "words.txt"; + ArrayList wordList = new ArrayList<>(); + + try (BufferedReader br = new BufferedReader(new FileReader(filename))) { + String line; + while ((line = br.readLine()) != null) { + wordList.add(line.trim()); + } + } catch (IOException e) { + System.err.println("Error reading file:" + e.getMessage()); + } + + Game g = new Game(wordList, 0); + + System.out.println("TIME IS " + g.gridTime); + assertTrue(g.gridTime <= 1000); + } + +} \ No newline at end of file