Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

package GuessMe; import java.util.Random; /* Remember to import java.util.Random, otherwise you will get errors! */ public class GuessMe{ /* Here you will be creating your

package GuessMe; import java.util.Random; /* Remember to import java.util.Random, otherwise you will get errors! */

public class GuessMe{

/* Here you will be creating your instance variables. When making instance variables, we think * about what information we need to store between method calls. The info we will need to store * is: * * 1) the word the user is trying to guess. We will need a String instance variable called 'word' for that. * 2) the number of guesses the user has left. We will need an int called 'remainingGuesses' for that. * 3) we need to know if the word has been found. We will need a boolean called 'wordFound' for that. * 4) we need a random number generator. We will need a Random object called 'rand' for that. * * Be sure to use the suggested variable names exactly, or you won't pass many of the tests. */ //Instance variables here:

/* This is the first of two constructors. This one is used in the main method. We initialize each * of our instance variables here. The player will start off with 10 guesses, the word obviously * has not been found yet. You will need to initialize 'rand' before you can generate the word, and * you will want to use the generateWord() helper method at the bottom of this file to initialize * 'word' that way you don't have to write the same code a ton of times. */ public GuessMe(){ } /* This is our second constructor. There is only one real difference: in this one we take in an * int which will act as the seed for our Random object. This is used for the tests. */ public GuessMe(int seed){

} /* This is our play method. It takes in a String called 'str' and compares it to the word the * player is trying to guess 'word'. It returns an int representing the number of letters which * are in the exact same place in both words. For example, if the word to find were "ABCD" play * would return: * * 1 on "AAAA" * 2 on "ABAA" * 2 on "AAAD" * 4 on "ABCD" * 4 on "abcd" * * NOTE these values are ONLY for the example word "ABCD" and will not be the same for your random * word. Looping and using charAt() on the strings would be very useful here. Remember we do not * care about upper/lower case here, so be sure to account for that. * * We also want to decrement the user's remaining guesses, and in the event they guess the word * we will also update wordFound to true before returning 4. */ public int play(String str){ } /* isOver simply returns true if the game is over, which is either when the player runs * out of guesses or when they have found the correct word. It returns false otherwise. */ public boolean isOver(){ } /* isWin returns true if the player has correctly guessed the word and false otherwise. */ public boolean isWin(){ } /* getRemainingGuesses returns the number of remaining guesses the player has. */ public int getRemainingGuesses(){ } /* reset returns nothing but resets the game. 'remainingGuesses' and 'wordFound' go back to * their default values and a new word is generated with generateWord(). */ public void reset(){ } /* getWord simply returns the word the user is attempting to guess. It is not tested, * and is only used in the main method. */ public String getWord(){ } /**********************************/ /* Helper Methods */ /**********************************/ /* A helper method is a private method which cannot be accessed outside of the file. Its * sole purpose is to be used in other constructors/methods and simply 'helps' them do their * job better. In our case, we need to generate a random word occassionally, so we make * a helper method so that we don't have to write the same code repeatedly. * * We will go about writing this by using our Random object. We will call nextInt(4) on our * Random object a total of four times to generate four different ints. Specifying 4 in the * method call forces it to generate an int between 0 and 4, including 0 but not including 4. * The ints correspond to a letter to add to our word as such: * * 0: 'A' * 1: 'B' * 2: 'C' * 3: 'D' * * We can build our String by starting with local variable which is an empty String ("") which we can * call retStr. Using String concatination (+=), we can add letters to our String based on the int * generated. Once we have done this 4 times, we can return the String. We do not use any instance * variables in this method. It may be helpful to use a loop here. */ private String generateWord(){ } Here is the main class:

package GuessMe;

import java.util.*;

public class GuessMeMain{

public static void main(String[] args){ //Here we declare and initialize our scanner Scanner scan = new Scanner(System.in); //Here we declare and initialize our "play again" boolean boolean again = true; System.out.println("Welcome to Guess Me!"); System.out.println("A random word will be generated. It will be four letters long and consist of the letters 'A', 'B', 'C', and 'D'. Your goal is to guess the word within the alloted guesses. Good luck! "); GuessMe game = new GuessMe();

while(again){ while(!game.isOver()){ System.out.println("You have " + game.getRemainingGuesses() + " guesses left."); System.out.println("Enter your guess:"); String guess = ""; while(true){ guess = scan.nextLine(); if(guess.length() == 4){ break; } System.out.println("Invalid guess, please enter a 4 letter word consisting of 'A', 'B', 'C', and 'D':"); } int score = game.play(guess); System.out.println("There are " + score + " correct letter placements."); } if(game.isWin()){ System.out.println("Congrats!"); } else { System.out.println("You lose! The word was: " + game.getWord()); } System.out.println("Would you like to play again? y/n"); if(Character.toLowerCase(scan.nextLine().charAt(0)) == 'n'){ again = false; } else { game.reset(); } } System.out.println("Goodbye!"); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database 101

Authors: Guy Kawasaki

1st Edition

0938151525, 978-0938151524

More Books

Students also viewed these Databases questions

Question

Understanding Group Leadership Culture and Group Leadership

Answered: 1 week ago