Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Hangman;
- import java.util.*;
- public class Hang {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- String guessList = "";
- int lives, correct;
- lives = 5;
- correct = 0;
- String word = "i like";
- String[] underscores = new String[word.length()];
- play(correct, lives, word, guessList, underscores);
- }
- public static void changeDisplay(int correct, int lives, String word, String guessList, String[] underscores) {
- String live = "Lives: " + lives;
- String newSpace = "\n";
- String right = "Correct: " + correct + newSpace;
- String display = "";
- for (int i = 0; i < underscores.length; i++) {
- display += underscores[i];
- }
- String fin = live + newSpace + display + newSpace + right;
- System.out.println(picture(fin, lives, correct, word));
- }
- public static void play(int correct, int lives, String word, String guessList, String[] underscores) {
- Scanner in = new Scanner(System.in);
- for (int i = 0; i < underscores.length; i++) {
- if (word.charAt(i) == ' ') {
- underscores[i] = " ";
- }
- else {
- underscores[i] = "_ ";
- }
- }
- String word1 = word;
- /*
- for (int i = 0; i < word.length(); i++) {
- if (word.charAt(i) != ' ') {
- word1 += word.charAt(i);
- }
- }
- System.out.println(word1);
- */
- changeDisplay(correct, lives, word1, guessList, underscores);
- while (lives > 0 && correct < word1.length()) {
- char guess = guesser(in.nextLine());
- guessList += Character.toString(guess);
- if (!inWord(guess, word1, underscores) && howMany(guessList, guess) == 1) {
- lives--;
- changeDisplay(correct, lives, word1, guessList, underscores);
- }
- else if (inWord(guess, word1, underscores) && howMany(guessList, guess) == 1) {
- correct = newCorrect(guess, word1, correct);
- changeDisplay(correct, lives, word1, guessList, underscores);
- }
- else {
- System.out.println("Already guessed. Guess again: ");
- }
- }
- }
- public static char guesser(String guesses) {
- char guess = guesses.charAt(0);
- return guess;
- }
- public static int howMany(String guessList, char guess) {
- int howMany = 0;
- for (int i = 0; i < guessList.length(); i++) {
- if (guess == guessList.charAt(i)) {
- howMany++;
- }
- }
- return howMany;
- }
- public static int letterDupe(char guess, String word) {
- int times = 0;
- for (int i = 0; i < word.length(); i++) {
- if (guess == word.charAt(i)) {
- times++;
- }
- }
- return times;
- }
- public static int newCorrect(char guess, String word, int correct) {
- for (int i = 0; i < letterDupe(guess, word); i++) {
- correct++;
- }
- return correct;
- }
- public static boolean inWord(char guess, String word, String[] underscores) {
- boolean inWord = false;
- for(int i = 0; i < word.length(); i++) {
- if (guess == word.charAt(i)) {
- underscores[i] = Character.toString(guess) + " ";
- inWord = true;
- }
- }
- return inWord;
- }
- public static String picture(String fin, int lives, int correct, String word) {
- String opening = " _______________ \n"
- +"| |\n"
- +"| 0 0 |\n"
- +"| |\n"
- +"| \\______/ |\n"
- +"| |\n"
- +"|_______________|\n";
- String hanger = " _______________ \n"
- +"| |\n"
- +"| 0 0 |\n"
- +"| |\n"
- +"| ______ |\n"
- +"| |\n"
- +"|_______________|\n";
- String hanger1 = " _______________ \n"
- +"| |\n"
- +"| X 0 |\n"
- +"| |\n"
- +"| ______ |\n"
- +"| |\n"
- +"|_______________|\n";
- String hanger2 = " _______________ \n"
- +"| |\n"
- +"| X X |\n"
- +"| |\n"
- +"| ______ |\n"
- +"| |\n"
- +"|_______________|\n";
- String hanger3 = " _______________ \n"
- +"| |\n"
- +"| X X |\n"
- +"| |\n"
- +"| /---------\\ |\n"
- +"| |\n"
- +"|_______________|\n";
- String lose =
- "| | ____ | ____ ______ _______ |\n"
- +"| || | | | | | | | | | |\n"
- +"|______|| | | | | | | |____ |_____| |\n"
- +" | | | | | | | | | | |\n"
- +" | | | | | | | | | | |\n"
- +" | |____| |_____| |_____ |____| _____| |______ .\n";
- if (lives == 5) {
- fin += opening;
- }
- else if (lives == 4) {
- fin += hanger;
- }
- else if (lives == 3) {
- fin += hanger1;
- }
- else if (lives == 2) {
- fin += hanger2;
- }
- else if (lives == 1) {
- fin += hanger3;
- }
- else {
- fin += lose;
- }
- if (correct == word.length() && lives == 5) {
- fin +=" _________ \n"
- + "| | _______ | |\n"
- + "| | ______ ______ | ______ _________ | |\n"
- + "|_________| | | |/ | | | | ____|____ |\n"
- + "| |______| | ___|___ |______| | | |\n"
- + "| | | | | | | |\n"
- + "| | | | | | | |\n"
- + "| |_______ | | |_______ |________ | .\n";
- }
- else if (correct == word.length()) {
- fin += "You win!";
- }
- return fin;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement