I'm having a tough time understanding something from one of my exercises in a book(Complete beginner).
The example code was to make a guessing game with three players and it generates a random int for all three players which has to come out the same as the randomly generated int by the game.
The code contains three classes, but this is the most important one where I have a question. The other two are just GameLauncher class and Player(The one that plays against the other three players) class.
public class GuessGame {
Player p1;
Player p2;
Player p3;
public void startGame(){
p1 = new Player();
p2 = new Player();
p3 = new Player();
int guessp1 = 10;
int guessp2 = 0;
int guessp3 = 0;
boolean p1isRight = false;
boolean p2isRight = false;
boolean p3isRight = false;
int targetNumber = (int) (Math.random() * 10);
System.out.println("I'm thinking of a number between 0 and 9...");
while(true){
System.out.println("Number to guess is "+targetNumber);
p1.guess();
p2.guess();
p3.guess();
guessp1 = p1.number;
System.out.println("Player one has guessed " + guessp1);
guessp2 = p2.number;
System.out.println("Player two has guessed " + guessp2);
guessp3 = p3.number;
System.out.println("Player three has guessed " + guessp3);
if (guessp1 == targetNumber) {
p1isRight = true;
}if (guessp2 == targetNumber){
p2isRight=true;
}if (guessp3 == targetNumber){
p3isRight=true;
}
if (p1isRight||p2isRight||p3isRight){
System.out.println("We have a winner!");
System.out.println("Player one got it right? " + p1isRight);
System.out.println("Player two got it right?" + p2isRight);
System.out.println("Player three got it right? " + p3isRight);
System.out.println("The game is over.");
break;
}else{
System.out.println("None of you got it right! Try again!");
}
}
}
}
From the above code:
int guessp1 = 10;
int guessp2 = 0;
int guessp3 = 0;
is the one that I don't understand. Originally, all of them are assigned the value 0. I tried to assign 10 to see what would happen, but nothing changed. The game played out just the same.
My question is, what is the significance for the value assigned to a declared integer if any at all? Especially in this situation.
Excluding normal uses like say just printing it or manipulating it with math.
Later in your routine you override the assigned value int the following code lines:
guessp1 = p1.number;
System.out.println("Player one has guessed " + guessp1);
guessp2 = p2.number;
System.out.println("Player two has guessed " + guessp2);
guessp3 = p3.number;
System.out.println("Player three has guessed " + guessp3);
So basically the assigned value is not beeing uset at all, but usually a good practice implies setting a default values to variables so on later use you can determine if a value was assigned or the variable still has its default value.
Defining a default value helps to avoid unexpected situations, In some programming languages created objects which was not assigned or set to null could have any garbage data according to the memory address they reference.
Some use a values like -1 where only positive numbers are expected and catch such a case where no value was assigned by asking if the default value still there or not. In your case the default value is 0.
Related
I am very new to Java Programming. For example, even if I roll the same number i still lose the bet. If I roll like one and fine, I still win the bet amount. I am trying to fix that problem for hours. But can't figure it out. Please, someone, help me. Thanks in advance.
Here is my code.
public class Dice {
private int dice;
public Random number;
//default constructor
public Dice() {
number = new Random();
}
//To generate random number between 1 to 6. random starts from 0. there is no 0 on dice.
//By adding one, it will start at 1 and end at 6
}
//Method to check two dice
public boolean isequal(int dice1,int dice2) {
}
else
}
public class Playgame
{
//
}
public static void main(String[] args) {
//
}
}
{
return false;
}
}
userinput.close();
}
}
At least one problem is here (there may be others) :
if(obj1.isequal(obj1.play(), obj1.play()) == true)
{
System.out.println("You rolled a " + toString(obj1.play()) + " and a "
+ toString(obj1.play()) );
When you print the message, you are calling obj1.play() again and generating 2 new random numbers. If you need to use the value twice (once for comparison and once for printing) then you should store it in a variable.
int firstRoll = obj1.play();
int secondRoll = obj1.play();
if(obj1.isequal(firstRoll, secondRoll) == true)
{
System.out.println("You rolled a " + toString(firstRoll) + " and a "
+ toString(secondRoll) );
//...
Each call to obj1.play() return a different values.
Hence your test: obj1.isEqual(obj1.play(), obj1.play()) will mostly not return true.
no need for the dice class if it is to generate the random number and checks whether two number is equal or not. try the code below it will work
Random random = new Random();
int n1 = random.nextInt(6) + 1;
int n2 = random.nextInt(6) + 1;
System.out.println("You rolled a " + toString(n1)+ " and a " +toString(n2));
if (n1 == n2) {
double win = betmoney * 2;
System.out.println("You win $" + win);
startmoney += win;
} else {
startmoney -= betmoney;
System.out.println("You lose $" + betmoney);
System.out.println("You left only $" + startmoney);
}
problem with your code is your generating random numbers two times 1.during condition check and 2. in the sysout statement. your program is working fine only. but due to this your confusing yourself that it.
Each time you call ob1.play() method, it will give you different numbers.
in if clause:
if(obj1.isequal(obj1.play(), obj1.play()) == true)
will give you two random values that different from two random values in if block:
System.out.println("You rolled a " + toString(obj1.play()) + " and a " + toString(obj1.play()) );
I am a beginner programmer so bear with me. I am currently working on a project for my coding class, where I am tasked with writing a program to simulate a batter facing a pitcher in a baseball game for one turn at bat. I think the problem in my code is a logic error, but I'm not quite sure how to fix it. I have tried added additional while loops and if statements but nothing has worked. The code in my batter class and pitcher class work completely fine, it's just the code in my driver. Stuff sometimes breaks out of loop when it's suppose to and other times it doesn't and sometimes it repeats the same line of text when it's not suppose to.
I would really appreciate some help on this. Thank you so much in advance.
Here is my code for the driver program:
public static void main(String[] args) {
Batter batter = new Batter();
batter.getname();
// making the pitcher object and calling the get name method.
Pitcher pitcher = new Pitcher();
pitcher.getname();
System.out.println(pitcher.getname() + " is pitching to " + batter.getname());
while (true) {
int ball = 0;
int strike = 0;
// desiding if the hit method gets called on the batter object
if (pitcher.pitch() == false) {
ball++;
System.out.println("The count is " + ball + " balls " + strike + " strikes");
}
if (pitcher.pitch()== false && ball ==4 ){
System.out.println("The count is " + ball + " balls " + strike + " strikes");
System.out.println(batter.getname() + " walked.");
break;
}
if (batter.hit() == false) {
strike++;
System.out.println("The count is " + ball + " balls " + strike + " strikes");
}
if (batter.hit() == false && strike == 3){
System.out.println("The count is " + ball + " balls " + strike + " strikes");
System.out.println(batter.getname() + " struck out.");
break;
}
if (batter.hit() == true && pitcher.pitch() == true) {
break;
}
}
}
At a quick glance I'd recommend the following.
For a start change to this as there is no point calling the getter every time so give it a variable name:
Batter batter = new Batter();
bName = batter.getname();
Pitcher pitcher = new Pitcher();
pName = pitcher.getname();
You can then replace batter.getName() & pitcher.Name() with bName & pName
I'm not a fan of while(true) and you need move ball and strike so try this instead:
int ball = 0, strike = 0;
boolean game = true;
while(game){
Then i'm not sure if your second if is right as you check for pitcher.pitch()==false in first if so try second one as:
if(ball == 4) {
Finally replace break with:
game = false;
You are resetting the values back to 0 every time the loop starts. Because you include the assignment inside the loop, it returns every time. Please note the number of the comments, meant to be read chronologically.
while (true) {
int ball = 0;
// [3] here, we are resetting ball to 0, so the ++ below is negated
int strike = 0;
if (pitcher.pitch() == false) {
ball++;
// [1] lets assume we get here, the value of ball now is 1.
// [2] we go to comment [3]
if you want to maintain the result, you should set the values before the loop.
int ball = 0;
int strike = 0;
// [1] both are 0 now
while (true) {
// [3] now, we maintain ball is 1. see 4
if (pitcher.pitch() == false) {
ball++;
// [2] setting ball to 1, repeating loop, see [3]
// [4] now we increment ball again, ball has value 2. Etc.
I am working through Head First Java, and my Random generator is amounting to 0. Here are my classes:
This is my class with the main method.
public class GameLauncher {
public static void main(String[] args) {
GuessGame game = new GuessGame();
game.startGame();
}
}
This is my player object class:
import java.util.Random;
public class Player {
int number = 0; //Where the guess goes
public void guess() {
//random1 is in GuessGame
Random random2 = new Random();
int number = random2.nextInt(10);
System.out.println("I'm guessing " + number);
}
}
Finally, this is the class where most of the code is happening.
import java.util.Random;
public class GuessGame {
//Guessgame has three instance variables for the three Player objects
Player p1;
Player p2;
Player p3;
public void startGame() {
//Create three Player objects and assign them to the three Player instance variables
p1 = new Player();
p2 = new Player();
p3 = new Player();
//Declare three variables to hold the three guesses the players make
int guessp1 = 0;
int guessp2 = 0;
int guessp3 = 0;
//Declare three variables to hold a true or false based on the player's answer
boolean p1isRight = false;
boolean p2isRight = false;
boolean p3isRight = false;
//Make a "target" number that the players have to guess
Random random = new Random();
//Generate a number between 0 and 9
int targetNumber = random.nextInt(10);
System.out.println("I'm thinking of a number between 0 and 9...");
while (true) {
System.out.println("Number to guess is " + targetNumber);
//Call each player's guess() method
p1.guess();
p2.guess();
p3.guess();
/*
Get each player's guess (the result of their guess() method
running) by accessing the number variable of each player
*/
guessp1 = p1.number;
guessp2 = p2.number;
guessp3 = p3.number;
System.out.println("Player one guessed " + guessp1);
System.out.println("Player two guessed " + guessp2);
System.out.println("Player three guessed " + guessp3);
/*
Check each player's guess to see if it matches the target number. If a player is right, then set that player's variable to be true (remember, we set it false by default)
*/
if (guessp1 == targetNumber) {
p1isRight = true;
}
if (guessp2 == targetNumber) {
p2isRight = true;
}
if (guessp3 == targetNumber) {
p3isRight = true;
}
//If player one OR player two OR player three is right... (the || operator means OR)
if (p1isRight || p2isRight || p3isRight) {
System.out.println("We have a winner!");
System.out.println("Player one got it right? " + p1isRight);
System.out.println("Player two got it right? " + p2isRight);
System.out.println("Player three got it right? " + p3isRight);
System.out.println("Game is over.");
break; //Game over, so break out of the loop
}
else {
//We must keep going because nobody got it right!
System.out.println("Players will have to try again.");
} //end if/else
} //end loop
} //end method
} //end class
I am new to these forums, so if I did something wrong please let me know :)
Does anyone know why this isn't working?
Thanks,
Lyfe
You are storing random number in local variable and you think you set it in instance variable
at line
int number = random2.nextInt(10);
change it to
this.number = random2.nextInt(10);
that atleast solves stated problem.
Also See
Java Variables
I am supposed to write a program that selects a random number between user given constraints, and asks the user to input guesses as to what this number is. The program gives feedback to the user as to whether or not the number is higher or lower than the user's guesses. The number of guesses, the number of games, the total guesses used throughout all of the games, and the lowest number of guesses used in one game are recorded.
These results are printed. The functions that responsible for running the game (playGame()) and the functions responsible for printing these results (getGameResults()) must be in two separate methods.
My problem is, I am not sure how to get the local variables that are modified throughout the course of the method playGame() to the getGameResults() method.
getGameResults() is intended to be called in another method, continuePlayTest(), which tests the user's input to determine whether or not they wish to continue playing the game, so I don't think that calling getGameResults() will work, otherwise this test will not work either. Unless I call continuePlayTest() in playGame(), but continuePlayTest() calls playGame() in its code so that would complicate things.
We can use ONLY the concepts that we've learned. We cannot use any concepts ahead.
So far, we've learned how to use static methods, for loops, while loops, if/else statements and variables. Global variables are bad style, so they cannot be used.
CODE:
public class Guess {
public static int MAXIMUM = 100;
public static void main(String[] args) {
boolean whileTest = false;
gameIntroduction();
Scanner console = new Scanner(System.in);
playGame(console);
}
// Prints the instructions for the game.
public static void gameIntroduction() {
System.out.println("This process allows you to play a guessing game.");
System.out.println("I will think of a number between 1 and");
System.out.println(MAXIMUM + " and will allow you to guess until");
System.out.println("you get it. For each guess, I will tell you");
System.out.println("whether the right answer is higher or lower");
System.out.println("than your guess.");
System.out.println();
}
//Takes the user's input and compares it to a randomly selected number.
public static void playGame(Scanner console) {
int guesses = 0;
boolean playTest = false;
boolean gameTest = false;
int lastGameGuesses = guesses;
int numberGuess = 0;
int totalGuesses = 0;
int bestGame = 0;
int games = 0;
guesses = 0;
games++;
System.out.println("I'm thinking of a number between 1 and " + MAXIMUM + "...");
Random number = new Random();
int randomNumber = number.nextInt(MAXIMUM) + 1;
while (!(gameTest)){
System.out.print("Your guess? ");
numberGuess = console.nextInt();
guesses++;
if (randomNumber < numberGuess){
System.out.println("It's lower.");
} else if (randomNumber > numberGuess){
System.out.println("It's higher.");
} else {
gameTest = true;
}
bestGame = guesses;
if (guesses < lastGameGuesses) {
bestGame = guesses;
}
}
System.out.println("You got it right in " + guesses + " guesses");
totalGuesses += guesses;
continueTest(playTest, console, games, totalGuesses, guesses, bestGame);
}
public static void continueTest(boolean test, Scanner console, int games, int totalGuesses, int guesses, int bestGame) {
while (!(test)){
System.out.print("Do you want to play again? ");
String inputTest = (console.next()).toUpperCase();
if (inputTest.contains("Y")){
playGame(console);
} else if (inputTest.contains("N")){
test = true;
}
}
getGameResults(games, totalGuesses, guesses, bestGame);
}
// Prints the results of the game, in terms of the total number
// of games, total guesses, average guesses per game and best game.
public static void getGameResults(int games, int totalGuesses, int guesses, int bestGame) {
System.out.println("Overall results:");
System.out.println("\ttotal games = " + games);
System.out.println("\ttotal guesses = " + totalGuesses);
System.out.println("\tguesses/games = " + ((double)Math.round(guesses/games) * 100)/100);
System.out.println("\tbest game = " + bestGame);
}
}
If you cannot use "global" variables, I guess your only option is passing parameters when calling the method. If you don't know how to declare and use methods with parameters, I don't know another answer.
EDIT/ADD
After you specified your question, circumstances and posted your code I got a working solution including comments.
public class Guess {
public static int MAXIMUM = 100;
public static void main(String[] args) {
boolean play = true; // true while we want to play, gets false when we quit
int totalGuesses = 0; // how many guesses at all
int bestGame = Integer.MAX_VALUE; // the best games gets the maximum value. so every game would be better than this
int totalGames = 0; // how many games played in total
Scanner console = new Scanner(System.in); // our scanner which we pass
gameIntroduction(); // show the instructions
while (play) { // while we want to play
int lastGame = playGame(console); // run playGame(console) which returns the guesses needed in that round
totalGames++; // We played a game, so we increase our counter
if (lastGame < bestGame) bestGame = lastGame; // if we needed less guesses last round than in our best game we have a new bestgame
totalGuesses += lastGame; // our last guesses are added to totalGuesses (totalGuesses += lastGame equals totalGuesses + totalGuesses + lastGame)
play = checkPlayNextGame(console); // play saves if we want to play another round or not, whats "calculated" and returned by checkPlayNextGame(console)
}
getGameResults(totalGames, totalGuesses, bestGame); // print our final results when we are done
}
// Prints the instructions for the game.
public static void gameIntroduction() {
System.out.println("This process allows you to play a guessing game.");
System.out.println("I will think of a number between 1 and");
System.out.println(MAXIMUM + " and will allow you to guess until");
System.out.println("you get it. For each guess, I will tell you");
System.out.println("whether the right answer is higher or lower");
System.out.println("than your guess.");
System.out.println();
}
// Takes the user's input and compares it to a randomly selected number.
public static int playGame(Scanner console) {
int guesses = 0; // how many guesses we needed
int guess = 0; // make it zero, so it cant be automatic correct
System.out.println("I'm thinking of a number between 1 and " + MAXIMUM + "...");
int randomNumber = (int) (Math.random() * MAXIMUM + 1); // make our random number. we don't need the Random class with its object for that task
while (guess != randomNumber) { // while the guess isnt the random number we ask for new guesses
System.out.print("Your guess? ");
guess = console.nextInt(); // read the guess
guesses++; // increase guesses
// check if the guess is lower or higher than the number
if (randomNumber < guess)
System.out.println("It's lower.");
else if (randomNumber > guess)
System.out.println("It's higher.");
}
System.out.println("You got it right in " + guesses + " guesses"); // Say how much guesses we needed
return guesses; // this round is over, we return the number of guesses needed
}
public static boolean checkPlayNextGame(Scanner console) {
// check if we want to play another round
System.out.print("Do you want to play again? ");
String input = (console.next()).toUpperCase(); // read the input
if (input.contains("Y")) return true; // if the input contains Y return true: we want play another round (hint: don't use contains. use equals("yes") for example)
else return false; // otherwise return false: we finished and dont want to play another round
}
// Prints the results of the game, in terms of the total number
// of games, total guesses, average guesses per game and best game.
public static void getGameResults(int totalGames, int totalGuesses, int bestGame) {
// here you passed the total guesses twice. that isnt necessary.
System.out.println("Overall results:");
System.out.println("\ttotal games = " + totalGames);
System.out.println("\ttotal guesses = " + totalGuesses);
System.out.println("\tguesses/games = " + ((double) (totalGuesses) / (double) (totalGames))); // cast the numbers to double to get a double result. not the best way, but it works :D
System.out.println("\tbest game = " + bestGame);
}
}
Hope I could help.
Is it a problem passing the variables between functions? ex:
public static void getGameResults(int games, int totalGuesses, int guesses, int bestGame) {
// implementation
}
Another option, assuming this is all in one class, is using private static memeber variables. They aren't global. Then again, they might be considered 'global' by your teacher for this assignment.
Given that you've only learnt how to use static methods, your only option is to pass the information from function to function via its arguments.
I am building a game for Java and I've gotten down to one of the last parts. There are still other things to fix which are easy fixes. Although there is one problem I can't figure out, which is that after every time I run the game I can't get new random numbers to show up.
For instance, if nobody guesses the number that is made at random by the computer from numbers (1-20) than the game repeats itself. You are playing 2 computers during this and their numbers are also made up at random (again 1-20 values).
Here is some of the code for the Player class I made
import java.util.Scanner;
import java.util.Random;
public class Player {
private double currentGuess = 0;
private String firstName;
private boolean isCorrect;
Scanner myScanner = new Scanner(System.in);
public Player() {
}
public double autoGuess() {
Random randomNumber = new Random();
double number = randomNumber.nextInt(20) + 1;
return number;
}
Here is my GuessGame class, which is where the action actually happens. I cannot figure out why when I call for the autoGuess method (from the player class) I cannot get new random numbers to show up after each game which resulted in nobody guessing the right number. Same goes for randomly generating the number to be guessed (WINNING_NUMBER), it still stays the same after every new game. Here is some of the code of my GuessGame class.
import java.util.Random;
public class GuessGame {
Player player1 = new Player();
Player player2 = new Player();
Player player3 = new Player();
double player2Guess = player2.autoGuess();
double player3Guess = player3.autoGuess();
int numberOfTries = 0;
public double generateWinningNumber() {
Random randomGenerate = new Random();
double randomNumber = randomGenerate.nextInt(20) + 1;
return randomNumber;
}
private final double WINNING_NUMBER = generateWinningNumber();
public void startGame() {
System.out.println("VM: Welcome we are going to play a number guessing game. I'm (the JVM)\n"
+ "going to randomly pick a number between 0 and 20. You and the 2 other\n"
+ "computer-generated players are going to try to guess that number. The game\n"
+ "will end when at least one of the players correctly guesses the number.\n");
System.out.println(WINNING_NUMBER);
System.out.print("What is your name? ");
String player1Name = player1.readName();
while (player1.isCorrect() == false)
{
System.out.print(player1Name + ", enter your guess: ");
player1.readGuess();
player2.autoGuess();
player3.autoGuess();
numberOfTries++;
if (player1.getCurrentGuess() == WINNING_NUMBER || player2Guess == WINNING_NUMBER || player3Guess == WINNING_NUMBER)
{
player1.setCorrect(true);
displayGuesses();
determineWinner();
}
else
{
displayGuesses();
displayHints();
System.out.println("No one guessed the number.\nPlayers will need to guess again.\n");
System.out.println("The winning number was: " + WINNING_NUMBER);
generateWinningNumber();
}
}
}
public void displayGuesses() {
System.out.print("Player 2's guess is: " + player2Guess + "\n");
System.out.print("Player 3's guess is: " + player3Guess + "\n\n");
}
There is more to this and obviously more to fix, but I'm trying to just get this issue out of the way. I have my driver class starting this game and also know I need to round my numbers to Ints so there isn't a decimal place. Also, all the hint method is referring to is just if statements that tell you if you are hot, warm, or cold. That runs perfectly fine. Just needing some help with figuring out how to get a new random number after every new unsuccessful game from my while statement.
Your generateWinningNumber() method returns a double, it does not actually change any values itself. Also, because WINNING_NUMBER is a constant, even if you did want to change it, you wouldn't be able to. You should definitely not make WINNING_NUMBER final, and either modify generateWinningNumber() so that it modifies your global variable itself, or leave it as-is and use its return value to assign to your non-constant winningNumber global.