Advertisement
SenpaiZero

Untitled

Apr 12th, 2024
811
0
153 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.33 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import java.util.Random;
  6. import java.util.Scanner;
  7.  
  8.  
  9. public class LabExer {
  10.  
  11.     public static void main(String[] args) {
  12.         String line = null;
  13.         String[] wordArray = new String[15];
  14.         Random rand = new Random();
  15.         int randomNum = rand.nextInt(14);
  16.         String randomWord = "";
  17.         String tempWord = "";
  18.         Scanner scanner = new Scanner(System.in);
  19.         String userInput;
  20.         int attemps = 0;
  21.         try {
  22.             // Path for txt file
  23.             FileReader file = new FileReader("C:\\Users\\Xeb\\eclipse-workspace\\test\\asd\\src\\asd\\words.txt");
  24.             BufferedReader buffer = new BufferedReader(file);
  25.             int indexArr = 0;
  26.             while ((line = buffer.readLine()) != null) {
  27.                 wordArray[indexArr] = line.trim(); // removing whitespace/blanks
  28.                 ++indexArr;
  29.               }    
  30.            
  31.             //Keep adding value until it's not empty or null
  32.             while (randomWord.isEmpty() || randomWord == null) {
  33.                 randomWord = wordArray[randomNum].trim();
  34.             }
  35.             tempWord = randomWord;
  36.             //Always have a blank of at least 50% of the word
  37.             char getElementRand[] = new char[(randomWord.length() / 2)];
  38.            
  39.             //Replacing random characters
  40.             for (int i = 0; i < getElementRand.length; i++) {
  41.                 randomWord = randomWord.replace(getElementRand[i] = randomWord.charAt(rand.nextInt(randomWord.length())), '?');
  42.             }
  43.            
  44.             boolean isContinue = true;
  45.             boolean isCorrect = false;
  46.             boolean isWrong = false;
  47.             System.out.println("Guess the word!\nType any number to quit\n\n");
  48.             do {
  49.                 if(!randomWord.equalsIgnoreCase(tempWord)) {
  50.                     System.out.println("== " + randomWord + " ==");
  51.                     System.out.print("Input: ");
  52.                     attemps++;
  53.                     userInput = scanner.next().toUpperCase();
  54.                    
  55.                     //Proceed if user input is not a number
  56.                     if(!userInput.matches("\\d+")) {
  57.                         //Keep looping until it goes through the whole word as character
  58.                         //And if it finds an equal character, it will proceed
  59.                         //else it's wrong
  60.                         for (int i = 0; i < tempWord.length(); i++) {
  61.                             if(userInput.equalsIgnoreCase(String.valueOf(tempWord.charAt(i)))) {
  62.                                 randomWord = randomWord.substring(0,i) + userInput + randomWord.substring(i+1);
  63.                                 isCorrect = true;
  64.                             } else {
  65.                                 isWrong = true;
  66.                             }
  67.                         }
  68.                        
  69.                         //Isolating the printing of correct and wrong answer
  70.                         //To stop looping/repeating the print
  71.                         if(isCorrect == true) {
  72.                             System.out.println("Correct Answer!");
  73.                             isCorrect = false;
  74.                         } else if (isWrong == true) {
  75.                             System.out.println("Wrong Answer! Please Try Again...");
  76.                             isWrong = false;
  77.                         }
  78.                         System.out.println("----------------------------------------");
  79.                     }
  80.                     else { //Stop the program if user input is number
  81.                         System.out.println("You Gave up! Better Luck Next Time...");
  82.                         isContinue = false;
  83.                     }
  84.                 }else { //Stops the program if user finished filling up the missing letters
  85.                     System.out.println("Congratulation! You Filled up the Missing Letters!"
  86.                                     + "\nThe Full Word is: " + tempWord
  87.                                     + "\nAttemps: " + attemps);
  88.                     isContinue = false;
  89.                 }
  90.             } while (isContinue);
  91.         } catch (FileNotFoundException e) {
  92.             System.out.println(e.getMessage());
  93.         } catch (IOException e) {
  94.             System.out.println(e.getMessage());
  95.         }
  96.  
  97.     }
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement