SX514LEFV

wordle ver 2 prog 1

Nov 25th, 2024 (edited)
36
-1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.14 KB | None | 0 1
  1. import java.util.Scanner;
  2. import java.util.Random;
  3. public class WordGame {
  4.     public static boolean isLetterInPlace(String answer, String guess, int position) {
  5.     if (answer.charAt(position) == guess.charAt(position)) {
  6.     //if the nth char of the "answer" string matches the nth char of the "guess" string, returns true, otherwise returns false
  7.         return true;
  8.     }
  9.     else {
  10.         return false;  
  11.         }
  12.     }
  13.     public static boolean isLetterInWord(String answer, char c) {
  14.             int positionliw = 0;
  15.             //position, liw is an abbreviation for letter in word
  16.             final int LENGTH = answer.length();
  17.             while (positionliw < LENGTH) {
  18.             if (answer.charAt(positionliw) == c) {
  19.             //if the char being checked in answer matches the char we are looking for from the guess, returns true
  20.                 return true;
  21.             }
  22.             positionliw++;
  23.             //checks the next char of the answer for that same char
  24.             }
  25.             return false;
  26.             //if it's nowhere to be found, returns false, the letter not being in the word at any position
  27.             }
  28.     public static int countLettersInPlace(String answer, String guess) {
  29.         int position = 0;
  30.         int lettersinplace = 0;
  31.         final int LENGTH = guess.length();
  32.         while (position < LENGTH) {
  33.             if (isLetterInPlace(answer, guess, position)) {
  34.                 lettersinplace++;
  35.                 //adds +1 to the count of letters that are correctly placed
  36.             }
  37.             position++;
  38.             //checks if the next letter is correctly placed
  39.         }
  40.         return lettersinplace;
  41.         //returns number of correctly placed letters
  42.     }
  43.     public static int countLettersInWord(String answer, String guess) {
  44.         int current = 0;
  45.         //current char position
  46.         int lettersinword = 0;
  47.         final int LENGTH = guess.length();
  48.         while (current < LENGTH) {
  49.             char c = guess.charAt(current);
  50.             //updates char every loop since we are changing the current position every loop (otherwise it would just check the same char for all 5 loops)
  51.             if (isLetterInWord(answer, c)) {
  52.                 lettersinword++;
  53.             //adds to count of correctly guessed (but possibly incorrectly placed) letters
  54.             }
  55.             current++;
  56.             //checks next letter
  57.         }
  58.         return lettersinword;
  59.         //returns number of correctly guessed (but possibly incorrectly placed) letters
  60.     }
  61.     public static boolean assessWord(String answer, String guess) {
  62.         int lettersinplace = countLettersInPlace(answer, guess);
  63.         //calls letter-in-place-counting method to check how many letters from answer and guess match up
  64.         int lettersinword = countLettersInWord(answer, guess);
  65.         //same thing only with letter-in-word-counting method
  66.         System.out.println(guess +" has " +lettersinplace +" correctly placed letters");
  67.         System.out.println(lettersinword +" of the letters in " +guess +" were also in the answer");
  68.         if (lettersinplace == answer.length()) {
  69.             return true;
  70.             //lets runGame method know whether the player guessed the word correctly
  71.         }
  72.         else {
  73.         return false;
  74.         }
  75.     }
  76.     public static void runGame(String answer) {
  77.         int loops = 0;
  78.         Scanner reader = new Scanner(System.in);
  79.         System.out.println("Type in your guess:");
  80.         String guess = reader.next();
  81.         boolean finishStatus = false;
  82.         while (loops <= 12 && finishStatus == false) {
  83.         //had to add a boolean to break the loop in the case of a successful guess
  84.             if (assessWord(answer, guess)) {
  85.                 System.out.println("Congratulations, you guessed the word!");
  86.                 finishStatus = true;
  87.                 //changes boolean to "true" which makes it no longer eligible for the loop condition
  88.             }
  89.             else {
  90.                 loops++;
  91.                 System.out.println("Type in your guess:");
  92.                 guess = reader.next();
  93.                 //lets the user try again
  94.             }
  95.         }
  96.         if (finishStatus == false) {
  97.         //if the user still hasn't guessed correctly in the last loop, prints out game over message
  98.         System.out.println("You lost :(");
  99.         }
  100.     }
  101.     public static void main(String[]args) {
  102.         java.util.Random random = new java.util.Random();
  103.         String[] options = new String[] {"twirl", "pearl", "spine", "niche", "visor", "tacky", "flown", "stoic", "vinyl", "truly", "blaze", "sixth", "weird", "tunic", "bawdy", "shout", "audio", "halve", "grant", "gamut", "prone", "stain"};
  104.         String answer = options[random.nextInt(options.length - 1)];
  105.         runGame(answer);
  106.         //run game based on this answer
  107.     }
  108. }
Add Comment
Please, Sign In to add comment