From e62ed6624cecfaca56afc4196a8aee26fd9ea715 Mon Sep 17 00:00:00 2001 From: Robert Eberle <123016766+TresEberle@users.noreply.github.com> Date: Tue, 30 Apr 2024 03:51:56 -0500 Subject: [PATCH] Add files via upload --- html/index.html | 383 +++++--------------- src/main/java/uta/cse3310/App.java | 100 ++++- src/main/java/uta/cse3310/Game.java | 13 +- src/main/java/uta/cse3310/Lobby.java | 8 +- src/main/java/uta/cse3310/Message.java | 2 +- src/main/java/uta/cse3310/NewNameEvent.java | 1 + src/main/java/uta/cse3310/Player.java | 10 +- src/main/java/uta/cse3310/PlayerType.java | 2 +- src/main/java/uta/cse3310/ServerEvent.java | 2 +- src/test/java/uta/cse3310/GridUnitTest.java | 114 ++---- src/test/java/uta/cse3310/banktest.java | 72 ++++ 11 files changed, 314 insertions(+), 393 deletions(-) create mode 100644 src/test/java/uta/cse3310/banktest.java diff --git a/html/index.html b/html/index.html index ec40ca9..5595c32 100644 --- a/html/index.html +++ b/html/index.html @@ -28,9 +28,9 @@

WORD GAME LOBBY

-

Nick Handles

+

