www.webdeveloper.com
Results 1 to 4 of 4

Thread: help for school project URGENT!

  1. #1
    Join Date
    Jan 2006
    Posts
    4

    Exclamation help for school project URGENT!

    basicly i need to make a basic hangman applet and i dont even know where to start. this is what my teacher said.

    using the decision making statments that we have learned so far, create a program that asks the user to guess a word. they have 6 chances to guess the word before they are hung. give them a chance to play two different games
    can someone please help this is due soon.

  2. #2
    Join Date
    Oct 2004
    Location
    The very depths of programming hell
    Posts
    828
    depends what decision making statements you have learned so far...?

    from the sounds of it you just want a simple, non-graphical application to ask users to guess the word itself rather than a full-scale hangman application... is that right, or would you like a complete hangman program which lets you guess letters rather than entire words?
    >I suck at this game, can you give me some pointers?

    >> 0x278AB4FF, 0xB5822BB4, 0xAD6E673D.

    > I hate you.


  3. #3
    Join Date
    Oct 2004
    Location
    The very depths of programming hell
    Posts
    828
    If it is just a completely non-graphical guessing game you're after, you can simply do something like this:

    PHP Code:
    class Hangman
    {
    String[] words={"word1","word2"}; //array of words to use
    BufferedReader user_in;

    public static 
    void main(String[] args)
     {
     
    String word_used=words[Math.floor(Math.random()*words.length)];
     try
      {
      
    user_in=new BufferedReader(new InputStreamReader(System.in));
      
     
      for(
    int i=0;i<6;i++)
       {
       
    String guess=user_in.readLine();
       
       if(
    guess.equals(word_used))
        {
        
    System.out.println("Correct!");
        
    System.exit(0);
        }
       else
        {
        
    System.out.print((i!=5)?"No, try again\n":"No, ");
        }
       }
      
    System.out.print("You're hung!");
      
    System.exit(0);
      }
     catch(
    IOException e)
      {
      
    e.printStackTrace();
      }
     }

    Sorry for any minor errors in that, I just hurriedly put that together in the quick reply box
    >I suck at this game, can you give me some pointers?

    >> 0x278AB4FF, 0xB5822BB4, 0xAD6E673D.

    > I hate you.


  4. #4
    Join Date
    Jan 2006
    Posts
    4
    i need it to look like this:
    http://img105.imageshack.us/img105/7184/hgjhg0gt.png

    PHP Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;

    public class 
    hangman extends Applet implements ActionListener {

    //this is the used letter array
     
    private boolean usd[] = new boolean[26];

     private 
    String guessme;

     private 
    int numguesses=0;

     private 
    boolean finished false;

     private 
    boolean won false;


     private 
    Button a[];
     

            public 
    void init() {
                
    int i;
                
    StringBuffer buffer;
                
    //            setLayout( new GridLayout( 2,13) );
                
                
                
    = new Button[26];
                
                
    // create all 26 buttons
                
    for (0<26i++) {
                    
    buffer = new StringBuffer();
                    
    buffer.append((char)(i+65));
                    
    a[i] = new Button(buffer.toString());
                    
    a[i].addActionListenerthis );
                    
    add(a[i]);
                }
                    
    // make the guessed word uppercase
                    
    guessme=getParameter("wrd").toUpperCase();

        }    


        public 
    void paint(Graphics g) {
                    
    //draw gallows and rope
                    
    setBackground(Color.white);
                    
    g.fillRect(1025015020);
                    
    g.fillRect(40,70,10,200);
                    
    g.fillRect(40,70,60,10);
                    
    g.setColor(Color.yellow);
                    
    g.fillRect(95,70,5,25);


             
    g.setColor(Color.orange);

             if (
    numguesses >=)
                    
    g.drawOval(82,95,30,30);

             
    g.setColor(Color.green);

             if (
    numguesses >=)
                    
    g.drawLine(97,125,97,150);

             if (
    numguesses >=)
                    
    g.drawLine(97,150,117,183);

             if (
    numguesses >=)
                    
    g.drawLine(97,150,77,183);

             if (
    numguesses >=)
                    
    g.drawLine(97,125,117,135);

             if (
    numguesses >=)
                    
    g.drawLine(97,125,77,135);

                    
    StringBuffer st = new StringBuffer();

                    for (
    int l=0l<=25l++) {
                            if (
    usd[l]) st.append((char)(l+65));
                            else 
    st.append(".");
                    }
                    
    g.setColor(Color.blue);

                    
    Font f = new Font("Courier",Font.ITALIC,14);
                    
                    
    g.setFont(f);
                    
    g.drawString(st.toString(),25,285);

                    
    StringBuffer guessed = new StringBuffer();

                    
    Font ff = new Font("Courier",Font.BOLD,24);
                    
    g.setColor(Color.black);

                    
    g.setFont(ff);

                    for (
    int mm=0;mm<guessme.length();mm++) {
                            if (
    usd[(int)guessme.charAt(mm)-65])
                                    
    guessed.append(guessme.charAt(mm));
                            else
                                    
    guessed.append(".");
                            }
                                              
                    
    g.drawString(guessed.toString(),75,230);


     
                 if (
    numguesses >=6) {
                    
    g.setColor(Color.white);
                    
    g.fillRect(7020020030);
                    
    g.setColor(Color.black);
                    
    g.drawString(guessme.toString(),75,230);
                    
    Font fff = new Font("Helvetica",Font.BOLD,36);
                    
    g.setFont(fff);

                    
    g.setColor(Color.red);
                    
    g.drawString("You lose!",200,100);

                    
    finished true;
                    }

                 if (
    won) {
                    
    Font fff = new Font("Helvetica",Font.BOLD,36);
                    
    g.setFont(fff);

    //                Color red=new Color.red
                    
    g.setColor(Color.red);

                    
    g.drawString("You Win!",200,100);
                    
    finished true;
                    }





        }


     public 
    void rer(int lett) {

       if (!
    finished) {

            
    boolean found=false;
            
    boolean www=false;

            if (!
    usd[lett]) {
                    for (
    int mm =0;mm<guessme.length();mm++) {
                            if (
    guessme.charAt(mm)==((char)(lett+65))) found=true;
                    }
                    if (!
    foundnumguesses++;

            }
            
            
    usd[lett] = true;

            for (
    int mm =0;mm<guessme.length();mm++) {
                  if (!
    usd[(int)(guessme.charAt(mm))-65]) www=true;
            }

            if (!
    wwwwon=true;        


            
    repaint();

       }

     }


        public 
    void actionPerformedActionEvent ev) {
          
    int i;
          for (
    026i++) {
              if (
    ev.getSource() == a[i]) { rer(i); }
          }




    the only problem is that i cant figure out where the words go and whenever i load it, it says nonintieted.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles