Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help, Word Guess Game Looping Problems

    import java.util.Scanner;
    import java.util.Arrays;
    /***********************************************************
    * WordGuess game is a Hangman like game for two (or more)
    * players. This is a two player version
    *The game of Hangman involves one player making a word and a second * player guessing the word by guessing each letter in turn. If the guessing *player cannot get it in 6 (or so depending on the version) tries, the other *player wins. If the guesser gets it in the requisite number of tries, then the *guesser wins. This is a variant of the Hangman game involving two players *guessing against each other.
    *
    *
    * @author JavaFish
    * @I can't seem to get my loop working....it's not behaving like i want it to. ***********************************************************/
    public class WordGuess
    {
    private Scanner kb; // use for all keyboard entry
    private Player player1; // first player
    private Player player2; // second player
    private WordBank wordList; // the dictionary to use




    /* alternate instance variable for the players
    deactivate the individual players if you choose to
    use this version. */
    // private Player[] players;

    /*********************************************************
    * Explicit value constructor that builds a random dictioinary
    *
    * @param player1 The name of player1
    * @param player2 The name of player2
    ********************************************************/
    public WordGuess(String player1, String player2)
    {
    wordList = new WordBank();
    wordList.getWord();

    this.player1 = new Player(player1);
    this.player2 = new Player(player2);

    }

    /*********************************************************
    * Explicit value constructor that builds a seeded dictioinary
    *
    * @param player1 The name of player1
    * @param player2 The name of player2
    * @param seed The seed to pass to WordBank constructor
    ********************************************************/
    public WordGuess(String player1, String player2, long seed)
    {
    wordList = new WordBank();
    wordList.getWord();

    this.player1 = new Player(player1);
    this.player2 = new Player(player2);

    }

    /*********************************************************
    * play game plays the game until one player reaches 10 wins
    *********************************************************/
    public void playGame()
    {

    boolean loopControl;
    boolean loopControlOne;
    boolean loopControlTwo;
    boolean playerTurn;
    boolean startOver;
    String playerInput;
    char playerCheck;
    String checkProgress;
    String checkGuess;
    String word;

    char[] dashes;
    char[] wordLetters;
    char[] lettersGuessed;

    kb = new Scanner(System.in);




    startOver = true;
    loopControl = true;
    playerTurn = true;

    System.out.printf("Welcome to Word Guess %s and %s\n\n", player1.getName(), player2.getName());

    if (loopControl)
    {
    startOver = true;
    word = wordList.getWord();

    dashes = new char[word.length()];
    wordLetters = new char[word.length()];
    lettersGuessed = new char[6];

    for(int i = 0; i < word.length(); i++)
    {
    dashes[i] = ('-');
    wordLetters[i] = word.charAt(i);
    }

    System.out.print('-');


    if(startOver)
    {
    checkGuess = new String(lettersGuessed);
    checkProgress = new String(dashes);
    loopControlOne = true;
    loopControlTwo = true;


    if(playerTurn)
    {
    loopControlOne = false;
    }


    if (loopControlOne)

    checkGuess = new String(lettersGuessed);
    checkProgress = new String(dashes);
    playerTurn = true;

    {
    for(int i = 0; i < word.length(); i++)
    {
    System.out.print(dashes[i]);
    }

    System.out.print("\nUsed letters: ");


    for(int i = 0; i < 6; i++)
    {
    if(lettersGuessed[i] != 0)
    {
    System.out.print(lettersGuessed[i] + " ");
    }
    }

    System.out.printf("\n%s, guess a letter. ", player1.getName());

    // Gets the players input and assigns it to variable playerInput
    playerInput = kb.nextLine();
    playerCheck = playerInput.charAt(0);
    playerInput = new String("" + playerCheck);

    System.out.println();

    for(int i = 0; i < word.length(); i++)
    {
    if(playerCheck == wordLetters[i])
    {
    dashes[i] = playerCheck;
    }
    }



    if(checkGuess.contains(playerInput))
    {
    System.out.printf("%s is already guessed. %s, you lose your turn.\n",
    playerInput, player1.getName());
    loopControlOne = false;
    }


    else if(word.contains(playerInput))
    {
    for(int i = 0; i < word.length(); i++)
    {
    if(wordLetters[i] == playerCheck)
    {
    dashes[i] = playerCheck;
    }
    }

    if(checkProgress.contains("-"))
    {
    System.out.printf("Correct: %s go again.\n", player1.getName());

    for(int i = 0; i < 6; i++)
    {
    if(lettersGuessed[i] != 0)
    {
    lettersGuessed[i] = playerCheck;
    i = 6;
    }
    Arrays.sort(lettersGuessed);
    }

    }

    else
    {
    System.out.printf("Correct. %s you win a point!\n", player1.getName());
    System.out.println(word);
    player1.iWon();
    System.out.printf("%s: %d\t%s: %d\n\n", player1.getName(), player1.getScore(), player2.getName(), player2.getScore());
    startOver = false;

    if(player2.getScore() == 5 || player1.getScore() == 5)
    {
    System.out.print("Game over. %s wins!\n");
    loopControl = false;
    loopControlTwo = false;

    }
    // Set up new loop that clears everything.

    }
    }


    else
    {
    System.out.printf("Incorrect. %s, you lose your turn.\n", player1.getName());
    loopControlOne = false;

    for(int i = 0; i < 6; i++)
    {
    if(lettersGuessed[i] == 0)
    {
    lettersGuessed[i] = playerCheck;
    i = 6;
    }
    Arrays.sort(lettersGuessed);
    }
    }
    }


    if (loopControlTwo)
    {
    checkGuess = new String(lettersGuessed);
    checkProgress = new String(dashes);
    playerTurn = true;


    for(int i = 0; i < word.length(); i++)
    {
    System.out.print(dashes[i]);
    }

    System.out.print("\nUsed letters: ");

    for(int i = 0; i < 6; i++)
    {
    System.out.print(lettersGuessed[i] + " ");
    }


    System.out.printf("%s, guess a letter. ", player2.getName());

    // Gets the players input and assigns it to variable playerInput
    playerInput = kb.nextLine();
    playerCheck = playerInput.charAt(0);
    playerInput = new String("" + playerCheck);

    for(int i = 0; i < word.length(); i++)
    {
    if(playerCheck == wordLetters[i])
    {
    dashes[i] = playerCheck;
    }
    }


    if(checkGuess.contains(playerInput))
    {
    System.out.printf("%s is already guessed. %s, you lose your turn.\n",
    playerInput, player1.getName());
    loopControlTwo = false;
    }


    else if(word.contains(playerInput) && checkProgress.contains("-"))
    {

    {

    System.out.printf("Correct: %s go again.\n", player2.getName());

    for(int i = 0; i <6; i++)
    {
    if(lettersGuessed[i] != 0)
    {
    lettersGuessed[i] = playerCheck;
    i = 6;
    }
    }
    Arrays.sort(lettersGuessed);

    }
    }


    else if(word.contains(playerInput))
    {
    System.out.printf("Correct. %s you win a point!\n", player1.getName());
    System.out.println(word);
    player1.iWon();
    System.out.printf("%s: %d\t%s: %d\n\n", player2.getName(), player2.getScore(), player1.getName(), player1.getScore());
    loopControlTwo = false;

    if(player2.getScore() == 0 || player1.getScore() == 0)
    {
    System.out.print("Game over. %s wins!\n");
    loopControl = false;
    }

    }
    else
    {
    System.out.print("Incorrect. %s, you lose your turn.\n");
    loopControlTwo = false;

    for(int i = 0; i < 6; i++)
    {
    if(lettersGuessed[i] == 0)
    {
    lettersGuessed[i] = playerCheck;
    i = 6;
    }
    Arrays.sort(lettersGuessed);
    }


    }


    }

    }


    }

    }

    }

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,986
    Thanks
    79
    Thanked 4,430 Times in 4,395 Posts
    (1) When posting code in a forum here, you need to wrap it in [ code ] .... [ /code ] tags (without spaces in them).

    (2) All you did was post code without saying *EXPLICITLY what the problem is. "It's not behaving like i want it to" might mean it's displaying in red on the screen instead of blue. You need to make a clear statement of the problem and give your best judgment about where the error, if any, is coming from.

    (3) This is the JavaSCRIPT forum. Your question is about JAVA. About the only thing the two language have in common are the first 4 letters of their names. So when you repost your code, properly formatted and explaining what your problem is, please do so in the right forum.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

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