Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: hangman 2 player game

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default hangman 2 player game

    I am creating a hangman code and i am done the basic code for my summative but i just need to add some small parts that i don't know how to do.

    So the game i am creating is 2 player, first player enters the word and second player guesses. i made the code but i don't know how to restrict the first player from adding random characters into the secret word because for my project the word can only contain letters. so i need help making the code so the player 1 can only enter letters.
    my code is below:



    /**
    * Auto Generated Java Class.
    */
    import java.util.Scanner;
    public class Hangman {


    public static void main(String[] args) {
    Scanner input= new Scanner(System.in);

    String player1,player2;
    int wrongguessamount = 0; //keeps the count of the amount of wrong guesses
    int correctguessamount = 0; //keeps count of the amount of correct guesses
    int maxincorrect=6;//makes sure the user cant guess more than 6 times

    String a,b,c,d,e,f,g,h,i,j,k,l,m,n,p,q,r,s,t,u,v,w,x,y,z;
    System.out.println("please enter a phrase player 1");
    player1=input.nextLine();//player 1 enters the secret word

    player1=player1.toLowerCase();//secret word get lower cased


    for (int ii=0;ii < player1.length();ii++)
    {
    System.out.print("*");//the secret word is hidden with stars
    }
    char[] lettersreveal = new char[player1.length()];
    for (int aa = 0; aa<lettersreveal.length; aa++) {
    lettersreveal[aa] = '*';
    }

    System.out.println("player2 guess a character");

    while ( wrongguessamount< maxincorrect&& correctguessamount<player1.length() )//player 2 starts guessing letters

    {
    String guess=input.nextLine();

    int match = player1.indexOf(guess);//it takes the guessed letter and tries to find it in the secret word
    if( match == -1){//if the guess is wrong it tells the user to guess again
    System.out.println("Wrong guess, Try again");
    wrongguessamount++;
    }

    if (match != -1) {
    System.out.println("Correct guess");
    lettersreveal[match] = guess.charAt(0); //matches and reveals the character guessed by player 2
    correctguessamount++;

    do{
    match = player1.indexOf(guess, match+1);//as long as guess is a match it will continue unitl done

    if (match != -1) {
    lettersreveal[match] = guess.charAt(0);//if guess is correct it matches to word
    correctguessamount++;




    }//finsihes last if statement
    }//closes do statement

    while (match != -1);//while statement for the do statement

    }//finishes first if statement


    System.out.println(lettersreveal);


    if( wrongguessamount==maxincorrect){
    System.out.println("Sorry, you have reached 6 trys, GAME OVER");
    }



    }//finishes first while statementl
    if(correctguessamount == player1.length()){
    System.out.println("GOOD JOB, YOU GUESSED THE WORD");
    }

    }//closes main
    }//closes class


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: hangman 2 player game

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    making the code so the player 1 can only enter letters.
    Use a loop to look at each letter in the word the user entered and test if each is a letter.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: hangman 2 player game

    iam not sure on how to do that can u give me an example of how it would be?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: hangman 2 player game

    begin loop through all characters in word
    get next char from word
    test if char is valid
    if not valid reject word and exit loop
    end loop

    See the String class for a method to get the next character.
    The Character class has methods for testing characters.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    asad1221 (May 27th, 2014)

Similar Threads

  1. Hangman Game
    By JohanMartin in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 18th, 2014, 06:25 AM
  2. Hangman Game
    By connorlm3 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 7th, 2013, 12:50 PM
  3. hangman game
    By candoa in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 10th, 2012, 10:10 PM
  4. java game bullets hit player 1 but not player 2
    By ajakking789 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 22nd, 2011, 08:19 AM
  5. java game, both players move for player 2 but not player 1
    By ajakking789 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 21st, 2011, 12:52 PM