Nicknames

    - +

    Leaderboard

    @@ -56,7 +56,7 @@

    Players Ready

    Start Game With

    - +
    @@ -88,6 +88,7 @@

    Player 2 Name

    + diff --git a/src/main/java/uta/cse3310/App.java b/src/main/java/uta/cse3310/App.java index 62bd721..145540a 100644 --- a/src/main/java/uta/cse3310/App.java +++ b/src/main/java/uta/cse3310/App.java @@ -47,6 +47,7 @@ import java.util.Collections; import java.util.ArrayList; import java.util.List; +import java.util.StringTokenizer; import org.java_websocket.WebSocket; import org.java_websocket.drafts.Draft; @@ -113,9 +114,7 @@ public void onOpen(WebSocket conn, ClientHandshake handshake) { conn.send(jsonString); System.out.println("sending " + jsonString); - // delete me String playerName = handshake.getFieldValue("playerName"); - - // delete me System.out.println("the player name is "+ playerName); + // Player newPlayer = new Player(playerName); @@ -155,10 +154,12 @@ public void onClose(WebSocket conn, int code, String reason, boolean remote) { @Override public void onMessage(WebSocket conn, String message) { + Game G = new Game(stats); System.out.println("Received message from client: " + message); // Parse the incoming message to extract sender and content Gson gson = new Gson(); + // broadcast the recieved message // broadcast(message); @@ -177,6 +178,7 @@ public void onMessage(WebSocket conn, String message) // Broadcast lobby information to all clients // broadcast(gson.toJson(lobby)); + /* if (false) { NewNameEvent N = gson.fromJson(message, NewNameEvent.class); // Lobby lobbys = conn.getAttachment(); @@ -198,8 +200,9 @@ public void onMessage(WebSocket conn, String message) broadcast(chatMessage); // broadcast(message); } - } + */ + // if (message.contains("content")) { // Use JSON parsing to extract sender and content @@ -217,31 +220,94 @@ public void onMessage(WebSocket conn, String message) L = createNewLobby(); } - // each kind of message is processed here + //Checking if the message is a newPlayer message if (message.indexOf("newPlayer") > 0) { - - // this is of type - // Received message from client: - // {"Type":"newPlayer","playerName":"aaaa","ConnectioID":1} + + // Received message from client: {"type":"newPlayer","playerName":"aaaa","ConnectioID":1} NewNameEvent N = gson.fromJson(message,NewNameEvent.class); - System.out.println("the name is " + N.playerName + " " + N.ConnectionID); - // new code added// - // Player playername = new Player(N.ConnectionID, N.playerName, 0, ""); - // Checking to see if players names are being added to the list - Player P = new Player(N.ConnectionID,N.playerName,0,"green"); + System.out.println("The name is: " + N.playerName + ", The Connection ID is: " + N.ConnectionID + ", The color is: " + N.playerColor) ; + + Player P = new Player(N.ConnectionID,N.playerName,0,N.playerColor); L.addPlayer(P); - // since the lobby has changed, let's send it out to everyone - String jsonString = gson.toJson(L.Players); + // since the lobby has changed, let's send it out to everyone + + String jsonString = gson.toJson(L); //conn.send(jsonString); broadcast(jsonString); // Player.printPlayerList(); // broadcastPlayerList(); // } + } + //Checking if the message is a player readying up + if (message.indexOf("readyPlayer") > 0) { + NewNameEvent N = gson.fromJson(message,NewNameEvent.class); + L.addToReadyQueue(L.getPlayerByName(N.playerName)); + + // since the lobby has changed, let's send it out to everyone + + String jsonString = gson.toJson(L); + broadcast(jsonString); } - } + if (message.startsWith("GamePlayer") == true) { + StringTokenizer string = new StringTokenizer(message," "); + string.nextToken(); + String name1 = string.nextToken(); + String name2 = string.nextToken(); + + if(L.getReadyQueueSize() < 2) + { + //ADD SOMETHING HERE TO DISPLAY NOT ENOUGH READY ON HTML + System.out.println("not enough to start"); + }else{ + L.removeFromReadyQueue(L.getPlayerByName(name1)); + L.removeFromReadyQueue(L.getPlayerByName(name2)); + String jsonString = gson.toJson(L); + broadcast(jsonString); + + //THIS PART NEEDS ADJUSTMENTS + + + Message GridMessage = new Message(G.grid.WordSearchGrid); + String GridJSONString = gson.toJson(GridMessage); + conn.send(GridJSONString); + + //Sending WordBank to server + Message WordBank = new Message(G.grid.WordsUsed); + String WordBankJSONString = gson.toJson(WordBank); + conn.send(WordBankJSONString); + } + } + + if(message.startsWith("WordCheck") == true) + { + + int j = 0; + Boolean Found = false; + StringTokenizer string = new StringTokenizer(message," "); + string.nextToken(); + String test = string.nextToken(); + for (String i : G.grid.WordsUsedLocations) + { + if(test.equals(i)){ + System.out.println("ITS ACTUALLY A WORD!"); + Message FoundWord = new Message(j); + String FoundWordJSONString = gson.toJson(FoundWord); + conn.send(FoundWordJSONString); + Found = true; + break; + } + j++; + } + if(Found == false){ + Message FoundWord = new Message(-1); + String FoundWordJSONString = gson.toJson(FoundWord); + conn.send(FoundWordJSONString); + } + } + } // private void broadcastPlayerList() { // List playerNames = Player.getPlayerList(); // Gson gson = new Gson(); diff --git a/src/main/java/uta/cse3310/Game.java b/src/main/java/uta/cse3310/Game.java index 021d8e6..6408493 100644 --- a/src/main/java/uta/cse3310/Game.java +++ b/src/main/java/uta/cse3310/Game.java @@ -1,6 +1,6 @@ package uta.cse3310; -import com.google.gson.Gson; +//import com.google.gson.Gson; public class Game { @@ -19,7 +19,6 @@ public class Game { Stats = s; Button = new PlayerType[9]; - grid = new Grid(); // initialize it @@ -36,6 +35,16 @@ public class Game { Msg[1] = "THE WORD SEARCH GAME"; } + public void SetBoard(PlayerType p, int[] b) { + // this method is only used for testing purposes + // p is the player to give the square to, and b + // is an array of button numbers + for (int i : b) { + Button[i] = p; + } + + } + public void StartGame() { } diff --git a/src/main/java/uta/cse3310/Lobby.java b/src/main/java/uta/cse3310/Lobby.java index 9483b3c..54c6370 100644 --- a/src/main/java/uta/cse3310/Lobby.java +++ b/src/main/java/uta/cse3310/Lobby.java @@ -8,7 +8,7 @@ public class Lobby { private List readyQueue; private PlayerType gameController; // Added field to track game controller private final int MAX_PLAYERS_PER_GAME = 4; // Maximum players per game - private String Type = "Lobby"; + private String type = "Lobby"; public Lobby() { players = new ArrayList<>(); @@ -56,7 +56,7 @@ public synchronized List getPlayers() { } public synchronized void addToReadyQueue(Player player) { - if (!(readyQueue.contains(player))) { + if (!readyQueue.contains(player)) { readyQueue.add(player); } // Start the game if the game controller is ready and there are enough players @@ -73,6 +73,10 @@ public synchronized List getReadyQueue() { return new ArrayList<>(readyQueue); } + public synchronized int getReadyQueueSize(){ + return readyQueue.size(); + } + public synchronized void startGame() { // Logic to start the game } diff --git a/src/main/java/uta/cse3310/Message.java b/src/main/java/uta/cse3310/Message.java index a20289f..3f2ce47 100644 --- a/src/main/java/uta/cse3310/Message.java +++ b/src/main/java/uta/cse3310/Message.java @@ -12,7 +12,7 @@ public class Message { private String content; public Message() { - // Default constructor + this.type = "Basic"; String content; } diff --git a/src/main/java/uta/cse3310/NewNameEvent.java b/src/main/java/uta/cse3310/NewNameEvent.java index 880db8d..bc49813 100644 --- a/src/main/java/uta/cse3310/NewNameEvent.java +++ b/src/main/java/uta/cse3310/NewNameEvent.java @@ -3,5 +3,6 @@ public class NewNameEvent { String Type; String playerName; + String playerColor; int ConnectionID; } diff --git a/src/main/java/uta/cse3310/Player.java b/src/main/java/uta/cse3310/Player.java index 0e26e41..0ec0888 100644 --- a/src/main/java/uta/cse3310/Player.java +++ b/src/main/java/uta/cse3310/Player.java @@ -22,19 +22,19 @@ public Player(int connectionID, String PlayerName, int PlayerPoints, String Play //Getter for player name public String getName(){ - return PlayerName; } //Getter for player points public int getPoints(){ - - return 1; + return PlayerPoints; } //Getter for player color public String getColor(){ - - return "player color"; + return PlayerColor; + } + public int getConnectionID(){ + return connectionID; } //getter for player list diff --git a/src/main/java/uta/cse3310/PlayerType.java b/src/main/java/uta/cse3310/PlayerType.java index fc1370a..87619ae 100644 --- a/src/main/java/uta/cse3310/PlayerType.java +++ b/src/main/java/uta/cse3310/PlayerType.java @@ -3,6 +3,6 @@ // A player can be an X or an O public enum PlayerType { - NOPLAYER, XPLAYER, OPLAYER, PLAYER1, PLAYER2, PLAYER3, PLAYER4 + NOPLAYER, XPLAYER, OPLAYER } diff --git a/src/main/java/uta/cse3310/ServerEvent.java b/src/main/java/uta/cse3310/ServerEvent.java index 3c1b59f..e7ce17b 100644 --- a/src/main/java/uta/cse3310/ServerEvent.java +++ b/src/main/java/uta/cse3310/ServerEvent.java @@ -4,5 +4,5 @@ public class ServerEvent { PlayerType YouAre; // Either an XPLAYER or a YPLAYER int GameId; int ConnectionID; - String Type ="ID"; + String type = "ID"; } diff --git a/src/test/java/uta/cse3310/GridUnitTest.java b/src/test/java/uta/cse3310/GridUnitTest.java index 5549f78..9bdbe8b 100644 --- a/src/test/java/uta/cse3310/GridUnitTest.java +++ b/src/test/java/uta/cse3310/GridUnitTest.java @@ -30,29 +30,41 @@ public static Test suite() { return new TestSuite(GridUnitTest.class); } - // Uses a set of words as a baseline for some set functionalities - public void wordListSetup(List WordList, boolean addWords) { - // Primary words for testtryAllDirections and testtryLocation - String[] Words = { "expressions", "exercises", "beautiful", "resumes", "familiar", "useful", - "linked", "lyrics", "scout", "rest", "epic", "love", "ends", "dual" }; - for (String word : Words) { - WordList.add((word).toUpperCase()); - } - - // Additional words for testminWordsAndMinDensity - if (addWords == true) { - // Since the current implementation of ReadInWordFile returns void, we currently - // can't get a random list. - String[] AdditionalWords = { "academic", "bookstore", "congratulations", "distributors", "dodge", "earn", - "fundamental", "generating", "ghost", "hammer", "headquarters", "identify", "instrumentation", - "jazz", "kernel", "knew", "lanes", "lights", "mainland", "many", "median", "mirrors", "prefix", - "prix", "same", "stylus", "treaty", "troubleshooting", "undergraduate", "unlock", "valid", "viking", - "wake", "watt", "xerox", "yellow", "zinc", "wind", "adobe", "acre", "ports", "mill", "custom", - "expo", "ever", "humor", "idol", "inch", "open", "opera", "order", "ours", "oxford", "page" }; - for (String word : AdditionalWords) { - WordList.add((word).toUpperCase()); - } - } + public void wordListSetup(List WordList) { + // Add words to word list individually + WordList.add(("expressions").toUpperCase()); // index 0 + WordList.add(("exercises").toUpperCase()); // index 1 + WordList.add(("beautiful").toUpperCase()); // index 2 + WordList.add(("resumes").toUpperCase()); // index 3 + WordList.add(("familiar").toUpperCase()); // index 4 + WordList.add(("useful").toUpperCase()); // index 5 + WordList.add(("linked").toUpperCase()); // index 6 + WordList.add(("lyrics").toUpperCase()); // index 7 + WordList.add(("scout").toUpperCase()); // index 8 + WordList.add(("rest").toUpperCase()); // index 9 + WordList.add(("epic").toUpperCase()); // index 10 + WordList.add(("love").toUpperCase()); // index 11 + WordList.add(("ends").toUpperCase()); // index 12 + WordList.add(("dual").toUpperCase()); // index 13 + + /** + * WordList - Size Of Word + * ========================================== + * expressions - 11 characters [Does Not Fit] + * exercises - 9 characters + * beautiful - 9 characters + * resumes - 7 characters + * familiar - 8 characters + * useful - 6 characters + * linked - 6 characters + * lyrics - 6 characters + * scout - 5 characters + * rest - 4 characters + * epic - 4 characters + * love - 4 characters + * ends - 4 characters + * dual - 4 characters + */ } // Simple test to see if a word can be inserted in all possible directions @@ -62,18 +74,17 @@ public void testtryAllDirections() { WordList = new ArrayList<>(); final char[][] WordSearchGrid; WordSearchGrid = new char[GridSize][GridSize]; - int wordsUsed = 0; int expectedCellsUsed = 0; // Add words to word list - wordListSetup(WordList, false); + wordListSetup(WordList); // Create Grid object Grid testGrid = new Grid(); // Code from the CreatWordSeachGrid function int MinCellsUsed = (int) (GridArea * TargetDensity); - System.out.println("\nGridUnitTest: Testing all directions...\nGridArea = " + GridArea); + System.out.println("\nTesting all directions...\nGridArea = " + GridArea); System.out.println("TargetDensity = " + TargetDensity); System.out.println("MinCellsUsed= " + MinCellsUsed); int CellsUsed = 0; @@ -94,21 +105,18 @@ public void testtryAllDirections() { CellsUsed += testGrid.tryLocation(WordSearchGrid, WordList.get(0), 5, 10, 10); expectedCellsUsed = 11; assertEquals(expectedCellsUsed, CellsUsed); - wordsUsed++; // Testing diagonal up left (dir = 6) // Should fit into 20x20 grid CellsUsed += testGrid.tryLocation(WordSearchGrid, WordList.get(0), 6, 10, 10); expectedCellsUsed = 21; assertEquals(expectedCellsUsed, CellsUsed); - wordsUsed++; // Testing horizontal left (dir = 4) // Should fit into 20x20 grid CellsUsed += testGrid.tryLocation(WordSearchGrid, WordList.get(0), 4, 10, 10); expectedCellsUsed = 31; assertEquals(expectedCellsUsed, CellsUsed); - wordsUsed++; // Testing diagonal up right (dir = 7) // Should NOT fit into 20x20 grid at r = 10, c = 10 @@ -127,9 +135,6 @@ public void testtryAllDirections() { // Unable to print resulting grid due to global variables in Grid.java - // Check if it doesn't meet the minimum word requirements - assertFalse(wordsUsed > MinCellsUsed); - System.out.println("Finished testing all directions...\n"); } @@ -146,20 +151,20 @@ public void testtryLocation() { int expectedCellsUsed = 0; // Add words to word list - wordListSetup(WordList, false); + wordListSetup(WordList); // Create Grid object Grid testGrid = new Grid(); // Code from the CreatWordSeachGrid function int MinCellsUsed = (int) (GridArea * TargetDensity); - System.out.println("\nGridUnitTest: Testing 10x10 Grid...\nGridArea = " + GridArea); + System.out.println("\nTesting 10x10 Grid...\nGridArea = " + GridArea); System.out.println("TargetDensity = " + TargetDensity); System.out.println("MinCellsUsed= " + MinCellsUsed); int CellsUsed = 0; // Unable to test if word is not inserted into grid for being out of bounds, due - // to overriding variables in Grid.java under a 10x10 grid. + // to overriding variables in Grid.java /** * Adding "exercises" @@ -395,43 +400,4 @@ public void testtryLocation() { // built into the function, or is untestable due to global variables in // Grid.java. - // Simple test that test placing words down with randomness involved. It is - // difficult to get the exact values for the test, we are testing the minimum - // words and minimum density. - public void testminWordsAndMinDensity() { - final List WordList; - WordList = new ArrayList<>(); - final char[][] WordSearchGrid; - WordSearchGrid = new char[GridSize][GridSize]; - int wordsUsed = 0; - - // Add words to word list - wordListSetup(WordList, true); - - // Create Grid object - Grid testGrid = new Grid(); - - // Code from the CreatWordSeachGrid function - System.out.println("\nGridUnitTest: Testing random word placement...\nGridArea = " + GridArea); - int CellsUsed = 0; - - // Insert words randomly into word grid - for (String word : WordList) { - CellsUsed += testGrid.tryPlaceWord(WordSearchGrid, word); - wordsUsed++; - } - - // Check if it meets the minimum and maximum word requirements - assertTrue(MinWordCount < wordsUsed); - assertTrue(MaxWordCount > wordsUsed); - - // Check if it meets the minimum density by using countValidWordCharacters - double WordDensitiy = (double) CellsUsed / GridArea; - System.out.println("WordDensitiy = " + WordDensitiy); - assertTrue(WordDensitiy <= 1); - assertTrue(WordDensitiy > TargetDensity); - - System.out.println("Finished testing random word placement\n"); - } - } \ No newline at end of file diff --git a/src/test/java/uta/cse3310/banktest.java b/src/test/java/uta/cse3310/banktest.java new file mode 100644 index 0000000..2baf6c4 --- /dev/null +++ b/src/test/java/uta/cse3310/banktest.java @@ -0,0 +1,72 @@ +package uta.cse3310; + +import java.io.*; +import java.util.*; + +public class banktest { + + public static void main(String[] args){ + + //System.out.println("...working..."); + + double density,charCount,grid,check; + density = 0.67; + grid= 50 * 50; + List wordlist = new ArrayList<>(); + + String copy,filename; + filename="words.txt"; + + //open file + try{ + File wordfile = new File(filename); + Scanner input = new Scanner (wordfile); + + //add words to list + while(input.hasNextLine()){ + copy = input.next(); + wordlist.add(copy); + } + input.close(); + }catch(Exception e){} + + /* + for (String t : wordlist){ + System.out.println(t); + } + */ + + charCount=0; + check=0; + Random picker = new Random(); + List newList = new ArrayList<>(); + + //density check + while(check