473,486 Members | 1,850 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Help with a Hangman program

24 New Member
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
Oct 9 '07 #1
47 11425
r035198x
13,262 MVP
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.
Oct 9 '07 #2
MarkoKlacar
296 Recognized Expert Contributor
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
Oct 9 '07 #3
araujo2nd
24 New Member
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
Oct 9 '07 #4
r035198x
13,262 MVP
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.
Oct 9 '07 #5
araujo2nd
24 New Member
I have already made that in Netbeans, should i show u the screen shot?
Oct 9 '07 #6
r035198x
13,262 MVP
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.
Oct 9 '07 #7
araujo2nd
24 New Member
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
Oct 9 '07 #8
r035198x
13,262 MVP
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?
Oct 9 '07 #9
araujo2nd
24 New Member
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):

Expand|Select|Wrap|Line Numbers
  1. package hangman;
  2.  
  3. import java.awt.*;
  4. import javax.swing.*;
  5. import java.util.*;
  6. import java.lang.*;
  7. import java.applet.*;
  8. import java.awt.event.*;
  9.  
  10. public class Main extends Applet implements ActionListener,ItemListener
  11.  Button button[]; 
  12.  TextField word,letter; 
  13.  TextField guess; 
  14.  Label label1,label2,label3; 
  15.  String alphabuttons[]; 
  16.  Panel panel1,panel2,panel3,pana,panb,panc; 
  17.  Font font; 
  18.  CheckboxGroup group; 
  19.  Checkbox beg,inte,adv; 
  20.  String begin[]; 
  21.  String inter[]; 
  22.  String advan[]; 
  23.  int lives,select; 
  24.  String answer = "        "; 
  25.  String missy; 
  26.  String buffy = "--------"; 
  27.  StringBuffer store2= new StringBuffer(buffy); 
  28.  int counta = lives; 
  29.  public void Main() 
  30.  { 
  31.   this.setLayout(new BorderLayout()); 
  32.   // sets up the array to produce the alphabet buttons on the grid 
  33.   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"}; 
  34.  
  35.   // sets up the arrays with the words to be guessed in all three stages 
  36.   begin = new String[]{"CAT","POWER","UNDER"}; 
  37.   inter = new String[]{"SECTION","VISITOR","INTENDED"}; 
  38.   advan = new String[]{"BEAUTIFUL","CHEERFUL","ENTHUSIASM"}; 
  39.  
  40.   // create and arrange panel 1 which will house the text fields and the labels 
  41.  
  42.   panel1 = new Panel(); // panel 1 will hold the two labels and text fields at the top of the applet 
  43.   add("North",panel1); 
  44.  
  45.   panel1.setLayout(new BorderLayout()); 
  46.   label1 = new Label("GUESS THE WORD"); // create the label 
  47.   font = new Font("TimesRoman",Font.ITALIC,16); 
  48.   label1.setFont(font); // set the font to the label 
  49.  
  50.   pana = new Panel(); 
  51.   panel1.add("North",pana); 
  52.   pana.add(label1); // add the label to the panel 
  53.   label1.setAlignment(label1.CENTER); // set the label to the centre 
  54.  
  55.   panb = new Panel(); 
  56.   panel1.add("Center",panb); 
  57.   word = new TextField(24); // creates the text field for the word set to 24 chars 
  58.   word.addActionListener(this); 
  59.   word.setEditable(true); // allows text to be edited 
  60.   panb.add(word,"CENTER"); // adds the text field to the panel 
  61.   label3 = new Label("Win/Lose"); 
  62.   label3.setFont(font); 
  63.   panb.add(label3); 
  64.   label1.setAlignment(label1.RIGHT); 
  65.   letter = new TextField(10); 
  66.   letter.addActionListener(this); 
  67.   panb.add(letter,"RIGHT"); 
  68.  
  69.   panc = new Panel(); 
  70.   panel1.add("South",panc); 
  71.  
  72.   label2 = new Label("How Many Goes Left"); // create the label and intiliase 
  73.   label2.setFont(font); // sets the font previously defined for label 1 
  74.   panc.add(label2); 
  75.   label2.setAlignment(label2.LEFT); // Sets the label left 
  76.  
  77.   guess = new TextField(3); // initialises the guess text field to 3 chars 
  78.   guess.addActionListener(this); 
  79.   panc.add(guess); 
  80.  
  81.   // create and arrange panel 2 which will house the alphabet grid 
  82.  
  83.   panel2 = new Panel(); 
  84.   add("Center",panel2); 
  85.  
  86.   // set gridLayout for panel2(int rows , int col, int hgap, int vgap 
  87.   panel2.setLayout(new GridLayout(4,7,3,3)); 
  88.  
  89.   button = new Button[28]; 
  90.  
  91.   for (int i=0;i<26;i+=1) 
  92.   { 
  93.    // set the buttons of the alphabet up using the array 
  94.    button[i] = new Button(alphabuttons[i]); 
  95.    panel2.add(button[i]); 
  96.    button[i].addActionListener(this); 
  97.   } 
  98.  
  99.   button[26] = new Button("Answer"); // creates the answer button 
  100.   button[26].setForeground(Color.green); 
  101.   button[26].addActionListener(this); 
  102.  
  103.   button[27] = new Button("Reset"); // creates the reset button 
  104.   button[27].setForeground(Color.yellow); 
  105.   button[27].addActionListener(this); 
  106.  
  107.   // add the remaining buttons to panel 2 
  108.   panel2.add(button[26]); 
  109.   panel2.add(button[27]); 
  110.  
  111.   // create and arrange panel 3 which will house the radio buttons for the level 
  112.  
  113.   panel3 = new Panel(); 
  114.   add("South",panel3); 
  115.  
  116.   group = new CheckboxGroup(); 
  117.  
  118.   // create the radio buttons 
  119.  
  120.   beg = new Checkbox("Beginner",group,false); 
  121.   beg.addItemListener(this); 
  122.  
  123.   inte = new Checkbox("Intermediate",group,false); 
  124.   inte.addItemListener(this); 
  125.  
  126.   adv = new Checkbox("Advanced",group,false); 
  127.   adv.addItemListener(this); 
  128.  
  129.   // add the radio buttons to panel3 
  130.   panel3.add(beg); 
  131.   panel3.add(inte); 
  132.   panel3.add(adv); 
  133.  } 
  134.  
  135.  
  136.  public void actionPerformed(ActionEvent e) 
  137.  { 
  138.   int j=0; 
  139.   int count=0; 
  140.   int k=0; 
  141.   String currentLetter; 
  142.   String temp=""; 
  143.   StringBuffer Sanswer = new StringBuffer(answer); 
  144.   Sanswer.setLength(answer.length()); 
  145.   boolean hang = false; 
  146.   boolean correct=true; 
  147.   store2.setLength(answer.length()); 
  148.   count = Sanswer.length(); 
  149.  
  150.  
  151.   for(k=0;k<=25;k+=1) 
  152.   { 
  153.    if (e.getSource()==button[k]) 
  154.    { 
  155.     currentLetter = (button[k].getLabel()); 
  156.     letter.setText(currentLetter); 
  157.  
  158.     for (j=0;j<count;j++) 
  159.     { 
  160.      //cycles through the letters in the word 
  161.      if (currentLetter.equals(String.valueOf(answer.charAt(j)))) 
  162.      { 
  163.       // replaces the letter if it is within the word 
  164.       store2.replace(j,j+1,currentLetter); 
  165.       temp=store2.toString(); 
  166.       lives+=1; // does not decrement the lives 
  167.       if(temp.equals(word.getText())) 
  168.       { 
  169.        letter.setText("YOU WIN"); 
  170.        letter.setForeground(Color.blue); 
  171.       } 
  172.      } 
  173.  
  174.     } 
  175.     lives-=1; // decrements the lives 
  176.     guess.setText(Integer.toString(lives)); 
  177.     word.setText(store2.toString()); 
  178.  
  179.     if (lives<1) 
  180.     { 
  181.      letter.setText("YOU LOSE"); 
  182.      letter.setForeground(Color.red); 
  183.     } 
  184.  
  185.    } 
  186.   } 
  187.  
  188.   if(e.getSource()==button[26]) // answer button 
  189.   { 
  190.  
  191.    word.setText(answer); 
  192.    word.setForeground(Color.darkGray); 
  193.    letter.setText("Unlucky"); 
  194.   } 
  195.  
  196.   if(e.getSource()==button[27]); 
  197.   { 
  198.   /* word.setText(null); 
  199.    guess.setText(null); 
  200.    letter.setText("delete"); 
  201.    for(int x=0;x<9;x++) 
  202.    { 
  203.     store2.replace(x,x+1,"-"); 
  204.    } 
  205.    count = Sanswer.length(); 
  206.    count=answer.length(); */ 
  207.   } 
  208.  } // ends actionPerformed 
  209.  
  210.  // invoked with the radio buttons are selected or deselected 
  211.  
  212.  public void itemStateChanged(ItemEvent e) 
  213.  { 
  214.   // checks if the button is pressed 
  215.   select = (int)(java.lang.Math.random()*4); // random number generator for the arrays 
  216.   StringBuffer miss = new StringBuffer(answer); 
  217.   if (e.getSource()==beg) 
  218.   { 
  219.    if(beg.getState()==true) 
  220.    { 
  221.     lives = 9; 
  222.     answer = (begin[select]); 
  223.  
  224.     for (int y=0;y<answer.length();y++) 
  225.     { 
  226.      miss.replace(y,y+1,"-"); 
  227.     } 
  228.     word.setText(miss.toString()); 
  229.     missy = miss.toString(); 
  230.     word.setForeground(Color.black); 
  231.     guess.setText(Integer.toString(lives)); 
  232.    } 
  233.   } 
  234.   else if (e.getSource()==inte) 
  235.   { 
  236.    if(inte.getState()==true) 
  237.    { 
  238.     lives = 8; 
  239.     answer = (inter[select]); 
  240.     for (int y=0;y<answer.length();y++) 
  241.     { 
  242.      miss.replace(y,y+1,"-"); 
  243.     } 
  244.     word.setText(miss.toString()); 
  245.     missy = miss.toString(); 
  246.     word.setForeground(Color.black); 
  247.     guess.setText(Integer.toString(lives)); 
  248.    } 
  249.   } 
  250.   else if(e.getSource()==adv) 
  251.   { 
  252.    if(adv.getState()==true) 
  253.    { 
  254.     lives = 7; 
  255.     answer = (advan[select]); 
  256.      for (int y=0;y<answer.length();y++) 
  257.     { 
  258.      miss.replace(y,y+1,"-"); 
  259.     } 
  260.     word.setText(miss.toString()); 
  261.     missy = miss.toString(); 
  262.     word.setForeground(Color.black); 
  263.     guess.setText(Integer.toString(lives)); 
  264.    } 
  265.   } 
  266.  } // ends itemStateChanged for the radio buttons 
  267.  
  268. }//ends the class
Oct 9 '07 #10
r035198x
13,262 MVP
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.
Oct 9 '07 #11
araujo2nd
24 New Member
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
Oct 9 '07 #12
r035198x
13,262 MVP
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.
Oct 9 '07 #13
araujo2nd
24 New Member
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
Oct 9 '07 #14
r035198x
13,262 MVP
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.
Oct 9 '07 #15
araujo2nd
24 New Member
kewl i just did that, i stored 200 words in a text file, its called dictionary.txt
Oct 9 '07 #16
r035198x
13,262 MVP
kewl i just did that, i stored 200 words in a text file, its called dictionary.txt
Great. Now you need to know how to read words from that text file.

This article will show you how. Make sure you also read the comments posted in that thread. After you're done reading that you should try the code and read words from your textfile. Also try to get a random word to display to the screen.
Hint: For the random word display part you may find the java.util.Random class helpful.
Oct 9 '07 #17
araujo2nd
24 New Member
ok, i just managed to read and print all the words, im not familiar with the java.util.Random class so do not know how to display a random word instead of all of them
Oct 9 '07 #18
r035198x
13,262 MVP
ok, i just managed to read and print all the words, im not familiar with the java.util.Random class so do not know how to display a random word instead of all of them
You should have opened the API docs page for it.

From here there are two ways of going about it.
Read all the words into an ArrayList<String> one word at a time. Then generate a number (using the Random class) between 0 and the number of words in the ArrayList (minus one). If the generated number is n, then displaying the nth string in the list is equivalent to displaying a random string.

P.S Always refuse to write any code if you do not have the API specs with you.

P.P.S We are still just playing around with some stuff here. The next step involves the algorithm which you are going to write.
Oct 9 '07 #19
araujo2nd
24 New Member
Okay. Will attempt it first thing tomorrow morning. How exactly should i set out the algorithm. I dont normally use algorithms as i dont really understand how to make them:(.. But once i get the random word to work ill let u know. I hope i can get that by tomorrow morning.
Oct 9 '07 #20
dav3
94 New Member
Okay. Will attempt it first thing tomorrow morning. How exactly should i set out the algorithm. I dont normally use algorithms as i dont really understand how to make them:(.. But once i get the random word to work ill let u know. I hope i can get that by tomorrow morning.

An algorithm is basically just a step by step list of whats going to happen in your program.

its also helpful to make a list of all the things your going to need.
You need to read a file
get user input
display user input as output
etc
etc

I have found recently that when i have a daunting project on my hands if you break it down and say "ok, today I am going to do this and if i have time start this, or polish this" the project seems to flow more smoothly. Sitting down and just starting to hack out code leads to problems. Even though coding it right away might seem like the quickest way to finish the project, its not.

This is a great site to come to for help, you just need to show your doing the work yourself and all the developers, admins, mods and experts are more then willing to guide us:)
Oct 9 '07 #21
araujo2nd
24 New Member
i have decided to go with the applet above, most of it was done by me in any case, i have to continue with it because its so close to done, and i rele do not have time to start from nothing, it has to be done by monday with the documentation which will take me hours:(

so all help with my applet would be much appreciated, i would like to change the applet into a java application, i only use NETBEANS and DRJava
Oct 10 '07 #22
r035198x
13,262 MVP
i have decided to go with the applet above, most of it was done by me in any case, i have to continue with it because its so close to done, and i rele do not have time to start from nothing, it has to be done by monday with the documentation which will take me hours:(

so all help with my applet would be much appreciated, i would like to change the applet into a java application, i only use NETBEANS and DRJava
I thought you said it was written by a good friend of yours.
Anyway good luck with it.

Edit: Please do not double post. It's against the site rules. Just keep one problem in one thread.
Oct 10 '07 #23
araujo2nd
24 New Member
So i take it that u dont want to help me? My friend did not code it, he helped me do the coding. But now that he left i have no help which is why i came here. Anyway thanks
Oct 10 '07 #24
r035198x
13,262 MVP
So i take it that u dont want to help me? My friend did not code it, he helped me do the coding. But now that he left i have no help which is why i came here. Anyway thanks
Of course I want to help, but only if you want to do it the correct way.
Oct 10 '07 #25
araujo2nd
24 New Member
Sir pls can u just help me from where i am, there is still a lot to do, i rele do not have time and have exams soon:(, i just rele want to get this off my chest, i managed to get it to finally run the applet (needed a main method), but now pls could u just help me in getting a high scores table which stores user name and number of tries, and also the reset button does not work and i have tried everything to fix it...i also have no idea how to get the hangman to appear in the gui
Oct 10 '07 #26
r035198x
13,262 MVP
Sir pls can u just help me from where i am, there is still a lot to do, i rele do not have time and have exams soon:(, i just rele want to get this off my chest, i managed to get it to finally run the applet (needed a main method), but now pls could u just help me in getting a high scores table which stores user name and number of tries, and also the reset button does not work and i have tried everything to fix it...i also have no idea how to get the hangman to appear in the gui
That's because you never designed for any of those things before you started.
So what's your current code now? Use code tags if you're going to post it. (It's the # icon on the reply controls menu when you're posting the code)
Oct 10 '07 #27
araujo2nd
24 New Member
My java code is:



Expand|Select|Wrap|Line Numbers
  1.  public class Hangman
  2. {
  3.     /**
  4.      * construct a Hangman game
  5.      */
  6.  
  7.     public Hangman()
  8.     {
  9.  myMisses = 0;
  10.  myWordIndex = 0;
  11.  myLettersUsed = new boolean[Character.MAX_VALUE];
  12.     }
  13.  
  14.     /**
  15.      * clear all variables to beginning-of-game state
  16.      * word shown user will be all blanks, all letters
  17.      * are eligible for "guessability"
  18.      */
  19.  
  20.     private void clear()
  21.     {
  22.  int k;
  23.  
  24.  // all letters can be guessed, none are used
  25.  
  26.  for(k=0; k < Character.MAX_VALUE; k++) {
  27.      myLettersUsed[k] = false;
  28.  }
  29.  
  30.  // word shown to user is all blanks
  31.  
  32.  mySecretWord = new char[myWords[myWordIndex].length()];
  33.  for(k=0; k < mySecretWord.length; k++) {
  34.      mySecretWord[k] = BLANK;
  35.  }
  36.     }
  37.  
  38.     /**
  39.      * process a user's guess, update state to reflect
  40.      * the guess. This might change # misses and the
  41.      * word displayed to the user
  42.      *
  43.      * @param ch is the character guessed by the user
  44.      */
  45.     private void guess(char ch)
  46.     {
  47.  int k;
  48.  boolean charFound = false;
  49.  
  50.  ch = Character.toLowerCase(ch);    // case doesn't matter
  51.  
  52.  for(k=0; k < mySecretWord.length; k++) {
  53.      if (! myLettersUsed[ch] &&
  54.   myWords[myWordIndex].charAt(k) == ch) {
  55.   mySecretWord[k] = ch;
  56.   charFound = true;
  57.      }
  58.  }
  59.  if (! myLettersUsed[ch] && ! charFound) {
  60.      myMisses++;
  61.  }
  62.  myLettersUsed[ch] = true;
  63.     }
  64.  
  65.     /**
  66.      * display (partially guessed) word to user
  67.      */
  68.     private void showWord()
  69.     {
  70.  int k;
  71.  for(k=0; k < mySecretWord.length; k++) {
  72.      System.out.print(mySecretWord[k]+" ");
  73.  }
  74.  System.out.println("\n# misses left = "+ (MISSES-myMisses));
  75.     }
  76.  
  77.     /**
  78.      * @return true if word has been guessed, else returne false
  79.      */
  80.  
  81.     private boolean wordGuessed()
  82.     {
  83.  int k;
  84.  for(k=0; k < mySecretWord.length; k++) {
  85.      if (mySecretWord[k] == BLANK) {
  86.   return false;
  87.      }
  88.  }
  89.  return true;
  90.     }
  91.  
  92.     /**
  93.      * plays a game of hangman. Repeated calls of this
  94.      * function will play different games (different words)
  95.      * up to some internal limit based on the number of different
  96.      * words. After all words have been used they'll be repeated
  97.      */
  98.  
  99.     public void play()
  100.     {
  101.  clear();
  102.  while (true) {
  103.      showWord();
  104.      System.out.print("guess> ");
  105.      String s = ConsoleInput.readString();
  106.      if (s.length() > 0) {      // typed something?
  107.   guess(s.charAt(0));
  108.      }
  109.      if (myMisses >= MISSES) {
  110.   System.out.println("you lose!!!");
  111.   break;
  112.      }
  113.      else if (wordGuessed()) {
  114.   System.out.println("you win!!!");
  115.   break;
  116.      }
  117.  }
  118.  myWordIndex = (myWordIndex + 1) % myWords.length;
  119.     }
  120.  
  121.     private String myWords[] = {
  122.  "apple",
  123.  "toothbrush",
  124.  "cucumber",
  125.  "strengthens",
  126.         "portfolio"
  127.     };
  128.  
  129.     private  char[] mySecretWord;       // what's shown to the user
  130.     private  int    myMisses;           // # misses so far
  131.     private  int    myWordIndex;        // which word is being guessed
  132.     private  boolean[] myLettersUsed;   // tracks letters guessed/used
  133.  
  134.     private final static char BLANK = '_';
  135.     private final static int  MISSES = 8;
  136.  
  137.     public static final void main(String args[])
  138.     {
  139.  Hangman hang = new Hangman();
  140.  hang.play();
  141.     }
  142. }
  143.  
That doesnt work:(

and the applet code now is:



Expand|Select|Wrap|Line Numbers
  1.  package hangman;
  2.  
  3. import java.awt.*;
  4. import javax.swing.*;
  5. import java.util.*;
  6. import java.lang.*;
  7. import java.applet.*;
  8. import java.awt.event.*;
  9.  
  10.  
  11. public class Main extends Applet implements ActionListener,ItemListener
  12.  
  13.     public static void main(String[] args) {
  14.         JFrame f = new JFrame("applet");
  15.         f.setSize(800, 600);
  16.         Main m = new Main();
  17.         m.Main();
  18.         f.add(m);
  19.         f.setVisible(true);
  20.     }
  21.  
  22.  Button button[]; 
  23.  TextField word,letter; 
  24.  TextField guess; 
  25.  Label label1,label2,label3; 
  26.  String alphabuttons[]; 
  27.  Panel panel1,panel2,panel3,pana,panb,panc; 
  28.  Font font; 
  29.  CheckboxGroup group; 
  30.  Checkbox beg,inte,adv; 
  31.  String begin[]; 
  32.  String inter[]; 
  33.  String advan[]; 
  34.  int lives,select; 
  35.  String answer = "        "; 
  36.  String missy; 
  37.  String buffy = "--------"; 
  38.  StringBuffer store2= new StringBuffer(buffy); 
  39.  int counta = lives; 
  40.  public void Main() 
  41.  { 
  42.   this.setLayout(new BorderLayout()); 
  43.   // sets up the array to produce the alphabet buttons on the grid 
  44.   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"}; 
  45.  
  46.   // sets up the arrays with the words to be guessed in all three stages 
  47.   begin = new String[]{"CAT","POWER","UNDER"}; 
  48.   inter = new String[]{"SECTION","VISITOR","INTENDED"}; 
  49.   advan = new String[]{"BEAUTIFUL","CHEERFUL","ENTHUSIASM"}; 
  50.  
  51.   // create and arrange panel 1 which will house the text fields and the labels 
  52.  
  53.   panel1 = new Panel(); // panel 1 will hold the two labels and text fields at the top of the applet 
  54.   add("North",panel1); 
  55.  
  56.   panel1.setLayout(new BorderLayout()); 
  57.   label1 = new Label("GUESS THE WORD"); // create the label 
  58.   font = new Font("TimesRoman",Font.ITALIC,16); 
  59.   label1.setFont(font); // set the font to the label 
  60.  
  61.   pana = new Panel(); 
  62.   panel1.add("North",pana); 
  63.   pana.add(label1); // add the label to the panel 
  64.   label1.setAlignment(label1.CENTER); // set the label to the centre 
  65.  
  66.   panb = new Panel(); 
  67.   panel1.add("Center",panb); 
  68.   word = new TextField(24); // creates the text field for the word set to 24 chars 
  69.   word.addActionListener(this); 
  70.   word.setEditable(true); // allows text to be edited 
  71.   panb.add(word,"CENTER"); // adds the text field to the panel 
  72.   label3 = new Label("Win/Lose"); 
  73.   label3.setFont(font); 
  74.   panb.add(label3); 
  75.   label1.setAlignment(label1.RIGHT); 
  76.   letter = new TextField(10); 
  77.   letter.addActionListener(this); 
  78.   panb.add(letter,"RIGHT"); 
  79.  
  80.   panc = new Panel(); 
  81.   panel1.add("South",panc); 
  82.  
  83.   label2 = new Label("How Many Goes Left"); // create the label and intiliase 
  84.   label2.setFont(font); // sets the font previously defined for label 1 
  85.   panc.add(label2); 
  86.   label2.setAlignment(label2.LEFT); // Sets the label left 
  87.  
  88.   guess = new TextField(3); // initialises the guess text field to 3 chars 
  89.   guess.addActionListener(this); 
  90.   panc.add(guess); 
  91.  
  92.   // create and arrange panel 2 which will house the alphabet grid 
  93.  
  94.   panel2 = new Panel(); 
  95.   add("Center",panel2); 
  96.  
  97.   // set gridLayout for panel2(int rows , int col, int hgap, int vgap 
  98.   panel2.setLayout(new GridLayout(4,7,3,3)); 
  99.  
  100.   button = new Button[28]; 
  101.  
  102.   for (int i=0;i<26;i+=1) 
  103.   { 
  104.    // set the buttons of the alphabet up using the array 
  105.    button[i] = new Button(alphabuttons[i]); 
  106.    panel2.add(button[i]); 
  107.    button[i].addActionListener(this); 
  108.   } 
  109.  
  110.   button[26] = new Button("Answer"); // creates the answer button 
  111.   button[26].setForeground(Color.green); 
  112.   button[26].addActionListener(this); 
  113.  
  114.   button[27] = new Button("Reset"); // creates the reset button 
  115.   button[27].setForeground(Color.yellow); 
  116.   button[27].addActionListener(this); 
  117.  
  118.   // add the remaining buttons to panel 2 
  119.   panel2.add(button[26]); 
  120.   panel2.add(button[27]); 
  121.  
  122.   // create and arrange panel 3 which will house the radio buttons for the level 
  123.  
  124.   panel3 = new Panel(); 
  125.   add("South",panel3); 
  126.  
  127.   group = new CheckboxGroup(); 
  128.  
  129.   // create the radio buttons 
  130.  
  131.   beg = new Checkbox("Beginner",group,false); 
  132.   beg.addItemListener(this); 
  133.  
  134.   inte = new Checkbox("Intermediate",group,false); 
  135.   inte.addItemListener(this); 
  136.  
  137.   adv = new Checkbox("Advanced",group,false); 
  138.   adv.addItemListener(this); 
  139.  
  140.   // add the radio buttons to panel3 
  141.   panel3.add(beg); 
  142.   panel3.add(inte); 
  143.   panel3.add(adv); 
  144.  } 
  145.  
  146.  
  147.  public void actionPerformed(ActionEvent e) 
  148.  { 
  149.   int j=0; 
  150.   int count=0; 
  151.   int k=0; 
  152.   String currentLetter; 
  153.   String temp=""; 
  154.   StringBuffer Sanswer = new StringBuffer(answer); 
  155.   Sanswer.setLength(answer.length()); 
  156.   boolean hang = false; 
  157.   boolean correct=true; 
  158.   store2.setLength(answer.length()); 
  159.   count = Sanswer.length(); 
  160.  
  161.  
  162.   for(k=0;k<=25;k+=1) 
  163.   { 
  164.    if (e.getSource()==button[k]) 
  165.    { 
  166.     currentLetter = (button[k].getLabel()); 
  167.     letter.setText(currentLetter); 
  168.  
  169.     for (j=0;j<count;j++) 
  170.     { 
  171.      //cycles through the letters in the word 
  172.      if (currentLetter.equals(String.valueOf(answer.charAt(j)))) 
  173.      { 
  174.       // replaces the letter if it is within the word 
  175.       store2.replace(j,j+1,currentLetter); 
  176.       temp=store2.toString(); 
  177.       lives+=1; // does not decrement the lives 
  178.       if(temp.equals(word.getText())) 
  179.       { 
  180.        letter.setText("YOU WIN"); 
  181.        letter.setForeground(Color.blue); 
  182.       } 
  183.      } 
  184.  
  185.     } 
  186.     lives-=1; // decrements the lives 
  187.     guess.setText(Integer.toString(lives)); 
  188.     word.setText(store2.toString()); 
  189.  
  190.     if (lives<1) 
  191.     { 
  192.      letter.setText("YOU LOSE"); 
  193.      letter.setForeground(Color.red); 
  194.     } 
  195.  
  196.    } 
  197.   } 
  198.  
  199.   if(e.getSource()==button[26]) // answer button 
  200.   { 
  201.  
  202.    word.setText(answer); 
  203.    word.setForeground(Color.darkGray); 
  204.    letter.setText("Unlucky"); 
  205.   } 
  206.  
  207.   if(e.getSource()==button[27]); 
  208.   { 
  209.   /* word.setText(null); 
  210.    guess.setText(null); 
  211.    letter.setText("delete"); 
  212.    for(int x=0;x<9;x++) 
  213.    { 
  214.     store2.replace(x,x+1,"-"); 
  215.    } 
  216.    count = Sanswer.length(); 
  217.    count=answer.length(); */ 
  218.   } 
  219.  } // ends actionPerformed 
  220.  
  221.  // invoked with the radio buttons are selected or deselected 
  222.  
  223.  public void itemStateChanged(ItemEvent e) 
  224.  { 
  225.   // checks if the button is pressed 
  226.   select = (int)(java.lang.Math.random()*4); // random number generator for the arrays 
  227.   StringBuffer miss = new StringBuffer(answer); 
  228.   if (e.getSource()==beg) 
  229.   { 
  230.    if(beg.getState()==true) 
  231.    { 
  232.     lives = 9; 
  233.     answer = (begin[select]); 
  234.  
  235.     for (int y=0;y<answer.length();y++) 
  236.     { 
  237.      miss.replace(y,y+1,"-"); 
  238.     } 
  239.     word.setText(miss.toString()); 
  240.     missy = miss.toString(); 
  241.     word.setForeground(Color.black); 
  242.     guess.setText(Integer.toString(lives)); 
  243.    } 
  244.   } 
  245.   else if (e.getSource()==inte) 
  246.   { 
  247.    if(inte.getState()==true) 
  248.    { 
  249.     lives = 8; 
  250.     answer = (inter[select]); 
  251.     for (int y=0;y<answer.length();y++) 
  252.     { 
  253.      miss.replace(y,y+1,"-"); 
  254.     } 
  255.     word.setText(miss.toString()); 
  256.     missy = miss.toString(); 
  257.     word.setForeground(Color.black); 
  258.     guess.setText(Integer.toString(lives)); 
  259.    } 
  260.   } 
  261.   else if(e.getSource()==adv) 
  262.   { 
  263.    if(adv.getState()==true) 
  264.    { 
  265.     lives = 7; 
  266.     answer = (advan[select]); 
  267.      for (int y=0;y<answer.length();y++) 
  268.     { 
  269.      miss.replace(y,y+1,"-"); 
  270.     } 
  271.     word.setText(miss.toString()); 
  272.     missy = miss.toString(); 
  273.     word.setForeground(Color.black); 
  274.     guess.setText(Integer.toString(lives)); 
  275.    } 
  276.   } 
  277.  } // ends itemStateChanged for the radio buttons 
  278.  
  279. }//ends the class
Oct 10 '07 #28
r035198x
13,262 MVP
Those are two different programs. So which one do you want to work with?

P.S It will have to be one that you understand because you will be making the changes to make it work.
Oct 10 '07 #29
araujo2nd
24 New Member
any, whichever 1 i could do quicker i suppose
Oct 10 '07 #30
r035198x
13,262 MVP
any, whichever 1 i could do quicker i suppose
Well it was supposed to be your choice ...

Since you're keen on using the second program (which your friend helped you make) ....

First it needs quite a lot of cleaning up:
2.) Import only the packages you need to use. i.e Remove the imports for java.lang (already imported for you), leave only the imports for
java.awt and java.awt.event. You haven't used the other packages in your program (yet)
2.) Constructors do not return a value (refer to your textbook) so you need to change that.
3.) Your Frame does not have a size. It probably shows as a very small icon when you run it. set it's size to something reasonable e.g (500, 500)
4.) You are creating a Java application so remove the extends applet and put extends Frame instead.
5.) Your Frame does not seem to have closing functionality.
You forgot
Expand|Select|Wrap|Line Numbers
  1. addWindowListener(new WindowAdapter() {
  2.             public void windowClosing(WindowEvent w) {
  3.                 System.exit(0);
  4.             }
  5.  
  6.         });
in the constructor
6.) I don't see any setVisible(true) at the end of that constructor as well so that Frame will not show.


See if you can clean those things up for now.
Oct 10 '07 #31
araujo2nd
24 New Member
ok i think i fixed it up

the beginning code is now

Expand|Select|Wrap|Line Numbers
  1. package hangman;
  2. package hangman;
  3.  
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. import javax.swing.JFrame;
  7.  
  8.  
  9. public class Main extends Frame implements ActionListener,ItemListener
  10.  
  11.     public static void main(String[] args) {
  12.         JFrame f = new JFrame();
  13.         f.setSize(500, 500);
  14.         Main m = new Main();
  15.         m.Main();
  16.         f.add(m);
  17.         f.setVisible(true);
  18.     }
  19.  
Is that a bit better?
Oct 10 '07 #32
r035198x
13,262 MVP
ok i think i fixed it up

the beginning code is now

Expand|Select|Wrap|Line Numbers
  1. package hangman;
package hangman;

import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;


public class Main extends Frame implements ActionListener,ItemListener
{

public static void main(String[] args) {
JFrame f = new JFrame();
f.setSize(500, 500);
Main m = new Main();
m.Main();
f.add(m);
f.setVisible(true);
}

Is that a bit better?
Not really.
When you extend Frame, Main becomes a Frame itself so creating both a Frame and a Main object is actually creating two Frames. you should create a Main object only.
Oct 10 '07 #33
araujo2nd
24 New Member
huh??
ok im confused:(, i cant even get the first part right, how the heck am i gonna get the program running by monday... im really bad at java so please forgive me. Do you think the other program i did would be easier because this 1 does seem very sophisticated and im not sure im at the level to do such sophisticated programs at the moment.
Oct 10 '07 #34
r035198x
13,262 MVP
huh??
ok im confused:(, i cant even get the first part right, how the heck am i gonna get the program running by monday... im really bad at java so please forgive me. Do you think the other program i did would be easier because this 1 does seem very sophisticated and im not sure im at the level to do such sophisticated programs at the moment.
Well, we were going very well yesterday until you decided to switch back to this version of the program and lost a full day. It's obvious you haven't gone through some basics tutorials yet and that's what I was going to point you to bit by bit until you get it working.
Of course you can revert to your version of the program and still be able to do it by Monday.
Willingness to learn and read are the important things here.
If you want to revert to your old version, get the part to read all the words from a file into an ArrayList working first and then post a version of your intended hangman algorithm.

P.S A lesson worth learning from all this is "Never postpone for tomorrow what you can do today"
Oct 10 '07 #35
araujo2nd
24 New Member
okay i have a plan, the program which my teacher helped me with (the first 1). just gets 1 error when compiled, its in the line: "String s = ConsoleInput.readString();"

Pls could u tell me how i could fix that error, once i have fixed that i promise that i will be willing to learn how to improve the program so that it does exactly what i would like it to. its already 9:37pm here and im willing to stay up till 11 tonight
Oct 10 '07 #36
dav3
94 New Member
okay i have a plan, the program which my teacher helped me with (the first 1). just gets 1 error when compiled, its in the line: "String s = ConsoleInput.readString();"

Pls could u tell me how i could fix that error, once i have fixed that i promise that i will be willing to learn how to improve the program so that it does exactly what i would like it to. its already 9:37pm here and im willing to stay up till 11 tonight
what is the error?

Could you use something like
Expand|Select|Wrap|Line Numbers
  1. String name_of_string = stdin.readline();
Oct 11 '07 #37
araujo2nd
24 New Member
the error i get is " Hangman.java:115: cannot find symbol
symbol : variable ConsoleInput
location: class Hangman
String s = ConsoleInput.readString(); "

I tried the code u gave me and i got "Hangman.java:115: cannot find symbol
symbol : variable stdin
location: class Hangman
String s = stdin.readline(); "
Oct 11 '07 #38
r035198x
13,262 MVP
the error i get is " Hangman.java:115: cannot find symbol
symbol : variable ConsoleInput
location: class Hangman
String s = ConsoleInput.readString(); "

I tried the code u gave me and i got "Hangman.java:115: cannot find symbol
symbol : variable stdin
location: class Hangman
String s = stdin.readline(); "
If you are using 1.5 or higher you can use the Scanner class to get input from the user.

Expand|Select|Wrap|Line Numbers
  1. Scanner input = new Scanner(System.in);
  2. String s = input.nextLine();
You should open the API docs for it and see how to use it.
Oct 11 '07 #39
araujo2nd
24 New Member
oka i have worked on it a bit, could i pls send some1 my project so that they can edit it and pls help me, when i click new game i would like it o reset everything, when i click ok i would like the text box called output to be filled:( pls help!! ill do anything, im so desperate im willing to pay a programmer to come help me
Oct 12 '07 #40
r035198x
13,262 MVP
oka i have worked on it a bit, could i pls send some1 my project so that they can edit it and pls help me, when i click new game i would like it o reset everything, when i click ok i would like the text box called output to be filled:( pls help!! ill do anything, im so desperate im willing to pay a programmer to come help me
If you are looking for someone to pay try www.rentacoder.com.
If you want free help here, just post your code (wrapped in code tags) and explain where you are getting problems.
Oct 12 '07 #41
araujo2nd
24 New Member
If you are looking for someone to pay try www.rentacoder.com.
If you want free help here, just post your code (wrapped in code tags) and explain where you are getting problems.
the thing is that im not have problems, i just cant get the proigram to do wat i would like it to do, im not advanced enough. Since its in netbeans i have 2 programs in the project, the only way some1 could help me is if i send u the project file and u open in in netbeans then edit that for me. i would very much appreciate it
Oct 13 '07 #42
r035198x
13,262 MVP
the thing is that im not have problems, i just cant get the proigram to do wat i would like it to do, im not advanced enough. Since its in netbeans i have 2 programs in the project, the only way some1 could help me is if i send u the project file and u open in in netbeans then edit that for me. i would very much appreciate it
You realize of course that you are going no where with this.
You heven't been paying attention to the responses I was giving at all.
Oct 13 '07 #43
araujo2nd
24 New Member
You realize of course that you are going no where with this.
You heven't been paying attention to the responses I was giving at all.
ok well sir i just got a code for many different classes. I have saved them as txt files and was wondering if u could pls help me add them all into a java application. It is a hangman appet but the last class called HangmanAppletImpl has a thing at the bottom showing how to change it into application, pls help me..i was paying attention its just that u do not understand how urgent this is, i did not have time to start the program from scratch, i just do not have the skills for that, sorry
Oct 13 '07 #44
r035198x
13,262 MVP
ok well sir i just got a code for many different classes. I have saved them as txt files and was wondering if u could pls help me add them all into a java application. It is a hangman appet but the last class called HangmanAppletImpl has a thing at the bottom showing how to change it into application, pls help me..i was paying attention its just that u do not understand how urgent this is, i did not have time to start the program from scratch, i just do not have the skills for that, sorry
The reason for the assignment was to give you the skill to do this yourself. If you had started early enough and followed my advice, you'd have the program working by now and also you'd be understanding every line of code in it.
Instead you resorted to code hunting which has left you none the wiser.
Now you want us to help you piece together the code you've hunted?
Oct 13 '07 #45
JosAH
11,448 Recognized Expert MVP
The probability that a posted problem is successfully solved is inversely proportional
to the number of replies in the thread.

kind regards,

Jos
Oct 13 '07 #46
r035198x
13,262 MVP
The probability that a posted problem is successfully solved is inversely proportional
to the number of replies in the thread.

kind regards,

Jos
Wise words indeed.
Oct 13 '07 #47
JosAH
11,448 Recognized Expert MVP
Wise words indeed.
I know and you just lowered chances that this thread terminates successfully ;-)

kind regards,

Jos (<--- oops! I did so too ;-)
Oct 13 '07 #48

Sign in to post your reply or Sign up for a free account.

Similar topics

1
3095
by: GM | last post by:
Hello. I am starting my 7th week of an intro to programming class and have a problem I have been working on. My instructor said we could look for source code on the internet and use it as long as...
1
1429
by: Brent W. Hughes | last post by:
I recently downloaded HelpMaker for free, but there was no documentation with it. Does someone know where I can find such documentation? Or, alternatively, where I can find another good free Help...
2
4664
by: atreide_son | last post by:
hello all... yes i'm a newbie and i need help. i have an assignment due for class, but I don't know i'm stuck and can't get out from under myself. here's the focus of the program: Write...
6
1452
by: k1ckthem1dget | last post by:
I dont even know how to do this program can someone help me. I am to create a program that when (1) Given the following: · A list of 11 integers; 1,2,3,3,3,2,2,1,3,4,5 ...
4
2586
by: princessfrost | last post by:
Hi! I was wondering if someone could please help me with a hangman program that I have to do. I have some ideas, but really don't know what to do or where to start. My program needs to be:...
3
2936
by: kaka_hunter | last post by:
#include <iostream> #include <fstream> using namespace std; const int max_tries=7; int earnings=0; int wordnum; void getword () { ifstream fin;
2
1770
by: ilogboy | last post by:
Below is a copy of my Hangman project... I am missing a couple more functions and I don't know how to enhance it... Here are the things I want it to do: 1) I want it to determine the scores for...
5
4176
by: av3rage | last post by:
I have never done any programming in my life but I have decided to go into engineering and in doing so we have to take this intro to programming course and I am pretty clueless. I am starting to get...
25
3930
by: yottabyte | last post by:
Hey, I just started programming this September and I have an assignment in which I am supposed to create a hangman game. This is my first post on this forum so forgive me for any "noob" mistakes Now...
0
7094
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7173
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7305
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5427
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3066
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
259
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.