Advertisement
vanbestglitch

Hangman (Using Java)

Jan 26th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.53 KB | None | 0 0
  1. import java.util.Scanner;
  2. /*
  3.     To play simply use .play() or just compile this in any java compiler (preferably eclipse)
  4.     Feel free to change up the code if you want. Also feel free to view any other projects I made.
  5. */
  6. public class Hangman {
  7.     private String word;
  8.     private int lives;
  9.     private String hint;
  10.    
  11.     public Hangman(String word,String hint) {
  12.     this.word = word;
  13.     this.hint = hint;
  14.     this.lives = 6;
  15.     }
  16.    
  17.     public Hangman() {
  18.         this.word = "";
  19.         this.hint = "";
  20.         this.lives = 6;
  21.     }
  22.    
  23.     public int getLives() {
  24.         return this.lives;
  25.     }
  26.    
  27.     public String getWord() {
  28.         return this.word;
  29.     }
  30.    
  31.     public void setWord(String s, String s2) {
  32.         this.word = s;
  33.         this.hint = s2;
  34.     }
  35.    
  36.     public void play() {
  37.        
  38.         Scanner scan = new Scanner(System.in);
  39.        
  40.         if(getWord() == "") {
  41.             System.out.println("Enter Word");
  42.             String word = scan.nextLine();
  43.        
  44.             System.out.println("Enter Hint");
  45.             String hint = scan.nextLine();
  46.        
  47.             setWord(word,hint);
  48.            
  49.             for(int i = 0; i < 1000; i++) {
  50.                 System.out.println();
  51.             }
  52.         }
  53.        
  54.        
  55.        
  56.         String[] wordArray = new String [getWord().length()];
  57.         String[] toOutput = new String [getWord().length()];
  58.         String output = "";
  59.        
  60.         for(int i = 0; i < word.length(); i++) {           
  61.             if(getWord().substring(i, i+1).toLowerCase().contains(" ")) {
  62.                 toOutput [i] = " ";
  63.                 wordArray[i] = " ";
  64.                 output+= " ";
  65.                
  66.             }else {
  67.                 toOutput [i] = "_";
  68.                 wordArray[i] = getWord().substring(i, i+1).toLowerCase();
  69.                 output += "_ ";
  70.             }
  71.            
  72.         }
  73.        
  74.         while (output.contains("_") && getLives() > 0) {
  75.            
  76.            
  77.             System.out.println(output);
  78.             System.out.println(this.hint);
  79.             System.out.println("Lives: " + this.lives);
  80.            
  81.             System.out.println("Input Letter");
  82.  
  83.            
  84.             String guess = scan.next();
  85.             guess = guess.toLowerCase();
  86.            
  87.             boolean correct = false;
  88.             output = "";
  89.            
  90.             for(int i = 0; i < this.word.length(); i++) {
  91.                
  92.                 if(wordArray[i].contains(guess)) { 
  93.                     toOutput[i] = wordArray[i];
  94.                     correct = true;
  95.                 }
  96.                 output += toOutput[i] + " ";
  97.             }
  98.            
  99.             System.out.println();
  100.            
  101.             if(correct == false) {
  102.                 this.lives--;  
  103.             }
  104.            
  105.         }
  106.        
  107.         if(lives != 0) {
  108.            
  109.             if (word != "") {
  110.                 System.out.println("You Won! The Word Was " + this.word);
  111.             }else {
  112.                 System.out.println("You Won! The Word Wasn't Set");
  113.             }
  114.            
  115.         }else {
  116.             System.out.println("You Lost! The Word Was " + this.word);
  117.         }
  118.    
  119.         scan.close();
  120.     }
  121.    
  122.    
  123.    
  124.     public static void main(String[] args) {
  125.         Hangman h = new Hangman();
  126.         h.play();
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement