Results 1 to 4 of 4
  1. #1
    Bremin is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default Need help to write Hangman

    Hello guys, i just registered. I need algorithm to write program which plays Hangman with user in DrJava. Could anyone help me how can i ?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need help to write Hangman

    This post shows no evidence of your current efforts, nor does it show a specific answerable question. You're going to have to do better if you want to increase your chances of getting help here. You would do well to read the link below on how to ask smart questions.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,763
    Blog Entries
    7
    Rep Power
    21

    Default Re: Need help to write Hangman

    That is going to be a 'knitting' program, i.e. everything crammed in one huge method (particularly your main( ... ) method). I wrote a hangman class once; it has six small methods (all less than 10 lines of code). I'm willing to show it to you after you've shown at least a bit of design effort; all you have now is a random selection of a word (in a quite clumsy way, I must say). Your turn.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Bremin is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default Re: Need help to write Hangman

    Ok, i have wrote:

    import java.util.Scanner;
    import java.lang.*;
    public class Hangman{
    public static void main(String[]args){
    Scanner scn =new Scanner(System.in);
    System.out.println("Would you like to play Hangman. Press y to play n to quit!!!");
    String ch="";
    int length=ch.length();


    String start = scn.nextLine();
    if(start.equals("N") || start.equals("n")){
    System.exit(1);
    }
    else if(start.equals("Y") || start.equals("y")){

    int series=(int)(Math.random()*6)+1;
    while(start.equals("Y") || start.equals("y")){
    switch(series){
    case 1:
    ch=("thecloser"); break;

    case 2:
    ch=("survivor"); break;

    case 3:
    ch=("merlin"); break;

    case 4:
    ch=("house"); break;

    case 5:
    ch=("simpsons"); break;

    case 6:
    ch=("dexter"); break;

    case 7:
    ch=("friends"); break;
    }

    length=ch.length();

    for(int i=1; i<=length; i++){
    System.out.print("_ ");

    StringBuffer guess=new StringBuffer(length);
    String g2 = "";
    String guessCh="";
    System.out.println(" guess");
    guessCh=scn.next();
    if(ch.charAt(i) == guessCh.charAt(0) )
    {
    g2 = g2+ch.charAt(i);
    guess.setCharAt(2*i,guessCh.charAt(0));

    }
    g2 ="";
    System.out.print(guess.toString());
    }
    }
    System.out.println("(You have 10 tries remaining)");
    System.out.println(length+" letter");
    System.out.println("Please enter your guess");
    String guess=scn.next();
    }}}
    But in output it shows this:
    http://imageupload.org/en/file/214864/adsapz.png.html

    i fixed it, too. But this crash shows up now :/
    http://imageupload.org/en/file/214882/adsapz.png.html
    for this code
    import java.util.Scanner;
    import java.lang.*;
    public class Hangman{
    public static void main(String[]args){
    Scanner scn =new Scanner(System.in);
    System.out.println("Would you like to play Hangman. Press y to play n to quit!!!");
    String ch="";
    int length=ch.length();
    int i=0;

    String start = scn.nextLine();
    if(start.equals("N") || start.equals("n")){
    System.exit(1);
    }
    else if(start.equals("Y") || start.equals("y")){

    int series=(int)(Math.random()*6)+1;
    while(start.equals("Y") || start.equals("y")){
    switch(series){
    case 1:
    ch=("thecloser"); break;

    case 2:
    ch=("survivor"); break;

    case 3:
    ch=("merlin"); break;

    case 4:
    ch=("house"); break;

    case 5:
    ch=("simpsons"); break;

    case 6:
    ch=("dexter"); break;

    case 7:
    ch=("friends"); break;
    }

    length=ch.length();
    for(i=1; i<=length; i++){
    System.out.print("_ ");
    }
    StringBuffer guess=new StringBuffer(length);
    String g2 = "";
    String guessCh="";
    System.out.println(" guess");
    guessCh=scn.next();
    if(ch.charAt(i) == guessCh.charAt(0) )
    {
    g2 = g2+ch.charAt(i);
    guess.setCharAt(2*i,guessCh.charAt(0));

    }
    g2 ="";
    System.out.print(guess.toString());

    }
    System.out.println("(You have 10 tries remaining)");
    System.out.println(length+" letter");
    System.out.println("Please enter your guess");
    String guess=scn.next();
    }}}
    Last edited by Bremin; 04-11-2012 at 06:59 PM.

Similar Threads

  1. Hangman with GUİ
    By KSBeyaz in forum New To Java
    Replies: 9
    Last Post: 11-20-2011, 07:45 PM
  2. Hangman
    By Feriscool in forum New To Java
    Replies: 23
    Last Post: 05-17-2011, 07:54 PM
  3. trying to write a program for hangman word game
    By durdanto in forum New To Java
    Replies: 1
    Last Post: 02-12-2011, 03:53 AM
  4. hangman
    By coltragon in forum New To Java
    Replies: 2
    Last Post: 01-16-2010, 10:56 AM
  5. Hangman Help!!!
    By chinasome in forum New To Java
    Replies: 5
    Last Post: 11-08-2008, 03:30 AM

Posting Permissions

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