Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Java help needed
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


comedianstar
Java help needed
2 Answer(s)      2 years and 3 months ago
Posted in : Java Beginners


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.


Advertisement
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?



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 for a program - Java Beginners
help needed for a program   i am having trouble understanding on what to code, so can some 1 help me out here. thanks in advance. Part I Write, compile and execute a Java program that displays the following prompts
Advertisements
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
urgent help needed in JDBC AND JAVA GUI - JDBC
urgent help needed in JDBC AND JAVA GUI  my application allows... want any one to help me convert from scanner to java GUI for this code...(); } } // thanks for any help rendered   Hi Friend, Try the following code
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 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
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
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
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
PHP help needed - Development process
PHP help needed  This is my code that I am using for search engine, can any body answer how to cheange the code if I want limited and per page result display??? code Career Search result Yami says <?php
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
Help needed for J2me and PHP - Development process
Help needed for J2me and PHP  Hello Sir I am a degree student. I have a small CRM application using J2me and PHP. I want to do cryptography using... but could not get result. I will appreciate your help. Regards
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
what programs are needed in java programming? - Java Beginners
what programs are needed in java programming?  What programs are needed in java programming?   Hi friend, For solving the problem visit to : http://www.roseindia.net/java/] Thanks  Hi friend
urjent code needed - Java Interview Questions
urjent code needed  Coding: - We have Document domain/entity object with properties of Long id, String fileName, String filePath, int docId. Please write a java code that will: 1. Implements hashCode() and equals
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
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 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
java help?
java help?  Write a program, where you first ask values to an array with 6 integer values and then count the sum and average of the values in methods (send in the array & return the counted value to the main program). Print
java help
java help   Write a Java program that does the following: 1.Creates a grading program based on the Baker College grading standard. You will need to look up the current grade standard. You may use only the letter grades without
java help
java help  How to Open CSV Files in a Microsoft Excel Application Using Java Code with example pgm
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
 

 

 

DMCA.com