Help with a Hangman program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • araujo2nd
    New Member
    • Oct 2007
    • 24

    Help with a Hangman program

    Originally Posted by araujo2nd
    my name is andre, im from south africa, i was wondering if any1 could help me with a hangman application, im now in grade 11 and have a huge portfolio piece to do by the 15th which i have just started now, i have always struggled with java and would rele appreciate all the help that i can get. Pls could some1 help me as much as u can, im nt asking u to do the program for me but if u could pls take me steps through it as i really would like to pass the piece as it goes into my matric portfolio for college. at the moment im using DR JAVA and netbeans for GUI

    The program should be a hangman program which stores random words, it must keep a high score table with the number of tries each user takes and my ask user to input their name before they play the game.

    many thanks in advanced
    Andre
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by araujo2nd
    Originally Posted by araujo2nd
    my name is andre, im from south africa, i was wondering if any1 could help me with a hangman application, im now in grade 11 and have a huge portfolio piece to do by the 15th which i have just started now, i have always struggled with java and would rele appreciate all the help that i can get. Pls could some1 help me as much as u can, im nt asking u to do the program for me but if u could pls take me steps through it as i really would like to pass the piece as it goes into my matric portfolio for college. at the moment im using DR JAVA and netbeans for GUI

    The program should be a hangman program which stores random words, it must keep a high score table with the number of tries each user takes and my ask user to input their name before they play the game.

    many thanks in advanced
    Andre
    Hi and welcome to TSDN. Hope you'll have a great time here.
    You realize of course that the first step is to design your program.
    You need to decide how to store all the words (with an allowance for easy addition of other categories say, countries, actors, Bible books e.t.c).

    Then you have to write down your hangman algorithm. This shoild be pretty stright forward but just make sure you consider all the special cases.
    The last thing to design is your interface. You need to design which components make up your interface and whether or not you're going to add a bit of animation in there e.t.c
    After the design is done, writing the program will be so easy it will be such a bore.

    Comment

    • MarkoKlacar
      Recognized Expert Contributor
      • Aug 2007
      • 296

      #3
      Hi,

      do you want your program to have a graphical user interface?
      I'd be happy to help you with some advice and feedback.

      How familiar are you with java?

      keep me posted.

      Cheers

      Comment

      • araujo2nd
        New Member
        • Oct 2007
        • 24

        #4
        Hi sir thank you for the reply

        Yes i would like a GUI for the program

        Im familiar with java but just not really good with it, the teacher at school is not great and i got lost in it, i can do most basics etc

        preferably i would like to create a program that presents a blank frame, and the user gets 7 inputs to guess the word (he can either type the whole word or letter by letter), each time he gets one wrong, a piece of the hangman is displayed, and once all of the hangman is put together, it must tell the user the word and that he has lost.

        many thanks

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by araujo2nd
          Hi sir thank you for the reply

          Yes i would like a GUI for the program

          Im familiar with java but just not really good with it, the teacher at school is not great and i got lost in it, i can do most basics etc

          preferably i would like to create a program that presents a blank frame, and the user gets 7 inputs to guess the word (he can either type the whole word or letter by letter), each time he gets one wrong, a piece of the hangman is displayed, and once all of the hangman is put together, it must tell the user the word and that he has lost.

          many thanks
          You really should create the interface last.
          It will all be much easier and faster if you start with the design like I said.

          Comment

          • araujo2nd
            New Member
            • Oct 2007
            • 24

            #6
            I have already made that in Netbeans, should i show u the screen shot?

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by araujo2nd
              I have already made that in Netbeans, should i show u the screen shot?
              What I'd really like to see is your hangman algorithm but I guess it won't hurt to attach it. You'd have to make a post, click the edit button on the post and then add the attachment from there.

              Comment

              • araujo2nd
                New Member
                • Oct 2007
                • 24

                #8
                i dont have an algorithm:( i dont ever do them correctly.

                The GUI is just very basic, i still do not know how to implement an image of the hangman into the Jframe

                im struggling to upload the pic, could i email it or PM to u?

                I have to work on it a lot tonight

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by araujo2nd
                  i dont have an algorithm:( i dont ever do them correctly.

                  The GUI is just very basic, i still do not know how to implement an image of the hangman into the Jframe

                  im struggling to upload the pic, could i email it or PM to u?

                  I have to work on it a lot tonight
                  No need for emails or PMs.
                  We prefer to have all the help provided on the open forum.
                  Sorry to say this but chances are if you can't design it, then you can't code it. You really need to work on the design part of programing if you want to make your programing life easier.
                  Have you listed down the various parts that make up this program yet?

                  Comment

                  • araujo2nd
                    New Member
                    • Oct 2007
                    • 24

                    #10
                    yes i have, previously a good friend of mine helped me code this program but he has left an im stuck with the program that builds but will not compile. it says no class found, he is really good with java so i dont know how he could have made that error.

                    the code is (netbeans):

                    [CODE=java]package hangman;

                    import java.awt.*;
                    import javax.swing.*;
                    import java.util.*;
                    import java.lang.*;
                    import java.applet.*;
                    import java.awt.event. *;

                    public class Main extends Applet implements ActionListener, ItemListener
                    {
                    Button button[];
                    TextField word,letter;
                    TextField guess;
                    Label label1,label2,l abel3;
                    String alphabuttons[];
                    Panel panel1,panel2,p anel3,pana,panb ,panc;
                    Font font;
                    CheckboxGroup group;
                    Checkbox beg,inte,adv;
                    String begin[];
                    String inter[];
                    String advan[];
                    int lives,select;
                    String answer = " ";
                    String missy;
                    String buffy = "--------";
                    StringBuffer store2= new StringBuffer(bu ffy);
                    int counta = lives;
                    public void Main()
                    {
                    this.setLayout( new BorderLayout()) ;
                    // sets up the array to produce the alphabet buttons on the grid
                    alphabuttons = new String[]{"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"} ;

                    // sets up the arrays with the words to be guessed in all three stages
                    begin = new String[]{"CAT","POWER", "UNDER"};
                    inter = new String[]{"SECTION","VIS ITOR","INTENDED "};
                    advan = new String[]{"BEAUTIFUL","C HEERFUL","ENTHU SIASM"};

                    // create and arrange panel 1 which will house the text fields and the labels

                    panel1 = new Panel(); // panel 1 will hold the two labels and text fields at the top of the applet
                    add("North",pan el1);

                    panel1.setLayou t(new BorderLayout()) ;
                    label1 = new Label("GUESS THE WORD"); // create the label
                    font = new Font("TimesRoma n",Font.ITALIC, 16);
                    label1.setFont( font); // set the font to the label

                    pana = new Panel();
                    panel1.add("Nor th",pana);
                    pana.add(label1 ); // add the label to the panel
                    label1.setAlign ment(label1.CEN TER); // set the label to the centre

                    panb = new Panel();
                    panel1.add("Cen ter",panb);
                    word = new TextField(24); // creates the text field for the word set to 24 chars
                    word.addActionL istener(this);
                    word.setEditabl e(true); // allows text to be edited
                    panb.add(word," CENTER"); // adds the text field to the panel
                    label3 = new Label("Win/Lose");
                    label3.setFont( font);
                    panb.add(label3 );
                    label1.setAlign ment(label1.RIG HT);
                    letter = new TextField(10);
                    letter.addActio nListener(this) ;
                    panb.add(letter ,"RIGHT");

                    panc = new Panel();
                    panel1.add("Sou th",panc);

                    label2 = new Label("How Many Goes Left"); // create the label and intiliase
                    label2.setFont( font); // sets the font previously defined for label 1
                    panc.add(label2 );
                    label2.setAlign ment(label2.LEF T); // Sets the label left

                    guess = new TextField(3); // initialises the guess text field to 3 chars
                    guess.addAction Listener(this);
                    panc.add(guess) ;

                    // create and arrange panel 2 which will house the alphabet grid

                    panel2 = new Panel();
                    add("Center",pa nel2);

                    // set gridLayout for panel2(int rows , int col, int hgap, int vgap
                    panel2.setLayou t(new GridLayout(4,7, 3,3));

                    button = new Button[28];

                    for (int i=0;i<26;i+=1)
                    {
                    // set the buttons of the alphabet up using the array
                    button[i] = new Button(alphabut tons[i]);
                    panel2.add(butt on[i]);
                    button[i].addActionListe ner(this);
                    }

                    button[26] = new Button("Answer" ); // creates the answer button
                    button[26].setForeground( Color.green);
                    button[26].addActionListe ner(this);

                    button[27] = new Button("Reset") ; // creates the reset button
                    button[27].setForeground( Color.yellow);
                    button[27].addActionListe ner(this);

                    // add the remaining buttons to panel 2
                    panel2.add(butt on[26]);
                    panel2.add(butt on[27]);

                    // create and arrange panel 3 which will house the radio buttons for the level

                    panel3 = new Panel();
                    add("South",pan el3);

                    group = new CheckboxGroup() ;

                    // create the radio buttons

                    beg = new Checkbox("Begin ner",group,fals e);
                    beg.addItemList ener(this);

                    inte = new Checkbox("Inter mediate",group, false);
                    inte.addItemLis tener(this);

                    adv = new Checkbox("Advan ced",group,fals e);
                    adv.addItemList ener(this);

                    // add the radio buttons to panel3
                    panel3.add(beg) ;
                    panel3.add(inte );
                    panel3.add(adv) ;
                    }


                    public void actionPerformed (ActionEvent e)
                    {
                    int j=0;
                    int count=0;
                    int k=0;
                    String currentLetter;
                    String temp="";
                    StringBuffer Sanswer = new StringBuffer(an swer);
                    Sanswer.setLeng th(answer.lengt h());
                    boolean hang = false;
                    boolean correct=true;
                    store2.setLengt h(answer.length ());
                    count = Sanswer.length( );


                    for(k=0;k<=25;k +=1)
                    {
                    if (e.getSource()= =button[k])
                    {
                    currentLetter = (button[k].getLabel());
                    letter.setText( currentLetter);

                    for (j=0;j<count;j+ +)
                    {
                    //cycles through the letters in the word
                    if (currentLetter. equals(String.v alueOf(answer.c harAt(j))))
                    {
                    // replaces the letter if it is within the word
                    store2.replace( j,j+1,currentLe tter);
                    temp=store2.toS tring();
                    lives+=1; // does not decrement the lives
                    if(temp.equals( word.getText()) )
                    {
                    letter.setText( "YOU WIN");
                    letter.setForeg round(Color.blu e);
                    }
                    }

                    }
                    lives-=1; // decrements the lives
                    guess.setText(I nteger.toString (lives));
                    word.setText(st ore2.toString() );

                    if (lives<1)
                    {
                    letter.setText( "YOU LOSE");
                    letter.setForeg round(Color.red );
                    }

                    }
                    }

                    if(e.getSource( )==button[26]) // answer button
                    {

                    word.setText(an swer);
                    word.setForegro und(Color.darkG ray);
                    letter.setText( "Unlucky");
                    }

                    if(e.getSource( )==button[27]);
                    {
                    /* word.setText(nu ll);
                    guess.setText(n ull);
                    letter.setText( "delete");
                    for(int x=0;x<9;x++)
                    {
                    store2.replace( x,x+1,"-");
                    }
                    count = Sanswer.length( );
                    count=answer.le ngth(); */
                    }
                    } // ends actionPerformed

                    // invoked with the radio buttons are selected or deselected

                    public void itemStateChange d(ItemEvent e)
                    {
                    // checks if the button is pressed
                    select = (int)(java.lang .Math.random()* 4); // random number generator for the arrays
                    StringBuffer miss = new StringBuffer(an swer);
                    if (e.getSource()= =beg)
                    {
                    if(beg.getState ()==true)
                    {
                    lives = 9;
                    answer = (begin[select]);

                    for (int y=0;y<answer.le ngth();y++)
                    {
                    miss.replace(y, y+1,"-");
                    }
                    word.setText(mi ss.toString());
                    missy = miss.toString() ;
                    word.setForegro und(Color.black );
                    guess.setText(I nteger.toString (lives));
                    }
                    }
                    else if (e.getSource()= =inte)
                    {
                    if(inte.getStat e()==true)
                    {
                    lives = 8;
                    answer = (inter[select]);
                    for (int y=0;y<answer.le ngth();y++)
                    {
                    miss.replace(y, y+1,"-");
                    }
                    word.setText(mi ss.toString());
                    missy = miss.toString() ;
                    word.setForegro und(Color.black );
                    guess.setText(I nteger.toString (lives));
                    }
                    }
                    else if(e.getSource( )==adv)
                    {
                    if(adv.getState ()==true)
                    {
                    lives = 7;
                    answer = (advan[select]);
                    for (int y=0;y<answer.le ngth();y++)
                    {
                    miss.replace(y, y+1,"-");
                    }
                    word.setText(mi ss.toString());
                    missy = miss.toString() ;
                    word.setForegro und(Color.black );
                    guess.setText(I nteger.toString (lives));
                    }
                    }
                    } // ends itemStateChange d for the radio buttons

                    }//ends the class[/CODE]
                    Last edited by Ganon11; Oct 12 '07, 02:55 PM. Reason: Please use the [CODE] tags provided.

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      1.) Use code tags when posting code. It's one of the site rules.
                      2.) I wanted to help because I though you wanted to write the program yourself.

                      Comment

                      • araujo2nd
                        New Member
                        • Oct 2007
                        • 24

                        #12
                        sorry wat are code tags?? im still new here

                        i do want to do it myself and i know if i do im definately not going to be finished by next monday:(. It is a really important piece and if i start now ill neva finish, i just dont have the experience yet

                        Comment

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #13
                          Originally posted by araujo2nd
                          sorry wat are code tags?? im still new here

                          i do want to do it myself and i know if i do im definately not going to be finished by next monday:(. It is a really important piece and if i start now ill neva finish, i just dont have the experience yet
                          If you start today and devote a few hours to it a day from today you will be done before this weekend. The important thing is to approach it the correct way.

                          Comment

                          • araujo2nd
                            New Member
                            • Oct 2007
                            • 24

                            #14
                            ok will do, could u pls tell me what was wrong with the other program tho...

                            ok how exactly should i start it? should i write down exactly what to do etc (im not sure exactly what ill need thats the problem), i no ill use an array to store the words etc

                            Comment

                            • r035198x
                              MVP
                              • Sep 2006
                              • 13225

                              #15
                              Originally posted by araujo2nd
                              ok will do, could u pls tell me what was wrong with the other program tho...

                              ok how exactly should i start it? should i write down exactly what to do etc (im not sure exactly what ill need thats the problem), i no ill use an array to store the words etc
                              Why not store the words in a text file or even in a database table?
                              Then you can change the words or add new words easily. Adding new words gives you the option of changing the category of the words being guessed.

                              Comment

                              Working...