Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hangman;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- public class Hangman {
- private final ArrayList<String> gameStructure = new ArrayList<>(List.of(" _______", " |.....|", " |.....|", " |",
- " |",
- " |", "/-\\"));
- private final ArrayList<String> bodyParts = new ArrayList<>(List.of("O\n", "/", "|", "\\"));
- private String getHead(){
- return gameStructure.get(3).concat("....." + bodyParts.get(0));
- }
- private String getChest() {
- return gameStructure.get(4).trim();
- }
- private String getRightArm() {
- return bodyParts.get(3);
- }
- private String getLeftArm() {
- return gameStructure.get(4).concat("...." + bodyParts.get(1));
- }
- private String getRightLeg(){
- return bodyParts.get(3);
- }
- private String getLeftLeg(){
- return gameStructure.get(5).concat("...." + bodyParts.get(1));
- }
- Scanner input = new Scanner(System.in);
- private final String sentence = input.next();
- private final StringBuilder maskedSentence = new StringBuilder("-".repeat(sentence.length()));
- private final StringBuilder correctCharsEntered = new StringBuilder();
- private final StringBuilder incorrectCharsEntered = new StringBuilder();
- Hangman() {
- boardDisplay();
- }
- public void hangmanGame(){
- while (true) {
- if (correctCharsEntered.length() == sentence.length() + 2) break;
- System.out.println("Enter the word you want to experiment hangman on ");
- String human = input.next();
- if (maskedSentence.toString().equals(sentence)) {
- System.out.println("The word was entered correctly");
- boardDisplay();
- System.out.println(maskedSentence);
- break;
- } else if (human.equals(sentence)) {
- System.out.println(human);
- break;
- }
- while (human.length() != 1) {
- System.out.println("Enter one character");
- human = input.next();
- }
- correctCharsEntered.append(human);
- if (isCorrectCharacter(sentence, correctCharsEntered)) {
- incorrectCharsEntered.append(correctCharsEntered.charAt(correctCharsEntered.length() - 1));
- if (incorrectCharsEntered.length() == 1) {
- System.out.println(" _______");
- System.out.println(" |.....|");
- System.out.println(" |.....|");
- System.out.print(getHead());
- System.out.println(" |");
- System.out.println(" |");
- System.out.println("/-\\");
- } else if (incorrectCharsEntered.length() == 2) {
- System.out.println(" _______");
- System.out.println(" |.....|");
- System.out.println(" |.....|");
- System.out.print(getHead());
- System.out.println(getLeftArm());
- System.out.println(" |");
- System.out.println("/-\\");
- } else if (incorrectCharsEntered.length() == 3) {
- System.out.println(" _______");
- System.out.println(" |.....|");
- System.out.println(" |.....|");
- System.out.print(getHead());
- System.out.println(getLeftArm() + getChest());
- System.out.println(" |");
- System.out.println("/-\\");
- } else if (incorrectCharsEntered.length() == 4) {
- System.out.println(" _______");
- System.out.println(" |.....|");
- System.out.println(" |.....|");
- System.out.print(getHead());
- System.out.println(getLeftArm() + getChest() + getRightArm());
- System.out.println(" |");
- System.out.println("/-\\");
- } else if (incorrectCharsEntered.length() == 5) {
- System.out.println(" _______");
- System.out.println(" |.....|");
- System.out.println(" |.....|");
- System.out.print(getHead());
- System.out.println(getLeftArm() + getChest() + getRightArm());
- System.out.println(getLeftLeg());
- System.out.println("/-\\");
- } else if (incorrectCharsEntered.length() == 6) {
- System.out.println(" _______");
- System.out.println(" |.....|");
- System.out.println(" |.....|");
- System.out.print(getHead());
- System.out.println(getLeftArm() + getChest() + getRightArm());
- System.out.println(getLeftLeg()+ getRightLeg());
- System.out.println("/-\\");
- System.out.println();
- System.out.println("GAME OVER");
- break;
- }
- System.out.println(maskedSentence);
- correctCharsEntered.deleteCharAt(correctCharsEntered.length() - 1);
- } else {
- for (int i = 0; i < sentence.length(); i++) {
- unMaskSentence(sentence, i);
- }
- }
- }
- }
- private void unMaskSentence(String sentence, int i) {
- if (sentence.charAt(i) == correctCharsEntered.charAt(correctCharsEntered.length() - 1)) {
- maskedSentence.setCharAt(i, correctCharsEntered.charAt(correctCharsEntered.length() - 1));
- }
- }
- private boolean isCorrectCharacter(String sentence, StringBuilder correctCharsEntered) {
- return !sentence.contains(correctCharsEntered);
- }
- private void boardDisplay() {
- for (String s : gameStructure) {
- System.out.println(s);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement