Guest User

Untitled

a guest
Apr 23rd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.81 KB | None | 0 0
  1. //simple Hangman game
  2.  
  3. import javax.swing.*;
  4. import java.util.*;
  5. import java.text.*;
  6.  
  7. public class hangman {
  8.     char[] correctChars;
  9.     int letterCounter;
  10.     String secretWord;
  11.     int triesLeft = 10;
  12.     final char BLANK = '-';
  13.     ArrayList checkIfWon;
  14.     int timesPlayed;
  15.    
  16.     public hangman() {
  17.         timesPlayed = 1;
  18.     }
  19.    
  20.     public static void main(String[] args) {
  21.         hangman game = new hangman();
  22.         game.play();
  23.     }
  24.     public void startup() {
  25.         if (timesPlayed % 2 == 0) {
  26.             secretWord = JOptionPane.showInputDialog("Player 2, enter a secret word:");
  27.         } else {
  28.             secretWord = JOptionPane.showInputDialog("Player 1, enter a secret word:");
  29.         }
  30.         correctChars = new char[secretWord.length()];
  31.         letterCounter = 0;
  32.         checkIfWon = new ArrayList();
  33.        
  34.         for (int i = 0; i < secretWord.length(); i++) {
  35.             System.out.print("-");
  36.         }
  37.         System.out.println();
  38.     }
  39.     public void defaultCorrectChars() {
  40.         for (int i = 0; i < correctChars.length; i++) {
  41.             correctChars[i] = BLANK;
  42.         }
  43.     }
  44.     public void play() {
  45.        
  46.         startup();
  47.         defaultCorrectChars();
  48.         int answer;
  49.         String guess;
  50.         while (true) {
  51.             if (timesPlayed % 2 == 0) {
  52.                 guess = JOptionPane.showInputDialog("Player 1, guess a letter:");
  53.             } else {
  54.                 guess = JOptionPane.showInputDialog("Player 2, guess a letter:");
  55.             }
  56.             for (int j = 0; j < secretWord.length(); j++) {
  57.                 if (secretWord.substring(j, j+1).equalsIgnoreCase(guess)) { // adds new guessed letter
  58.                     correctChars[j] = guess.charAt(0);
  59.                     System.out.print(guess);
  60.                     letterCounter++;
  61.                 } else if (correctChars[j] != BLANK) { // re-adds correctly guessed letters
  62.                     System.out.print(correctChars[j]);
  63.                 } else {
  64.                     System.out.print("-");
  65.                 }
  66.             }
  67.             if (letterCounter == 0) {
  68.                 System.out.println();
  69.                 System.out.println("Sorry there were no " +guess+"(s). You lost a try.");
  70.                 System.out.println();
  71.                 triesLeft--;
  72.                 if (triesLeft == 0) {
  73.                     JOptionPane.showMessageDialog(null, "You're all out of tries, better luck next time!\nThank you for playing.");
  74.                     System.exit(0);
  75.                 }
  76.             } else {
  77.                 for (int b = 0; b < letterCounter; b++) {
  78.                     checkIfWon.add(new Boolean(true));
  79.                 }
  80.                 if (checkIfWon.size() == secretWord.length()) {
  81.                     JOptionPane.showMessageDialog(null, "Congratulations, you solved the secret word.");
  82.                     answer = Integer.parseInt(JOptionPane.showInputDialog("Would you like to play again? (0 for yes, 1 for no"));
  83.                     if (answer == 0) {
  84.                         timesPlayed++;
  85.                         play();
  86.                     } else {
  87.                         JOptionPane.showMessageDialog(null, "Thanks for playing!");
  88.                         System.exit(0);
  89.                     }  
  90.                 }
  91.                 System.out.println();
  92.                 System.out.println("There were " + letterCounter + " " + guess + "(s)" + " in the secret word");
  93.                 System.out.println();
  94.             }
  95.             letterCounter = 0;
  96.         }
  97.     }
  98. }
Add Comment
Please, Sign In to add comment