Java help needed

Java help needed

Create a web application that will allow a user to play the popular Hangman computer game using Java application program.

How to play Hangman: The computer will randomly choose a word from an array. This array must have a minimum of 10 words and must be loaded from a text file of words. A sample text file named Words.txt is created. The same number of labels, as there is letters in the chosen word must be displayed on the game frame screen so that the user knows how many letters are in the word. The user will try to guess the word by clicking a letter from a display of all letters of the alphabet (or by typing in the letters in the labels). Each letter is a separate button that can be clicked.

Each time the user chooses a letter, the computer checks to see if that letter is in the chosen word in the right position. If the letter is in the word, it will be displayed in the correct position and a message of correct will be displayed. If it is not in the word, a part of the man will be drawn on the game screen. The object of the game is to try and guess the word before the man is hanged. If the man is hanged, i.e. after 6 loses, the user loses and the correct word is displayed.

The program must also ask the user whether they wish to play again and keep scores for the user for a playing session. The following statics must be displayed per playing session on the web form: number of games played, number correct and number lost. Also have an option to allow the user to add new words to the file. Instructions: 1. Display however many labels as needed by the number of characters of the chosen word. 2. Add 4 buttons or menu options: One for accessing help using the program, one for adding new words, one for clearing the screen/ restart/ Start a new game and one for exiting the program. 3. In the correct event handler, write program code to check if the entered letter is the correct letter. 4. If the letter is incorrect display or draw one part of the man(One of the body part images) 5. If 6 parts of the man√Ę‚?¨‚?Ęs body is displayed, the user loses. Report that the game is over. 6. If the clear game/restart/new game button is pressed reset all labels to empty and choose another word. 7. If the help button is pressed, show a help screen that gives instructions on how to use the game.

View Answers

August 23, 2012 at 11:37 AM

Here is a simple program of Hangman game.

import java.util.*;

public class Hangman{
    public static void main( String[] args){
        Scanner scan = new Scanner( System.in);
        int totalMistake = 0;
        boolean b = false;

        System.out.println("Welcome to Hangman Game:)!!" );
        SecretWord word = new SecretWord();
        Input input = new Input();
        while(totalMistake < 4 && !word.isSolved()){
            System.out.println(word);
            char c = input.getNewLetter();
            if (!word.update(c))
                totalMistake = totalMistake + 1 ;
        }
        if (word.isSolved())
            System.out.println("Great, you done it.");
        else
            System.out.println("Sorry, too many errors.");
    }
 }

class SecretWord{
    String  visible, secret;

public SecretWord(){
    String [] wordList = { "Giraffe", "Camel", "Lion", "Bear","Zebra","Monkey","Leopard","Rhinoceros","Elephant","Dear" };
    int rand;
    rand = (int) (Math.random()*4);
    secret = wordList[rand];
    int wordLength = secret.length();

    visible = "";
    for( int i = 0; i < secret.length(); i++){
        char c = secret.charAt(i);
        if( c == ' ' )
            visible += c;
        else
            visible += '_';
     }
   }
    public String toString(){
       return visible;
    } 
    public boolean update(char c){
        char[] secretArray = new char[secret.length()];
        char[] visibleArray = new char[secret.length()];
        String s = "";
        boolean contains = false ;

        for(int i = 0; i < secret.length(); i++){
            secretArray[i] = secret.charAt(i);
            visibleArray[i] = visible.charAt(i);
        }
        for(int k = 0; k < secret.length();k++){
            if(secretArray[k] == c){
                visibleArray[k] = c;
                contains = true;
            }
        }
        for(int m=0; m < secret.length(); m++ )
        {
            s = s + visibleArray[m];
        }
        visible = s;

        return contains;
   }
    public boolean isSolved()
    {   
        return secret.equalsIgnoreCase(visible);
    }
}

 class Input
{   
   ArrayList<String> keyList ; 
    boolean chosen;

    public Input()
    {
        keyList = new ArrayList<String>();
    }

    public void keyChosen( String key )
    {
        keyList.add( key );
    }

    public boolean hasBeenChosen( String key)
    {
        chosen = keyList.contains( key );
        return chosen;
    }

    public char getNewLetter() 
    {
        Scanner scan=new Scanner( System.in);
        char a;
        boolean chosen;
        String aTmp;   
        do
        {   
            System.out.println("Enter a Letter");
            a = scan.next().charAt( 0); 
            aTmp = "" + a;
            chosen = hasBeenChosen( aTmp);
            if(!chosen)
                keyChosen( aTmp);
            else
            {   
                System.out.println("You've already used that one. Please enter a new Letter");
            }
       }
        while(chosen);
        return a;  
    }
}

August 29, 2012 at 9:34 PM

Can you please show me how to do the following using Jframe form?

Ads









Related Tutorials/Questions & Answers:
Java help needed
Java help needed  Create a web application that will allow a user to play the popular Hangman computer game using Java application program. How... as needed by the number of characters of the chosen word. 2. Add 4 buttons or menu
help needed - Java Interview Questions
help needed  What are the new features added in jdk 1.5?  Hi friend, The following were the new features added in java 1.5.... * Autoboxing/unboxing ------------------------- This link will help you
Advertisements
help Needed with requirements
help Needed with requirements  Following are my requirements : * Maintain a student database *Based on a student's test results ,reports should... develop it in Java and it has to be a web app
Help needed in JSP
Help needed in JSP  Hi .. I want to Update the multiple values of database using checkboxes and want to set the session for selected checkboxes..? please answer if any one knows as soon as possible
Help needed on java standalone app and default browser interaction.
Help needed on java standalone app and default browser interaction.  ... the url thats displaying on my default browser to my java standalone application? Platform Windows Browser IE7 Any from of help would be deeply apperciated
Code Help Needed
Code Help Needed  I am trying to help a buddy of mine with this program and I can't seem to figure it out. I am still working on it but any help...[test]); line. Again thank you for any help. import java.util.*; import
urgent help needed!!!!
....please help me.... 1)state.jsp: <%@page import="java.sql.*"%>
urgent help needed!!!!
access country and state....please help me.... 1)state.jsp: <%@page import
Help needed for Form Validation please help!
Help needed for Form Validation please help!  I have created a page which has login as well as Signup fields. I validated the login part by using jsp...;%@ page language="java" session="true" %> <% //Getting the text
look up values help needed
look up values help needed  Hi, I have a table that returns rows with a total weight (say 212.5 for example). I have another look up table that has... of 2.55 as 212.5kgs is between 195.5 and 220.5 can anyone help how I can do
servlet redirect problem help needed
servlet redirect problem help needed  package p; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import
servlet redirect problem help needed
servlet redirect problem help needed  package p; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import
CAN ANYONE SOLVE THIS FOR ME. URGENT HELP NEEDED. PLEASE HELP
CAN ANYONE SOLVE THIS FOR ME. URGENT HELP NEEDED. PLEASE HELP   The Task Write a class whose objects will represent students. The class should have three fields for representing a student‚??s
CAN ANYONE SOLVE THIS FOR ME. URGENT HELP NEEDED. PLEASE HELP
CAN ANYONE SOLVE THIS FOR ME. URGENT HELP NEEDED. PLEASE HELP   Write a class whose objects will represent students.The class should have three fields for representing a student√Ę‚?¨‚?Ęs name, this student√Ę‚?¨‚?Ęs
Needed java code
Needed java code  1.Java code for the time used to display your name, one character at a time, for 200,000 times . 2.Java code for the time used to display your name, one character at a time in reverse order, for 200,000 times
java help!
java help!   I need help starting off a program I am trying to write for class. The class should contain a private instance variable to represent temperature in Degrees Celsius and it should have the following methods
Help With Java...
Help With Java...  Write a java program that can take a positive integer greater than 2 as input and write out the number of times one must repeatedly divide this number by 2 before getting a value less than 2
help in java
help in java   Rational Numbers Rational numbers are numbers that can be represented as a fraction p / q where p is an integer number and q is a positive integer (q != 0). Design and implement a Java class RationalNumber
help in java
help in java  Write an application that prompt the user to enter a number to use as an array size, and then attempt to declare an array using the entered size. Java generates a NegativeArraySizeException if wou attempt to create
help for java
help for java  1.convert the following Pseudo code to java if/else structure if student?s grade is greater than or equal to 90 Print ?A? else if student?s grade is greater than or equal to 80 Print ?B? else
help in java
help in java  3.Flights Design and implement a class called Flight that represents an airline flight. It should contain instance data that represents the airline name, flight number, and the flight's origin and destination cities
help in java
help in java  Write a program that will evaluate simple expressions such as 17 + 3 and 3.14159 * 4.7. The expressions are to be typed in by the user. The input always consist of a number, followed by an operator, followed
help in java
help in java  Design and implement a class called Sphere that contains instance data that represents the sphere's diameter. Define the Sphere constructor to accept and initialize the diameter, and include getter and setter
help in java
help in java  The factorial of a nonnegative integer n is written n! (pronounced √Ę‚?¨Ň? n factorial√Ę‚?¨¬?) and is defined as follows: n!=n . (n-1) . (n-2) . .... . 1 (for values of n greater than or equal to 1) and n!=1 (for n
help in java
help in java  Gross And Dozens Design and implement a class called GrossAndDozens to convert a given number of eggs into the number of gross, the number of dozens, and to the number of left over eggs. If you have N eggs, then you
help in java
help in java  Shopping cart Create 3 shopping carts from a Cart class. Each class should have four items in it, a title, a price for the 4 items and the number of each. Your shopping cart should use private variables, so use
help in java
help in java   Team Roster Create a program called TeamRoster that uses a class called Roster. Roster should contain a team name and the names of all... or help me on this pleas.. 7. Gross And Dozens Design and implement a class
help in java
help in java  Create 3 shopping carts from a Cart class. Each class should have four items in it, a title, a price for the 4 items and the number of each. Your shopping cart should use private variables, so use getter and setter
help in java
help in java  Create a class named Order that performs order processing of a single item. The class has five instance variables (fields) : customer name, customer number, quantity ordered, unit price, and total price. Define
help in java
help in java  Write a Temperature class that has two instance variables: a temperature value (a floating-point number) and a character for the scale, either C for Celsius or F for Fahrenheit. The class should have a number
help in java
help in java  Temperature Write a Temperature class that has two instance variables: a temperature value (a floating-point number) and a character for the scale, either C for Celsius or F for Fahrenheit. The class should have

Ads