Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Random;
- public class WordGame {
- public static boolean isLetterInPlace(String answer, String guess, int position) {
- if (answer.charAt(position) == guess.charAt(position)) {
- //if the nth char of the "answer" string matches the nth char of the "guess" string, returns true, otherwise returns false
- return true;
- }
- else {
- return false;
- }
- }
- public static boolean isLetterInWord(String answer, char c) {
- int positionliw = 0;
- //position, liw is an abbreviation for letter in word
- final int LENGTH = answer.length();
- while (positionliw < LENGTH) {
- if (answer.charAt(positionliw) == c) {
- //if the char being checked in answer matches the char we are looking for from the guess, returns true
- return true;
- }
- positionliw++;
- //checks the next char of the answer for that same char
- }
- return false;
- //if it's nowhere to be found, returns false, the letter not being in the word at any position
- }
- public static int countLettersInPlace(String answer, String guess) {
- int position = 0;
- int lettersinplace = 0;
- final int LENGTH = guess.length();
- while (position < LENGTH) {
- if (isLetterInPlace(answer, guess, position)) {
- lettersinplace++;
- //adds +1 to the count of letters that are correctly placed
- }
- position++;
- //checks if the next letter is correctly placed
- }
- return lettersinplace;
- //returns number of correctly placed letters
- }
- public static int countLettersInWord(String answer, String guess) {
- int current = 0;
- //current char position
- int lettersinword = 0;
- final int LENGTH = guess.length();
- while (current < LENGTH) {
- char c = guess.charAt(current);
- //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)
- if (isLetterInWord(answer, c)) {
- lettersinword++;
- //adds to count of correctly guessed (but possibly incorrectly placed) letters
- }
- current++;
- //checks next letter
- }
- return lettersinword;
- //returns number of correctly guessed (but possibly incorrectly placed) letters
- }
- public static boolean assessWord(String answer, String guess) {
- int lettersinplace = countLettersInPlace(answer, guess);
- //calls letter-in-place-counting method to check how many letters from answer and guess match up
- int lettersinword = countLettersInWord(answer, guess);
- //same thing only with letter-in-word-counting method
- System.out.println(guess +" has " +lettersinplace +" correctly placed letters");
- System.out.println(lettersinword +" of the letters in " +guess +" were also in the answer");
- if (lettersinplace == answer.length()) {
- return true;
- //lets runGame method know whether the player guessed the word correctly
- }
- else {
- return false;
- }
- }
- public static void runGame(String answer) {
- int loops = 0;
- Scanner reader = new Scanner(System.in);
- System.out.println("Type in your guess:");
- String guess = reader.next();
- boolean finishStatus = false;
- while (loops <= 12 && finishStatus == false) {
- //had to add a boolean to break the loop in the case of a successful guess
- if (assessWord(answer, guess)) {
- System.out.println("Congratulations, you guessed the word!");
- finishStatus = true;
- //changes boolean to "true" which makes it no longer eligible for the loop condition
- }
- else {
- loops++;
- System.out.println("Type in your guess:");
- guess = reader.next();
- //lets the user try again
- }
- }
- if (finishStatus == false) {
- //if the user still hasn't guessed correctly in the last loop, prints out game over message
- System.out.println("You lost :(");
- }
- }
- public static void main(String[]args) {
- java.util.Random random = new java.util.Random();
- 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"};
- String answer = options[random.nextInt(options.length - 1)];
- runGame(answer);
- //run game based on this answer
- }
- }
Add Comment
Please, Sign In to add comment