Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hangmanproject;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class HangmanProject {
- static String word = "patience";
- public static void main(String[] args) {
- int life = 5;
- System.out.println("Hello and Welcome to Hangman Game");
- System.out.println("In this game you have 5 chances to guess the word or a letter in the secret Word");
- System.out.println();
- System.out.print("Here is your secret word ");
- char [] dash =word.toCharArray();
- for(int i = 0; i < word.length(); i++ ) {
- System.out.print(dash[i] = '-');
- if (word.charAt(i) == ' ') {
- dash[i] = ' ';
- }
- } System.out.println();
- playGame(life, dash);
- }
- public static void playGame ( int life, char[]dash) {
- @SuppressWarnings("resource")
- Scanner sc = new Scanner (System.in);
- while(life > 0 ) {
- System.out.print("\nPress 1 : To guess the secret word \nPress 2 : To guess a letter");
- int num = sc.nextInt();
- if(num == 1) {
- guessTheword( life, dash);
- }
- if(num == 2) {
- guessAletter( life, dash);
- }
- }
- }
- public static void guessTheword( int chance, char[]dash) {
- ArrayList <String> wordContainer = new ArrayList <String>();
- @SuppressWarnings("resource")
- Scanner scan = new Scanner(System.in);
- System.out.println("Please type your Guess word:");
- scan = new Scanner(System.in);
- String yourGuess = scan.nextLine();
- if(wordContainer.contains(yourGuess)) {
- System.out.println("You have tried it before!");
- }
- wordContainer.add(yourGuess);
- if (yourGuess.equals(word)) {
- System.out.println("Congratulations You Guessed the word!!! ");
- } else {
- chance --;
- System.out.println(chance);
- System.out.println("Sorry Wrong Guess! " + chance + " chances remaining");
- drawTheHangman(chance);
- playGame (chance,dash);
- }
- }
- public static void guessAletter( int chance, char [] dash) {
- @SuppressWarnings("resource")
- Scanner input = new Scanner (System.in);
- ArrayList <Character> letterContainer = new ArrayList <Character>();
- System.out.println("Please Guess a letter ");
- input = new Scanner(System.in);
- char letter = input.next().charAt(0);
- if(letterContainer.contains(letter)) {
- System.out.println("You have tried it before");
- } letterContainer.add(letter);
- if(word.contains(letter + "")) {
- updatedGuessed(word, dash,letter);
- }else {
- chance --;
- System.out.println("Sorry Wrong Guess! " + chance + " chances remaining");
- drawTheHangman(chance);
- playGame (chance,dash);
- }
- if (word.equals(String.valueOf(dash))) {
- System.out.println("Congratulations you guessed the Word!");
- chance = 0;
- }
- System.out.println(dash);
- }
- public static void updatedGuessed(String word, char[]dash, char letter) {
- for (int y = 0; y < word.length(); y++) {
- if (word.charAt(y) == letter) {
- dash[y] = letter;
- }
- }
- }
- public static void drawTheHangman(int remainingGuess) {
- if (remainingGuess == 0) {
- System.out.println("You Lose! R.I.P." +
- "\n ________" +
- "\n | |"+
- "\n | Ö"+
- "\n | /|\\"
- + "\n | / \\" +
- "\n | " +
- "\n/|\\ ");
- System.out.println();
- System.out.println("The secret word is " + word);
- }
- else if (remainingGuess == 1) {
- System.out.println(" ________"
- + "\n | |" +
- "\n |"
- + "\n |" +
- "\n |" +
- "\n |" +
- "\n/|\\");
- } else if (remainingGuess == 2) {
- System.out.println(" ________" +
- "\n |" +
- "\n |" +
- "\n |" +
- "\n |" +
- "\n |" +
- "\n/|\\");
- } else if (remainingGuess == 3) {
- System.out.println(" |"
- + "\n |"
- + "\n |"
- + "\n |"
- + "\n |"
- + "\n |"
- + "\n/|\\");
- } else if (remainingGuess == 4) {
- System.out.println("/|\\");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement