7 Replies - 7314 Views - Last Post: 19 March 2009 - 12:08 AM Rate Topic: -----

#1 Summerrset  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 20-February 09

Hi-Low game in Java

Post icon  Posted 18 March 2009 - 10:11 PM

Okay so I'm seriously about to start banging my head against a wall (programming is great like that, huh?).
I have the following assignment to complete:
----------------------------------------------------------------------------------------------------------
Design and implement a GUI that allows you to play the Hi-Lo guessing game against the computer.
The GUI should have two JLabel objects and a JTextField.

The game begins when the computer chooses a random number between 1 and 100 (inclusive)
and asks you to guess the number by using a JLabel to display "Enter your guess #n here ==>".
Next to the JLabel is a JTextField to accept your number (exactly like the Fahrenheit program
on page 198-9). You enter a guess and hit the Enter/Return key. Then your program gets the
number String and converts it to an int (again, like the Fahrenheit program). If you guess
the number correctly, both labels display "You Win!". Otherwise one label displays "Low = p"
where p is the lowest value in the range of possible guesses, and the other displays "High = q"
where q is the highest value in the range of possible guesses. So when Low is 1, High is 100,
and your guess is 75 (which is too high), the labels will display "Low is 1" and "High is 74".
If the next guess is 14 (which is too low) then the labels will display "Low is 15" and "High
is 74". Thus, the user knows the range of possible values for the next guess.

The game ends when either
(1) the user enters the random number the computer picked, at which point the two labels announce
"You Win!", the game stops, and the JTextField will still display the winning number.
or
(2) the user makes 7 incorrect guesses, at which point one label states "Sorry, you lose"
and the other label states "The number was xxx", where xxx is the random number the computer
selected at the outset.

The user can play another time by closing the GUI and running the program again.

----------------------------------------------------------------------------------------------------------
I really want to learn Java but I'm beginning to realize that its probably not meant to be, because after three days of trying I still can't seem to wrap my mind around how to get this thing to work. So I decided to start small with one round of a high-lo game with computer. Everything compiled and it worked great so I thought I'd build on that and try opening it in a JPanel. Everything went downhill from there.

Here's what I have so far:
HiLoGame.java-> (Which won't compile.)
import javax.swing.JFrame;

public class HiLoGame
{
	public static void main (String[] args)
	{
		JFrame frame = new JFrame ("HILO Guessing Game");
		frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
	
		frame.getContentPane().add(new HiLoGamePanel());
		frame.pack();
		frame.setVisible(true);
	}
}



and HiLoGamePanel.java-> (This part compiles just fine.)
import java.util.*;
import javax.swing.*;

public class HiLoGamePanel extends JPanel
{
	public HiLoGamePanel()
	{
		int answer, guess;
		int lowVal, highVal;
		int lowGuess, highGuess;
		int guessCount;

		lowVal = 1;
		highVal = 100;
		
		Scanner scan = new Scanner (System.in);
		Random generator = new Random();
		
		answer = generator.nextInt(100)+1;
		
		System.out.println ("Low: " + lowVal);
		System.out.println ("High: " + highVal);
		System.out.print ("Enter your guess number here ==> ");
		guess = scan.nextInt();
		
		if(guess == answer)
		System.out.println ("You Win!");
		else
		{		
		System.out.print ("You Lose!");
		}		
	}
}



This is obviously just a start, and far away from what it needs to be for the assignment, but I'm honestly stuck and starting to pull my hair out over it. I'm really hoping someone can 1) let me know why what I have works until I try to get it to open in a JPanel, and 2) steer me in the right direction with the rest of it. I'd really, really appreciate any help you could offer!

Is This A Good Question/Topic? 0
  • +

Replies To: Hi-Low game in Java

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Hi-Low game in Java

Posted 18 March 2009 - 10:41 PM

all the classes compile, what's the problem??
Was This Post Helpful? 0
  • +
  • -

#3 Summerrset  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 20-February 09

Re: Hi-Low game in Java

Posted 18 March 2009 - 10:44 PM

View Postmostyfriedman, on 18 Mar, 2009 - 09:41 PM, said:

all the classes compile, what's the problem??


It will run but won't extend the JPanel window like its suppose to and I'm not sure why it won't.
Was This Post Helpful? 0
  • +
  • -

#4 virgul  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 269
  • Joined: 18-March 09

Re: Hi-Low game in Java

Posted 18 March 2009 - 10:58 PM

I have a couple questions about your program.

1: why are u not using a JFrame
2: do you want everything in the one frame


here is my code from my program im writing for my final project, it might be helpful


change Container to w.e urs is called

I personally always set the layout to null (this lets you place things like buttons). I dislike their flow layout/other built-in ones

import java.awt.GridLayout;

import javax.swing.JFrame;

public class GUI {
	public static void main(String[] args)
	{
		Container frame = new Container("Frame");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLayout(null);
		frame.setSize(800, 600);
		frame.setResizable(false);
		frame.setVisible(true);
		
	}
}


This post has been edited by virgul: 18 March 2009 - 11:00 PM

Was This Post Helpful? 0
  • +
  • -

#5 Summerrset  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 20-February 09

Re: Hi-Low game in Java

Posted 18 March 2009 - 11:11 PM

View Postvirgul, on 18 Mar, 2009 - 09:58 PM, said:

I have a couple questions about your program.

1: why are u not using a JFrame
2: do you want everything in the one frame


First of all, thank you for your response. :)

Second, I really am very new at Java so you'll have to bare with me for sounding like an idiot but... I thought I was using JFrame...? Am I not? I do want everything in JFrame, but in separate panels. I think anyway. I'd like to set it up so that the low count is on the left, the place the user inputs their guess is in the middle, and the highest guess is on the right. How could I make that work?
Was This Post Helpful? 0
  • +
  • -

#6 virgul  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 269
  • Joined: 18-March 09

Re: Hi-Low game in Java

Posted 18 March 2009 - 11:55 PM

Ok, wow didnt see u had that there sorry,

well here is a bad version of what you want i think


import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;

public class HiLoGameGUI extends JPanel
{
   private int WIDTH = 300;
   private int HEIGHT = 75;

   private JFrame frame;
   private JPanel panel;
   private JLabel inputLabel;
   private JLabel highLable;
   private JLabel lowLable;
   private JTextField UserGuess;
   
   String answer, guess;
   public HiLoGameGUI()
   {
	  frame = new JFrame ("HiLo Guessing Game");
	  frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

	  
	  int lowVal, highVal;
	  int lowGuess, highGuess;
	  int guessCount;

	  lowVal = 1;
	  highVal = 100;
		
	  Scanner scan = new Scanner (System.in);
	  Random generator = new Random();
		
	  answer = generator.nextInt(100)+1+"";
	  
	  inputLabel = new JLabel ("Enter your Guess: ");
	  lowLable = new JLabel ("Low is: "+ lowVal);
	  highLable = new JLabel ("High is: "+ highVal);
	  UserGuess = new JTextField (5);
	  UserGuess.addActionListener (new GuessList());

	  panel = new JPanel();
	  panel.setPreferredSize (new Dimension(WIDTH, HEIGHT));
	  panel.setBackground (Color.yellow);
	  panel.add (lowLable);
	  panel.add (inputLabel);
	  

	  frame.getContentPane().add (panel);
	  panel.add (UserGuess);
	  panel.add (highLable);
   }
   //-----------------------------------------------------------------
   //  Displays the primary application frame.
   //-----------------------------------------------------------------
   public void display()
   {
	  frame.pack();
	  frame.show();
   }

   //*****************************************************************
   //  Represents an action listener for the temperature input field.
   //*****************************************************************
   private class GuessList implements ActionListener
   {
	  //--------------------------------------------------------------
	  //  Performs the conversion when the enter key is pressed in
	  //  the text field.
	  //--------------------------------------------------------------
	  public void actionPerformed (ActionEvent event)
	  {
		 int Guess, celsiusTemp;

		 String text = UserGuess.getText();

		 Guess = Integer.parseInt (text);
		 
	  }
   }
}




public class HiLo
{
   //-----------------------------------------------------------------
   //  Creates and displays the temperature converter GUI.
   //-----------------------------------------------------------------
   public static void main (String[] args)
   {
	  HiLoGameGUI HILO = new HiLoGameGUI();
	  HILO.display();
   }
}




Now what you need to do is make it do something with your guess
it sees you hit enter, just doesn't do anything.

This can easily be tested by using a system.out in the Actiolist.
Was This Post Helpful? 1
  • +
  • -

#7 Summerrset  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 20-February 09

Re: Hi-Low game in Java

Posted 19 March 2009 - 12:04 AM

You're a life safer! Thank you a million!
Was This Post Helpful? 0
  • +
  • -

#8 virgul  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 269
  • Joined: 18-March 09

Re: Hi-Low game in Java

Posted 19 March 2009 - 12:08 AM

no problem, if you need any explanation of the program, or want me to clean it up/finish it. I'd be willing to, just not tonight.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1