?
Solved

Random Number

Posted on 2009-02-24
4
Medium Priority
?
375 Views
Last Modified: 2013-11-23
Dear all,

Below is my code:


The issue i am having is with the Random Number generator.

All it seems to generate is 0 and that's the only way to win.

Could someone help me out in getting the code to generator other numbers that 0 but between 0 - 100.

Regards

Lyon JT
//
//  GuessAndWin.java
//  GuessAndWin
//
//  Created by Lyon Jay Till on 18/02/2009.
//  Copyright (c) 2009 __MyCompanyName__. All rights reserved.
//
//  A simple signed Java applet
//
 
import java.awt.*;
import java.awt.BorderLayout.*;
import java.applet.*;
import javax.swing.*;
import java.util.*;
import java.awt.Component.*;
import java.awt.event.*;
import java.util.Random.*;
import java.lang.Math;
 
public class GuessAndWin extends JApplet implements ActionListener
	{
		
		/* Visible elements */
		
		/* Text boxs */
		JTextField theGuess = new JTextField(3); 
		JTextField thePlayer = new JTextField(20);
		
		/* Labels */
		JLabel bankRoll = new JLabel("100.00 Pounds");
		
		/* Buttons */
		JButton newPlayer = new JButton("New Player");
		JButton newNumber = new JButton("New Number");
		
		/* Textarea */
		JTextArea theOutput = new JTextArea();
		//theOutput.setVisible(true);
		
		/* None Visible Elements */
		
		/* Player name */
		String playerName = new String();
		
		/* Computers number */
		int theNumber;
		
		/* Number of tries & games */
		
		int numTries = 0;
		int numGames = 0;
		
		/* Amount of current players bankroll */
		
		double amtRemaining = 100.0;	
		
		/* Random number for the computer */
		
		Random randomizer = new Random();
		
		
		public void init()
		{
			setLayout(new BorderLayout(30,30)); // Creates a method & object of BorderLayout(Built in class)
			
			/* Layout */
			
			/* Making object of panel */
			Panel p1 = new Panel();
			Panel p2 = new Panel();
			
			/* Labels */
			JLabel mgLbl = new JLabel("Make your guess: ");
			
			
			/* Adding */
			
			/* p1 */
			
			/* Labels */
			p1.add(mgLbl);
			
			/* Other */
			p1.add(theGuess);
			p1.add(bankRoll);
			
			/* p2 */
			/* Other */
			p2.add(newPlayer);
			p2.add(thePlayer);
			p2.add(newNumber);
			
			/* p1 */
			/* Add the top bar */
			add("North", p1);
			/* p2 */
			/* Add the bottom bar */
			add("South", p2);
			
			/* Dummy JLabels & Adding */
			/* Left side */
			add("West", new JLabel("		"));
			/* Right side */
			add("East", new JLabel("		"));
			
			/* Textarea & Adding */
			add("Center", theOutput);
			theOutput.revalidate();
			theOutput.repaint();
			
			/* End of Layout */
			
			/* Methods */
			
			newPlayer();
		}
		
		public GuessAndWin() // Action listener
		{
			/* Adding actionListener */
			
			/* Buttons */
			newPlayer.addActionListener(this);
			newNumber.addActionListener(this);
			/* Textbox */
			thePlayer.addActionListener(this);
			theGuess.addActionListener(this);
		}
		
		public void actionPerformed(ActionEvent e)
		{
			
			if(e.getSource() == newPlayer)
			{
				newPlayer();
			}
			else if(e.getSource() == thePlayer)
			{
				addPlayer();
			}
			else if(e.getSource() == newNumber)
			{
				newGame();
				theGuess.setEnabled(true);
				theGuess.requestFocus(true);
				theGuess.setBackground(Color.white);
				thePlayer.setEnabled(false);
				
			}
			else if(e.getSource() == theGuess)
			{
				newGuess();
			}
			
		}
		
		
		public void newPlayer()
		{
			/* Clear the the output textfield */
			theOutput.setText("");
			theOutput.append("Enter your name in the text box below and press Return");
			
			/* Disable the ability to type text in this area */
			theOutput.setEnabled(false); //The text area in the centre has now been disabled
			theGuess.setEnabled(false);
			newPlayer.setEnabled(false);
			newNumber.setEnabled(false);
			
			/* Set the colour of the text box */
			theGuess.setBackground(Color.white);
			
			/* Enable the player textfield */
			thePlayer.setEnabled(true);
			thePlayer.setText("");
			
			/* Set the keyboard focus & background color on thePlayer */
			thePlayer.requestFocus(true);
			thePlayer.setBackground(Color.white);
			
		}
		
		public void addPlayer()
		{
			playerName = thePlayer.getText();
			equals();
		}
		
		public void newGame()
		{
			numGames = numGames + 1;
			
			int randomInt = randomizer.nextInt(100);
			randomInt = theNumber;
			
			displayInstructions();
			
			amtRemaining = amtRemaining - 1.00;
			
			updateScore();
		}
		
		public void newGuess()
		{
			theOutput.setText("");
			
			String curStr = theGuess.getText();
			int curGuess = Integer.parseInt(curStr);
			
			numTries = numTries + 1;
			
			if(curGuess == theNumber)
			{
				gameWon();
			}
			else
			{
				theGuess.requestFocus(true);
				theGuess.setBackground(Color.white);
				
				switch (numTries)
				{ 
					case 1:
						theOutput.setText("");
						theOutput.append("Try either a higher or lower number");
						theOutput.append("\nCurrent go: " + numTries + "\n" + "Current guess: " + curGuess);
						theOutput.append("\nTry again " + playerName);
						theOutput.append("\n8 attempts remaining!");
						break; 
						
					case 2:
						theOutput.setText("");
						theOutput.append("Try either a higher or lower number");
						theOutput.append("\nCurrent go: " + numTries + "\n" + "Current guess: " + curGuess);
						theOutput.append("\nTry again " + playerName);
						theOutput.append("\n7 attempts remaining!");
						break;
						
					case 3: 
						theOutput.setText("");
						theOutput.append("Try either a higher or lower number");
						theOutput.append("\nCurrent go: " + numTries + "\n" + "Current guess: " + curGuess);
						theOutput.append("\nTry again " + playerName);
						theOutput.append("\n6 attempts remaining!");
						break;
						
					case 4:
						theOutput.setText("");
						theOutput.append("Try either a higher or lower number");
						theOutput.append("\nCurrent go: " + numTries + "\n" + "Current guess: " + curGuess);
						theOutput.append("\nTry again " + playerName);
						theOutput.append("\n5 attempts remaining!");
						break;
						
					case 5: 
						theOutput.setText("");
						theOutput.append("Try either a higher or lower number");
						theOutput.append("\nCurrent go: " + numTries + "\n" + "Current guess: " + curGuess);
						theOutput.append("\nTry again " + playerName);
						theOutput.append("\n4 attempts remaining!");
						break;
						
					case 6:
						theOutput.setText("");
						theOutput.append("Try either a higher or lower number");
						theOutput.append("\nCurrent go: " + numTries + "\n" + "Current guess: " + curGuess);
						theOutput.append("\nTry again " + playerName);
						theOutput.append("\n3 attempts remaining!");
						break;
						
					case 7:
						theOutput.setText("");
						theOutput.append("Try either a higher or lower number");
						theOutput.append("\nCurrent go: " + numTries + "\n" + "Current guess: " + curGuess);
						theOutput.append("\nTry again " + playerName);
						theOutput.append("\n2 attempts remaining!");
						break;
						
					case 8:
						theOutput.setText("");
						theOutput.append("Try either a higher or lower number");
						theOutput.append("\nCurrent go: " + numTries + "\n" + "Current guess: " + curGuess);
						theOutput.append("\nTry again " + playerName);
						theOutput.append("\n1 attempt remaining!");
						break;
						
					default:
						theOutput.setText("");
						theOutput.append("********** Loser **********");
						theOutput.append("\nSorry "+ playerName + " you are not as good as you think you are!");
						theGuess.setEnabled(false);
						break;
						
				}
				
			}
		}
		
		
		public void equals()
		{
			if(playerName == "")
			{
				theOutput.append("You must insert a name before you can play");
			}
			else
			{
				amtRemaining = 100.0;
				numGames = 0;
				
				/* Calling methods */
				newGame();
				updateScore();
				
				thePlayer.setEnabled(false);
				thePlayer.setBackground(Color.white);
				
				newPlayer.setEnabled(true);
				newNumber.setEnabled(true);
				theGuess.setEnabled(true);
				
				theGuess.requestFocus(true);
				theGuess.setBackground(Color.white);
				
			}
		}
		
		public void updateScore()
		{
			thePlayer.setText(playerName);
			
			bankRoll.setText(amtRemaining + " Pounds");
			
			
			
		}
		
		public void displayInstructions()
		{
			theOutput.setText("");
			theOutput.append("Instructions: \nEnter a randon number");
			
		}
		
		
		public void gameWon()
		{
			theOutput.setText("");
			
			double curWinning;
			
			switch (numTries)
			{ 
				case 1:
					theOutput.append("********** WINNER **********");
					curWinning = + 2.00;
					theOutput.append("\n" + "You have won: 2.00 Pounds" );
					amtRemaining = curWinning + amtRemaining;
					break; 
					
				case 2:
					theOutput.append("********** WINNER **********");
					curWinning = + 1.75;
					theOutput.append("\n" + "You have won: 1.75 Pounds" );
					amtRemaining = curWinning + amtRemaining;
					break;
					
				case 3: 
					theOutput.append("********** WINNER **********");
					curWinning = + 1.50;
					theOutput.append("\n" + "You have won: 1.50 Pounds" );
					amtRemaining = curWinning + amtRemaining;
					break;
					
				case 4:
					theOutput.append("********** WINNER **********");
					curWinning = + 1.25;
					theOutput.append("\n" + "You have won: 1.25 Pounds" );
					amtRemaining = curWinning + amtRemaining;
					break;
					
				case 5: 
					theOutput.append("********** WINNER **********");
					curWinning = + 1.00;
					theOutput.append("\n" + "You have won: 1.00 Pounds" );
					amtRemaining = curWinning + amtRemaining;
					break;
					
				case 6:
					theOutput.append("********** WINNER **********");
					curWinning = + 0.75;
					theOutput.append("\n" + "You have won: 0.75 Pounds" );
					amtRemaining = curWinning + amtRemaining;
					break;
					
				case 7:
					theOutput.append("********** WINNER **********");
					curWinning = + 0.50;
					theOutput.append("\n" + "You have won: 0.50 Pounds" );
					amtRemaining = curWinning + amtRemaining;
					break;
					
				case 8:
					theOutput.append("********** WINNER **********");
					curWinning = + 0.25;
					theOutput.append("\n" + "You have won: 0.25 Pounds" );
					amtRemaining = curWinning + amtRemaining;
					break;
					
				default:
					theOutput.append("********** LOSER **********");
					curWinning = + 0.00;
					theOutput.append("\n" + "Unlucky you have not won!" );
					amtRemaining = curWinning + amtRemaining;
					break;
					
			}
			
			playerName = thePlayer.getText();
			theOutput.append("\n" + "Current Player: " + playerName + " " + "\n" +"Amount Won: " + amtRemaining + " Pounds");
			
			theOutput.append("\nPress New Number to continue");
			
			updateScore();
			
			theGuess.setBackground(Color.white);
			theGuess.setEnabled(false);
			newNumber.requestFocus(true);
			
		}
		
	}

Open in new window

0
Comment
Question by:LyonJay
  • 2
4 Comments
 
LVL 7

Expert Comment

by:humanonomics
ID: 23720720
Well I don't see "randomizer.nextInt();" used anywhere in your code.
0
 
LVL 92

Assisted Solution

by:objects
objects earned 400 total points
ID: 23720722
>                   randomInt = theNumber;

get rid of that line (195)

0
 
LVL 9

Accepted Solution

by:
arioh earned 800 total points
ID: 23720743
change  "randomInt = theNumber;" to  "theNumber = randomInt;" in the "newGame()"
0
 
LVL 7

Assisted Solution

by:humanonomics
humanonomics earned 800 total points
ID: 23720744
Sorry, missed out on line number 194, the problem is

You are generating the random number using 194:
int randomInt = randomizer.nextInt(100);
                  
but what ever value is stored in randomInt, gets replaced by the value in  theNumber(which is 0 by default); on line number 195.
                  
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month12 days, 19 hours left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question