Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Hangman {
- public static void main(String[] args)
- {
- Scanner scanner = new Scanner(System.in);
- String guesses[] = { "one", "two", "three", "four",
- "five", "eat", "sidewalks", "dog", "please", "doctor",
- "six", "element", "fear", "feel",
- "loyalty", "pride", "humble", "lust", "love", "blood"};
- boolean weArePlaying = true;
- while(weArePlaying)
- {
- int randomNumber = (int)(Math.random()*guesses.length);
- char randomWordToGuess[] = guesses[randomNumber].toCharArray();
- int ammountOfGuesses = 6;
- char playerGuess[] = new char[randomWordToGuess.length];
- for(int i=0; i<playerGuess.length; i++)
- {
- playerGuess[i] = '_';
- }
- boolean wordIsGuessed = false;
- int tries = 0;
- while(tries != ammountOfGuesses)
- {
- System.out.println("Current Guesses: ");
- print(playerGuess);
- System.out.printf("You have %d tries left.\n", ammountOfGuesses-tries);
- System.out.println("Enter a letter: ");
- char input = scanner.nextLine().charAt(0);
- char[]letters=new char[6];
- tries++;
- for(int i=0; i<randomWordToGuess.length; i++)
- {
- if(randomWordToGuess[i] == input)
- {
- playerGuess[i] = input;
- tries--;
- }
- else
- letters[i]=input;
- }
- if(isTheWordGuessed(playerGuess))
- {
- wordIsGuessed = true;
- System.out.println("Congratulations, the word was "+"\n\t''"+guesses[randomNumber]+"''.");
- break;
- }
- }
- /* End of wordIsGuessed */
- if(!wordIsGuessed){
- System.out.println("You lose, the word was "+"\n\t''"+guesses[randomNumber]+"''.");
- }
- System.out.println("Would you like to play again? (yes/no) ");
- String choice = scanner.nextLine();
- if(choice.equals("no")){
- weArePlaying = false;
- }
- }/*End of We Are Playing*/
- System.out.println("Game Over!");
- }
- public static void print(char array[]){
- for(int i=0; i<array.length; i++){ // Assign empty dashes at start "_ _ _ _ _ _ _ _"
- System.out.print(array[i] + " ");
- }
- System.out.println();
- }
- public static boolean isTheWordGuessed(char[] array){
- boolean condition = true;
- for(int i=0; i<array.length; i++){
- if(array[i] == '_'){
- condition = false;
- }
- }
- return condition;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement