Java Hangman

By | 31. Oktober 2013

Wenn man einfach mal Lust hat, etwas Kleines zu programmieren, nimmt man gerne einen alten Klassiker. Ich habe mich für Hangman (“Galgenmännchen”) entschieden. Diese Version ist sehr minimalistisch gehalten. Vielleicht füge ich demnächst noch eine GUI hinzu.

Eine Liste mit über 5000 Wörtern, passend für das Spiel:

Download
words.txt | 52 kB

View.java

public class View {

	public View(){
		Core core = new Core();
		core.newRound();	 
	}
	
	public static void main(String args[]) {
		new View();
	}
}

Core.java

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
 
public class Core {
 
	private List<Word> words = new ArrayList<>();
	private Word round;
	 
	public Core() {
		loadWordList();
	}
	 
	public void loadWordList() {
		String line = "";
		try {
			File file = new File(this.getClass().getClassLoader()
			.getResource("words.txt").getFile());
			@SuppressWarnings("resource")
			BufferedReader in = new BufferedReader(new FileReader(file));
			while ((line = in.readLine()) != null) {
				words.add(new Word(line));
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	 
	public void newRound() {
		round = words.get(new Random().nextInt(words.size()));
		while (!round.checkIfFinished()) {
			System.out.println(String.valueOf(round.getToSolve()));
			round.guessChar(readChar());
			if (round.getGuessCounter()>8) {
				break;
			}
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		if (round.checkIfFinished()) {
			System.out.println("Gewonnen!");
		} else {
			System.out.println("Verloren!");
			System.out.println("Das gesuchte Wort lautete: " + String.valueOf(round.getWord()));
		}
	}
	 
	public char readChar() {
		BufferedReader read = new BufferedReader(new InputStreamReader(
		System.in));
		String input = new String();
		try {
			input = read.readLine();
		} catch (java.io.IOException e) {
			System.out.println(e);
		}
		char back = input.charAt(0);
		return back;
	}
}

Word.java

public class Word {
 
	private char[] word;
	private int size;
	private int guessCounter = 0;
	 
	private char[] toSolve;
	 
	public Word(String word) {
		this.word = word.toUpperCase().toCharArray();
		size = this.word.length;
		toSolve = new char[size];
		for (int i = 0; i < toSolve.length; i++) {
			toSolve[i] = '_';
		}
	}
	 
	public char[] getWord() {
		return word;
	}
	 
	public int getSize() {
		return size;
	}
	 
	public char[] getToSolve() {
		return toSolve;
	}
	 
	public int getGuessCounter() {
		return guessCounter;
	}
	 
	public boolean guessChar(char c){
		boolean guess = false;
		c = Character.toUpperCase(c);
		for (int i=0; i<word.length; i++) {
			if (c==word[i]) {
				toSolve[i] = c;
				guess = true;
			}
		}
		if (guess) {
			System.out.println("Der Buchstabe " + c + " war dabei!");
		} else {
			guessCounter++;
			System.out.println("Der Buchstabe " + c + " war nicht dabei!");
		}
		return guess;
	}
	 
	public boolean checkIfFinished(){
		boolean finished = true;
		for (int i = 0; i < toSolve.length; i++) {
			if (toSolve[i]== '_') {
				finished = false;
			}
		}
		return finished;
	} 
}

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert