forked from IF-LK-2020/wordle
Ifception der Wordle-Server Klasse
An der Bearbeitung der Abfragen gearbeitet, versucht die giveGuess-Methode zu fixen
This commit is contained in:
parent
02d6252537
commit
81e54b9d39
30
Game.java
30
Game.java
|
@ -1,14 +1,13 @@
|
|||
public class Game {
|
||||
private String word;
|
||||
private String[] guesses;
|
||||
private boolean won;
|
||||
|
||||
// TODO:
|
||||
Game() {
|
||||
word = "";
|
||||
}
|
||||
|
||||
public void startGame() {
|
||||
|
||||
won = false;
|
||||
guesses = new String[5];
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -22,17 +21,17 @@ public class Game {
|
|||
* @return Eine Zahlenfolge mit der Korrektheit des Worts
|
||||
*/
|
||||
public String giveGuess(String guess) {
|
||||
String g = guess;
|
||||
StringBuilder g = new StringBuilder(guess);
|
||||
StringBuilder word = new StringBuilder(this.word);
|
||||
String s = "";
|
||||
for (int i = 0; i < word.length(); i++) {
|
||||
if (guess.charAt(0) == word.charAt(0)) {
|
||||
if (g.charAt(0) == word.charAt(0)) {
|
||||
word.deleteCharAt(0);
|
||||
s += "2";
|
||||
} else {
|
||||
boolean found = false;
|
||||
for (int r = 0; r < word.length(); r++) {
|
||||
if (r != i && guess.charAt(0) == word.charAt(r)) {
|
||||
for (int r = 1; r < word.length(); r++) {
|
||||
if (g.charAt(0) == word.charAt(r)) {
|
||||
s += "1";
|
||||
found = true;
|
||||
word.deleteCharAt(r);
|
||||
|
@ -43,10 +42,11 @@ public class Game {
|
|||
s += "0";
|
||||
}
|
||||
}
|
||||
guess.replaceFirst(guess, "");
|
||||
g.deleteCharAt(0);
|
||||
}
|
||||
if(storeInArray(g)) {
|
||||
if(storeInArray(g.toString())) {
|
||||
stopGame();
|
||||
won = !word.isEmpty();
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
@ -67,12 +67,16 @@ public class Game {
|
|||
return true;
|
||||
}
|
||||
|
||||
public void startWOTD(String wotd) {
|
||||
word = wotd;
|
||||
public void startGame(String word) {
|
||||
this.word = word;
|
||||
}
|
||||
|
||||
public boolean isGameRunning() {
|
||||
return word.equals("") || word.equals(null);
|
||||
return !word.equals("") || !word.equals(null);
|
||||
}
|
||||
|
||||
public boolean won() {
|
||||
return won;
|
||||
}
|
||||
|
||||
private void stopGame() {
|
||||
|
|
|
@ -59,7 +59,7 @@ public class Query {
|
|||
*/
|
||||
public boolean checkLogin(String username, String password) {
|
||||
// TODO: Methode fertigstellen
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* Gibt ein Zufälliges Wort aus der Datenbank zurück
|
||||
|
@ -67,7 +67,7 @@ public class Query {
|
|||
*/
|
||||
public String getWord() {
|
||||
// TODO: Methode fertigstellen
|
||||
return "";
|
||||
return "12345";
|
||||
}
|
||||
/**
|
||||
* Gibt das Wordle des Tages, welches aus der Datenbank stammt, zurück. Ergibt pro Tag immer das gleiche Wordle.
|
||||
|
@ -75,7 +75,7 @@ public class Query {
|
|||
*/
|
||||
public String getWOTD() {
|
||||
// TODO: Methode fertigstellen
|
||||
return "";
|
||||
return "abcde";
|
||||
}
|
||||
/**
|
||||
* Prüft ob das Wort in der Datenbank vorhanden ist
|
||||
|
|
|
@ -116,4 +116,8 @@ public class User {
|
|||
public void setWonInTurn(int[] wonInTurn) {
|
||||
this.wonInTurn = wonInTurn;
|
||||
}
|
||||
|
||||
public boolean loggedIn() {
|
||||
return password != null && username != null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
|
@ -48,84 +50,111 @@ public class WordleServer extends Server {
|
|||
String connectionID = pClientIP + ":" + Integer.toString(pClientPort);
|
||||
User u = users.get(connectionID);
|
||||
u.setConnectionID(connectionID);
|
||||
if (pMessage.length() < 4) {
|
||||
send(pClientIP, pClientPort, "-ERR Command not valid in this state");
|
||||
|
||||
if (pMessage.equals("QUIT")) {
|
||||
send(pClientIP, pClientPort, "+OK Bye");
|
||||
close();
|
||||
}
|
||||
|
||||
String m = pMessage.substring(0, 3);
|
||||
switch (u.getState()) {
|
||||
case 1:
|
||||
// Anmeldephase
|
||||
if (m.equalsIgnoreCase("USER")) {
|
||||
m = pMessage.substring(6, pMessage.length() - 1);
|
||||
u.setUsername(m);
|
||||
send(pClientIP, pClientPort, "+OK");
|
||||
} else if (m.equalsIgnoreCase("PASS")) {
|
||||
m = pMessage.substring(6, pMessage.length() - 1);
|
||||
u.setPassword(m);
|
||||
if (u.getPassword() != null && u.getUsername() != null) {
|
||||
if (query.checkLogin(u.getUsername(), u.getPassword())) {
|
||||
send(pClientIP, pClientPort, "+OK welcome");
|
||||
u.setState(3);
|
||||
} else {
|
||||
send(pClientIP, pClientPort, "+ERR login not valid");
|
||||
} else {
|
||||
String m = pMessage.substring(0, 4);
|
||||
System.out.println("Message: " + m + " State: " + u.getState());
|
||||
if (pMessage.equals("QUIT")) {
|
||||
send(pClientIP, pClientPort, "+OK Bye");
|
||||
close();
|
||||
}
|
||||
switch (u.getState()) {
|
||||
case 1:
|
||||
// Anmeldephase
|
||||
if (m.equalsIgnoreCase("USER")) {
|
||||
m = pMessage.substring(5, pMessage.length() - 1);
|
||||
u.setUsername(m);
|
||||
send(pClientIP, pClientPort, "+OK");
|
||||
} else if (m.equalsIgnoreCase("PASS")) {
|
||||
m = pMessage.substring(5, pMessage.length() - 1);
|
||||
u.setPassword(m);
|
||||
if (u.getPassword() != null && u.getUsername() != null) {
|
||||
if (query.checkLogin(u.getUsername(), u.getPassword())) {
|
||||
send(pClientIP, pClientPort, "+OK welcome");
|
||||
u.setState(3);
|
||||
} else {
|
||||
u.setPassword("");
|
||||
u.setUsername("");
|
||||
send(pClientIP, pClientPort, "-ERR login not valid");
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if(m.equalsIgnoreCase("WOTD")) {
|
||||
//TODO: Prüfen, wie weit der Spieler schon mit dem Spiel ist
|
||||
u.setState(2);
|
||||
send(pClientIP, pClientPort, "+OK Game ready");
|
||||
} else if(m.equalsIgnoreCase("PLAY")) {
|
||||
|
||||
} else {
|
||||
send(pClientIP, pClientPort, "-ERR Command not valid in this state");
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
// Spielphase
|
||||
if(m.equalsIgnoreCase("SEND")) {
|
||||
|
||||
} else if(m.equalsIgnoreCase("INFO")) {
|
||||
int n = Integer.parseInt("0" + pMessage.substring(4,pMessage.length()).replaceAll(" ", ""));
|
||||
if(n != 0) {
|
||||
|
||||
} else if (m.equalsIgnoreCase("WOTD")) {
|
||||
// TODO: Prüfen, wie weit der Spieler schon mit dem Spiel ist
|
||||
u.getGame().startGame(query.getWOTD());
|
||||
u.setState(2);
|
||||
send(pClientIP, pClientPort, "+OK Game ready");
|
||||
} else {
|
||||
|
||||
send(pClientIP, pClientPort, "-ERR Command not valid in this state");
|
||||
}
|
||||
} else if(m.equalsIgnoreCase("EXIT")) {
|
||||
|
||||
} else {
|
||||
send(pClientIP, pClientPort, "-ERR Command not valid in this state");
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
// Zwischenphase(Angemeldet)
|
||||
if(m.equalsIgnoreCase("STAT")) {
|
||||
send(pClientIP, pClientPort, "+OK" +
|
||||
'\n' + "times played: " + u.getTimesPlayed() +
|
||||
'\n' + "win percentage: " + u.getWinPercentage() +
|
||||
'\n' + "current streak: " + u.getCurrentStreak() +
|
||||
'\n' + "max streak: " + u.getMaxStreak() +
|
||||
'\n' + "won in 1 turn: " + u.getWonInTurnIndex(1) +
|
||||
'\n' + "won in 2 turn: " + u.getWonInTurnIndex(2) +
|
||||
'\n' + "won in 3 turn: " + u.getWonInTurnIndex(3) +
|
||||
'\n' + "won in 4 turn: " + u.getWonInTurnIndex(4) +
|
||||
'\n' + "won in 5 turn: " + u.getWonInTurnIndex(5) +
|
||||
'\n' + "won in 6 turn: " + u.getWonInTurnIndex(6) +
|
||||
'\n' + ".");
|
||||
} else if(m.equalsIgnoreCase("PLAY")) {
|
||||
|
||||
} else {
|
||||
send(pClientIP, pClientPort, "-ERR Command not valid in this state");
|
||||
}
|
||||
break;
|
||||
break;
|
||||
case 2:
|
||||
// Spielphase
|
||||
if (m.equalsIgnoreCase("SEND")) {
|
||||
m = pMessage.substring(5, pMessage.length());
|
||||
if(m.length() == 5) {
|
||||
if(query.checkWord(m)) {
|
||||
String a = "+OK " + u.getGame().giveGuess(m);
|
||||
if(!u.getGame().isGameRunning()) {
|
||||
if(u.getGame().won()) {
|
||||
a += " game won";
|
||||
} else {
|
||||
a += " game lost";
|
||||
}
|
||||
} else {
|
||||
a += " game continues";
|
||||
}
|
||||
send(pClientIP, pClientPort, a);
|
||||
} else {
|
||||
send(pClientIP, pClientPort, "-ERR word not in dict");
|
||||
}
|
||||
} else {
|
||||
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 {
|
||||
|
||||
}
|
||||
} else if (m.equalsIgnoreCase("EXIT")) {
|
||||
|
||||
} else {
|
||||
send(pClientIP, pClientPort, "-ERR Command not valid in this state");
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
// Zwischenphase(Angemeldet)
|
||||
if (m.equalsIgnoreCase("STAT")) {
|
||||
send(pClientIP, pClientPort,
|
||||
"+OK" + '\n' + "times played: " + u.getTimesPlayed() + '\n' + "win percentage: "
|
||||
+ u.getWinPercentage() + '\n' + "current streak: " + u.getCurrentStreak() + '\n'
|
||||
+ "max streak: " + u.getMaxStreak() + '\n' + "won in 1 turn: "
|
||||
+ u.getWonInTurnIndex(1) + '\n' + "won in 2 turn: " + u.getWonInTurnIndex(2) + '\n'
|
||||
+ "won in 3 turn: " + u.getWonInTurnIndex(3) + '\n' + "won in 4 turn: "
|
||||
+ u.getWonInTurnIndex(4) + '\n' + "won in 5 turn: " + u.getWonInTurnIndex(5) + '\n'
|
||||
+ "won in 6 turn: " + u.getWonInTurnIndex(6) + '\n' + ".");
|
||||
} else if (m.equalsIgnoreCase("PLAY")) {
|
||||
if (isToday(u.getLastDayWOTDFinished())) {
|
||||
u.getGame().startGame(query.getWord());
|
||||
u.setState(2);
|
||||
send(pClientIP, pClientPort, "+OK Game ready");
|
||||
} else {
|
||||
send(pClientIP, pClientPort, "-ERR wotd not finished");
|
||||
}
|
||||
} else {
|
||||
send(pClientIP, pClientPort, "-ERR Command not valid in this state");
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public boolean isToday(int pDay) {
|
||||
return pDay == (int) (86400000.0 / Instant.now().toEpochMilli());
|
||||
LocalDate now = LocalDate.now();
|
||||
LocalDate epoch = LocalDate.ofEpochDay(0);
|
||||
return pDay == ChronoUnit.DAYS.between(epoch, now);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue