commit
main
oi 2023-01-11 14:29:31 +01:00
parent 807866179e
commit db9aebd3a5
4 changed files with 279 additions and 0 deletions

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-18">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Semesteraufgabe</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@ -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<String> words = new ArrayList<>();
ArrayList<String> 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;
}
}

View File

@ -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