Skip to content

Commit

Permalink
merged Luis and Thanh's code
Browse files Browse the repository at this point in the history
  • Loading branch information
Doan04 committed Apr 25, 2024
1 parent aa0532b commit f2a7494
Show file tree
Hide file tree
Showing 4 changed files with 370 additions and 33 deletions.
2 changes: 1 addition & 1 deletion html/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
338 changes: 323 additions & 15 deletions src/main/java/uta/cse3310/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class Game {


public int gameNum; //coorealates games and corresponding players
public Matrix m = new Matrix();
public Matrix matrix = new Matrix();
public ArrayList<Player> players; //players in this game can also get the 'numPlayers' easily
public ArrayList<Words> wordsFound = new ArrayList<Words>(); //words found in the game
public ArrayList<String> wordBank = new ArrayList<String>(); //easily get a hold of the wordbank to display during a game
Expand All @@ -22,7 +22,7 @@ public class Game {
// Possible attributes needed
public boolean isOpen;
public int numPlayers;
public char[][] matrix = m.grid;
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
Expand Down Expand Up @@ -77,30 +77,338 @@ public void highlightCell(int playerIdx, int[] coord){
casted[1] = coord[1];
temps.set(playerIdx, casted);
}
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<Player> 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++){


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();
}


public void startGame(){
}

//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
4 changes: 2 additions & 2 deletions src/main/java/uta/cse3310/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,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));
Expand Down
Loading

0 comments on commit f2a7494

Please sign in to comment.