Hi, for my Java class I have built a number guessing game. I need to separate my code into two classes and incorporate a try-catch statement. Any help would be appreciated! Here is my code.
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import java.awt.Color;
import java.awt.color.*;
import java.util.Random;
public class game {
public static void main (String [] args) {
UIManager.put("OptionPane.backround", Color.white);
UIManager.put("Pandelbackround", Color.white);
UIManager.put("Button.background", Color.white);
Random nextRandom = new Random();
int randomNum = nextRandom.nextInt(1000);
boolean playerCorrect = false;
String keyboardInput;
int playerGuess = 0;
int numPlayerGuesses = 0;
boolean running = false;
while( !playerCorrect) {
numPlayerGuesses = numPlayerGuesses + 1;
JOptionPane JOptionPane1 = new JOptionPane();
long startTime = System.nanoTime();
keyboardInput = JOptionPane1.showInputDialog("Guess my number between 0 and 1000!");
UIManager.put("OptionPane.background", Color.white);
UIManager.put("Panel.background", Color.white);
playerGuess = Integer.parseInt( keyboardInput);
}
if( playerGuess > randomNum) {
JOptionPane JOptionPane2 = new JOptionPane();
UIManager.put("OptionPane.background", Color.red);
UIManager.put("Panel.background", Color.red);
JOptionPane2.showMessageDialog(null, "You guessed too high, try again!");
}
else if ( playerGuess < randomNum) {
JOptionPane JOptionPane3 = new JOptionPane();
UIManager.put("OptionPane.background", Color.blue);
UIManager.put("Panel.background", Color.blue);
JOptionPane3.showMessageDialog(null, "You guessed too low, try again!");
}
else {
JOptionPane JOptionPane4 = new JOptionPane();
long startTime = 0;
long estimatedTime = System.nanoTime() - startTime;
UIManager.put("OptionPane.background", Color.green);
UIManager.put("Panel.background", Color.green);
JOptionPane.showMessageDialog(null, "Congrats! you guessed THE NUMBER!!! it only took you " + numPlayerGuesses + " tries in " + estimatedTime + " seconds!");
playerCorrect = true;
}
System.exit(0);
}
}
JAVA PROGRAM
GameLogic class
package test.refactor;
//game logic class
public class GameLogic {
public GameLogic(){
}
//will determine result based on plater guess
public int play(int playerGuess,int
randomNumber){
int result;
if(playerGuess <
randomNumber){
result =
-1;
}else if(playerGuess >
randomNumber){
result =
1;
}else{ //equal
result =
0;
}
return result;
}
}
Updated Game class (refactored)
package test.refactor;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import java.awt.Color;
import java.awt.HeadlessException;
import java.awt.color.*;
import java.util.Random;
public class Game {
public static void main (String [] args) {
UIManager.put("OptionPane.backround",
Color.white);
UIManager.put("Pandelbackround",
Color.white);
UIManager.put("Button.background",
Color.white);
Random nextRandom = new
Random();
int randomNum =
nextRandom.nextInt(1000);
boolean playerCorrect =
false;
String keyboardInput;
int playerGuess = 0;
int numPlayerGuesses = 0;
boolean running =
false;//unused
GameLogic gl = new
GameLogic();
long startTime =
System.nanoTime();
String message = null;
while( !playerCorrect) {//loop
until player has guessed correctly
numPlayerGuesses = numPlayerGuesses + 1;
updateUIManager(1000);
keyboardInput =
JOptionPane.showInputDialog("Guess my number between 0 and
1000!");
try{
playerGuess =
Integer.parseInt( keyboardInput);
}catch(NumberFormatException nfe){ //handled
exception here. If not proper inpput , it will display error and go
to top of loop
message = "Invalid
input";
JOptionPane.showMessageDialog(null, message);
continue;
}
int result = gl.play(playerGuess, randomNum);
//call gameLogic and its play method
updateUIManager(result); //update color based on
game result
message = getMessage(result, startTime,
numPlayerGuesses);//update message based on gameresult
JOptionPane.showMessageDialog(null, message);//
show message
if(result == 0){
playerCorrect = true;//check
if player is correct in guessing
}
}
System.exit(0);
}
/**
* update UI manager based on value
* @param gameResult
*/
private static void updateUIManager(int value){
Color color = null;
if(value == 1000){
//at start
color =
Color.white;
}else if(value > 0){
color=
Color.red;
}else if(value < 0){
color =
Color.blue;
}else{ //equals
color =
Color.green;
}
UIManager.put("OptionPane.background", color);
UIManager.put("Panel.background",
color);
}
/**
* generate message based on game result
* @param gameResult
* @param startTime
* @param numOfGuess
* @return
*/
private static String getMessage(int gameResult,long
startTime,int numOfGuess){
String message = null;
if(gameResult > 0){
message = "You
guessed too high, try again!";
}else if(gameResult < 0){
message = "You
guessed too low, try again!";
}else{ //equals
long
estimatedTime = System.nanoTime() - startTime;
message =
"Congrats! you guessed THE NUMBER!!! it only took you " +
numOfGuess + " tries in " + estimatedTime + " seconds!";
}
return message;
}
}
Output
Hi, for my Java class I have built a number guessing game. I need to separate...
Practically dying here with this, need help ASAP, just need to do hide(), hideBoth(), and matchFound(), then implement them, so far in my version i tentatively made them, but I don't know ho to implement them so i posted the original code before i made my version of the three methods listed above. Need help fast, this is ridiculous. Implement just one requirement at a time. For example, try implementing the case where after the user clicks 2 squares, both...
Java Program The goal of this assignment is to develop an event-based game. You are going to develop a Pong Game. The game board is a rectangle with a ball bouncing around. On the right wall, you will have a rectangular paddle (1/3 of the wall height). The paddle moves with up and down arrows. Next to the game board, there will be a score board. It will show the count of goals, and count of hits to the paddle....
Modify Java Code below: - Remove Button Try the Number -Instead of Try the Number button just hit enter on keyboard to try number -Remove Button Quit import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; // Main Class public class GuessGame extends JFrame { // Declare class variables private static final long serialVersionUID = 1L; public static Object prompt1; private JTextField userInput; private JLabel comment = new JLabel(" "); private JLabel comment2 = new JLabel(" "); private int...
In Java Swing, in my ButtonHandler class, I need to have 2 different showDialog messages show when my acceptbutton1 is pressed. One message is if the mouse HAS been dragged. One is if the mouse HAS NOT been dragged. /// I tried to make the Points class or the Mouse Dragged function return a boolean of true, so that I could construct an IF/THEN statement for the showDialog messages, but my boolean value was never accepted by ButtonHandler. *************************ButtonHandler class************************************...
JAVA JAR HELP...ASAP I have the code that i need for my game Connect 4, but i need to create a jar file . the instructions are below. It has to pass some parameters. I am really confused on doing so.l I dont know what to do next. Can someone help me and give detailed descritiopm opn how you ran the jar file in CMD import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Connect4 { ...
This is java. Goal is to create a pacman type game. I have most of the code done just need to add in ghosts score dots etc. Attached is the assignment, and after the assignment is my current code. import java.awt.Color; import java.awt.Dimension; import java.awt.Image; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.io.File; import java.io.FileNotFoundException; import java.util.Arrays; import java.util.Scanner; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; public class Maze extends JFrame implements KeyListener { private static final String[] FILE = {...
FIX CODE-- import random number=random.randint ('1', 'another number') print("Hello, CIS 101") print("Let's play a guessing Game!called guess my number.") print("The rules are: ") print("I think of a number and you'll have to guess it.") guess = random.randint(1, 5) print("You will have " + str(guess) + " guesses.") YourNumber = ??? unusedguesses=int(guess) while unusedguesses < guess: if int(YourNumber) == number: print("YAY, You have guessed my number") else: unusedguesses=int(guess)-1 if unusedguesses == 0: break else: print("Try again!") print("The number was ", str(number))
Introduction to Java Programming Question: I’m doing a java game which user clicks on two different blocks and see the number and remember their locations and match the same number. I’m basically looking for codes that could make my memory match game more difficult or more interesting.(a timer, difficulty level, color, etc.) GUI must be included in the code. Here is what I got so far: package Card; import javax.swing.JButton; public class Card extends JButton{ private int id; private boolean...
Introduction to Java Programming Question: I’m doing a java game which user clicks on two different blocks and see the number and remember their locations and match the same number. I’m basically looking for codes that could make my memory match game more difficult or more interesting.(a timer, difficulty level, color, etc.) GUI must be included in the code. Here is what I got so far: package Card; import javax.swing.JButton; public class Card extends JButton{ private int id; ...
Need help writing Java code for this question. CircleFrame class is provided below. what happen after u add the code and follow the requirement? It would be nice to be able to directly tell the model to go into wait mode in the same way that the model's notify method is called. (i.e. notify is called directly on the model, whereas wait is called indirectly through the suspended attribute.) Try altering the the actionPerformed method of the SliderListener innner class...