//importing required classes
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
//defining class
public class StartGUI extends JFrame implements
ActionListener{
//declaring variable
String str;
JComboBox cb;
JButton btn;
//defining constructor
public StartGUI() {
//initiallizing the
variables
String
st[]={"Easy","Difficult"};
cb=new
JComboBox(st);
btn=new
JButton("Start");
//adding the component
to the gui
setLayout(new
GridLayout(2,1));
add(cb);
add(btn);
//adding listener
btn.addActionListener(this);
}
//defining main function
public static void main(String[] args) {
//creating object
StartGUI gui=new
StartGUI();
//gui size
gui.setSize(300,300);
//making it
visible
gui.setVisible(true);
//adding the exit
function for closing the GUI
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//coding the button action
@Override
public void actionPerformed(ActionEvent e)
{
//getting the difficulty
level
String
level=""+cb.getSelectedItem();
//creating object
GamePage gp=new
GamePage(level);
//gui size
gp.setSize(300,300);
//making it
visible
gp.setVisible(true);
//adding the exit
function for closing the GUI
gp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
----------------------------------------
//importing required classes
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
//defining class
public class GamePage extends JFrame implements
ActionListener{
//declaring variable
JLabel lb;
JTextField tf;
JButton btn,btn1;
JPanel pl;
int secretNumber,count=0;
//constructor definition
public GamePage(String level) {
//getting random
generator to generate random values
Random ran=new
Random();
//comparing the
difficulty level opted by user and calculating the secret
number
if(level.equals("Easy")){
secretNumber=ran.nextInt(10)+1;
}
else{
secretNumber=ran.nextInt(100)+1;
}
//initiallizing
variable
lb=new JLabel();
tf=new
JTextField();
btn=new
JButton("Guess");
btn1=new JButton("Give
UP");
pl=new JPanel();
//adding component to
gui
add(tf,BorderLayout.NORTH);
add(lb);
pl.add(btn);
pl.add(btn1);
add(pl,BorderLayout.SOUTH);
//listener to the
button
btn.addActionListener(this);
btn1.addActionListener(this);
}
//coding button action
@Override
public void actionPerformed(ActionEvent e)
{
String
cmd=e.getActionCommand();
if(cmd.equals("Guess")){
String val=tf.getText();
int num;
try{
count++;
//getting the user input and converting into number format
num=Integer.parseInt(val);
//comparing
the user guess with secret number
if(num<secretNumber){//guess is less
//showing message to the user
lb.setText("Guess "+count+": Your guess is less than the secret
number\n");
}
else if(num>secretNumber){//guess is greater
//showing message to the user
lb.setText("Guess "+count+": Your guess is greater than the secret
number\n");
}
else{//if the guess and the secret number is equal
//showing message to the user
lb.setText("Guess "+count+": Your guess is right\n");
}
if(count==10){
//creating object
SwingGUI sgui=new SwingGUI(secretNumber);
//gui size
sgui.setSize(300,300);
//making it visible
sgui.setVisible(true);
//adding the exit function for closing the GUI
sgui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
catch(NumberFormatException nfe){
//showing message to the user
lb.setText("Guess "+count+": Number is invalid");
}
}
else{
//creating object
SwingGUI sgui=new SwingGUI(secretNumber);
//gui size
sgui.setSize(300,300);
//making it visible
sgui.setVisible(true);
//adding the exit function for closing the GUI
sgui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
}
----------------------------------------------------------
//importing required classes
import javax.swing.JFrame;
import javax.swing.JLabel;
//defining class
public class SwingGUI extends JFrame{
//declaring variable
JLabel lb;
//constructor definition
public SwingGUI(int number) {
//initiallizing
variable
lb=new JLabel("The
number is : "+number);
add(lb);
}
}
---------------------------------------------------------
Output:
The game must be done in java in Netbeans 1. Write a Guessing Game. In the...
Write a JAVA program that plays a number guessing game with the user. A sample run for the game follows. User input is shown in boldface in the sample run. Welcome to the game of Guess It! I will choose a number between 1 and 100. You will try to guess that number. If your guess wrong, I will tell you if you guessed too high or too low. You have 6 tries to get the number. OK, I am...
Write a C++ program to play the numberguessing game with user as follows.The user determines a number between 1~100.The program has 4 tries to guess the number byproviding a range (low, high) on each try.The user tells the program whether the number isbelow, within, or above the range.The program announces a game loss after 4 incorrecttries.
Write a Java program that implements the number guessing game where the computer tries to guess a number in your head. You pick a number between 0 and 100, and the computer generates a guess. You then type 1 if your number is larger than the computer’s guess, -1 if your number is smaller, and 0 if the number is correct. The computer should keep trying to guess the number by adjusting the guess based on your feedback (so if...
on python i need to code a guessing game. After the player has guessed the random number correctly, prompt the user to enter their name. Record the names in a list along with how many tries it took them to reach the unknown number. Record the score for each name in another list. When the game is quit, show who won with the least amount of tries. this is what i have so far: #Guess The Number HW assignment import...
Python Program Python: Number Guessing Game Write a Python function called "Guess.py" to create a number guessing game: 1. Function has one input for the number to Guess and one output of the number of attempts needed to guess the value (assume input number will always be between 1 and 1000). 2. If no number was given when the function was called, use random.randint(a,b) to generate a random number between a=1 and b=1000. You will also need to add an...
Java Listed below is code to play a guessing game. In the game, two players attempt to guess a number. Your task is to extend the program with objects that represent either a human player or a computer player. boolean checkForWin (int guess, int answer) { System.out.println("You guessed" + guess +"."); if (answer == guess) { System.out.println( "You're right! You win!") ; return true; } else if (answer < guess) System.out.println ("Your guess is too high.") ; else System.out.println ("Your...
Write a guessing game where the user has to guess a secret number. After every guess the program tells the user whether their number was too large or too small. At the end the number of tries needed should be printed. It counts only as one try if they input the same number multiple times consecutively. Using PYTHON.
Lab #6 Number Guessing Game – Loops and Nested Loops Requirements: Design, develop, test, and submit a program for a Number Guessing game. The program will select a random number between 0 and 100, allow the user to try up to 10 times to guess the number, and tell the user if each guess is too high, too low, or correct. The actual game portion must be a function...you can use more than one function in your program. The amount...
Specification Write a program that allows the user to play number guessing games. Playing a Guessing Game Use rand() function from the Standard C Library to generate a random number between 1 and 100 (inclusive). Prompt the user to enter a guess. Loop until the user guesses the random number or enters a sentinel value (-1) to give up. Print an error message if the user enters a number that is not between 1 and 100. Print an error message...
Write a Java application program that plays a number guessing game with the user. In the starter code that you are given to help you begin the project, you will find the following lines of code: Random generator = args.length == 0 ? new Random() : new Random(Integer.parseInt(args[0])); int secret = generator.nextInt(100); You must keep these lines of code in your program. If you delete these lines, or if you change thse lines in any way, then your program...