Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- public class Bsp07 {
- public static int count;
- public static String word = "Winter";
- public static char level = SavitchIn.readChar();
- public static void main(String[] args) {
- System.out.println("Guess a word!");
- System.out.println("Level (e(asy), m(edium), d(ifficult), q(uit)): " + level);
- setProblem(word);
- int asterisk = 1; //am Anfang muss man immer mindestens einen Buchstaben erraten
- int versuche = 0;
- char[] help_word = cover(count);
- while(asterisk == 1) { //es gibt noch Buchstaben zu erraten
- System.out.print("Word: ");
- System.out.println(help_word);
- System.out.print("Guess: ");
- char guess = SavitchIn.readChar();
- System.out.println();
- /*if(guess == 'q') {
- break;
- }*/
- help_word = uncover(help_word, guess);
- for(int i = 0; i < help_word.length; i++) {
- if(help_word[i] == '*') {
- asterisk = 1;
- break;
- } else asterisk = 0;
- }
- versuche ++;
- }
- System.out.println("Word: " + word);
- System.out.println("You made it in " + versuche + " tries!");
- }
- public static void setProblem(String word) { //Defines the problem word to be guessed. @param word the "secret" word
- double help = 0;
- if(level == 'e') {
- help = 0.25;
- } else if(level == 'm') {
- help = 0.5;
- } else help = 0.75;
- count = (int) (word.length() * help);
- if(count == 0) {
- count = 1;
- }
- }
- public static char[] cover(int count) {
- char[] cover = word.toCharArray();
- Random rnd = new Random();
- int i = 0;
- while(i < count) {
- int randomIndex = rnd.nextInt(cover.length); //erzeugt eine zufaellige ganze Zahl im Intervall [0; cover Laenge)
- if(cover[randomIndex] != '*') {
- cover[randomIndex] = '*';
- i++;
- }
- }
- return cover;
- }
- public static char[] uncover(char[] cover, char guess) {
- String guess_help = Character.toString(guess);
- char help, help_GK;
- if(guess_help.equals(guess_help.toLowerCase())) { //Versuch ist ein Kleinbuchstabe
- help = guess_help.charAt(0); //nur ein Buchstabe pro Versuch
- help_GK = guess_help.toUpperCase().charAt(0);
- } else { //Versuch ist ein Grossbuchstabe
- help = guess_help.charAt(0);
- help_GK = guess_help.toLowerCase().charAt(0);
- }
- for(int i = 0; i < cover.length; i++) {
- if(word.charAt(i) == help){
- cover[i] = help;
- } else if(word.charAt(i) == help_GK){
- cover[i] = help_GK;
- }
- }
- return cover;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement