Ask your own question, for FREE!
Computer Science 13 Online
OpenStudy (ellecullen):

Basic Game help: Revise the BasicGame logic so that the user who takes more than three guesses to get it right will never succeed in guessing the right answer. code: public class BasicGame1 extends BasicGame { private String itsSecretWord = "ducky"; private String itsUsersWord = "none"; public void playOneGame() { askUsersFirstChoice(); while (shouldContinue()) { showUpdatedStatus(); askUsersNextChoice(); } showFinalStatus(); } //============================ } Object class in comments.

OpenStudy (ellecullen):

Object class: public class BasicGame extends Object { private String itsSecretWord = "duck"; private String itsUsersWord = "none"; public void playManyGames() { playOneGame(); while (JOptionPane.showConfirmDialog (null, " play again?") == JOptionPane.YES_OPTION) playOneGame(); } //========================== public void playOneGame() { askUsersFirstChoice(); while (shouldContinue()) { showUpdatedStatus(); askUsersNextChoice(); } showFinalStatus(); } //=========================== public void askUsersFirstChoice() { itsUsersWord = JOptionPane.showInputDialog ("Guess the secretword:"); } //============================ public boolean shouldContinue() { return ! itsSecretWord.equals (itsUsersWord); } //============================ public void showUpdatedStatus() { JOptionPane.showMessageDialog (null, "That was wrong. Hint:It quacks."); } //============================ public void askUsersNextChoice() { askUsersFirstChoice(); } //============================= public void showFinalStatus() { JOptionPane.showMessageDialog (null, "That was right. \nCongratulations."); } //============================== }

OpenStudy (woodrow73):

Did you make this code?

OpenStudy (woodrow73):

@ElleCullen

OpenStudy (ellecullen):

Yes, I typed it based on some information in my textbook. Part of my work was to do an object class.

OpenStudy (woodrow73):

Ok, you can create another field in the BasicGame1 class called numGuesses, and when numGuesses hits 3, then move to a different while loop that executes forever... asking the user for their guess each iteration.

OpenStudy (woodrow73):

@ElleCullen

OpenStudy (woodrow73):

*when numGuesses hits 3 && the latest guess was false

OpenStudy (woodrow73):

if(numGuesses >= 3 && !latestGuess) purgatory();

OpenStudy (ellecullen):

I did this, public class BasicGame1 extends BasicGame { private String itsSecretWord = "ducky"; private String itsUsersWord = "none"; String itsMark = ""; // I added another declaration public void playOneGame() { askUsersFirstChoice(); while (shouldContinue()) { showUpdatedStatus(); askUsersNextChoice(); } showFinalStatus(); playManyGames(); JOptionPane.showMessageDialog (null, "You cannot succeed in getting the correct answer if you take >3 guesses."); } //============================ public void askUsersFirstChoice() { itsUsersWord = JOptionPane.showInputDialog ("Guess the secretword:"); itsMark = "x"; // this is to represent guess number 1 } //============================ public void askUsersNextChoice() { askUsersFirstChoice(); itsMark = itsMark + "x"; // represents the next guess as itsMark = "x" } //============================= public boolean shouldContinue() { // return ! itsSecretWord.equals (itsUsersWord); return ! (itsSecretWord.equals(itsUsersWord) || itsMark.equals ("xxx")); // here, I just replaced the statement to verify that the user will get the right // answer after 3 guesses only } //============================ public void showFinalStatus() { JOptionPane.showMessageDialog (null, "That was right. \nCongratulations."); itsMark = ""; // here, I put the its Mark to make sure everything works, since // I updated everything else } //============================== }

OpenStudy (woodrow73):

Nice :) there's many ways to go about it-- I'll show another way about how one could go about it; ``` import javax.swing.JOptionPane; public class BasicGame1 extends BasicGame { private String itsSecretWord = "ducky"; private String itsUsersWord = "none"; private int numGuesses = 1; public void playOneGame() { JOptionPane.showMessageDialog(null, "After your first 3" + " guesses, you will never be able to get the right answer."); askUsersFirstChoice(); while (shouldContinue() && numGuesses < 3) { numGuesses++; showUpdatedStatus(); askUsersNextChoice(); } if(numGuesses == 3 && shouldContinue()) this.purgatory(); //calls purgatory method in this class else JOptionPane.showMessageDialog(null, "Congrats- you win."); } public void purgatory(){ while(true){ askUsersNextChoice(); JOptionPane.showMessageDialog(null, "Incorrect answer."); } } ```

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!