Advertisement
Guest User

Untitled

a guest
Nov 21st, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.50 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class Bsp07 {
  4.    
  5.     public static int count;
  6.     public static String word = "Winter";
  7.     public static char level = SavitchIn.readChar();
  8.  
  9.     public static void main(String[] args) {
  10.        
  11.         System.out.println("Guess a word!");       
  12.         System.out.println("Level (e(asy), m(edium), d(ifficult), q(uit)): " + level);
  13.        
  14.         setProblem(word);
  15.  
  16.         int asterisk = 1;       //am Anfang muss man immer mindestens einen Buchstaben erraten
  17.         int versuche = 0;
  18.         char[] help_word = cover(count);
  19.        
  20.         while(asterisk == 1) {      //es gibt noch Buchstaben zu erraten
  21.             System.out.print("Word: ");
  22.             System.out.println(help_word);
  23.             System.out.print("Guess: ");
  24.             char guess = SavitchIn.readChar();
  25.             System.out.println();
  26.            
  27.             /*if(guess == 'q') {
  28.                 break;
  29.             }*/
  30.            
  31.             help_word = uncover(help_word, guess);
  32.            
  33.            
  34.             for(int i = 0; i < help_word.length; i++) {
  35.                 if(help_word[i] == '*') {
  36.                     asterisk = 1;
  37.                     break;
  38.                 } else asterisk = 0;
  39.             }
  40.                    
  41.             versuche ++;
  42.         }
  43.        
  44.         System.out.println("Word: " + word);
  45.         System.out.println("You made it in " + versuche + " tries!");
  46.     }
  47.    
  48.     public static void setProblem(String word) {        //Defines the problem word to be guessed.        @param word           the "secret" word
  49.  
  50.         double help = 0;
  51.         if(level == 'e') {
  52.             help = 0.25;
  53.         } else if(level == 'm') {
  54.             help = 0.5;
  55.         } else help = 0.75;
  56.        
  57.         count = (int) (word.length() * help);
  58.        
  59.         if(count == 0) {
  60.             count = 1;
  61.         }
  62.     }
  63.  
  64.     public static char[] cover(int count) {
  65.         char[] cover = word.toCharArray();
  66.         Random rnd = new Random();
  67.        
  68.         int i = 0;
  69.         while(i < count) {
  70.             int randomIndex = rnd.nextInt(cover.length);        //erzeugt eine zufaellige ganze Zahl im Intervall [0; cover Laenge)
  71.             if(cover[randomIndex] != '*') {
  72.                 cover[randomIndex] = '*';
  73.                 i++;
  74.             }
  75.         }
  76.        
  77.         return cover;
  78.     }
  79.    
  80.     public static char[] uncover(char[] cover, char guess) {
  81.         String guess_help = Character.toString(guess);
  82.        
  83.         char help, help_GK;
  84.         if(guess_help.equals(guess_help.toLowerCase())) {       //Versuch ist ein Kleinbuchstabe
  85.             help = guess_help.charAt(0);        //nur ein Buchstabe pro Versuch
  86.             help_GK = guess_help.toUpperCase().charAt(0);
  87.         } else {        //Versuch ist ein Grossbuchstabe
  88.             help = guess_help.charAt(0);
  89.             help_GK = guess_help.toLowerCase().charAt(0);
  90.         }
  91.        
  92.         for(int i = 0; i < cover.length; i++) {
  93.             if(word.charAt(i) == help){
  94.                 cover[i] = help;
  95.             } else if(word.charAt(i) == help_GK){
  96.                 cover[i] = help_GK;
  97.             }
  98.         }
  99.        
  100.         return cover;
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement