Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.38 KB | None | 0 0
  1. import java.lang.*;
  2. import java.util.*;
  3. import java.util.Random;
  4. /**
  5.  * Day2 exercise.
  6.  * "generate a random String of 5 characters. on each guess, you say how many
  7.  *  letters are the correct letter in the correct spot, and how many letters
  8.  *  are a correct letter but in the wrong spot"
  9.  * @author messa
  10.  */
  11. public class Day2 {
  12.  
  13. public static String generate_random_word(int random_word_leght){
  14. //This class returns "random_word" string of lenght given in "random_word_leght"  
  15.    
  16. //Initialize stringBuilder
  17. StringBuilder stringBuilder = new StringBuilder();
  18.  
  19. String random_word=""; //random word the be generated
  20. for ( int i=0; i < random_word_leght; i++)
  21. {
  22. //Alphabet list to choose characters from.
  23. String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; //52
  24.  
  25. //Get random number from alphabet 0-51 to get the letter. 52 its not a typo.
  26. int  random_number = new Random().nextInt(52);
  27.  
  28. //Choose alphabet letter, convert it from char to string.
  29. String random_letter=Character.toString(alphabet.charAt(random_number));
  30.  
  31. //Join each "random_letter" toghether as "random_word".
  32. stringBuilder.append(random_letter);
  33. random_word= stringBuilder.toString();
  34. }
  35.  
  36. return random_word;
  37. }
  38.    
  39. public static void count_letters(String user_input,String rword){  
  40. //This part of program is counting number of characters from "user_input" and
  41. //"random_word" strings to find:
  42. // - maching letters at the same positions
  43. // - maching letters at all postions
  44.    
  45.  int correct_characters=0;
  46.  int correct_characters_at_same_spot=0;      
  47.  int number=0;  
  48.  
  49.     for ( int i=0; i < user_input.length(); i++)
  50.     {
  51.        //Finding the correct characters at the same spot.
  52.        if(user_input.charAt(i)==rword.charAt(i))
  53.         {
  54.             correct_characters_at_same_spot=correct_characters_at_same_spot+1;
  55.         }
  56.        //Finding the correct characters at all postions.
  57.        while(number<user_input.length()){
  58.        if(user_input.charAt(number)==rword.charAt(i))
  59.        {
  60.             correct_characters=correct_characters+1;
  61.        }
  62.        number=number+1;
  63.        }
  64.     }
  65.     System.out.println("Correct characters: "+correct_characters);
  66.     System.out.println("Correct characters at same spot: "+correct_characters_at_same_spot);          
  67. }
  68.  
  69.  
  70.    
  71.    
  72.    public static void main(String[] args) {
  73.  
  74.         int random_word_leght=2;
  75.         //Generates random word used in game. Lengt passed by argument.
  76.         String rword=generate_random_word(random_word_leght);
  77.        
  78.         //When user enters the corrent answer that will switch to truth;
  79.         //!!!
  80.         //!!!
  81.         boolean correct_answer=false;
  82.  
  83. while (correct_answer==false)
  84.         {
  85.          //Reads the word from user store it as int "input" string.
  86.          System.out.println("Guess the word [5 characters]:");
  87.          Scanner sc = new Scanner(System.in);
  88.          String user_input = sc.next();
  89.          
  90.          //String user_input="aBcD";
  91.          System.out.println(rword);
  92.          System.out.println(user_input);
  93.  
  94.          //Count the letters position and print the output.
  95.          count_letters(user_input,rword);
  96.        
  97.         if(count_letters.correct_characters_at_same_spot == count_letters.user_input.length){
  98.              correct_answer==true;
  99.              System.out.println("CORRECT: ");
  100.         };
  101.        
  102.         }
  103.  
  104.    }
  105.    
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement