Creating A Guessing Game In Java
Nov 30, 2014
I am creating a Guessing game program in java code. I am having an issue with the guess class and main/tester class running. The instructions for the game are The computer generates a random # and the user must guess that number in 7 or fewer guesses. If the guesses exceed 7, then the game is over and the user is asked if they want to 'play again?'These are things I need to incorporate into my code:
If Statement
A Loop of some kind
At least three imported methods
At least two methods you create
Obtains input from the user
At least two instance variables
At least two local variables
Some form of concatenation
At least two calculations
import java.util.Random;
public class Guess
{
int computersNumber; // A random number picked by the computer.
int usersGuess = 0; // A number entered by user as a guess.
int guessCount = 0; // Number of guesses the user has made.
[code]...
View Replies
ADVERTISEMENT
Feb 10, 2015
I'm in an introduction CSE class and we're working with Java. Basically I need to create a program using scanner, while loops, and pseudorandom numbers. My program is a guessing game and I will post below the code that I already have. Basically I can get the game to work and get it to ask if you want to play another game and it keeps going until you say no. But my issue is getting the statistics of the game down. I need a separate method not in main to produce this at the end of the game once the user decides to stop playing:
Overall results:
total games = int
total guesses = int
guesses/games = double
best game = int
here's my code:
import java.util.*;
public class Guess {
public static void main(String[] args) {
intro(100);
Scanner console = new Scanner(System.in);
game(console, 100);
int numGuesses = 0;
int numGames = 1;
int end = 0;
[code]....
View Replies
View Related
Oct 2, 2014
I tried this program, its guessing game. Where program decide 1 number and user will guess number. I am getting 1 problem. When user want to play game again.the random number remain same. So where i can put reset random in code..? And 1 more question if I want to write driver code for this. What should i transfer to driver code.
import java.util.Random;
import java.util.Scanner;
public class GuessGame {
public static void main(String args[])
{
String choice="y";
[Code] ......
View Replies
View Related
Jan 4, 2015
I am using eclipse for this project. Every time I run this program the choose method will always choose 0. Ignore the Sleep.sleep() method, there is another class in the program that performs it.
import java.util.Scanner;
public class gamestart {
public static int num;
public static String name;
public static String diff;
public static String choice;
[Code] ....
View Replies
View Related
Aug 6, 2014
I have a guessing game that im working on but it wont add my buttons to the frame, why?
// GuessGame.java
// Guess the number
import javax.swing.JFrame;
public class GuessGame extends JFrame {
public static void main(String args[]) {
GuessGame panel = new GuessGame();
panel.setSize(400,350);
[Code] ...
The window pull up and even in the design view it shows everything on there but when i run its just a blank window.
View Replies
View Related
Nov 6, 2014
Here is my instangable class
The application should generate a random number between 1 and 10 and then ask the user to enter one single number between 1 and 10 to guess what the secret number is
public class Guess{
//Variables
private int Num;
private int rNo;
private String message;
//Constructors
public Guess(){
rNo = 0;
message = "";
[Code] .....
Two errors are
Guess.java:43: error: illegal start of expression
public String getMessage(){
uess.java:43: error: ';' expected
public String getMessage(){
View Replies
View Related
Aug 22, 2014
/*
/ GuessGame.java
*/
import java.util.Scanner;
import java.util.Random;
public class GuessGame {
public static void main(String[] args) {
[code]....
tracking the number of games played, and also when running the code, I have to press "1" twice for it to actually load up the game again.
View Replies
View Related
Dec 29, 2013
I have a project about programing a game in java . In this project you are required to design and implement a word guessing game. In the game, user selects one of the available dictionaries (animals, plants, technical, names, etc. ), and the program randomly chooses one word from that dictionary and displays it by scrambling the letters. The user tries to guess the word while a guess counter starts to count down (lets assume it starts from 5).
While counter counts towards zero, the program chooses -randomly- two misplaced letters and puts them into correct positions to reveal the word more and give the user a hint. However, giving a new hint reduces the total value of the question. Lets say if the initial value of the question is 500 when all letters are scrambled, it reduces to 400 after 1st incorrect guess and hint, then after every incorrect guess it reduces more. You can determine the value of the question by the length of the word. The other details of the game is as follows:
1.You must design at least three classes (for example game, scrambledword, and dictionary).
2.Dictionaries are written in text files. If you design a class for dictionary, it must be constructed by a user chosen category ((animals, plants, technical, names, etc). In the constructor, the dictionary text file corresponding to this category must be opened and read; and words in that dictionary must be read into memory (e.g. String array). You must submit your dictionaries with your code. Do not share dictionaries between friends.
3.The game will record your total points and name in a text file where a hall of fame which has different users and their total points in 10 questions. You can design and implement a class for this.
4.Bonus+5%: Put user interface modules textboxes, buttons, etc Or
a.Use class hierarchy
b.Use of thread and timers and reducing the question value by timer instead of guesses.
View Replies
View Related
Jun 3, 2014
What's wrong with my code. It says 'you win' even when I guess an incorrect number. And then the 'win' message keeps repeating. I'm thinking I need a way to generate a new input? And then maybe take the 'win' message out of the loop, but that breaks it too.
View Replies
View Related
Mar 8, 2014
I was told to write a program which generates a random number between 0 to 5 *including 5* and give the user 3 chances to guess this number:
-If the user enters a wrong number within 0 to 5, the user has lost one opportunity.
-If the user enters a number out of range, the program should prompt the user to enter a number within the range
-If the user has 3 unsuccessful attempts, the program will print the number.
I was also instructed to use :
Random randomNumber=new Random();
int i= randomNumber.nextInt(6)
I have attempted it and this is what i have so far.
import java.util.Random;
import java.util.Scanner;
public class Exercise1
{
public static void main(String[] args)
{
Random randomNumber=new Random();
[Code] .....
View Replies
View Related
Apr 8, 2014
Ok, here's what I got.
import java.util.Random;
import java.util.Scanner;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
public class demoGuessGame {
public static void main(String[] args) {
Random any = new Random();
Scanner input = new Scanner (System.in);
try {
int maxNum, maxTries, guess;
boolean win = false;
[Code] .....
I want to make it loop again when asked, "Do you want to play again?"
View Replies
View Related
Sep 6, 2014
I wrote the following code for a lottery game. The problem comes when I run it. If one number of a 3 digit number matches, it's supposed to print "you win 1000" But regardless if a number matches or not, it says "sorry, no matches" ...
import java.util.Scanner;
public class Lottery {
public static void main(String[] args){
//generate lottery number
int lottery = (int)(Math.random() * 1000);
//Prompt the user to enter numbers
Scanner input = new Scanner (System.in);
System.out.print("Enter your numbers (3 digits): ");
int guess = input.nextInt();
[Code] .....
View Replies
View Related
Feb 10, 2015
I've been working on a program where you can interact with the computer and play a guessing game of numbers between 1 and 100. It's mainly finished, I just need to add in my comments and stuff. My one problem about it that I cannot figure out is trying to get the results to give the correct int. At the end of the game, it's supposed to return the user information about how well they did like this:
Overall results:
total games = int
total guesses = int
guesses/game = double
best game = int
best game should give you the least amount of guesses you had during a particular round of the game. Here is what I have so far:
import java.util.*;
public class Guess {
public static final int MAX = 100;
public static void main(String[] args) {
intro(MAX);
Scanner console = new Scanner(System.in);
int tempBestGame = 0;
[Code] .....
View Replies
View Related
Apr 13, 2014
package guess.the.numbers;
import java.awt.*;
import javax.swing.*;
import java.util.Random;
import java.awt.event.*;
ublic class GuessTheNumbers extends JFrame{
private JButton guessBtn;
private JButton restartBtn;
[Code] ....
I am getting a strange error and it almost seems like its not comparing it to the random generated number just the guess that i entered before. Here are my error messages.
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at guess.the.numbers.GuessTheNumbers$ButtonHandler.actionPerformed(GuessTheNumbers.java:119)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
[Code] .....
View Replies
View Related
Mar 8, 2015
Anyway I am creating a game for my A2 coursework, most commonly known as Checkers. I have completed my code and everything works as I had planned except that the CheckerBoard itself as well as the checkerpieces do not appear to be showing.
The section of were my board should be present is just a black space. Although my board does not appear to be displaying, all of the actions I perform on it such as clicking certain section produces the planned response, and although I've checked through my code I cannot work out what I've done wrong.
CheckerBoard content = new CheckerBoard(); // Sets the CheckerBoard values into the content to be used in the next line
application.setContentPane(content); // Container holds the values together, Content pane of the CheckerBoard
application.pack(); // Use preferred size of content to set size of application.
Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
application.setLocation( (screensize.width - application.getWidth())/2,
(screensize.height - application.getHeight())/2 );
[Code] ....
I have noticed is that within the BoardComplete(), if you change the setBackground(Color.BLACK) to WHITE, it will change the colour of the section to white, but the checkboard is still unshown.
View Replies
View Related
May 10, 2014
I'm trying to create a tile based map JPanel but all I get is a white screen. I'm fairly new to the Java Swing and AWT package so I've been watching tutorials on YouTube so learn as much as I can. I don't know where I'm going wrong.
I've got three classes: Window.java which includes the Main method, Panel.java which is the JPanel and Tile.java to draw all the images into an array.
Window.java:
import javax.swing.JFrame;
public class Window extends JFrame {
public Window() {
setTitle("Project");
setSize(500, 400);
setLocationRelativeTo(null);
[Code] ....
Panel.java:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JPanel;
public class Panel extends JPanel implements Runnable {
private Image dbImage;
[code]....
I've checked through everything and still cannot find what I'm doing wrong. I did try different codes but I just got errors instead.
View Replies
View Related
Jun 16, 2014
Write I program that generates a random number and asks the user to guess what the number is. If the user's guess is higher than the random number, the program should display "Too high, try again." If the user's guess is lower than the random number, the program should display "Too low, try again." The program should use a loop that repeats until the user correctly guesses the random number.
public class NumberGuess
{
public static void main(String[] args) {
// Create a Random object.
final static Random rand = new Random();
// max number is the upward bound number
final int MAXNUMBER = 10;
/**
* Method should return a random number within the upward bound MAX_NUMBER.
[code]....
I get compiler error message
NumberGuess.java:25: error: ';' expected
int getRandomNumber ();
^
NumberGuess.java:63: error: reached end of file while parsing
System.out.println("You got it right in " + numberOfGuesses + " guesses.");
^
How do I fix this?
View Replies
View Related
Dec 31, 2014
Here is my Code for the Game Of Life that I am programming to teach my self java. I am trying to create a GUI and I have done so and a window displays however i don't understand how i can get the Game of Life to display within that GUI?
import java.io.*;
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.FlowLayout; //Provides default layout managing
import javax.swing.JLabel;
public class LifeMain extends JFrame
[Code] ......
View Replies
View Related
Jul 9, 2014
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
public class memgame extends JFrame
{
Timer myTimer;
[Code] ....
View Replies
View Related
Feb 26, 2014
I'm in the process of building an algorithm for a leaderboard for my game, but now I'm stuck.
- It is a two-player game.
- The leaderboard is supposed to display the top 5 players.
- Scores will be saved to an external .txt file as a reference.
Also, may I add that I still cannot decide if I should only consider the winner's score for the leaderboard, or also the loser's.
For example:
Player 1 is Juliet.
Player 2 is Romeo.
At the end of the match, Juliet's score is greater than Romeo's.
Should I only evaluate Juliet's score for the leaderboard, or both of their scores? As of now my algorithm considers only the winner's score.
As of the moment I'm also only thinking of using JLabel to display the scores, but I'm not sure if this is the most efficient way to do it. I'm considering JTable.
So here's my current algorithm:
1) When the user starts a new match/returns to menu/exits game: (I provided 4 options: new game (scores remain but the board is reset), new match (everything is reset), menu, and exit.)
public class Game
{
//This class also holds the code for the operation of the game
if (event.getSource() == newmatch)//or menu or exit {
if (p1score != 0 || p2score != 0) {
Leaderboard x;
[Code] .....
3) The next step which is I'm highly unsure of is to create an ArrayList for the scores (I'm missing the part about the player's name because well I'm not sure as well how I will approach it) and then sort it, but the problem after that would be how to make the score agree with the player's name, if you get what I mean.
Like for example the scores are:
Anna = 1
Pamela = 5
Gabby = 3
Sorted: 5, 3, 1
Pamela has the highest score, but how will I be able to display her name together with her score?
4) I have a checker which determines if the ArrayList of scores is empty, and if it is, it will automatically display the player's name and score (this is for the first pair of players).
It's something like this:
if (board.isEmpty()){
label1.setText(winnername);
scorelabel1.setText(winnerscore);
} else {
//
}
There's a whole lot of buzz going on and now I don't know how I should proceed.
View Replies
View Related
Mar 3, 2015
The question pretty much says it all. My problem seems to be when adding the neighbours, I am always getting a Index out of Bounds problem. I know this is because the code is reaching for the edge of the table, for example if the column, i = 0 and the statement says to perform i - 1 and return it, then we are going to have a problem.
Similarly with anything like j = 20, j + 1 (as the grid only has 20 spaces). I understand the problem, but I am unsure of how to solve it. I have tried messing around with the if statements, but I continue to get the 'out of bounds' problem...
import javax.swing.*;
public class GameOfLife {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int cellChoice = 0;
int newCells = 1;
int generation = 1;
int neighbours = 0;
[Code] .....
View Replies
View Related
May 17, 2014
I created the below code and it works fine. It is a deck card game. I just want to divide my code into classes so that it seems more organized.
Java Code:
package getimage;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.*;
[code]....
View Replies
View Related
Aug 31, 2014
I'm trying to create a cursor for a game that moves square by square. While it will move to the next square, though, it leaves the image of the previous cursor on the last square it was on.
As a visual explanation, this is what the program looks like on launch:
This is what it's suppose to look like after you press the right arrow key once (made by forcibly changing launch coordinates):
And this is what it actually looks like after you press the right arrow key once:
Here is the code for the program:
package cursortest;
import javax.swing.*;
import java.awt.*;
import javax.imageio.*;
import java.io.*;
import java.awt.event.*;
public class CursorTest extends JPanel implements KeyListener{
[Code] ......
I'm fully aware that I could just use g.clearRect on the area and remove it for sure, but I know for a fact I shouldn't have to as I have another program I made a long time ago that tried to do something similar without needing to resort to that.
View Replies
View Related
Sep 9, 2014
how to create a tree data structure in java.I tried with a class consisting of node field and arraylist child nodes. but it does not satisfy the requirement.the requirement is that,
root: child1,child2,child3
child1:child4,child5
child2:child6,child7
on traversing it should print all the nodes as given above.no node should have same elements as child.
View Replies
View Related
Apr 16, 2015
I have a problem with creating a rectangle in Java. When I create the first rectangle all is ok. But the next, is the problem.
View Replies
View Related
Jul 30, 2014
I'm getting a DOMException, "HIERARCHY_REQUEST_ERR". I know that the problem is from the following code towards the bottom in my function, but I don't know how to deal with it. When I get rid of doc.appendChild(staff);, I get rid of the problem, but it obviously doesn't add the new entry to my root element.
Element staff = doc.createElement("Staff");
doc.appendChild(staff);
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
[Code] .....
View Replies
View Related