diff --git a/html/main.js b/html/main.js index 9d85255..8462304 100644 --- a/html/main.js +++ b/html/main.js @@ -108,7 +108,7 @@ connection.onmessage = function(evt){ document.getElementById("lobby").style.display = "none"; document.getElementById("gameArea").style.display = "block"; document.getElementById("grid").style.display = "block"; - wordgrid = obj.matrix; + wordgrid = obj.matrixDup; startTimer(); // modify the game grid colorgrid = obj.colorGrid; @@ -118,19 +118,19 @@ connection.onmessage = function(evt){ document.getElementById(i + "," + j).innerHTML = wordgrid[i][j]; // apply colors using Game.colorgrid; - if(colorgrid[i][j] == 'w'){ + if(colorgrid[i][j] == 'W'){ document.getElementById(i + "," + j).style.backgroundColor = "white"; } - else if(colorgrid[i][j] == 'r'){ + else if(colorgrid[i][j] == 'R'){ document.getElementById(i + "," + j).style.backgroundColor = "red"; } - else if(colorgrid[i][j] == 'g'){ + else if(colorgrid[i][j] == 'G'){ document.getElementById(i + "," + j).style.backgroundColor = "green"; } - else if(colorgrid[i][j] == 'b'){ + else if(colorgrid[i][j] == 'B'){ document.getElementById(i + "," + j).style.backgroundColor = "blue"; } - else if(colorgrid[i][j] == 'y'){ + else if(colorgrid[i][j] == 'Y'){ document.getElementById(i + "," + j).style.backgroundColor = "yellow"; } diff --git a/src/main/java/uta/cse3310/Game.java b/src/main/java/uta/cse3310/Game.java index ee4e0f3..08f7299 100644 --- a/src/main/java/uta/cse3310/Game.java +++ b/src/main/java/uta/cse3310/Game.java @@ -9,10 +9,20 @@ import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; public class Game { + + + public int gameNum; //coorealates games and corresponding players + public Matrix matrix = new Matrix(); + public ArrayList players; //players in this game can also get the 'numPlayers' easily + public ArrayList wordsFound = new ArrayList(); //words found in the game + public ArrayList wordBank = new ArrayList(); //easily get a hold of the wordbank to display during a game + + + // Possible attributes needed public boolean isOpen; public int numPlayers; - public char[][] matrix = new char[30][30]; + public char[][] matrixDup = matrix.grid; public char[][] colorGrid = new char[30][30]; // temps is a list of coordinate pairs. Javascript will parse // it by associating the index with a color @@ -20,20 +30,38 @@ public class Game { public ArrayList temps = new ArrayList(); public ArrayList scores = new ArrayList(); public ArrayList names = new ArrayList(); + + //still need to assign player color at this point + public Game(ArrayList players, int gameNum){ + this.gameNum = gameNum; + this.isOpen = true; + for(int k = 0; k < 30; k ++){ //init colorGrid to all white + for(int i = 0; i < 30; i ++){ + colorGrid[k][i] = 'W'; + } + } + this.players = players; + for(Player p: players){ + p.gameNum = gameNum; + } + + } + public Game(){ + this.players = new ArrayList(); this.isOpen = true; for(int i = 0; i < 30; i++){ for(int j = 0; j < 30; j++){ - Random r = new Random(); - char c = (char)(r.nextInt(26) + 'a'); - matrix[i][j] = c; - colorGrid[i][j] = 'w'; + //Random r = new Random(); + //char c = (char)(r.nextInt(26) + 'a'); + //matrix[i][j] = c; //no longer need this randomly created matrix + colorGrid[i][j] = 'W'; } } numPlayers = 0; } - // Adds the players queued + // Adds the players queued //this might end up being unused public void addEntries(Player player){ scores.add(player.score); names.add(player.name); @@ -48,31 +76,337 @@ public void highlightCell(int playerIdx, int[] coord){ casted[0] = coord[0]; casted[1] = coord[1]; temps.set(playerIdx, casted); + System.out.println(temps.get(playerIdx)[0] + " " + temps.get(playerIdx)[1]); } + public boolean checkWord(int[] startCoords, int[] endCoords){ + // parses coords, strings together word, checks if it's inside words used + // Placeholder for testing purposes: randomly returns true or false + return true; + } + //verifies word has not been found yet + //verifies word is within our grid + //adds valid words to the 'wordsFound' list + //return true if valid, false if not valid + public boolean verifyWord(String word){ - public void highlightWord(int playerIdx, int[] startCoords, int[] endCoords){ - + //Alwasy have to check inverse of every word + StringBuilder SBWord = new StringBuilder(word); + SBWord.reverse(); + String inverseWord = SBWord.toString(); + + + //check if word has not been found yet + if(wordsFound.isEmpty()){ + //proceed since word hasnt been found yet + } + else{ + for(Words w : wordsFound){ + if(w.word.equals(word) || w.word.equals(inverseWord)){ + //word has been previously found + return false; + } + } + } + + //look up the word within the grid + if(matrix.wordLookUp(word) != null){ //returns a 'Words' object if found + wordsFound.add(matrix.wordLookUp(word)); + return true; + } + + return false; } - public void playerSetReady(ArrayList playerList){ - // check number of ready players every time this thing is called, start the game immediately the moment it hits 2 - Lobby readyplayer = new Lobby(playerList); - if(readyplayer.numReady == 2){ - startGame(); + //verifies word has not been found yet + //verifies word is within our grid + //adds valid words to the 'wordsFound' list + //return true if valid, false if not valid + public Words verifyWordCoords(int xStart, int yStart, int xEnd, int yEnd){ + + //Alwasy have to check inverse of every word + + + //check if word has not been found yet + if(wordsFound.isEmpty()){ + //proceed since word hasnt been found yet } - + else{ + for(Words w : wordsFound){ + + int xS = w.x_startPoint; + int yS = w.y_startPoint; + int xE = w.x_endPoint; + int yE = w.y_endPoint; + // if coordinates match, then we've already found this word + if(xStart == xS && xEnd == xE && yStart == yS && yEnd == yE){ + return null; + // word already found + } + else if(xStart == xE && xEnd == xS && yStart == yE && yEnd == yS){ + return null; + } + } + } + + //look up the word within the grid + // if(matrix.wordLookUp(word) != null){ //returns a 'Words' object if found + // wordsFound.add(matrix.wordLookUp(word)); + // return true; + // } + + + //try and find the word picked by player within our grid + //matrix.wordsUsed is the list of all words within our grid + for(Words w: matrix.usedWordList){ + + int xS = w.x_startPoint; + int yS = w.y_startPoint; + int xE = w.x_endPoint; + int yE = w.y_endPoint; + // if coordinates match, then we've already found this word + if(xStart == xS && xEnd == xE && yStart == yS && yEnd == yE){ + return w; + // word already found + } + else if(xStart == xE && xEnd == xS && yStart == yE && yEnd == yS){ + return w; + } + + + } + + + + return null; } - public boolean checkWord(int[] startCoords, int[] endCoords){ - // parses coords, strings together word, checks if it's inside words used - // Placeholder for testing purposes: randomly returns true or false - return true; + //verify player belongs to this game + //returns true if valid false if player doesnt belong + public boolean verifyPlayer(Player player){ + //we can just check nicknames since they are all unique + for(Player p: players){ + if(p.name.equals(player.name)){ + return true; //player part of this game + } + } + return false; //player not part of this game } + //highlights words that have been found and validated + public void highlight(Words word, char color){ + //word.x_startPoint; + //word.y_startPoint; + //word.x_endPoint; + //word.y_endPoint; + + if(word.x_startPoint ==word.x_endPoint){ //vertical inserted word + int x = word.x_startPoint; + for(int y = word.y_startPoint; y <= word.y_endPoint; y++){ + + + + //choose appropriate color + char c = colorMixer(colorGrid[y][x], color); + //System.out.println(c); //debugging + colorGrid[y][x] = c; + + } + } + else if(word.y_startPoint == word.y_endPoint){ //horizontal inserted word + int y = word.y_startPoint; + for(int x = word.x_startPoint; x <= word.x_endPoint; x++){ - public void startGame(){ + + char c = colorMixer(colorGrid[y][x], color); + colorGrid[y][x] = c; + + } + } + else if(word.x_startPoint < word.x_endPoint){ //TOP-LEFT -> BOTTOM-RIGHT + int x = word.x_startPoint; //increases + int y = word.y_startPoint; //increases + //both x and y must move the same + while(x<= word.x_endPoint && y<= word.y_endPoint){ + + + char c = colorMixer(colorGrid[y][x], color); + colorGrid[y][x] = c; + x++; + y++; + + } + } + else if(word.x_startPoint > word.x_endPoint){ //TOP-RIGHT -> BOTTOM-RIGHT + int x = word.x_startPoint; //decreases + int y = word.y_startPoint; //increases + //both x and y must move the same + while(x>= word.x_endPoint && y<= word.y_endPoint){ + + + + char c = colorMixer(colorGrid[y][x], color); + colorGrid[y][x] = c; + x--; + y++; + + } + } + else{ + System.out.println("Orientation not found"); + } } + //mixes Rred, Yyellow, Blue, Zgrey can handle any other colors + public char colorMixer(char color1, char color2){ + //we have four original colors + //R(red) //O(orange) + //Y(yellow) //G(green) + //B(blue) //V(violet) + //Z(grey) //B(black ... dark grey) + + if(color1 == color2){ //same colors + return color1; + } + else if(color1 == 'W'){ //a white mix + return color2; + } + else if(color2 == 'W'){ //a white mix + return color1; + } + else if(color1 == 'Z' || color2 == 'Z'){ //grey color with anything else + return 'B'; + } + + + //no white colors no grey colors and no 'equal colors' + switch(color1){ + case 'R': + switch(color2){ + case 'Y': + return 'O'; + case 'B': + return 'V'; + default: + return 'B'; + } + + case 'Y': + switch(color2){ + case 'R': + return 'O'; + case 'B': + return 'G'; + default: + return 'B'; + } + + case 'B': + switch(color2){ + case 'R': + return 'V'; + case 'Y': + return 'G'; + default: + return 'B'; + } + + default: + return 'B'; + + + } + + + + + + + + + } + + //word found by a player returns true if word is adds points to player + public boolean playerFoundWord(int[] startCoords, int[] endCoords){ + + if(verifyWordCoords(startCoords[1], startCoords[0], endCoords[1], endCoords[0]) != null){ + wordsFound.add(verifyWordCoords(startCoords[1], startCoords[0], endCoords[1], endCoords[0])); + //word verified and added to 'wordsFound' + } + else{ + return false; + } + //go from coord to figureing out if the word is valid + //recreate word validation via coordinates + + // int[] startCoords = [yS, xS] + // int[] endCoords = [yE, xE] + //Thanh int xS = startCoords[1]; + + //words xS, yS, xE, yE + // + //edit score + // p.score += 1; // possible change here -------------------------- + //highlight cooresponding portion of the colorGrid + // Words w = wordsFound.get(wordsFound.size() - 1); + // highlight(w, 'W'); + + + return true; + } + + //a player has exited the game + public boolean playerExit(Player p){ + //make sure player belongs to this game + if(verifyPlayer(p)){ + players.remove(p); + return true; + } + else{ + return false; + } + + } + + //display the scoreboard + public void printScoreBoard(){ + System.out.println("--------- Scoreboard ----------"); + for(Player p: players){ + System.out.println(p.name + " : " + " " + p.score); + } + } + + public void printColorGrid(){ + System.out.println("\n----- Color Grid ----(W-white)(R-red)(Y-yellow)(Z-grey)(B- blue)(O-orange)(G-green)(V-violet)"); + for (int y = 0; y < matrix.numRows; y++) { + for (int x = 0; x < matrix.numCols; x++) { + + System.out.print(colorGrid[y][x] + " "); + + + + } + System.out.println(); + } + + + } + + //check if the game has been won (if all words have been found) + public boolean gameFinished(){ + if(wordsFound.size() == matrix.usedWordList.size()){ + System.out.println("Game Finished"); + printScoreBoard(); + this.matrix.printGrid(); + printColorGrid(); + + return true; + } + else{ + return false; + } + } + + public void endGame(){ } }//class's curly diff --git a/src/main/java/uta/cse3310/Main.java b/src/main/java/uta/cse3310/Main.java index 0fb7605..1f71cdd 100644 --- a/src/main/java/uta/cse3310/Main.java +++ b/src/main/java/uta/cse3310/Main.java @@ -13,11 +13,14 @@ import org.java_websocket.handshake.ClientHandshake; import org.java_websocket.server.WebSocketServer; import org.json.simple.JSONObject; +import org.json.simple.JSONArray; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonArray; import com.google.gson.JsonObject; + + public class Main extends WebSocketServer { // Vector games = new Vector(); Game[] games = new Game[5]; @@ -193,7 +196,17 @@ public void onMessage(WebSocket conn, String message) { // NEEDS HEAVY AMOUNT OF REWORK. THIS IS A BAREBONES PROTOTYPE if(U.code == 500){ - color(U,conn,gson); + int destGame = 0; + int index = 0; + // find out what game we're working with + for(Player x: playerList){ + // find who sent it + if(conn == x.playerConn){ + destGame = x.gameNum; + index = x.index; + } + } + color(gson, index, destGame, U); } @@ -206,9 +219,15 @@ public void onMessage(WebSocket conn, String message) { } - - + public void displayWordsInBank(){ + JSONObject words = new JSONObject(); + //JSONArray wordsArray = new JSONArray(wordBankList); //These two errors are cause because they cant find the symbol. + //words.put("Word Bank", wordArray); //Solution may be that we need to move methods from Matrix file to here. + String jsonBank = words.toJSONString(); + broadcast(jsonBank); + } + public void handleChatMessage(UserMsg userMsg) { // Construct JSON object representing the chat message JSONObject chatMessage = new JSONObject(); @@ -226,24 +245,12 @@ public void handleChatMessage(UserMsg userMsg) { - public void color(UserMsg U, WebSocket conn, Gson gson){ - int destGame = 0; - int index = 0; - // find out what game we're working with - for(Player x: playerList){ - // find who sent it - if(conn == x.playerConn){ - destGame = x.gameNum; - index = x.index; - } - } + public void color(Gson gson, int index, int destGame, UserMsg U){ // User wants to highlight a cell. + System.out.println(games[destGame].temps); if(U.endCoords == null){ System.out.println(U.name + " highlighted cell. Modify temps"); int[] firstCoord = {U.startCoords[0],U.startCoords[1]}; - // tempVal[0] = U.startCoords[0]; - // tempVal[1] = U.startCoords[1]; - // games[destGame].temps.set(index, tempVal); games[destGame].highlightCell(index, firstCoord); } else{ @@ -253,8 +260,8 @@ public void color(UserMsg U, WebSocket conn, Gson gson){ int x2 = U.endCoords[1]; System.out.printf("[%d, %d] -> [%d, %d]\n", y,x,y2,x2); if(y == y2 && x == x2){ - // if the two coords are identical, null temp, - System.out.println("identical coords. Modifying temps"); + System.out.println("identical coords."); + // clear highlighted starting cell for player Integer[] clearVal = {-1,-1}; games[destGame].temps.set(index, clearVal); } @@ -262,8 +269,8 @@ else if(x2 - x == 0 || y2 - y == 0){ // slope is either vertical or horizontal. check word int[] startCoords = {y,x}; int[] endCoords = {y2,x2}; - boolean me = games[destGame].checkWord(startCoords,endCoords); - if(me){ + boolean isWord = games[destGame].playerFoundWord(startCoords, endCoords); + if(isWord){ System.out.println("straight checkout returned true. modify colorgrid."); // larger length is the non zero; use this for word length; int length = Math.max(Math.abs(y2-y), Math.abs(x2-x)); @@ -284,29 +291,66 @@ else if(x2 - x == 0 || y2 - y == 0){ games[destGame].colorGrid[y][end - i] = indexToChar(index); } } + // clear highlighted starting cell for player Integer[] clearVal = {-1,-1}; games[destGame].temps.set(index, clearVal); } else{ - System.out.println("straight checkout return false. clear temps"); + System.out.println("Word not found or already found."); + // clear highlighted starting cell for player Integer[] clearVal = {-1,-1}; games[destGame].temps.set(index, clearVal); } } - // else if(Math.abs((y2-y)/(x2-x)) == 1){ // diagonal slope - // // sys out what direction we're highlighting. - // } + else if(Math.abs((U.endCoords[0] - U.startCoords[0])/(U.endCoords[1] - U.startCoords[1])) == 1){ // diagonal slope + System.out.println(Math.abs((U.endCoords[0] - U.startCoords[0])/(U.endCoords[1] - U.startCoords[1]))); + int length = Math.max(Math.abs(y2-y), Math.abs(x2-x)); + int endY = U.endCoords[0]; + int endX = U.endCoords[1]; + int[] startCoords = {x,y}; + int[] endCoords = {x2,y2}; + System.out.printf("%d/%d ", y2-y, x2-x); + boolean isWord = games[destGame].playerFoundWord(startCoords,endCoords); + if(isWord){ + if(y2-y > 0 && x2-x > 0){ + System.out.println("Down Right."); + for(int i = 0; i < length + 1; i++){ + System.out.printf("[%d, %d]\n", endY - i, endX - i); + games[destGame].colorGrid[endY - i][endX - i] = indexToChar(index); + } + } + else if(y2-y < 0 && x2-x > 0){ + System.out.println("Up Right."); + for(int i = 0; i < length + 1; i++){ + System.out.printf("[%d, %d]\n", endY + i, endX - i); + games[destGame].colorGrid[endY + i][endX - i] = indexToChar(index); + } + } + else if(y2-y > 0 && x2-x < 0){ + System.out.println("Down Left."); + for(int i = 0; i < length + 1; i++){ + System.out.printf("[%d, %d]\n", endY - i, endX + i); + games[destGame].colorGrid[endY - i][endX + i] = indexToChar(index); + } + } + else{ + System.out.println("Up Left."); + for(int i = 0; i < length + 1; i++){ + System.out.printf("[%d, %d]\n", endY + i, endX + i); + games[destGame].colorGrid[endY + i][endX + i] = indexToChar(index); + } + } + } + Integer[] clearVal = {-1,-1}; + games[destGame].temps.set(index, clearVal); + } else{ - // slope is invalid; modify temps + // slope is invalid; do nothing System.out.println("invalid slope."); + // clear highlighted starting cell for player Integer[] clearVal = {-1,-1}; games[destGame].temps.set(index, clearVal); - } - // if(dx == 0 || dy == 0){ - // } - // User chose a complete start and end of a word. check if the word is correct. - // if yes, modify colorgrid, otherwise, use temps.set(index, {-1,-1}) to remove the temp value for the player with the index; - } + }} for(Player y: playerList){ // check if the player is in the destined game if(games[destGame].names.contains(y.name)){ @@ -315,22 +359,23 @@ else if(x2 - x == 0 || y2 - y == 0){ y.playerConn.send(jsonString); } } + System.out.println(games[destGame].temps.toString()); } // parses user's index to a color-character public char indexToChar(int index){ if(index == 0){ - return 'r'; + return 'R'; } else if(index == 1){ - return 'g'; + return 'G'; } else if(index == 2){ - return 'b'; + return 'B'; } else if(index == 3){ - return 'y'; + return 'Y'; } else{ return '-'; @@ -367,6 +412,7 @@ public void onStart() { } public static void main(String[] args) { + System.out.println(System.getProperty("user.dir")); // http = 9028; int httpport = Integer.parseInt(System.getenv("HTTP_PORT")); // Set up the http server diff --git a/src/main/java/uta/cse3310/Matrix.java b/src/main/java/uta/cse3310/Matrix.java index d462e66..bf8f128 100644 --- a/src/main/java/uta/cse3310/Matrix.java +++ b/src/main/java/uta/cse3310/Matrix.java @@ -1,6 +1,4 @@ package uta.cse3310; - - import java.lang.StringBuilder; import java.io.BufferedReader; import java.io.FileReader; @@ -14,6 +12,9 @@ //this class is based around a 50 x 50 grid //everything else is just to keep track of this grid and the data withing public class Matrix { + + public int sharedLetterCount; //keeps count of how many letters are shared by two words + public float density; //percent of letters used for words (1.00 == every letter belongs to a word) public ArrayList wordList; //a list of all the words available (loaded from a file) public ArrayList usedWordList; //a list of all the words used/inserted in the grid @@ -21,7 +22,7 @@ public class Matrix { public int numCols; //number of columns in grid public float randomness; // still not sure what this is supposed to hold public int numFillerCharacters; //number of charachters used to fill in empty spaces in the grid - public char[][] grid; //the grid itsefl + public char[][] grid; //the grid of words itsefl public ArrayList fillerCharachters; //a list of ALL possible filler charachters aka alphabette @@ -31,6 +32,9 @@ public class Matrix { //default constructor //HARDCODED FILE TO READ FROM Matrix(){ + + sharedLetterCount = 0; + //initiate all values to a default density = 0; @@ -39,8 +43,8 @@ public class Matrix { //printWordList(); //debugging usedWordList = new ArrayList(); - numRows = 50; - numCols = 50; + numRows = 30; + numCols = 30; randomness = 0; numFillerCharacters = 0; @@ -53,10 +57,10 @@ public class Matrix { //printFillerCharacters(); //debugging fillGrid(); - //printGrid(); + //printGrid(); //prints grid before filler charachters inserted numFillerCharacters = insertFillerChar(); - printGrid(); - //printUsedWordList(); //debugging + printGrid(); //prints completed grid //debugging + printUsedWordList(); //debugging } @@ -230,8 +234,7 @@ public void horizontalWordInsert(String word){ for(int k = x; k <= x_endpoint; k ++){ if(grid[y][k] == '0' || grid[y][k] == letters[curr]){ - //doesnt fit - //crash = true; + } else{ crash = true; @@ -245,6 +248,9 @@ public void horizontalWordInsert(String word){ curr = 0; for(int k = x; k <= x_endpoint; k ++){ + if(grid[y][k] == letters[curr]){ //shared letters + sharedLetterCount++; + } grid[y][k] = letters[curr]; curr ++; } @@ -292,8 +298,7 @@ public void verticalWordInsert(String word){ for(int k = y; k <= y_endpoint; k ++){ if(grid[k][x] == '0' || grid[k][x] == letters[curr]){ - //doesnt fit - //crash = true; + } else{ crash = true; @@ -307,6 +312,9 @@ public void verticalWordInsert(String word){ curr = 0; for(int k = y; k <= y_endpoint; k ++){ + if(grid[k][x] == letters[curr]){ + sharedLetterCount++; + } grid[k][x] = letters[curr]; curr ++; } @@ -356,8 +364,7 @@ public void diagonalWordInsert1(String word){ for(int k = 0; k < word.length(); k ++){ if(grid[y + k][x + k] == '0' || grid[y + k][x + k] == letters[curr]){ - //doesnt fit - //crash = true; + //letter is valid } else{ crash = true; @@ -371,6 +378,9 @@ public void diagonalWordInsert1(String word){ curr = 0; for(int k = 0; k < word.length(); k ++){ + if(grid[y + k][x + k] == letters[curr]){ + sharedLetterCount++; + } grid[y + k][x + k] = letters[curr]; curr ++; } @@ -421,8 +431,8 @@ public void diagonalWordInsert2(String word){ for(int k = 0; k < word.length(); k ++){ if(grid[y + k][x - k] == '0' || grid[y + k][x - k] == letters[curr]){ - //doesnt fit - //crash = true; + + } else{ crash = true; @@ -436,6 +446,9 @@ public void diagonalWordInsert2(String word){ curr = 0; for(int k = 0; k < word.length(); k ++){ + if(grid[y + k][x - k] == letters[curr]){ + sharedLetterCount++; + } grid[y + k][x - k] = letters[curr]; curr ++; } @@ -495,6 +508,8 @@ public float calcDensity(){ //prints grid in its current state public void printGrid(){ + + System.out.println("---------- Word Grid ---------"); for (int y = 0; y < numRows; y++) { for (int x = 0; x < numCols; x++) { //System.out.print(grid[y][x] + " "); @@ -525,12 +540,34 @@ public void printWordList(){ System.out.println(word); } } + + public List wordBankList; + + public void wordBank(){ + wordBankList = new ArrayList<>(); + } + + public void fillWordBank(List usedWordList){ + wordBankList.clear(); + + for(Words w : usedWordList){ + wordBankList.add(w.word); + } + } + + public void displayWordBank(){ + System.out.println("Word Bank:\n"); + for(String word : wordBankList){ + System.out.println(" " + word); + } + } + //prints the list of words used in our grid public void printUsedWordList(){ for(Words wrd : usedWordList){ - System.out.println(wrd.word); + System.out.println(wrd.word + ": " + "(" + wrd.x_startPoint + ", " + wrd.y_startPoint + ") , (" + wrd.x_endPoint + ", " + wrd.y_endPoint + ") "); } } diff --git a/src/main/java/uta/cse3310/backEndFunctionalityExamples/Game.java b/src/main/java/uta/cse3310/backEndFunctionalityExamples/Game.java index 34ec12f..1f119ac 100644 --- a/src/main/java/uta/cse3310/backEndFunctionalityExamples/Game.java +++ b/src/main/java/uta/cse3310/backEndFunctionalityExamples/Game.java @@ -91,6 +91,69 @@ public boolean verifyWord(String word){ return false; } + //verifies word has not been found yet + //verifies word is within our grid + //adds valid words to the 'wordsFound' list + //return true if valid, false if not valid + public Words verifyWordCoords(int xStart, int yStart, int xEnd, int yEnd){ + + //Alwasy have to check inverse of every word + + + //check if word has not been found yet + if(wordsFound.isEmpty()){ + //proceed since word hasnt been found yet + } + else{ + for(Words w : wordsFound){ + + int xS = w.x_startPoint; + int yS = w.y_startPoint; + int xE = w.x_endPoint; + int yE = w.y_endPoint; + // if coordinates match, then we've already found this word + if(xStart == xS && xEnd == xE && yStart == yS && yEnd == yE){ + return null; + // word already found + } + else if(xStart == xE && xEnd == xS && yStart == yE && yEnd == yS){ + return null; + } + } + } + + //look up the word within the grid + // if(matrix.wordLookUp(word) != null){ //returns a 'Words' object if found + // wordsFound.add(matrix.wordLookUp(word)); + // return true; + // } + + + //try and find the word picked by player within our grid + //matrix.wordsUsed is the list of all words within our grid + for(Words w: matrix.usedWordList){ + + int xS = w.x_startPoint; + int yS = w.y_startPoint; + int xE = w.x_endPoint; + int yE = w.y_endPoint; + // if coordinates match, then we've already found this word + if(xStart == xS && xEnd == xE && yStart == yS && yEnd == yE){ + return w; + // word already found + } + else if(xStart == xE && xEnd == xS && yStart == yE && yEnd == yS){ + return w; + } + + + } + + + + return null; + } + //verify player belongs to this game //returns true if valid false if player doesnt belong public boolean verifyPlayer(Player player){ @@ -237,24 +300,29 @@ else if(color1 == 'Z' || color2 == 'Z'){ //grey color with anything else } //word found by a player returns true if word is adds points to player - public boolean playerFoundWord(Player p, String word){ + public boolean playerFoundWord(Player p, int[] startCoords, int[] endCoords){ - if(verifyPlayer(p)){ - //player was verified - } - else{ - return false; - } - - if(verifyWord(word)){ + if(verifyWordCoords(startCoords[1], startCoords[0], endCoords[1], endCoords[0]) != null){ + wordsFound.add(verifyWordCoords(startCoords[1], startCoords[0], endCoords[1], endCoords[0])); //word verified and added to 'wordsFound' } else{ return false; } + //go from coord to figureing out if the word is valid + //recreate word validation via coordinates + + // int[] startCoords = [yS, xS] + // int[] endCoords = [yE, xE] + //Thanh int xS = startCoords[1]; + + //words xS, yS, xE, yE + // + + //edit score - p.score += 1; + p.score += 1; // possible change here -------------------------- //highlight cooresponding portion of the colorGrid Words w = wordsFound.get(wordsFound.size() - 1); highlight(w, p.color); diff --git a/src/main/java/uta/cse3310/backEndFunctionalityExamples/GameTest.java b/src/main/java/uta/cse3310/backEndFunctionalityExamples/GameTest.java deleted file mode 100644 index 1019c5c..0000000 --- a/src/main/java/uta/cse3310/backEndFunctionalityExamples/GameTest.java +++ /dev/null @@ -1,65 +0,0 @@ -import java.util.ArrayList; - - -public class GameTest { - - - - public static void main(String[] args){ - - - Player p1 = new Player("p1"); - Player p2 = new Player("p2"); - Player p3 = new Player("p3"); - Player p4 = new Player("p4"); - - - ArrayList players = new ArrayList(); - players.add(p1); - players.add(p2); - players.add(p3); - players.add(p4); - - //pass in an array of players ready to play - Game game = new Game(players, 0); - - //for this test just continue finding words until the game is won - - - //only use things through GAME dont acces players or anything else - int index = 0; - boolean gameStatus = false; - while(!gameStatus){ //loop until the game is finsihed - for(Player p: players){ //let each player find a word - //chose a word to find automatically - //get the next word - Words word = game.matrix.usedWordList.get(index); - //get the word in string format - //System.out.println(word.word); //debugging - String wordFound = word.word; - index ++; - - - //can be edited to work with coordinates instead of string words - game.playerFoundWord(p, wordFound); //tell the game a player possibly found a word - if(game.gameFinished()){ - gameStatus = true; - break; - } - - - - - } - } - - //when game is done we can transfer players back to the lobby - //remove their color - //remove their gameID - //list them as 'NOT READY' - - - - - } -} diff --git a/src/main/java/uta/cse3310/backEndFunctionalityExamples/Player.java b/src/main/java/uta/cse3310/backEndFunctionalityExamples/Player.java index 8326542..ed66a6d 100644 --- a/src/main/java/uta/cse3310/backEndFunctionalityExamples/Player.java +++ b/src/main/java/uta/cse3310/backEndFunctionalityExamples/Player.java @@ -2,7 +2,7 @@ public class Player { public String name; public int score; public int index; - public int gameID; + public int gameID; //same as gameNum public boolean isReady; public char color; diff --git a/src/test/java/uta/cse3310/ChatTest.java b/src/test/java/uta/cse3310/ChatTest.java index b7dab60..b0f0de4 100644 --- a/src/test/java/uta/cse3310/ChatTest.java +++ b/src/test/java/uta/cse3310/ChatTest.java @@ -6,5 +6,5 @@ public class ChatTest extends TestCase { - + assert(true); } diff --git a/src/test/java/uta/cse3310/GameTest.java b/src/test/java/uta/cse3310/GameTest.java index dfc4820..900d797 100644 --- a/src/test/java/uta/cse3310/GameTest.java +++ b/src/test/java/uta/cse3310/GameTest.java @@ -18,8 +18,8 @@ public void testGameInitialization() { assertTrue(game.isOpen); assertEquals(0, game.numPlayers); - assertEquals(30, game.matrix.length); - assertEquals(30, game.matrix[0].length); + assertEquals(30, game.matrixDup.length); + assertEquals(30, game.matrixDup[0].length); assertEquals(30, game.colorGrid.length); assertEquals(30, game.colorGrid[0].length); assertEquals(0, game.temps.size()); diff --git a/src/main/java/uta/cse3310/wordlist_copy.txt b/wordlist_copy.txt similarity index 100% rename from src/main/java/uta/cse3310/wordlist_copy.txt rename to wordlist_copy.txt