Jump to content


Help Needed With Word Guessing Game


  • Please log in to reply
3 replies to this topic

#1 riley1275

riley1275

    Newbie

  • Members
  • Pip
  • 7 posts
  • Gender:Male
  • Interests:Computing

Posted 13 November 2011 - 02:51 PM

Hi,

I have to create a java word game to guess words.
the question asks:
Using an appropriate screen layout, design, implement and test a java swing application to produce a game called Guess the word.

When the game begins, the program should choose the word at random from a bank of words.

The user is invited to enter a guess and have it checked. Any letters that are guessed correctly should be indicated to the user.

I am not sure how to code this to make the program choose a word at random from the Words Array. Any help would be greatly appreciated.

Here is my code so far:

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


public class WordGuessgame
{
	JFrame frame;
	JPanel panel;
	JLabel label;
	JTextArea word;

	String [] GuessWords;
	
	JButton checkletter; 
	JButton giveup;
	
	String wordSoFar = "", updatedWord = "";	
	
	
	String [] Words  = { // Creating Array of words.
	 "Bank", "Assignment", "Dog", "Car", "House", "Network", "Programming", "Tea",
	 "JCreator", "Banbridge"};
	
	char [] Letters = {'A', 'B','C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
	'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 
	'X', 'Y', 'Z'};
	
	String [] Guesswords ={ "_ _ _ _ ", "_ _ _ _ _ _ _ _ _ _", "_ _ _", "_ _ _", "_ _ _ _ _", "_ _ _ _ _ _ _", "_ _ _ _ _ _ _ _ _ _ _",
	"_ _ _", "_ _ _ _ _ _ _ _", "_ _ _ _ _ _ _ _ _"};
		



	public static void main(String[]args)
	{
		WordGuessgame app = new WordGuessgame();
		app.go();
	}
	
	public void go()
	{	
		frame = new JFrame();
		panel = new JPanel();
		frame.getContentPane().add(BorderLayout.NORTH, panel);
		frame.setVisible(true);
		frame.setSize(800,800);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		panel.setLayout(new FlowLayout());
		
		
		
		label = new JLabel(" Word Guessing Game");
		panel.add(label);
		checkletter = new JButton("Check");
		panel.add(checkletter);
		
		
		word = new JTextArea("");
		panel.add(word);
		
		giveup = new JButton("Give up!");
		panel.add(giveup);
		
		
	
	}
	
	
}

	 


#2 Kraicheck

Kraicheck

    Advanced Member

  • Members
  • PipPipPip
  • 884 posts
  • Gender:Male
  • Location:Belgium

Posted 13 November 2011 - 06:59 PM

Use the Random class in the util package to generate a number between 0 (inclusive) and the size of your array (exclusive).

#3 Blackiey

Blackiey

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 21 November 2011 - 04:14 AM

or Math.Random()

that is if you are a AP students taking the AP test

#4 sholmes

sholmes

    Member

  • Members
  • PipPip
  • 11 posts
  • Gender:Male
  • Location:Long Lake, NY
  • Interests:Delphi & Java

Posted 21 November 2011 - 06:36 PM

if you have an array of 0..n strings, then use the random generator (Math.random method) to generate a number to be used to random select a array string.

Use google. Its your best friend.   Google java + random   or Java + Math.Random    and then google Java + random + array

you will be surprised at the vast emount of info one can find <smile>


int number = 0;
number = 1 + (int) (99.*Math.random());// get a random number between 1 & 100
Delphi

type
  TTest1 = class
  public
    constructor Create;
    destructor Destroy; override;
    procedure Method1;
  end;


JAVA

public class Test1 {
  public Test1() {
    //
  }

  public void method1() {
    //
  }
}