Working with methods for a safecracker game - java

I am just busy with learning Java and my task is making a safecracker game. I need to do this game with classes and methods. But I came to until a point and I can't go further. Below I share my code and my questions. If you could have a look I will be so appreciate.
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
entrance();
playGame();
quitGame();
}
private static void entrance() {
System.out.println("Welcome to the SafeCracker!\nI need your help to open the safe box." +
"\nThe code is with 3 digits and we need to find it out as quick as possible.\nLet's write your guess!");
}
private static int playGame() {
int[] safeCode = {takeRandomSafeCode(), takeRandomSafeCode(), takeRandomSafeCode()};
int guess = takeGuess();
//Below I need to use a for each loop but I don't get the logic of it. I stuck here. I need to check every numbers one by one but how?
for (int safeDigit : safeCode) {
if (safeDigit == guess) {
System.out.println("Your number is correct");
}
}
return playGame(); // with this return type I also have a problem.
If I return this method, it keeps going to play again and again.
But I don't know also which return type I need to give.
}
private static int takeGuess() {
Scanner keyboard = new Scanner(System.in);
int userGuess = keyboard.nextInt();
return userGuess;
}
private static int takeRandomSafeCode() {
Random random = new Random();
int result = random.nextInt(10);
return result;
}
private static int quitGame() {
System.out.println("Do you want to play again?\nPress 1 for play again\nPress 2 for quit the game!");
Scanner key = new Scanner(System.in);
int userWannaPlay = key.nextInt();
if(userWannaPlay == 1) {
System.out.println(playGame());
} else if (userWannaPlay == 2) {
System.out.println(quitGame());
} else {
System.out.println("You entered an invalid number. If you want to play again or quit, you need to click 1 or 2!");
}
return userWannaPlay; //And also quitGame method. I want to ask the users that if they want to play or not and according to answer I would like to run "playGame" method again or quit game.
}
}

Try to use a loop for your game.
You can set quitGame variable from playGame method or you can create a new method for user decision.
public static void main(String [] args){
entrance();
do{
playGame();
}while(!quitGame)
}
public void playGame(){
//Your code is here
}

