From 9ecd839df1c351fd0a5363608e89ed2c8096ab6d Mon Sep 17 00:00:00 2001 From: Maxim Derksen Date: Thu, 24 Mar 2022 11:33:46 +0100 Subject: [PATCH] Befehl Info und Exit Info und Exit implementiert sowie eine neue Methode checkWord() in Game erstellt --- Game.java | 30 ++++++++++++++++++++++++++++-- WordleServer.java | 31 +++++++++++++++++++++---------- 2 files changed, 49 insertions(+), 12 deletions(-) diff --git a/Game.java b/Game.java index 08567a9..232a472 100644 --- a/Game.java +++ b/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]; } diff --git a/WordleServer.java b/WordleServer.java index 9fa072a..54686f3 100644 --- a/WordleServer.java +++ b/WordleServer.java @@ -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()); @@ -65,11 +64,11 @@ public class WordleServer extends Server { case 1: // Anmeldephase if (m.equalsIgnoreCase("USER")) { - m = pMessage.substring(5, pMessage.length() - 1); + m = pMessage.substring(5, pMessage.length()); u.setUsername(m); send(pClientIP, pClientPort, "+OK"); } else if (m.equalsIgnoreCase("PASS")) { - m = pMessage.substring(5, pMessage.length() - 1); + m = pMessage.substring(5, pMessage.length()); u.setPassword(m); if (u.getPassword() != null && u.getUsername() != null) { if (query.checkLogin(u.getUsername(), u.getPassword())) { @@ -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"); }