diff --git a/Semesteraufgabe/.classpath b/Semesteraufgabe/.classpath new file mode 100644 index 0000000..cb2aa0c --- /dev/null +++ b/Semesteraufgabe/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/Semesteraufgabe/.project b/Semesteraufgabe/.project new file mode 100644 index 0000000..dd44a67 --- /dev/null +++ b/Semesteraufgabe/.project @@ -0,0 +1,17 @@ + + + Semesteraufgabe + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/Semesteraufgabe/src/Hangman.java b/Semesteraufgabe/src/Hangman.java new file mode 100644 index 0000000..5cb0750 --- /dev/null +++ b/Semesteraufgabe/src/Hangman.java @@ -0,0 +1,152 @@ +import java.io.File; +import java.io.FileNotFoundException; +import java.util.ArrayList; +import java.util.Scanner; + +public class Hangman { + + static Scanner sc = new Scanner(System.in); + + public static void main(String[] args) throws FileNotFoundException { + + Scanner sc1 = new Scanner(new File("src/words.txt")); + + // Setup + ArrayList words = new ArrayList<>(); + ArrayList wordGuess = new ArrayList<>(); + while (sc1.hasNext()) { + words.add(sc1.nextLine()); + } + + // Wählt ein zufälliges Wort aus der Dazei + String word = words.get((int) (Math.random() * words.size())); + char[] wordSplit = word.toCharArray(); + + String guess = ""; + String charUsed = ""; + int lives = 6; + // Zählt wie viele Felder noch frei sind + int counter = 0; + + // Schleife läufz solange, wie mehr als 0 Leben vorhanden sind + while (lives > 0) { + + // Ausgeben des erratendem Fortschritts + System.out.println(getProgress(guess, word, wordSplit)); + counter = getProgress1(counter, guess, word); + + // Gewinnbedingung + if (counter == 0) { + System.out.println("Congratulations! You won."); + break; + } + + System.out.println(); + System.out.println("Letters used:" + " " + charUsed); + System.out.print("Words used: "); + for (int i = 0; i < wordGuess.size(); i++) { + System.out.print(wordGuess.get(i) + ", "); + } + System.out.println(); + + + // Eingabe zum Erraten + String letter = getInput(); + + if (letter.length() == 1) { + + // Überprüfen ob die Eingabe bereits versucht wurde + if (charUsed.contains(letter)) { + System.out.println("Letter already used. Try again:"); + continue; + } + + charUsed += letter + ", "; + + // Überprüfen ob das Wort den erratenen Buchstaben enthält + if (word.contains(letter)) { + guess += letter; + } else { + System.out.println("Wrong! The word doesn't contain the letter " + letter); + lives--; + } + + // Hangman Figur/Leben und Verlierbedingung + System.out.println(showMistakes(lives, word)); + + if (lives == 0) { + break; + } + + } else if (letter.length() == word.length()) { + // Gewinnbedingung falls ein Wort geraten wird + if(letter.equals(word)) { + System.out.println("Congratulations! You won."); + break; + } + // Fügt die falsch geratenen Wörter zu einer ArrayList hinzu + else { + System.out.println("Wrong! The word " + letter + " is not the correct answer"); + wordGuess.add(letter); + lives--; + } + System.out.println(showMistakes(lives, word)); + } else { + System.out.println("Ungültige Eingabe\nTry Again!"); + continue; + } + } + sc.close(); + sc1.close(); + } + + // Gibt das Wort mit den bereits erratenen Buchstaben aus + public static String getProgress(String guess, String word, char[] wordSplit) { + String character = ""; + for (int i = 0; i < word.length(); i++) { + if (guess.contains(String.valueOf(word.charAt(i)))) { + character += wordSplit[i] + " "; + } else { + character += "_ "; + } + } + return character; + } + + // Zählt die bereits erratenen Zeichen + public static int getProgress1(int counter, String guess, String word) { + counter = 0; + for (int i = 0; i < word.length(); i++) { + if (guess.contains(String.valueOf(word.charAt(i)))) { + } else { + counter++; + } + } + return counter; + } + + public static String getInput() { + System.out.print("Enter a letter:\n>>"); + return sc.nextLine(); + } + + // Liefert einen String je nach Lebensanzahl + public static String showMistakes(int lives, String word) { + String result = ""; + if (lives == 5) { + result = "\n\n\n\n\n\n\n___|___"; + } else if (lives == 4) { + result = " |\n |\n |\n |\n |\n |\n |\n___|___"; + } else if (lives == 3) { + result = " _________\n |\n |\n |\n |\n |\n |\n___|___"; + } else if (lives == 2) { + result = " _________\n |/\n |\n |\n |\n |\n |\n___|___"; + } else if (lives == 1) { + result = " _________\n |/ |\n |\n |\n |\n |\n |\n___|___"; + } else if (lives == 0) { + result = " _________\n |/ |\n | O\n | \\|/\n | |\n | / \\\n |\n___|___\nYou lose! The word was " + + word; + } + return result; + } +} \ No newline at end of file diff --git a/Semesteraufgabe/src/words.txt b/Semesteraufgabe/src/words.txt new file mode 100644 index 0000000..859f338 --- /dev/null +++ b/Semesteraufgabe/src/words.txt @@ -0,0 +1,100 @@ +freedom +happiness +knowledge +success +opportunity +independence +friendship +courage +honesty +loyalty +leadership +creativity +determination +justice +fairness +respect +responsibility +kindness +patience +generosity +compassion +forgiveness +gratitude +tolerance +understanding +integrity +confidence +security +safety +prosperity +wealth +prosperity +abundance +prosperity +richness +affluence +success +victory +triumph +glory +honor +fame +reputation +respect +admiration +esteem +awe +reverence +dignity +prestige +celebrity +honor +glory +distinction +excellence +achievement +merit +worth +value +importance +significance +magnitude +gravity +consequence +consequence +effect +impact +influence +power +authority +control +command +sway +domination +leadership +supervision +management +direction +guidance +tutelage +instruction +teaching +education +learning +study +research +investigation +inquiry +examination +analysis +appraisal +evaluation +assessment +judgement +discernment +perception +understanding +insight +wisdom +enlightenment \ No newline at end of file