Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.Random;
- import java.util.Scanner;
- public class LabExer {
- public static void main(String[] args) {
- String line = null;
- String[] wordArray = new String[15];
- Random rand = new Random();
- int randomNum = rand.nextInt(14);
- String randomWord = "";
- String tempWord = "";
- Scanner scanner = new Scanner(System.in);
- String userInput;
- int attemps = 0;
- try {
- // Path for txt file
- FileReader file = new FileReader("C:\\Users\\Xeb\\eclipse-workspace\\test\\asd\\src\\asd\\words.txt");
- BufferedReader buffer = new BufferedReader(file);
- int indexArr = 0;
- while ((line = buffer.readLine()) != null) {
- wordArray[indexArr] = line.trim(); // removing whitespace/blanks
- ++indexArr;
- }
- //Keep adding value until it's not empty or null
- while (randomWord.isEmpty() || randomWord == null) {
- randomWord = wordArray[randomNum].trim();
- }
- tempWord = randomWord;
- //Always have a blank of at least 50% of the word
- char getElementRand[] = new char[(randomWord.length() / 2)];
- //Replacing random characters
- for (int i = 0; i < getElementRand.length; i++) {
- randomWord = randomWord.replace(getElementRand[i] = randomWord.charAt(rand.nextInt(randomWord.length())), '?');
- }
- boolean isContinue = true;
- boolean isCorrect = false;
- boolean isWrong = false;
- System.out.println("Guess the word!\nType any number to quit\n\n");
- do {
- if(!randomWord.equalsIgnoreCase(tempWord)) {
- System.out.println("== " + randomWord + " ==");
- System.out.print("Input: ");
- attemps++;
- userInput = scanner.next().toUpperCase();
- //Proceed if user input is not a number
- if(!userInput.matches("\\d+")) {
- //Keep looping until it goes through the whole word as character
- //And if it finds an equal character, it will proceed
- //else it's wrong
- for (int i = 0; i < tempWord.length(); i++) {
- if(userInput.equalsIgnoreCase(String.valueOf(tempWord.charAt(i)))) {
- randomWord = randomWord.substring(0,i) + userInput + randomWord.substring(i+1);
- isCorrect = true;
- } else {
- isWrong = true;
- }
- }
- //Isolating the printing of correct and wrong answer
- //To stop looping/repeating the print
- if(isCorrect == true) {
- System.out.println("Correct Answer!");
- isCorrect = false;
- } else if (isWrong == true) {
- System.out.println("Wrong Answer! Please Try Again...");
- isWrong = false;
- }
- System.out.println("----------------------------------------");
- }
- else { //Stop the program if user input is number
- System.out.println("You Gave up! Better Luck Next Time...");
- isContinue = false;
- }
- }else { //Stops the program if user finished filling up the missing letters
- System.out.println("Congratulation! You Filled up the Missing Letters!"
- + "\nThe Full Word is: " + tempWord
- + "\nAttemps: " + attemps);
- isContinue = false;
- }
- } while (isContinue);
- } catch (FileNotFoundException e) {
- System.out.println(e.getMessage());
- } catch (IOException e) {
- System.out.println(e.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement