Merge remote-tracking branch 'origin/main' into main

This commit is contained in:
2022-03-24 11:36:14 +01:00
2 changed files with 47 additions and 10 deletions

View File

@@ -47,7 +47,6 @@ public class Game {
guess.deleteCharAt(0); guess.deleteCharAt(0);
} }
if (word.isEmpty()) { if (word.isEmpty()) {
System.out.println("öasdlkihg");
won = 1; won = 1;
stopGame(); stopGame();
} else if (storeInArray(guess.toString())) { } else if (storeInArray(guess.toString())) {
@@ -56,6 +55,33 @@ public class Game {
} }
return s; return s;
} }
public String checkGuess(String g) {
StringBuilder guess = new StringBuilder(g);
StringBuilder word = new StringBuilder(this.word);
String s = "";
for (int i = 0; i < this.word.length(); i++) {
if (guess.charAt(0) == word.charAt(0)) {
word.deleteCharAt(0);
s += "2";
} else {
boolean found = false;
for (int r = 1; r < word.length(); r++) {
if (guess.charAt(0) == word.charAt(r)) {
s += "1";
found = true;
word.deleteCharAt(r);
break;
}
}
if (!found) {
s += "0";
}
}
guess.deleteCharAt(0);
}
return s;
}
/** /**
* Speichert das Wort in der nächsten freien Stelle im Array und gibt danach * Speichert das Wort in der nächsten freien Stelle im Array und gibt danach
@@ -82,7 +108,7 @@ public class Game {
return !word.equals("") || !word.equals(null); return !word.equals("") || !word.equals(null);
} }
private void stopGame() { public void stopGame() {
word = ""; word = "";
guesses = new String[guesses.length - 1]; guesses = new String[guesses.length - 1];
} }

View File

@@ -53,7 +53,6 @@ public class WordleServer extends Server {
u.setConnectionID(connectionID); u.setConnectionID(connectionID);
if (pMessage.length() < 4) { if (pMessage.length() < 4) {
send(pClientIP, pClientPort, "-ERR Command not valid in this state"); send(pClientIP, pClientPort, "-ERR Command not valid in this state");
} else { } else {
String m = pMessage.substring(0, 4); String m = pMessage.substring(0, 4);
System.out.println("Message: " + m + " State: " + u.getState()); System.out.println("Message: " + m + " State: " + u.getState());
@@ -104,7 +103,6 @@ public class WordleServer extends Server {
if (m.length() == 5) { if (m.length() == 5) {
if (query.checkWord(m)) { if (query.checkWord(m)) {
String a = "+OK " + u.getGame().giveGuess(m); String a = "+OK " + u.getGame().giveGuess(m);
System.out.println(u.getGame().getGameStatus());
switch (u.getGame().getGameStatus()) { switch (u.getGame().getGameStatus()) {
case (1): case (1):
a += " game continues"; a += " game continues";
@@ -126,14 +124,27 @@ public class WordleServer extends Server {
send(pClientIP, pClientPort, "-ERR wrong format"); send(pClientIP, pClientPort, "-ERR wrong format");
} }
} else if (m.equalsIgnoreCase("INFO")) { } else if (m.equalsIgnoreCase("INFO")) {
int n = Integer.parseInt("0" + pMessage.substring(4, pMessage.length()).replaceAll(" ", "")); String substring = "0";
if (n != 0) { if (m.length() > 4) {
substring += pMessage.substring(5, pMessage.length()).replaceAll(" ", "");
} else { }
int n = Integer.parseInt("0" + substring);
String[] guesses = u.getGame().getGuesses();
if (6 >= n && n >= 1) {
String guess = guesses[n];
send(pClientIP, pClientPort, "+OK " + guess + " " + u.getGame().checkGuess(guess));
} else if (n == 0) {
String a = "+OK ";
for (int i = 0; i < 6; i++) {
if (guesses[i] == null) {
a += guesses[i] + " " + u.getGame().checkGuess(guesses[i]);
}
}
send(pClientIP, pClientPort, a);
} }
} else if (m.equalsIgnoreCase("EXIT")) { } else if (m.equalsIgnoreCase("EXIT")) {
u.getGame().stopGame();
u.setState(3);
} else { } else {
send(pClientIP, pClientPort, "-ERR Command not valid in this state"); send(pClientIP, pClientPort, "-ERR Command not valid in this state");
} }