Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.*;
- import java.util.*;
- import java.util.Random;
- /**
- * Day2 exercise.
- * "generate a random String of 5 characters. on each guess, you say how many
- * letters are the correct letter in the correct spot, and how many letters
- * are a correct letter but in the wrong spot"
- * @author messa
- */
- public class Day2 {
- public static String generate_random_word(int random_word_leght){
- //This class returns "random_word" string of lenght given in "random_word_leght"
- //Initialize stringBuilder
- StringBuilder stringBuilder = new StringBuilder();
- String random_word=""; //random word the be generated
- for ( int i=0; i < random_word_leght; i++)
- {
- //Alphabet list to choose characters from.
- String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; //52
- //Get random number from alphabet 0-51 to get the letter. 52 its not a typo.
- int random_number = new Random().nextInt(52);
- //Choose alphabet letter, convert it from char to string.
- String random_letter=Character.toString(alphabet.charAt(random_number));
- //Join each "random_letter" toghether as "random_word".
- stringBuilder.append(random_letter);
- random_word= stringBuilder.toString();
- }
- return random_word;
- }
- public static void count_letters(String user_input,String rword){
- //This part of program is counting number of characters from "user_input" and
- //"random_word" strings to find:
- // - maching letters at the same positions
- // - maching letters at all postions
- int correct_characters=0;
- int correct_characters_at_same_spot=0;
- int number=0;
- for ( int i=0; i < user_input.length(); i++)
- {
- //Finding the correct characters at the same spot.
- if(user_input.charAt(i)==rword.charAt(i))
- {
- correct_characters_at_same_spot=correct_characters_at_same_spot+1;
- }
- //Finding the correct characters at all postions.
- while(number<user_input.length()){
- if(user_input.charAt(number)==rword.charAt(i))
- {
- correct_characters=correct_characters+1;
- }
- number=number+1;
- }
- }
- System.out.println("Correct characters: "+correct_characters);
- System.out.println("Correct characters at same spot: "+correct_characters_at_same_spot);
- }
- public static void main(String[] args) {
- int random_word_leght=2;
- //Generates random word used in game. Lengt passed by argument.
- String rword=generate_random_word(random_word_leght);
- //When user enters the corrent answer that will switch to truth;
- //!!!
- //!!!
- boolean correct_answer=false;
- while (correct_answer==false)
- {
- //Reads the word from user store it as int "input" string.
- System.out.println("Guess the word [5 characters]:");
- Scanner sc = new Scanner(System.in);
- String user_input = sc.next();
- //String user_input="aBcD";
- System.out.println(rword);
- System.out.println(user_input);
- //Count the letters position and print the output.
- count_letters(user_input,rword);
- if(count_letters.correct_characters_at_same_spot == count_letters.user_input.length){
- correct_answer==true;
- System.out.println("CORRECT: ");
- };
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement