Viel hinzugefügt

-Datenbank-Tabellen hinzugefügt (zum letzten Wordle-game und won_on_turn_n Tabelle)
-Ifception fortgesetzt, das geben eines guesses funktioniert nun
-give guess gefixt
This commit is contained in:
Maxim Derksen 2022-03-23 21:01:11 +01:00
parent 81e54b9d39
commit 2c14b051df
5 changed files with 242 additions and 173 deletions

View File

@ -1,13 +1,15 @@
public class Game {
private String word;
private String[] guesses;
private boolean won;
// 0 wenn es keinen Wert gibt, 1 wenn das letzte Spiel gewonnen wurde und 2 wenn
// es verloren wurde
private int won;
// TODO:
Game() {
word = "";
won = false;
guesses = new String[5];
won = 0;
guesses = new String[4];
}
/**
@ -20,18 +22,18 @@ public class Game {
* @param guess ,der String mit dem Wort
* @return Eine Zahlenfolge mit der Korrektheit des Worts
*/
public String giveGuess(String guess) {
StringBuilder g = new StringBuilder(guess);
public String giveGuess(String g) {
StringBuilder guess = new StringBuilder(g);
StringBuilder word = new StringBuilder(this.word);
String s = "";
for (int i = 0; i < word.length(); i++) {
if (g.charAt(0) == word.charAt(0)) {
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 (g.charAt(0) == word.charAt(r)) {
if (guess.charAt(0) == word.charAt(r)) {
s += "1";
found = true;
word.deleteCharAt(r);
@ -42,17 +44,22 @@ public class Game {
s += "0";
}
}
g.deleteCharAt(0);
guess.deleteCharAt(0);
}
if(storeInArray(g.toString())) {
if (word.isEmpty()) {
System.out.println("öasdlkihg");
won = 1;
stopGame();
} else if (storeInArray(guess.toString())) {
won = 2;
stopGame();
won = !word.isEmpty();
}
return s;
}
/**
* Speichert das Wort in der nächsten freien Stelle im Array und gibt danach zurück, ob das Array voll ist
* Speichert das Wort in der nächsten freien Stelle im Array und gibt danach
* zurück, ob das Array voll ist
*
* @param word
* @return Boolean
@ -61,7 +68,7 @@ public class Game {
for (int i = 0; i < guesses.length; i++) {
if (guesses[i] == null) {
guesses[i] = word;
return i == guesses.length - 1;
return false;
}
}
return true;
@ -74,12 +81,40 @@ public class Game {
public boolean isGameRunning() {
return !word.equals("") || !word.equals(null);
}
public boolean won() {
return won;
}
private void stopGame() {
word = "";
guesses = new String[guesses.length - 1];
}
public String[] getGuesses() {
return guesses;
}
public void setGuesses(String[] guesses) {
this.guesses = guesses;
}
public void setWord(String word) {
this.word = word;
}
/**
* Gibt 0 zurück wenn das Spiel nicht läuft und noch kein Spiel gespielt wurde
* seitdem das Objekt existiert. Gibt 1 zurück wenn das Spiel läuft. Gibt 2
* zurück wenn das letzte beendete Spiel gewonnen wurde und kein Spiel läuft.
* Gibt 3 zurück wenn das letzte beendete Spiel verloren wurde und kein Spiel
* läuft.
*
* @return Eine Zahl von 0 bis 3
*/
public int getGameStatus() {
if (word.equals("") && won == 0) {
return 0;
}
if (!word.equals("")) {
return 1;
}
return won + 1;
}
}

View File

@ -4,10 +4,12 @@ public class Query {
Query() {
db = new DatabaseConnector("", 0, "wordle.db", "", "");
}
/**
* Speichert die Daten des Users aus der Datenbank auf dem User-Objekt
* @param user
*/
/**
* Speichert die Daten des Users aus der Datenbank auf dem User-Objekt
*
* @param user
*/
public void pullStats(User user) {
String[][] r;
db.executeStatement("SELECT * FROM user WHERE user LIKE" + user.getUsername());
@ -26,59 +28,64 @@ public class Query {
wonInTurn[5] = Integer.parseInt(r[10][0]);
user.setWonInTurn(wonInTurn);
user.setLastDayWOTDFinished(Integer.parseInt(r[11][0]));
user.setLastDayWOTDPlayed(Integer.parseInt(r[11][0]));
}
/**
* Pusht die Daten des Users im Parameter auf die Datenbank
* @param user
*/
/**
* Pusht die Daten des Users im Parameter auf die Datenbank
*
* @param user
*/
public void pushStats(User user) {
// TODO: Methode korrigieren (Update ist nicht auf den User beschränkt)
// TODO: Methode korrigieren (Update ist nicht auf den User beschränkt) und neue Tabelle lastGuesses berücksichtigen
db.executeStatement("UPDATE INTO Stats (current_streak," + " max_streak, " + "wins, " + "times_played, "
+ "won_on_1, " + "won_on_2, " + "won_on_3, " + "won_on_4, " + "won_on_5, " + "won_on_6, "
+ "last_day_WOTD_finished) " + "VALUES ("
+ Integer.toString(user.getCurrentStreak())
+ Integer.toString(user.getMaxStreak())
+ Integer.toString(user.getWins())
+ Integer.toString(user.getTimesPlayed())
+ Integer.toString(user.getWonInTurnIndex(0))
+ Integer.toString(user.getWonInTurnIndex(1))
+ Integer.toString(user.getWonInTurnIndex(2))
+ Integer.toString(user.getWonInTurnIndex(3))
+ Integer.toString(user.getWonInTurnIndex(4))
+ Integer.toString(user.getWonInTurnIndex(5))
+ Integer.toString(user.getLastDayWOTDFinished())
+ ")");
+ "last_day_WOTD_finished) " + "VALUES (" + Integer.toString(user.getCurrentStreak())
+ Integer.toString(user.getMaxStreak()) + Integer.toString(user.getWins())
+ Integer.toString(user.getTimesPlayed()) + Integer.toString(user.getWonInTurnIndex(0))
+ Integer.toString(user.getWonInTurnIndex(1)) + Integer.toString(user.getWonInTurnIndex(2))
+ Integer.toString(user.getWonInTurnIndex(3)) + Integer.toString(user.getWonInTurnIndex(4))
+ Integer.toString(user.getWonInTurnIndex(5)) + Integer.toString(user.getLastDayWOTDPlayed()) + ")");
}
/**
* Überprüft ob es den Username und das Passwort in der Datenbank gibt und gibt den Erfolg als Boolean zurück
* @param username
* @param password
* @return Ein Boolean
*/
/**
* Überprüft ob es den Username und das Passwort in der Datenbank gibt und gibt
* den Erfolg als Boolean zurück
*
* @param username
* @param password
* @return Ein Boolean
*/
public boolean checkLogin(String username, String password) {
// TODO: Methode fertigstellen
return true;
}
/**
* Gibt ein Zufälliges Wort aus der Datenbank zurück
* @return Ein Word als String
*/
/**
* Gibt ein Zufälliges Wort aus der Datenbank zurück
*
* @return Ein Word als String
*/
public String getWord() {
// TODO: Methode fertigstellen
return "12345";
}
/**
* Gibt das Wordle des Tages, welches aus der Datenbank stammt, zurück. Ergibt pro Tag immer das gleiche Wordle.
* Gibt das Wordle des Tages, welches aus der Datenbank stammt, zurück. Ergibt
* pro Tag immer das gleiche Wordle.
*
* @return Das Wort als String
*/
public String getWOTD() {
// TODO: Methode fertigstellen
return "abcde";
}
/**
* Prüft ob das Wort in der Datenbank vorhanden ist
*
* @param word ,ein String mit dem Wort
* @return Boolean
*/

209
User.java
View File

@ -1,123 +1,138 @@
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class User {
private String connectionID;
private String username;
private String password;
private int timesPlayed;
private int winPercentage;
private int currentStreak;
private int maxStreak;
private int lastDayWOTDFinished;
private int state;
private int wins;
private int [] wonInTurn;
private Game game;
private String connectionID;
private String username;
private String password;
private int timesPlayed;
private int winPercentage;
private int currentStreak;
private int maxStreak;
private int lastDayWOTDPlayed;
private int state;
private int wins;
private int[] wonInTurn;
private Game game;
public User (String pConnectionID){
connectionID = pConnectionID;
game = new Game();
}
public Game getGame(){
return game;
}
public String getConnectionID() {
return connectionID;
}
public User(String pConnectionID) {
connectionID = pConnectionID;
game = new Game();
}
public int getCurrentStreak() {
return currentStreak;
}
public Game getGame() {
return game;
}
public int getLastDayWOTDFinished() {
return lastDayWOTDFinished;
}
public String getConnectionID() {
return connectionID;
}
public int getMaxStreak() {
return maxStreak;
}
public int getCurrentStreak() {
return currentStreak;
}
public int getTimesPlayed() {
return timesPlayed;
}
public int getLastDayWOTDPlayed() {
return lastDayWOTDPlayed;
}
public int getState() {
return state;
}
public int getMaxStreak() {
return maxStreak;
}
public void setWins(int wins) {
this.wins = wins;
}
public int getTimesPlayed() {
return timesPlayed;
}
public int getWinPercentage() {
return winPercentage;
}
public int getState() {
return state;
}
public int getWonInTurnIndex(int index) {
return wonInTurn[index];
}
public void setWins(int wins) {
this.wins = wins;
}
public int[] getWonInTurn() {
return wonInTurn;
}
public int getWinPercentage() {
return winPercentage;
}
public String getPassword() {
return password;
}
public int getWonInTurnIndex(int index) {
return wonInTurn[index];
}
public String getUsername() {
return username;
}
public int[] getWonInTurn() {
return wonInTurn;
}
public int getWins() {
return wins;
}
public String getPassword() {
return password;
}
public void setConnectionID(String connectionID) {
this.connectionID = connectionID;
}
public String getUsername() {
return username;
}
public void setCurrentStreak(int currentStreak) {
this.currentStreak = currentStreak;
}
public int getWins() {
return wins;
}
public void setLastDayWOTDFinished(int lastDayWOTDFinished) {
this.lastDayWOTDFinished = lastDayWOTDFinished;
}
public void setConnectionID(String connectionID) {
this.connectionID = connectionID;
}
public void setMaxStreak(int maxStreak) {
this.maxStreak = maxStreak;
}
public void setCurrentStreak(int currentStreak) {
this.currentStreak = currentStreak;
}
public void setPassword(String password) {
this.password = password;
}
public void updateLastDayWOTDPlayed() {
lastDayWOTDPlayed = (int) ChronoUnit.DAYS.between(LocalDate.ofEpochDay(0), LocalDate.now());
}
public void setState(int state) {
this.state = state;
}
public void setMaxStreak(int maxStreak) {
this.maxStreak = maxStreak;
}
public void setTimesPlayed(int timesPlayed) {
this.timesPlayed = timesPlayed;
}
public void setPassword(String password) {
this.password = password;
}
public void setUsername(String username) {
this.username = username;
}
public void setState(int state) {
this.state = state;
}
public void setWinPercentage(int winPercentage) {
this.winPercentage = winPercentage;
}
public void setTimesPlayed(int timesPlayed) {
this.timesPlayed = timesPlayed;
}
public void setWonInTurnIndex(int index, int content) {
this.wonInTurn[index] = content;
}
public void setUsername(String username) {
this.username = username;
}
public void setWonInTurn(int[] wonInTurn) {
this.wonInTurn = wonInTurn;
}
public boolean loggedIn() {
return password != null && username != null;
}
public void setWinPercentage(int winPercentage) {
this.winPercentage = winPercentage;
}
public void setWonInTurnIndex(int index, int content) {
this.wonInTurn[index] = content;
}
public void setWonInTurn(int[] wonInTurn) {
this.wonInTurn = wonInTurn;
}
public boolean loggedIn() {
return password != null && username != null;
}
public String[] getLastGuesses() {
return game.getGuesses();
}
public void setLastGuesses(String[] guesses) {
game.setGuesses(guesses);
}
public void setLastDayWOTDPlayed(int time) {
lastDayWOTDPlayed = time;
}
}

View File

@ -14,8 +14,6 @@ public class WordleServer extends Server {
new WordleServer();
}
private DatabaseConnector db;
private HashMap<String, User> users;
private Query query;
@ -26,7 +24,6 @@ public class WordleServer extends Server {
public WordleServer(int pPort) {
super(pPort);
query = new Query();
db = new DatabaseConnector("", 0, "wordle.db", "", "");
users = new HashMap<>();
}
@ -42,7 +39,11 @@ public class WordleServer extends Server {
@Override
public void processClosingConnection(String pClientIP, int pClientPort) {
String connectionID = pClientIP + ":" + Integer.toString(pClientPort);
User u = users.get(connectionID);
if (u.loggedIn()) {
query.pushStats(u);
}
}
@Override
@ -56,7 +57,7 @@ public class WordleServer extends Server {
} else {
String m = pMessage.substring(0, 4);
System.out.println("Message: " + m + " State: " + u.getState());
if (pMessage.equals("QUIT")) {
if (pMessage.equalsIgnoreCase("QUIT")) {
send(pClientIP, pClientPort, "+OK Bye");
close();
}
@ -73,7 +74,6 @@ public class WordleServer extends Server {
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("");
@ -81,10 +81,18 @@ public class WordleServer extends Server {
}
}
} 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");
if (!isToday(u.getLastDayWOTDPlayed())) {
send(pClientIP, pClientPort, "+OK game ready");
u.getGame().startGame(query.getWOTD());
u.setState(2);
u.updateLastDayWOTDPlayed();
} else if (u.getGame().getGameStatus() == 1) {
send(pClientIP, pClientPort, "+OK Game continues");
u.setState(2);
u.updateLastDayWOTDPlayed();
} else {
send(pClientIP, pClientPort, "-ERR game finished");
}
} else {
send(pClientIP, pClientPort, "-ERR Command not valid in this state");
}
@ -93,17 +101,22 @@ public class WordleServer extends Server {
// Spielphase
if (m.equalsIgnoreCase("SEND")) {
m = pMessage.substring(5, pMessage.length());
if(m.length() == 5) {
if(query.checkWord(m)) {
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";
System.out.println(u.getGame().getGameStatus());
switch (u.getGame().getGameStatus()) {
case (1):
a += " game continues";
break;
case (2):
a += " game won";
u.setState(3);
break;
case (3):
a += " game lost";
u.setState(3);
break;
}
send(pClientIP, pClientPort, a);
} else {
@ -115,7 +128,7 @@ public class WordleServer extends Server {
} else if (m.equalsIgnoreCase("INFO")) {
int n = Integer.parseInt("0" + pMessage.substring(4, pMessage.length()).replaceAll(" ", ""));
if (n != 0) {
} else {
}
@ -137,7 +150,7 @@ public class WordleServer extends Server {
+ 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())) {
if (isToday(u.getLastDayWOTDPlayed()) && u.getGame().getGameStatus() != 1) {
u.getGame().startGame(query.getWord());
u.setState(2);
send(pClientIP, pClientPort, "+OK Game ready");
@ -150,11 +163,10 @@ public class WordleServer extends Server {
break;
}
}
}
public boolean isToday(int pDay) {
LocalDate now = LocalDate.now();
LocalDate epoch = LocalDate.ofEpochDay(0);
return pDay == ChronoUnit.DAYS.between(epoch, now);
return pDay == (int) ChronoUnit.DAYS.between(LocalDate.ofEpochDay(0), LocalDate.now());
}
}

BIN
wordle.db

Binary file not shown.