forked from IF-LK-2020/wordle
Merge remote-tracking branch 'origin/main' into main
This commit is contained in:
commit
74968d0f09
30
Game.java
30
Game.java
|
@ -47,7 +47,6 @@ public class Game {
|
|||
guess.deleteCharAt(0);
|
||||
}
|
||||
if (word.isEmpty()) {
|
||||
System.out.println("öasdlkihg");
|
||||
won = 1;
|
||||
stopGame();
|
||||
} else if (storeInArray(guess.toString())) {
|
||||
|
@ -56,6 +55,33 @@ public class Game {
|
|||
}
|
||||
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
|
||||
|
@ -82,7 +108,7 @@ public class Game {
|
|||
return !word.equals("") || !word.equals(null);
|
||||
}
|
||||
|
||||
private void stopGame() {
|
||||
public void stopGame() {
|
||||
word = "";
|
||||
guesses = new String[guesses.length - 1];
|
||||
}
|
||||
|
|
|
@ -53,7 +53,6 @@ public class WordleServer extends Server {
|
|||
u.setConnectionID(connectionID);
|
||||
if (pMessage.length() < 4) {
|
||||
send(pClientIP, pClientPort, "-ERR Command not valid in this state");
|
||||
|
||||
} else {
|
||||
String m = pMessage.substring(0, 4);
|
||||
System.out.println("Message: " + m + " State: " + u.getState());
|
||||
|
@ -104,7 +103,6 @@ public class WordleServer extends Server {
|
|||
if (m.length() == 5) {
|
||||
if (query.checkWord(m)) {
|
||||
String a = "+OK " + u.getGame().giveGuess(m);
|
||||
System.out.println(u.getGame().getGameStatus());
|
||||
switch (u.getGame().getGameStatus()) {
|
||||
case (1):
|
||||
a += " game continues";
|
||||
|
@ -126,14 +124,27 @@ public class WordleServer extends Server {
|
|||
send(pClientIP, pClientPort, "-ERR wrong format");
|
||||
}
|
||||
} else if (m.equalsIgnoreCase("INFO")) {
|
||||
int n = Integer.parseInt("0" + pMessage.substring(4, pMessage.length()).replaceAll(" ", ""));
|
||||
if (n != 0) {
|
||||
|
||||
} else {
|
||||
|
||||
String substring = "0";
|
||||
if (m.length() > 4) {
|
||||
substring += pMessage.substring(5, pMessage.length()).replaceAll(" ", "");
|
||||
}
|
||||
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")) {
|
||||
|
||||
u.getGame().stopGame();
|
||||
u.setState(3);
|
||||
} else {
|
||||
send(pClientIP, pClientPort, "-ERR Command not valid in this state");
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue