Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //create new Console
- c = new Console ();
- f = new Console ();
- //declare variables
- String inputWord; //the word entered by player 1 that player 2 will guess
- char inputLetter; //the letter entered by player 2 that is the guess
- String category; //the category entered by player 1 which acts as a hint for player 2
- String tempString = ""; //a temporary string variable that holds the guess, used to convert char to String so that we can use the replace method
- int numGuesses = 0; //counter which keeps track of the incorrect guesses
- StringBuffer blanks = new StringBuffer (""); //a string buffer object that prints out the "?"'s based on the length of the word entered by player 1
- boolean isWinner; //used in main to check if player 2 has correctly guessed the word, then it will break
- //print instructions then clear them (working)
- instructions();
- c.readLine();
- c.clear();
- //get word input from user (working)
- c.println("Player 1, please enter the word you would like Player 2 to guess. Please do not enter any spaces or punctuation. It will be deleted.");
- inputWord = c.readLine();
- inputWord = inputWord.toUpperCase();
- c.println("Please enter a category that your word falls under. Don't be too specific!");
- category = c.readLine();
- //print category in other console (working)
- f.println("Category: " + category);
- //draw underscores for each letter of the word (working)
- for (int i = 0; i < inputWord.length(); i++)
- {
- blanks = blanks.append("?");
- }//rof
- //print the underscores out (working)
- f.print(blanks.toString());
- //draw template (working)
- drawGallows();
- //clear screen
- c.clear();
- //prompt for user 2 to enter input
- c.println("Player 2, please enter your letter guess in uppercase.");
- c.println("If you think you know the word, just type it in!");
- //main game loop
- whileLoop:
- while (numGuesses < 7)
- {
- c.println (numGuesses);
- //get letter guess from player 2 (working)
- inputLetter = c.readChar();
- //converts the char to a string so we can use the StringBuffer method replace
- tempString = Character.toString(inputLetter);
- //if the letter is somewhere in the word then replace
- if (inputWord.indexOf(inputLetter) != -1)
- {
- //Loop to check every letter in word, outputs letter if letter is located in the word
- for (int i = 0; i < inputWord.length(); i++)
- {
- if (inputWord.charAt(i) == inputLetter)
- {
- //boolean isLetterdetected is true if theres a match
- //isLetterDetected = true;
- //replaces the underscore with the correctly guessed letter
- blanks.replace(i,i+1,tempString);
- //clear the screen
- f.clear();
- //reprint the template and category
- drawGallows();
- f.println("Category: " + category);
- //print the new stringbuffer
- f.print(blanks.toString());
- }//fi
- }//rof
- }//fi
- //else the letter is not anywhere in the word so increase counter, and draw body part
- else
- {
- numGuesses++;
- c.println (numGuesses);
- drawBodyPart(numGuesses);
- }
- //check if player 2 has correctly solved the word
- forLoop:
- for (int i = 0; i < blanks.length(); i++)
- {
- if (blanks.indexOf("?") == -1)
- {
- isWinner = true;
- results (numGuesses, isWinner, inputWord);
- break whileLoop;
- }//fi
- else
- {
- break forLoop;
- }//esle
- }//rof
- }//elihw
Add Comment
Please, Sign In to add comment