If I return this method, it keeps going to play again and again. But I
don't know also which return type I need to give.
Your playGame*( method calls itself recursively in its last line return playGame(). I guess you did that to return anything at all. If you think about your problem you may come to the conclusion that you don't want to return anything at all (as you do not know what to do with it). In this case you may return nothing aka void as you did in your main method.
And also quitGame method. I want to ask the users that if they want
to play or not and according to answer I would like to run "playGame"
method again or quit game
You have to think about what you want. You want to call a method again and again depending on a condition. For that you can either use a loop or recursion. For exmaple you could change you main method slightly and add a do-while-loop.
public static void main(String[] args) {
entrance();
int condition;
do {
playGame();
condition = quitGame();
} while (condition == 1);
Don't forget to change you quitGame method because there you are trying to solve your problem recursively (remove the if clause). If you want to do it recursively though ignore the above and look at this snippet:
private static int quitGame() {
System.out.println("Do you want to play again?\nPress 1 for play again\nPress 2 for quit the game!");
Scanner key = new Scanner(System.in);
int userWannaPlay = key.nextInt();
if(userWannaPlay == 1) {
playGame(); // you dont need a println here
} else if (userWannaPlay == 2) {
// you dont need to anything here
System.out.println("Quitting...");
} else {
System.out.println("You entered an invalid number. If you want to play again or quit, you need to click 1 or 2!");
// call quitGame again to ask for the choice again
quitGame();
}
return userWannaPlay; // if you do it like this, this return is also unnecessary and you could use a void method without returning anything
}

Related

Removing a method call from the stack

suppose I have a game with a text interface. Now, when starting the game the player needs to tell the game their age.
public void getAge() {
System.out.println("Please enter your age");
int age;
try {
Scanner sc = new Scanner(System.in);
age = sc.nextInt();
} catch (InputMismatchException e) {
System.out.println("You made a mistake. Let's start this over!");
getAge();
}
gameLoop(age);
}
Suppose the user makes 100 errors, and the method is called over and over again. Do the 100 calls stay on the call stack? Is there any way to remove them, perhaps by returning a value instead?
Is this a memory leak (although small)?
Anyway, I would like to hear your thoughts, on how this could be improved.
Try a loop (Just pseudocode)
bool done = false;
while (!done)
{
try {
// Get the age
if (valid) { done = true; }
}
catch { // ...
// Handle the exception make sure done is still false
}
} // End while

How to get this piece of code into loop, so that it asks a user input each time until the result is "kill"?

I was practicing this piece of code from the book 'Head First Java' and I'm quite confused on the positioning of the loop here.The code is for creating a kind of game that has a random dotcom word(ex: abc.com) occupying some array elements. here I gave that dotcom word the positions from 3 to 5 in the array, and the user tries guessing the position.
import java.util.Scanner;
public class RunTheGame {
public static void main(String[] args) {
MainGameClass sampleObj= new MainGameClass();
int[] location = {3,4,5};
sampleObj.setdotcomLocationCells(location);
Scanner input= new Scanner(System.in);
System.out.println("Enter your guess");
int userGuess=input.nextInt();
String answer = sampleObj.checkForDotcom(userGuess);
System.out.println(answer);
}
}
package simpleDotComGame;
public class MainGameClass {
int[] DotcomLocationCells;
int numOfHits=0;
public void setdotcomLocationCells(int[] location) {
DotcomLocationCells= location;
}
public String checkForDotcom(int userGuess) {
String result="miss";
for(int cell:DotcomLocationCells) {
if(cell == userGuess) {
result ="hit";
numOfHits++;
break;
}
} // end for loop
if(numOfHits == DotcomLocationCells.length) {
result = "kill";
System.out.println("The number of tries= "+numOfHits);
}
}
do {
<insert code where answer result is created>
} while (!answer.equals("kill"))
upd.: but you must override the equals method for correct use, because if you see how method declared in Object.class you find
public boolean equals(Object obj) {
return (this == obj);
You are allowed to declare the variable before initializing it:
String answer;
do {
answer = sampleObj.checkForDotcom(userGuess);
System.out.println(answer);
} while (!answer.equals("kill");
Also beware of the semantics of Scanner.nextInt(): if it can't parse the input as an int (for example, it contains letters), it will throw an exception, but won't jump over the invalid input. You'll have to use Scanner.nextLine() to force-jump over it, otherwise you'll get an infinite loop.
Rachna, the loop will be positioned, around the following code:
Scanner input= new Scanner(System.in);
System.out.println("Enter your guess");
int userGuess=input.nextInt();
String answer=sampleObj.checkForDotcom(userGuess);
System.out.println(answer);
//For string you must use the equals
if ("kill".equals(answer)){
break;
}
The reason why is that the kill command must be evaluated inside the loop to break it, and the input to continuously ask the user input until he hits all targets.
Here's how the loop should be positioned.
String answer = "";
do{
System.out.println("Enter your guess");
int userGuess=input.nextInt();
answer=sampleObj.checkForDotcom(userGuess);
System.out.println(answer);
}
while(!answer.equals("kill"));
NOTE: Never Check for String equality using == in Java unless you know what you're doing (also read as, unless you know the concept of String Constant Pool).

How to print text many time in Java? (use self-written methods and while-structure)

I'm doing this really basic Java Programming course.
The aim of the exercise is to ask the user how many times the text has to be printed, and then print it. But as in the comment in the code it was said that ''do not change method definition'' , I have no idea how to proceed to print the text the amount asked. (I was thinking that the i could be added somehow to the method but idk)
Here is the code what I have done so far:
public class ManyPrints {
// NOTE: do not change the method definition, e.g. add parameters to method
public static void printText() {
System.out.println("In the beginning there were the swamp, the hoe and Java.");
// Write your code here
}
public static void main(String[] args) {
// ask the user how many times the text should be printed
// use the while structure to call the printText method several times
Scanner reader = new Scanner(System.in);
System.out.println("How many?");
int number = Integer.parseInt(reader.nextLine());
int i = 0;
while (i <= number) {
i++;
printText();
}
}
}
Thanks in advance if you have the time to help me !:)
As i see your given code, you take a variable int i = 0; and make a condition i <= number in a while loop. Which is counting from 0 to given number. Suppose given number is 10. So the loop is counting 0 to 10 and the text is printing 11 time.And you also need a Scanner class.So your code should look like,
import java.util.Scanner;
public class ManyPrints {
// NOTE: do not change the method definition, e.g. add parameters to method
public static void printText() {
System.out.println("In the beginning there were the swamp, the hoe and Java.");
// Write your code here
}
public static void main(String[] args) {
// ask the user how many times the text should be printed
// use the while structure to call the printText method several times
Scanner reader = new Scanner(System.in);
System.out.println("How many?");
int number = Integer.parseInt(reader.nextLine());
int i = 1;
while (i <= number) {
i++;
printText();
}
}
}
There is nothing wrong with the code except a few points, you did great.
you need to import Scanner
change i = 0;toi = 1;
So you get this
import java.util.Scanner;//Add this line
public class ManyPrints {
public static void printText() {//no problems here
System.out.println("In the beginning there were the swamp, the hoe and Java.");
}
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.println("How many?");
int number = Integer.parseInt(reader.nextLine());
int i = 1;//it should be i = 1; and not i = 0;
while (i <= number) {
i++;
printText();
}
}//done
}
Now if you want to proceed further(I take it that you are a complete beginner).
Get a simple IDE like BlueJ. Create a class. Write the code. Compile it. Run it.
You get the output.
Happy Coding.

Restarting program from a certain point after an if

I started studying Java not too long ago, I am currently trying to make a little game to see if I got the things I saw right.
I want to make a "game" that let's you choose between two dialogue options which have different consequences.
This is the code I used:
package programs;
import java.util.Scanner;
public class Programma1_0 {
public static void main(String[] args) {
System.out.println(
"You wake up in a laboratory. You don't remember ever being there. You actually don't remember anything.");
System.out.println("A door opens, a girl comes towards you.");
System.out.println("Girl:<<Hi, I see you woke up. How are you feeling?>>");
System.out.println("(Write Good or Bad)");
Scanner first = new Scanner(System.in);
String firstch = first.nextLine();
if (firstch.equals("Good")) {
System.out.println("Great, we have a lot to explain.");
} else if (firstch.equals("Bad")) {
System.out.println("You should be alright in an hour or so. You've slept for a long time.");
} else {
System.out.println("(I told you to write Good or Bad)");
}
}
}
So far it's working as intended. The only problem is that if I write something other than Good or Bad i get the message "(I told you to write Good or Bad)" and the program terminates. Is there a way to automatically restart it? If i put more choices in, I want the program to automatically restart from the question where it terminated (So I don't play through half of the game, get a question wrong and have to restart the program from the start), is that possible?
Thanks.
You can accomplish this by putting this before your if statement.
while (true) {
if (firstch.equals("Good") || firstch.equals("Bad"))
break;
else {
System.out.println("(I told you to write Good or Bad)");
firstch = first.nextLine();
}
}
Then you can also remove the last else part of your if statement.
Now it will continue asking for a new input till it gets either "Good" or "Bad"
You can simply put your if-else statement inside the do-while loop, that way you can loop through until you get correct response
int i = 0;
do {
System.out.println("(Write Good or Bad)");
firstch = first.nextLine();
if (firstch.equals("Good")) {
System.out.println("Great, we have a lot to explain.");
i = 0;
} else if (firstch.equals("Bad")) {
System.out.println("You should be alright in an hour or so. You've slept for a long time.");
i = 0
} else {
System.out.println("(I told you to write Good or Bad)");
i = 1;
}
} while (i == 1);
You can partition your program into separate methods. Here I created a method called retrieveAnswer() which its only task to create a Scanner and get input. This method will return a String as seen in the public static String header.
Another method I created was entitled getResult() which takes a String argument and will now compare the String passed from
String firstch = retrieveAnswer();
getResult(firstch);
If the result goes to the else block, it will call retrieveAnswer() and pass the value returned to getResult() as seen in getResult(retrieveAnswer()) which will then restart the whole process.
There are multiple solutions to this, but I just took the recursion route instead. Good luck with Java! If you are confused, look more into methods as they are VERY essential in programming.
import java.util.Scanner;
public class Source {
public static void main(String[] args) {
System.out.println(
"You wake up in a laboratory. You don't remember ever being there. You actually don't remember anything.");
System.out.println("A door opens, a girl comes towards you.");
System.out.println("Girl:<<Hi, I see you woke up. How are you feeling?>>");
System.out.println("(Write Good or Bad)");
String firstch = retrieveAnswer();
getResult(firstch);
}
public static String retrieveAnswer(){
Scanner first = new Scanner(System.in);
String firstch = first.nextLine();
return firstch;
}
public static void getResult(String firstch){
if (firstch.equals("Good")) {
System.out.println("Great, we have a lot to explain.");
} else if (firstch.equals("Bad")) {
System.out.println("You should be alright in an hour or so. You've slept for a long time.");
} else {
System.out.println("(I told you to write Good or Bad)");
getResult(retrieveAnswer());
}
}
}

Infinite loop when prompting for user input?

I am in an intro to java class and have we have to create a Mine Sweeper game. I have the game complete for the most part. But I have having 1 issue where my program goes into a crazy infinite loop.
When the user Guesses wrong and hits a "bomb", the game correctly ends. But when they guess correctly and do not hit a bomb, the program enters a infinite loop for some reason and I have to terminate it.
I use 2 2dArrays, one that displays to the player as all '?', and the other that has actual bombs placed on it, that I compare guesses to and update the player board. I feel like the problem is in my GetGuess method. Not sure why this infinite loop only happens the the user makes a correct guess.
The first guess prompt works fine, and if it hits a bomb, the game ends. If the guess is good, the program should ask for another guess, but instead
The infinte loop prints:
Enter row number:
null
over and over again until I terminate. Can anyone help me figure out where this is coming from?
Here is my main and Game class.
public class MineSweeper {
public static void main(String[] args) {
Game test = new Game();
test.loadBombs(test.bombBoard);
test.loadArray(test.board);
test.loadNumber(test.bombBoard);
test.displayBoard(test.board);
test.displayBoard(test.bombBoard);
while(test.gameOver==false)//Continue to get user guess until win or lose
{
test.getGuess(test.board, test.bombBoard);
test.displayBoard(test.board);
}
}
public class Game {
int arrayDimension = 9;
char[][] board = new char[arrayDimension][arrayDimension];
char[][] bombBoard = new char[arrayDimension][arrayDimension];
static boolean gameOver;
public static void getGuess(char[][] boardArray,char[][]bombBoard)
{
int rowGuess = 0, colGuess=0;
Scanner reader = new Scanner(System.in);
//Prompt user guess
boolean valid = false;//Check if guess is valid
while(valid == false)
{
try{
System.out.println("Enter row number: ");
rowGuess= reader.nextInt();
System.out.println("Enter column number: ");
colGuess = reader.nextInt();
if((rowGuess<0||rowGuess>8) && (colGuess<0||colGuess>8))//Check if guess is valid
{
throw new IndexOutOfBoundsException("This row and column is out of bounds!");
}
else if(rowGuess<0 || rowGuess >8)
{
throw new InputMismatchException("Invalid Row Choice!");
}
else if(colGuess<0 || colGuess >8)
{
throw new InputMismatchException("Invalid Column Choice!");
}
else{}
}
catch(Exception e)
{ System.out.println(e.getMessage());
}
if(rowGuess >= 0 && rowGuess<=8) //Guess was valid, place * at location
{
if(colGuess >=0 && colGuess<=8)
{
boardArray[rowGuess][colGuess]= '*';
reader.close();
valid = true;
}
}
}
char answer = bombBoard[rowGuess][colGuess];//check bomb board with user guess
if(answer == '#')//Bomb, you lose
{
boardArray[rowGuess][colGuess]= answer;
System.out.println("You hit a bomb, game over!");
gameOver = true;
}
else//correct guess, Place number of bombs around guess, continue guessing.
{
boardArray[rowGuess][colGuess]= answer;
gameOver = false;
}
}
Additional to the other answers, I'd like to warn you about this code:
if(colGuess >=0 && colGuess<=8) {
boardArray[rowGuess][colGuess]= '*';
reader.close(); // this line is dangerous
valid = true;
}
Here you're closing the current InputStream of System.in. The problem with this is, that it cannot be re-opend by simply calling Scanner reader = new Scanner(System.in); on the next getGuess method call. That means you're unable to retrieve more user input and furthermore a method call on reader can cause a NoSuchElementException.
To fix this, move the Scanner reader variable to the class fields:
public class Game {
// ... other fields
Scanner reader = new Scanner(System.in);
and create a new method that will close this resource if the game is over:
public void finishGame() {
// do other stuff to close the game
reader.close();
}
You can call this method like this:
while(test.gameOver==false)//Continue to get user guess until win or lose
{
test.getGuess(test.board, test.bombBoard);
test.displayBoard(test.board);
}
test.finishGame(); // Game is over, close all resources
In this part of your code
else//correct guess, Place number of bombs around guess, continue guessing.
{
boardArray[rowGuess][colGuess]= answer;
gameOver = false;
}
You want something that will set gameOver to be true when the user has found all of the empty spaces. You should use a variable to keep track of the number of correct guesses the player has made, and if this number plus the number of bombs equals the number of tiles on the board then you want game over to be true. This will exit the outer loop in your test runner class.
Infinite loops mostly happen if you try to read a closed scanner. I would also suggest you keep a refence of it
as an object variable or at least in your main function:
Scanner reader = new Scanner(System.in);
Game test = new Game();
test.loadBombs(test.bombBoard);
test.loadArray(test.board);
test.loadNumber(test.bombBoard);
test.displayBoard(test.board);
test.displayBoard(test.bombBoard);
Hope it helps.

Categories

Resources