Skip to content

Commit

Permalink
fixed coloring bug caused by capitalization.
Browse files Browse the repository at this point in the history
  • Loading branch information
Doan04 committed Apr 26, 2024
1 parent f2a7494 commit d10db59
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 118 deletions.
10 changes: 5 additions & 5 deletions html/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

Expand Down
8 changes: 3 additions & 5 deletions src/main/java/uta/cse3310/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ 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
Expand Down Expand Up @@ -343,14 +344,11 @@ public boolean playerFoundWord(int[] startCoords, int[] endCoords){

//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');
// Words w = wordsFound.get(wordsFound.size() - 1);
// highlight(w, 'W');


return true;
Expand Down
98 changes: 66 additions & 32 deletions src/main/java/uta/cse3310/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,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);
}


Expand Down Expand Up @@ -235,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{
Expand All @@ -262,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);
}
Expand Down Expand Up @@ -293,29 +291,64 @@ 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];
System.out.printf("%d/%d ", y2-y, x2-x);
boolean isWordinBank = games[destGame].checkWord(U.startCoords,U.endCoords);
if(isWordinBank){
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)){
Expand All @@ -324,22 +357,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 '-';
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/uta/cse3310/backEndFunctionalityExamples/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public boolean verifyWord(String word){
//verifies word is within our grid
//adds valid words to the 'wordsFound' list
//return true if valid, false if not valid
public boolean verifyWordCoords(int xStart, int yStart, int xEnd, int yEnd){
public Words verifyWordCoords(int xStart, int yStart, int xEnd, int yEnd){

//Alwasy have to check inverse of every word

Expand All @@ -113,11 +113,11 @@ public boolean verifyWordCoords(int xStart, int yStart, int xEnd, int yEnd){
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 false;
return null;
// word already found
}
else if(xStart == xE && xEnd == xS && yStart == yE && yEnd == yS){
return false;
return null;
}
}
}
Expand All @@ -139,19 +139,19 @@ else if(xStart == xE && xEnd == xS && yStart == yE && yEnd == yS){
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 true;
return w;
// word already found
}
else if(xStart == xE && xEnd == xS && yStart == yE && yEnd == yS){
return true;
return w;
}


}



return false;
return null;
}

//verify player belongs to this game
Expand Down Expand Up @@ -302,7 +302,8 @@ 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, int[] startCoords, int[] endCoords){

if(verifyWordCoords(startCoords[1], startCoords[0], endCoords[1], endCoords[0])){
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{
Expand All @@ -321,7 +322,7 @@ public boolean playerFoundWord(Player p, int[] startCoords, int[] endCoords){


//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);
Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion src/test/java/uta/cse3310/ChatTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

public class ChatTest extends TestCase {


assert(true);
}
4 changes: 2 additions & 2 deletions src/test/java/uta/cse3310/GameTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit d10db59

Please sign in to comment.