How do I implement a "Beeping" Sound to my code? - java

So my code runs fine and all; no problems, or atleast I think it doesn't have any; I need to add two final things: first one is a way to make a beep sound (think I have to use toolkit, actionListener, etc) and the other thing, is a way to print the current random number the program generates (something like a toString). I've tried my professor's methods for the beep sound and nothing, so now I'm here. Thanks in advance.
Tried some methods from my professors code but nothing.
//Interface
package edu.pupr.LottoGame;
public interface Ask {
public void ask();
}
//Class
package edu.pupr.LottoGame;
import java.util.Random;
import javax.swing.JOptionPane;
public class theLotto {
//VARIABLE
public int values;
//OVERLOAD CONSTRUCTOR
public theLotto(int values) {
this.values = values;
}
//MUTATOR
public void setValues(int values) {
this.values = values;
}
//ACCESSOR
public int getValues() {
return values;
}
//ANONYMOUS CLASS
public void start() {
Ask ak = new Ask() {
public void ask() {
//ANONYMOUS VARIABLES USED
String input = null;
int reply;
int randomNum;
//BEGINNING OF FIRST DO-WHILE
do {
//INPUT OF USER : 4 DIGITS
input = JOptionPane.showInputDialog("Plese enter 4 digits (1000-9999)");
values = Integer.parseInt(input);
}while(values < 1000 || values > 9999);
//ENDING OF FIRST DO WHILE
//RANDOM NUMBER GENERATOR
Random rn = new Random();
randomNum = 1000 + rn.nextInt(10000 - 1000);
//DETERMINES IF THE USER WON OR LOST
if(values == randomNum)
JOptionPane.showMessageDialog(null, "You Won! The num: " + randomNum);
else
JOptionPane.showMessageDialog(null, "You Lost! The wining num was: " + randomNum);
//BEGINNING OF SECOND DO-WHILE
do {
//WANNA PLAY AGAIN PANEL
reply = JOptionPane.showConfirmDialog(null, "Continue?", null, JOptionPane.YES_NO_OPTION);
//IF OPTION IS YES
if (reply == JOptionPane.YES_OPTION) {
//BEGINNIG OF THIRD DO-WHILE
do {
//INPUT OF USER : 4 DIGITS
input = JOptionPane.showInputDialog("Plese enter 4 digits (1000-9999)");
values = Integer.parseInt(input);
}while(values < 1000 || values > 9999);
//END OF THIRD DO-WHILE
//RANDOM NUMBER GENERATOR
Random rn2 = new Random();
randomNum = 1000 + rn2.nextInt(10000 - 1000);
//DETERMINES IF THE USER WON OR LOST
if(values == randomNum)
JOptionPane.showMessageDialog(null, "You Won! The num: " + randomNum);
else
JOptionPane.showMessageDialog(null, "You Lost! The wining num was: " + randomNum);
}
//IF OPTION IS NO
else {
JOptionPane.showMessageDialog(null, "GOODBYE");
System.exit(0);
}
}while(reply != JOptionPane.NO_OPTION);
//END OF SECOND DO-WHILE
}
};
//PART OF ANONYMOUS
ak.ask();
}
}
//Class Driver
package edu.pupr.LottoGame;
public class theLottoDriver {
public static void main(String [] args) {
theLotto lt = new theLotto(0);
lt.start();
}
}
Ouput should be something like this:
Enter Number: 3267
09/07/2019 09:50:23:
throw #1: 4392
throw #2: 8391
You lost !!!

Related

My Java code that is about for loop with factorial doesnt work like it should [duplicate]

