Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import static java.lang.System.*;
- import java.io.IOException;
- import java.io.File;
- import java.util.Scanner;
- public class Hangman
- {
- private int maxGuesses = 6;
- private String word = null;
- private int wordLength;
- private char hiddenWord[] = null;
- private String letterGuesser = "";
- public int completeWord = 0;
- public boolean complete = false;
- private int wrongGuesses = 0;
- private String allGuesses = "";
- private boolean playAgain = true;
- public Hangman()
- {
- System.out.println("Welcome to Hangman! nnYour goal is to guess the letters in the word displayed in blanks nnThese letters, if guessed corectly, will form a word. nnYou will have 5 lives nnWhen you run out of guesses, Game over nnGood luck!nn");
- while (playAgain==true)
- {
- System.out.println("nnNew Game!");
- complete=false;
- wrongGuesses = 0;
- letterGuesser = "";
- allGuesses = "";
- hiddenWord = null;
- completeWord = 0;
- wordLength = word.length();
- createHiddenWord(wordLength);
- while (complete==false)
- {
- isGuessCorrect(letterGuesser());
- isWordComplete();
- }
- askPlayerForAgain();
- }
- }
- public void setWord()
- {
- Scanner scan = new Scanner(new File("Words.txt"));
- double random = Math. random() * 1000 + 1;
- for(int i=0; random==i; i++)
- {
- scan.nextLine();
- }
- word=file.nextLine();
- }
- private void createHiddenWord(int length)
- {
- hiddenWord = new char[length];
- char(hiddenWord, '_');
- System.out.println("The word is : " + new String(hiddenWord));
- }
- static Scanner key = new Scanner(System.in);
- public char letterGuesser()
- {
- System.out.print("nPick a letter : ");
- String a = key.nextLine();
- while (a.equals(""))
- {
- System.out.print("nPick a letter : ");
- a = key.nextLine();
- }
- allGuesses = allGuesses + a.charAt(0);
- System.out.println("nGuessed Letters: " +allGuesses);
- return a.charAt(0);
- }
- public boolean isGuessCorrect(char guess)
- {
- char wordArray[] = word.toCharArray();
- boolean correct = false;
- for(int i = 0; i<wordLength; i++)
- if(wordArray[i] == guess)
- {
- hiddenWord[i] = guess;
- correct = true;
- }
- if(correct==true)
- {
- System.out.println("nAwesome guess! Keep goin'!");
- }
- else
- {
- wrongGuesses++;
- int wrongLeft = maxGuesses-wrongGuesses;
- System.out.println("nWrong guess! You have " + wrongLeft + " guesses left.");
- }
- if(wrongGuesses==maxGuesses)
- {
- System.out.println("nThe word really was : " + word);
- complete=true;
- }
- if(complete==false)
- {
- System.out.println(hiddenWord);
- return (correct);
- }
- else
- {
- return (false);
- }
- }
- public boolean isWordComplete()
- {
- String s=new String(hiddenWord);
- if (s.equals(word))
- {
- System.out.println("nYou have successfully completed this word! Nice job!");
- complete = true;
- completeWord++;
- }
- return(complete);
- }
- public boolean askPlayerForAgain()
- {
- System.out.println("nWould you like to play again? y/n");
- String response = key.nextLine();
- if (response.equals("y"))
- {
- playAgain=true;
- }
- else
- {
- playAgain=false;
- }
- return (playAgain);
- }
- }
- //Hangman
- public class HangmanRunner
- {
- public static void main(String args[]) throws IOException;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement