Advertisement
SX514LEFV

wordle prog 1

Nov 25th, 2024 (edited)
142
-1
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.91 KB | None | 0 1
  1. import java.util.Scanner;
  2. public class WordGame {
  3.     public static boolean isLetterInPlace(String answer, String guess, int position) {
  4.     if (answer.charAt(position) == guess.charAt(position)) {
  5.     //if the nth char of the "answer" string matches the nth char of the "guess" string, returns true, otherwise returns false
  6.         return true;
  7.         }
  8.     else {
  9.         return false;  
  10.         }
  11.     }
  12.     public static boolean isLetterInWord(String answer, char c) {
  13.             int positionliw = 0;
  14.             //position, liw is an abbreviation for letter in word
  15.             final int LENGTH = answer.length();
  16.             while (positionliw < LENGTH) {
  17.             if (answer.charAt(positionliw) == c) {
  18.             //if the char being checked in answer matches the char we are looking for from the guess, returns true
  19.                 return true;
  20.             }
  21.             positionliw++;
  22.             //checks the next char of the answer for that same char
  23.             }
  24.             return false;
  25.             //if it's nowhere to be found, returns false, the letter not being in the word at any position
  26.             }
  27.     public static int countLettersInPlace(String answer, String guess) {
  28.         int position = 0;
  29.         int lettersinplace = 0;
  30.         final int LENGTH = guess.length();
  31.         while (position < LENGTH) {
  32.             if (isLetterInPlace(answer, guess, position)) {
  33.                 lettersinplace++;
  34.                 //adds +1 to the count of letters that are correctly placed
  35.             }
  36.             position++;
  37.             //checks if the next letter is correctly placed
  38.         }
  39.         return lettersinplace;
  40.         //returns number of correctly placed letters
  41.     }
  42.     public static int countLettersInWord(String answer, String guess) {
  43.         int current = 0;
  44.         //current char position
  45.         int lettersinword = 0;
  46.         final int LENGTH = guess.length();
  47.         while (current < LENGTH) {
  48.             char c = guess.charAt(current);
  49.             //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)
  50.             if (isLetterInWord(answer, c)) {
  51.                 lettersinword++;
  52.             //adds to count of correctly guessed (but possibly incorrectly placed) letters
  53.             }
  54.             current++;
  55.             //checks next letter
  56.         }
  57.         return lettersinword;
  58.         //returns number of correctly guessed (but possibly incorrectly placed) letters
  59.     }
  60.     public static boolean assessWord(String answer, String guess) {
  61.         int lettersinplace = countLettersInPlace(answer, guess);
  62.         //calls letter-in-place-counting method to check how many letters from answer and guess match up
  63.         int lettersinword = countLettersInWord(answer, guess);
  64.         //same thing only with letter-in-word-counting method
  65.         System.out.println(guess +" has " +lettersinplace +" correctly placed letters");
  66.         System.out.println(lettersinword +" of the letters in " +guess +" were also in the answer");
  67.         if (lettersinplace == answer.length()) {
  68.             return true;
  69.             //lets runGame method know whether the player guessed the word correctly
  70.         }
  71.         else {
  72.         return false;
  73.         }
  74.     }
  75.     public static void runGame(String answer) {
  76.         int loops = 0;
  77.         Scanner reader = new Scanner(System.in);
  78.         System.out.println("Type in your guess:");
  79.         String guess = reader.next();
  80.         boolean finishStatus = false;
  81.         while (loops <= 12 && finishStatus == false) {
  82.         //had to add a boolean to break the loop in the case of a successful guess
  83.             if (assessWord(answer, guess)) {
  84.                 System.out.println("Congratulations, you guessed the word!");
  85.                 finishStatus = true;
  86.                 //changes boolean to "true" which makes it no longer eligible for the loop condition
  87.         }
  88.             else {
  89.                 loops++;
  90.                 System.out.println("Type in your guess:");
  91.                 guess = reader.next();
  92.                 //lets the user try again
  93.         }
  94.         }
  95.         if (finishStatus == false)
  96.         //if the user still hasn't guessed correctly in the last loop, prints out game over message
  97.         {
  98.         System.out.println("You lost :(");
  99.     }
  100.     }
  101.     public static void main(String[]args) {
  102.         Scanner reader = new Scanner(System.in);
  103.         System.out.println("Please enter word");
  104.         //input answer
  105.         String answer = reader.next();
  106.         runGame(answer);
  107.         //run game based on this answer
  108.     }
  109. }
Advertisement
Comments
  • SensaBG
    138 days
    # Java 1.81 KB | 0 0
    1. PLZ, do not write your Java code with an opening bracket on a new line.
    2. PLZ, do not write your Java code with an opening bracket on a new line.
    3. PLZ, do not write your Java code with an opening bracket on a new line.
    4.  
    5. This may be a good practice in other languages like c#, but in Java, this is considered a GARBAGE CODE.
    6.  
    7. This means that anyone who sees it will automatically close the file or even block you.
    8.  
    9. Please do not pollute the code with mixed conventions. Either follow entirely the thrashy new line bracket pattern or just stick to the native Java approaches.  
    10.  
    11. But combining everything makes your code overrated even for a Thrash Can and it may be best to start learning the language anew from "A" to "Z", starting with how to write basic Java classes, methods, and variables.
    12.  
    13. You may have to create the same class 100 times until your brain comprehends that brackets on new lines are not meant for Java.
    14.  
    15. Repeat after me:
    16.  
    17. 1. Open brackets on new lines are not meant for Java. Never type move the opening brackets on a brand-new line since it's a thrash approach.
    18. 2. Open brackets on new lines are not meant for Java. Never type move the opening brackets on a brand-new line since it's a thrash approach.
    19. 3. Open brackets on new lines are not meant for Java. Never type move the opening brackets on a brand-new line since it's a thrash approach.
    20. 4. Open brackets on new lines are not meant for Java. Never type move the opening brackets on a brand-new line since it's a thrash approach.
    21.  
    22. You can continue yourself for homework later on - it looks like you need it considering the struggle to remember it.
    23.  
    24. This is very serious and it is subject to penalties if it is being done on a production code. For example, I would have fired you immediately without even allowing you to say something - just GO AWAY!
Add Comment
Please, Sign In to add comment
Advertisement