This question already has answers here:
What is a debugger and how can it help me diagnose problems?
(2 answers)
Closed 4 years ago.
My Java code should let the user enter a number and then calculate the factorial of that number and I need to use "for loop" When I enter number 5 it tells me the factorial is 6 when it should be 120. I have tried to watch tutorials with factoring loop but they wont work, i think its because i have "do" command that gets values from calling
Here is the code:
static Scanner kboard = new Scanner(System.in); //variable to read in values
public static void main(String[] args) {
int choice = 0;
String dummy = "";
String forename = "";
String surname = "";
int number = 0;
do {
System.out.println("1. display the user name, 2. calculate factorial, 3. exit");
choice = kboard.nextInt();
dummy = kboard.nextLine(); //strips out the return
if (choice == 1) {
forename = getforename();
surname = getsurname();
displaydetails(forename, surname);
}
if (choice == 2) {
number = getnumber();
calcfactorial(number);
}
if (choice == 3) {
System.out.println("bye");
}
} while (choice != 3);
}
public static String getforename() {
String newforename = "";
System.out.println("Please enter your forename ?");
newforename = kboard.next();
return (newforename);
} // end getforename
public static String getsurname() {
/*
Code to prompt the user to enter a surname
*/
String newsurname = "";
System.out.println("Please enter your surname ?");
newsurname = kboard.next();
return (newsurname);
} // end getsurname
public static void displaydetails(String displayforename, String displaysurname) {
/*
Code will carry out prescribed changes and display the result
*/
char displayinitial;
String displayusername = "";
displaysurname = displaysurname.toUpperCase();
displayinitial = displayforename.charAt(0);
displayusername = displayinitial + displaysurname;
System.out.println("Your username is " + displayusername);
}
public static int getnumber() {
System.out.println("What numbers factorial do you want to know?");
int newnumber = kboard.nextInt();
return newnumber;
}
public static void calcfactorial(int newnumber) {
int count = 0;
int factorial = 1;
if (newnumber > 0) {
for (count = 1; count <= newnumber; count++); {
factorial = factorial * count;
System.out.println("Factorial of " + newnumber + " is: " + factorial);
}
} else {
System.out.println("Number must be positive");
}
}
If you had used a debugger, then you could tell that it's only executing the multiplication in the calcfactorial method once, and count is already 6 at this point. Reasons:
First, remove the semicolon at the end of the for condition loop. It is acting as the body of the for loop. This makes count equal to newnumber + 1, or 6.
Second, move your print statement after the end of the for loop, but still within the if block. Otherwise you'll get newnumber lines of printouts.
I am not going to fully give away the answer...user azurefrog posted a helpful link on debugging code.
However, your for loop is doing something you don't intend:
public static void calcfactorial(int newnumber)
{
int count = 0;
int factorial = 1;
if (newnumber > 0)
{
for (count=1;count<=newnumber;count++);
{
factorial = factorial * count; System.out.println("Factorial of "+newnumber+" is: "+factorial);
}
}
else
{
System.out.println("Number must be positive");
}
}
Reading through this, the line factorial = factorial * count; is just going to do 1*1, 1*2, 1*3, 1*4, 1*5, etc. for whatever is entered to be calculated for factorial. This is not the correct logic.
I recommend thinking through the logic of the for loop a bit more carefully. You should be using the number entered (i.e. 5 for the example you've given) somewhere in that loop.

Creating Methods that will produce the same result

I'm in a Beginner Java class and I'm confused about using additional methods and using them in another. I think that I have most of my assignment done but I just need help to create the methods. One is to generate the question and the other one is to display the message.
I know that in order to call a method
public static test(num1,num2,num3)
but in my code, how do I make it so that I call the method and still make it loop correctly?
In the assignment instructions that was given to me, In order to do that, I have to write a method named
public static in generateQuestion()
and
public static void displayMessage(boolean isCorrect)
This is my code:
//For Random Generator
import java.util.Random;
//Uses a class Scanner
import java.util.Scanner;
public class Assign6
{
public static void main(String[] args)
{
//Scanner to Obtain Input from CW
Scanner input = new Scanner(System.in);
//Generate Random Number for Quiz
Random randomNumbers = new Random();
int number1 = 0;
int number2 = 0;
int answer = 0;
//Rolls number from 1-9
number1 = randomNumbers.nextInt(9);
number2 = randomNumbers.nextInt(9);
//Question prompt
System.out.println("How much is " +number1+ " times " +number2+ "? ");
answer = input.nextInt();
//If Else While Statements
if(answer == (number1*number2))
{
System.out.println("Good job! You got it right!");
}
else
{
while (answer !=(number1*number2))
{
System.out.println("You got it wrong, try again!");
answer = input.nextInt();
}
}
}
}
You are going to have two methods
public static void generateQuestion()
Which is going to hold the code to generate the random values and output it. It will return void because all it's doing is printing out.
Next you will have
public static void displayMessage(boolean isCorrect)
which will be called if if(answer == (number1*number2)) is true with true as the parameter. Otherwise it will still be called, but the parameter passed in will be false. This method will determine if isCorrect is true or false, and output the appropriate message.
If I got it right, I have a solution that might be a little stupid but will work for your assignment.
If you make generateQuestion that makes two random ints, prints the question and returns their multiple (answer).
And displayMessgae that prints "Good job! You got it right!" if isCorrect is true and "You got it wrong, try again!" else,
you can call generateQuestion, then get an answer (in main), and loop until answer is correct (according to return value of generateQuestion).
Every time you get a new answer (in loop), call displayMessgae(false).
After the loop ended call displayMessgae(true)
This is my working code for this:
//For Random Generator
import java.util.Random;
//Uses a class Scanner
import java.util.Scanner;
public class Assign6
{
public static int generateQuestion()
{
Random r = new Random();
int x = r.nextInt(9), y = x = r.nextInt(9);
System.out.println("How much is " + x + " times " + y + "? ");
return x * y;
}
public static void displayMessage(boolean isCorrect)
{
if (isCorrect)
System.out.println("Good job! You got it right!");
else
System.out.println("You got it wrong, try again!");
}
public static void main(String[] args)
{
//Scanner to Obtain Input from CW
Scanner input = new Scanner(System.in);
int rightAnswer = 0;
rightAnswer = generateQuestion();
while (input.nextInt() != rightAnswer)
displayMessage(false);
displayMessage(true);
}
}
If I understand your question correctly, It's simply a matter of separating the functionality that prints a question and displays the answer into separate methods. See my edited version of your code below
public class Assign6
{
public static void main(String[] args)
{
// Scanner to Obtain Input from CW
Scanner input = new Scanner(System.in);
// Generate Random Number for Quiz
Random randomNumbers = new Random();
int number1 = 0;
int number2 = 0;
int answer = 0;
// Rolls number from 1-9
number1 = randomNumbers.nextInt(9);
number2 = randomNumbers.nextInt(9);
displayQuestion("How much is " + number1 + " times " + number2 + "?");
answer = input.nextInt();
// If Else While Statements
if (answer == (number1 * number2))
{
displayMessage(Boolean.TRUE);
}
else
{
while (answer != (number1 * number2))
{
displayMessage(Boolean.FALSE);
answer = input.nextInt();
}
}
}
public static void displayQuestion(String q)
{
System.out.println(q);
}
public static void displayMessage(Boolean isCorrect)
{
if (isCorrect)
{
System.out.println("Good job! You got it right!");
}
else
{
System.out.println("You got it wrong, try again!");
}
}
}

Java / Creating hi/low game using multiple methods, stuck at returning no. of guesses

I'm creating a high/low guessing game as part of a study assignment, and the part im stuck at is getting the amount of guesses returned to the main method. We have specifically been told that the main method has to print the number of guesses, while the method "playGame" actually does the playing.
There's more to the code, a method called giveReponse that checks if the number is correct or too high/low, but it works as intended. I get "Cannot find symbol" when trying to print how many guesses it took to complete the game.
If it wasn't so important to print it in the main method I'd print the amount in the method playGame, but thats a no-go. What am I doing wrong?
The code looks like this:
public class HiLo {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Welcome to classic guessing game of high/low!");
String difficulty = scan.next();
if (difficulty.equals("easy")) {
playGame(10);
} else if (difficulty.equals("medium")) {
playGame(100);
} else if (difficulty.equals("hard")) {
playGame(1000);
}
System.out.println("You won in" + guesses + "attempts.");
}//EndsMain
public static int playGame(int maxNumber) {
Scanner scan = new Scanner(System.in);
int rannr = (int)(Math.random() * maxNumber) +1;
int answer = rannr;
int guess = 0;
int guesses = 0;
System.out.println("Game is starting...");
do {
guess = scan.nextInt();
guesses ++;
giveResponse(answer, guess);
if (answer == guess) {
break;
}
} while (answer != guess);
return guesses;
} //Ends playGame
Your method playGame( ) is returning a value but since is not assigned to no variable, those returns are getting lost...
additional to that it looks like the code is not complete:
this statement is not going to let you compile:
System.out.println("You won in" + guesses + "attempts.");
because the only guesses variable I see in there is scoped in the playGame method....
do instead something like:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Welcome to classic guessing game of high/low!");
int guesses = 0;
String difficulty = scan.next();
if (difficulty.equals("easy")) {
guesses = playGame(10);
} else if (difficulty.equals("medium")) {
guesses = playGame(100);
} else if (difficulty.equals("hard")) {
guesses = playGame(1000);
}
The problem is at:
System.out.println("You won in" + guesses + "attempts.");
Since the variable guesses is not defined in the main method, the compiler doesn't know what you are referencing to with that symbol.
But since playGame() returns the number of guesses, I'd recommend something like this:
if (difficulty.equals("easy")) {
System.out.println("You won in" +playGame(10)+ "attempts.");
} else if (difficulty.equals("medium")) {
System.out.println("You won in" +playGame(100)+ "attempts.");
} else if (difficulty.equals("hard")) {
System.out.println("You won in" +playGame(1000)+ "attempts.");
}
Here's a solution that shows how easy it would be to restrict guesses. Not much extra effort or thought:
package games;
import java.util.Random;
import java.util.Scanner;
/**
* HiLo guessing game
* Created by Michael
* Creation date 4/9/2016.
* #link https://stackoverflow.com/questions/36522303/java-creating-hi-low-game-using-multiple-methods-stuck-at-returning-no-of-gu
*/
public class HiLo {
public static void main(String [] args) {
int maxValue = (args.length > 0) ? Integer.parseInt(args[0]) : 100;
int maxGuesses = (args.length > 1) ? Integer.parseInt(args[1]) : 5;
Scanner scanner = new Scanner(System.in);
String answer = "Y";
do {
play(scanner, maxValue, maxGuesses);
System.out.println("Play again? [Y/N]: ");
answer = scanner.next();
System.out.println(String.format("You answered %s; let's play again!", answer));
} while ("Y".equalsIgnoreCase(answer));
}
private static void play(Scanner scanner, int maxValue, int maxGuesses) {
int value = new Random().nextInt(maxValue) + 1;
int numGuesses = 0;
boolean match = false;
do {
System.out.println(String.format("Guess a value between 1 and %d: ", maxValue));
int guess = Integer.parseInt(scanner.next());
if (guess < value) {
System.out.println(String.format("Too low; guess again. (%d guesses left)", (maxGuesses-numGuesses-1)));
} else if (guess > value) {
System.out.println(String.format("Too high; guess again (%d guesses left)", (maxGuesses-numGuesses-1)));
} else {
match = true;
System.out.println(String.format("You got it right in %d guesses! ", numGuesses+1));
break;
}
} while (!match && ++numGuesses < maxGuesses);
if (!match) {
System.out.println(String.format("The correct answer was %d; you're only allowed %d guesses. Better luck next time!", value, maxGuesses));
}
}
}

How do i get the array to hold the number of games played and run it for each player?

I have an assignment where two users are to enter their names, then get the number of games they played, then get the scores for each game that they played, put the scores in an array (or two arrays???), then compare the two arrays and scores to see who won each game or if they're tied, then display results. I don't have to sort it or search through it I don't believe.
Ss my teacher says getting the scores has to be a void method, but I'm not sure how I get the values from this void method into arrays for each player so I can compare the score values.
public class Lab9 {
public static void inputScores(int[] array, String name) {
String inputScores = "";
for (int i = 1; i < array.length; i++) {
inputScores = JOptionPane.showInputDialog(name + " enter your score for game " + i);
array[i] = Integer.parseInt(inputScores);
}
}
public static void main(String[] args) {
int numberOfGames = getPositiveIntOrQuit("How many games were played?", "Lab 9 (by Jarvis),");
String name = getStringOrQuit("Player 1-What is your name?", "Lab 9 (by Jarvis");
String name1 = getStringOrQuit(" Player 2 - What is your name?","Lab 9(by Jarvis");
int score = getNonNegativeIntOrQuit(name + " enter your score for game" , "Lab 9 (by Jarvis)");
}
public static String getStringOrQuit(String prompt, String title) {
String userInputString;
userInputString = JOptionPane.showInputDialog(null, prompt, title, JOptionPane.QUESTION_MESSAGE);
// Did user hit Cancel or OK with nothing typed?
if (userInputString == null || userInputString.trim().equals("")) {
JOptionPane.showMessageDialog(null,"No input, so program will terminate now.");
System.exit(0);
}
return userInputString;
} // getStringOrQuit
public static int getPositiveIntOrQuit(String prompt, String title) {
String userInputString;
int userInputInt = 0;
do {
userInputString = JOptionPane.showInputDialog(null, prompt, title, JOptionPane.QUESTION_MESSAGE);
// Did user hit Cancel or OK with nothing typed?
if (userInputString == null || userInputString.trim().equals("")) {
JOptionPane.showMessageDialog(null,"No input, so program will terminate now.");
System.exit(0);
}
else
try {
userInputInt = Integer.parseInt(userInputString); // This line might throw an exception.
// Ok, if conversion in above line worked, check if input is a positive integer.
if (userInputInt < 1)
JOptionPane.showMessageDialog(null,"Bad input value. It must be a positive integer.",
"Input error", JOptionPane.ERROR_MESSAGE);
}
catch (NumberFormatException exc) {
JOptionPane.showMessageDialog(null,"Bad input value. It must be a positive integer.",
"Input error", JOptionPane.ERROR_MESSAGE);
}
} while (userInputInt < 1);
return userInputInt;
} // getPositiveIntOrQuit
public static int getNonNegativeIntOrQuit(String prompt, String title) {
String userInputString;
int userInputInt = -1;
do {
userInputString = JOptionPane.showInputDialog(null, prompt, title, JOptionPane.QUESTION_MESSAGE);
// Did user hit Cancel or OK with nothing typed?
if (userInputString == null || userInputString.trim().equals("")) {
JOptionPane.showMessageDialog(null,"No input, so program will terminate now.");
System.exit(0);
}
try {
userInputInt = Integer.parseInt(userInputString); // This line might throw an exception.
// Ok, if conversion in above line worked, check if input is a positive integer.
if (userInputInt < 0)
JOptionPane.showMessageDialog(null,"Bad input value. It must be a non-negative integer.",
"Input error", JOptionPane.ERROR_MESSAGE);
}
catch (NumberFormatException exc) {
JOptionPane.showMessageDialog(null,"Bad input value. It must be a non-negative integer.",
"Input error", JOptionPane.ERROR_MESSAGE);
}
} while (userInputInt < 0);
return userInputInt;
}
}
Ive written up some code, take it as my solution to your problem.
I would create a hashmap ( or two, one for each player ) and map the game number to the score. You can then use this hashmap to compare scores quite easily
Example ( This is rough code i did it quickly ):
public class random {
private static Scanner keyboard = new Scanner(System.in);
private static HashMap<Integer, String> gameMap = new HashMap<Integer, String>();
public static void mapScores(){
System.out.println("How many games have been played?");
int numOfGames = keyboard.nextInt();
String score = "";
for(int i = 0; i < numOfGames; i++){
System.out.println("Please enter the score for game: " + i);
score = keyboard.nextLine();
gameMap.put(i, score);
}
}
}
EDIT:
Okay, then i would suggest declaring your integer arrays statically. Makes for easy access. You're saying an array that holds the players scores and the players name, which is why id suggest a hashmap because and array cannot store the name. I'll demonstrate,:
public class random {
private static Scanner keyboard = new Scanner(System.in);
private static HashMap<String, int[]> gameMap = new HashMap<String, int[]>();
private static int[] hello = new int[10];
public static void mapScores(int[] array, String name){
String inputScores = "";
for (int i = 1; i < array.length; i++) {
inputScores = JOptionPane.showInputDialog(name + " enter your score for game " + i);
array[i] = Integer.parseInt(inputScores);
}
}
public static void main(String[] args) {
random.gameMap.put("name", new int[10]);
random.mapScores(gameMap.get("name"), "name");
for(int i = 0; i < gameMap.get("name").length; i++){
System.out.println(gameMap.get("name")[i]);
}
}
}
Obviously i haven't taken the time to correctly create the objects but hopefully you can see the idea despite the bad code. Hope this helps!

Issues with Guessing game java class~

This is a code to play a guessing game that i made, but the problem is that there were several issues which me as a beginner in java are not good at and need some guidance with. along the code there were some errors that i highlighted with an arrow on the side.
import java.util.*;
public class GuessingGame
{
private static Player house;
private static Player player;
private static int wins;
private static int loses;
private String name;
int card1,card2;
private int value;
public void Player(String name){
this.name=name;
card1 = (Integer) null;
card2 = (Integer) null;
}
public void Card(int value){
this.value = value;
}
public int getValue(){
return value;
}
public void acceptDeal(Card card1, Card card2){
Random r = new Random();
int value = r.nextInt(13) + 1;
card1 = new Card(value); <<<<<<<<======= Error 1
value = r.nextInt(13) + 1;
card2 = new Card(value); <<<<<<<<======= Error 2
}
public static void init()
{
house = new Player("House"); <<<<<<<<======= Error 3
player = new Player("Player"); <<<<<<<<======= Error 4
wins = 0;
loses = 0;
}
public static void playGame()
{
Scanner scan = new Scanner(System.in);
char option, playAgain;
int houseHandStrength, playerHandStrength;
System.out.println("Welcome to our card guess 1.0 game!");
System.out.println();
do {
// Deal cards to the house and player.
house.acceptDeal(new Card(houseHandStrength), new Card(houseHandStrength)); <<<<<=== Error 5
player.acceptDeal(new Card(playerHandStrength), new Card(playerHandStrength)); <<<<<=== Error 6
System.out.println(house);
// Determine whether the player wants to play this hand.
do {
System.out.print("Deal cards? (Y/N) ");
option = Character.toLowerCase(scan.next().charAt(0));
}
while (option != 'n' && option != 'y');
if (option == 'y')
{
System.out.println(player);
// Display hand strength of both players.
houseHandStrength = house.getHandStrength(); <<<<<=== Error 7
playerHandStrength = player.getHandStrength(); <<<<<=== Error 8
System.out.println("The dealer's hand strength is: " + houseHandStrength);
System.out.println("Your hand strength is: " + playerHandStrength);
System.out.println();
// If the player has a stronger hand.
if (player.getHandStrength() > house.getHandStrength())
{
System.out.println("** You won the hand! **");
wins++;
}
else {
System.out.println("The house wins this round!");
loses++;
}
}
// Display the win/lose statistics.
System.out.println("Current wins: " + wins);
System.out.println("Current loses: " + loses);
// Prompt whether the user wants to play again.
do {
System.out.print("Would you like to play again? (Y/N) ");
playAgain = Character.toLowerCase(scan.next().charAt(0));
}
while (playAgain != 'n' && playAgain != 'y');
System.out.println();
System.out.println("*******************************************************");
}
while (playAgain == 'y');
System.out.println();
System.out.println("Thank you for playing!");
}
public static void main(String[] args)
{
init();
playGame();
}
}
First of all welcome to StackOverflow. It's nice to see that you have found and used the homework tag. Keep in mind that for people to be able to help you, you need to give more information. What do you mean by error, what happens when you run the code etc
regarding the errors you get, it appears as you haven't really defined classes Card and Player, what you have there in your code are two methods GuessingGame.Card() and GuessingGame.Player() in your GuessingGame class. Change them to inner (or outer) classes and it should be fine ;)
Maybe you need to import your other classes at the top?
The problems seem to be only in your own classes, what do the program output say about the errors?
public void Player(String name)...
and
public void Card(int value)...
should be classes right? Declare them as classes in another file and include them to the main file.
In your previous Question card1 and card2 were of type Card. That was right, now you have changed this and now it is wrong.
You seemed to have bunched up your code. You've combined the Player, Card and Game classes. I don't have a Java compiler handy, but what you're looking to do is to break out the three models.
Error 1-6 were a result of trying to instantiate new objects when the class doesn't even exist. Error 7-8 were a result of trying to call a methods on the same.
import java.util.*;
class Player {
int card1, card2;
private String name;
public void Player(String name){
this.name=name;
card1 = (Integer) null;
card2 = (Integer) null;
}
public void acceptDeal(Card card1, Card card2){
Random r = new Random();
int value = r.nextInt(13) + 1;
card1 = new Card(value); <<<<<<<<======= Error 1
value = r.nextInt(13) + 1;
card2 = new Card(value); <<<<<<<<======= Error 2
}
}
class Card {
private int value;
public void Card(int value){
this.value = value;
}
public int getValue(){
return value;
}
}
public class GuessingGame
{
private static Player house;
private static Player player;
private static int wins;
private static int loses;
public static void init()
{
house = new Player("House"); <<<<<<<<======= Error 3
player = new Player("Player"); <<<<<<<<======= Error 4
wins = 0;
loses = 0;
}
public static void playGame()
{
Scanner scan = new Scanner(System.in);
char option, playAgain;
int houseHandStrength, playerHandStrength;
System.out.println("Welcome to our card guess 1.0 game!");
System.out.println();
do {
// Deal cards to the house and player.
house.acceptDeal(new Card(houseHandStrength), new Card(houseHandStrength)); <<<<<=== Error 5
player.acceptDeal(new Card(playerHandStrength), new Card(playerHandStrength)); <<<<<=== Error 6
System.out.println(house);
// Determine whether the player wants to play this hand.
do {
System.out.print("Deal cards? (Y/N) ");
option = Character.toLowerCase(scan.next().charAt(0));
}
while (option != 'n' && option != 'y');
if (option == 'y')
{
System.out.println(player);
// Display hand strength of both players.
houseHandStrength = house.getHandStrength(); <<<<<=== Error 7
playerHandStrength = player.getHandStrength(); <<<<<=== Error 8
System.out.println("The dealer's hand strength is: " + houseHandStrength);
System.out.println("Your hand strength is: " + playerHandStrength);
System.out.println();
// If the player has a stronger hand.
if (player.getHandStrength() > house.getHandStrength())
{
System.out.println("** You won the hand! **");
wins++;
}
else {
System.out.println("The house wins this round!");
loses++;
}
}
// Display the win/lose statistics.
System.out.println("Current wins: " + wins);
System.out.println("Current loses: " + loses);
// Prompt whether the user wants to play again.
do {
System.out.print("Would you like to play again? (Y/N) ");
playAgain = Character.toLowerCase(scan.next().charAt(0));
}
while (playAgain != 'n' && playAgain != 'y');
System.out.println();
System.out.println("*******************************************************");
}
while (playAgain == 'y');
System.out.println();
System.out.println("Thank you for playing!");
}
public static void main(String[] args)
{
init();
playGame();
}
}

Categories

Resources