Advertisement
TheRightGuy

Advice?

Feb 7th, 2022
1,011
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.98 KB | None | 0 0
  1. package hangman;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Scanner;
  6.  
  7. public class Hangman {
  8.     private final ArrayList<String> gameStructure = new ArrayList<>(List.of(" _______", " |.....|", " |.....|", " |",
  9.             " |",
  10.             " |", "/-\\"));
  11.     private final ArrayList<String> bodyParts = new ArrayList<>(List.of("O\n", "/", "|", "\\"));
  12.     private String getHead(){
  13.         return gameStructure.get(3).concat("....." + bodyParts.get(0));
  14.     }
  15.     private String getChest() {
  16.         return gameStructure.get(4).trim();
  17.     }
  18.     private String getRightArm() {
  19.         return bodyParts.get(3);
  20.     }
  21.     private String getLeftArm() {
  22.         return gameStructure.get(4).concat("...." + bodyParts.get(1));
  23.     }
  24.     private String getRightLeg(){
  25.         return bodyParts.get(3);
  26.     }
  27.     private String getLeftLeg(){
  28.         return gameStructure.get(5).concat("...." + bodyParts.get(1));
  29.     }
  30.     Scanner input = new Scanner(System.in);
  31.     private final String sentence = input.next();
  32.     private final StringBuilder maskedSentence = new StringBuilder("-".repeat(sentence.length()));
  33.     private final StringBuilder correctCharsEntered = new StringBuilder();
  34.     private final StringBuilder incorrectCharsEntered = new StringBuilder();
  35.  
  36.     Hangman() {
  37.         boardDisplay();
  38.     }
  39.  
  40.     public void hangmanGame(){
  41.         while (true) {
  42.             if (correctCharsEntered.length() == sentence.length() + 2) break;
  43.             System.out.println("Enter the word you want to experiment hangman on ");
  44.             String human = input.next();
  45.             if (maskedSentence.toString().equals(sentence)) {
  46.                 System.out.println("The word was entered correctly");
  47.                 boardDisplay();
  48.                 System.out.println(maskedSentence);
  49.                 break;
  50.             } else if (human.equals(sentence)) {
  51.                 System.out.println(human);
  52.                 break;
  53.             }
  54.             while (human.length() != 1) {
  55.                 System.out.println("Enter one character");
  56.                 human = input.next();
  57.             }
  58.             correctCharsEntered.append(human);
  59.             if (isCorrectCharacter(sentence, correctCharsEntered)) {
  60.                 incorrectCharsEntered.append(correctCharsEntered.charAt(correctCharsEntered.length() - 1));
  61.                 if (incorrectCharsEntered.length() == 1) {
  62.                     System.out.println(" _______");
  63.                     System.out.println(" |.....|");
  64.                     System.out.println(" |.....|");
  65.                     System.out.print(getHead());
  66.                     System.out.println(" |");
  67.                     System.out.println(" |");
  68.                     System.out.println("/-\\");
  69.                 } else if (incorrectCharsEntered.length() == 2) {
  70.                     System.out.println(" _______");
  71.                     System.out.println(" |.....|");
  72.                     System.out.println(" |.....|");
  73.                     System.out.print(getHead());
  74.                     System.out.println(getLeftArm());
  75.                     System.out.println(" |");
  76.                     System.out.println("/-\\");
  77.                 } else if (incorrectCharsEntered.length() == 3) {
  78.                     System.out.println(" _______");
  79.                     System.out.println(" |.....|");
  80.                     System.out.println(" |.....|");
  81.                     System.out.print(getHead());
  82.                     System.out.println(getLeftArm() + getChest());
  83.                     System.out.println(" |");
  84.                     System.out.println("/-\\");
  85.                 } else if (incorrectCharsEntered.length() == 4) {
  86.                     System.out.println(" _______");
  87.                     System.out.println(" |.....|");
  88.                     System.out.println(" |.....|");
  89.                     System.out.print(getHead());
  90.                     System.out.println(getLeftArm() + getChest() + getRightArm());
  91.                     System.out.println(" |");
  92.                     System.out.println("/-\\");
  93.                 } else if (incorrectCharsEntered.length() == 5) {
  94.                     System.out.println(" _______");
  95.                     System.out.println(" |.....|");
  96.                     System.out.println(" |.....|");
  97.                     System.out.print(getHead());
  98.                     System.out.println(getLeftArm() + getChest() + getRightArm());
  99.                     System.out.println(getLeftLeg());
  100.                     System.out.println("/-\\");
  101.                 } else if (incorrectCharsEntered.length() == 6) {
  102.                     System.out.println(" _______");
  103.                     System.out.println(" |.....|");
  104.                     System.out.println(" |.....|");
  105.                     System.out.print(getHead());
  106.                     System.out.println(getLeftArm() + getChest() + getRightArm());
  107.                     System.out.println(getLeftLeg()+ getRightLeg());
  108.                     System.out.println("/-\\");
  109.                     System.out.println();
  110.                     System.out.println("GAME OVER");
  111.                     break;
  112.                 }
  113.                 System.out.println(maskedSentence);
  114.                 correctCharsEntered.deleteCharAt(correctCharsEntered.length() - 1);
  115.  
  116.             } else {
  117.                 for (int i = 0; i < sentence.length(); i++) {
  118.                     unMaskSentence(sentence, i);
  119.                 }
  120.             }
  121.         }
  122.     }
  123.  
  124.     private void unMaskSentence(String sentence, int i) {
  125.         if (sentence.charAt(i) == correctCharsEntered.charAt(correctCharsEntered.length() - 1)) {
  126.             maskedSentence.setCharAt(i, correctCharsEntered.charAt(correctCharsEntered.length() - 1));
  127.         }
  128.     }
  129.  
  130.     private boolean isCorrectCharacter(String sentence, StringBuilder correctCharsEntered) {
  131.         return !sentence.contains(correctCharsEntered);
  132.     }
  133.  
  134.     private void boardDisplay() {
  135.         for (String s : gameStructure) {
  136.             System.out.println(s);
  137.         }
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement