Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.qtech.hangman;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Scanner;
- import java.util.concurrent.ThreadLocalRandom;
- public class Main {
- private static Scanner stdin = new Scanner(System.in);
- private static ArrayList<String> words = new ArrayList<String>();
- private static boolean exit;
- private static boolean won;
- private static boolean lost;
- private static String currentPhrase;
- private static StringBuilder usedLetters = new StringBuilder();
- private static ArrayList<Character> phraseStatus = new ArrayList<Character>();
- private static int attemptsLeft;
- public static void main(String[] args) {
- initPhraseList();
- while(!exit) {
- cls();
- switch(doMenu())
- {
- case 1:
- resetVariables();
- doGame();
- break;
- case 2:
- exit = true;
- return;
- }
- }
- }
- public static void initPhraseList() {
- words.add("josh");
- words.add("faggot");
- words.add("bitch");
- words.add("jews");
- words.add("josh is gay");
- words.add("fuck bitches get money");
- words.add("lolwin");
- }
- private static void resetVariables() {
- won = false;
- lost = false;
- attemptsLeft = 6;
- phraseStatus.clear();
- currentPhrase = words.get(ThreadLocalRandom.current().nextInt(0, words.size()));
- for(char c:currentPhrase.toCharArray()) { if(c==' ') phraseStatus.add(' '); else phraseStatus.add('_'); }
- usedLetters.delete(0, usedLetters.length());
- }
- public static int doMenu() {
- System.out.println(" HANGMAN ");
- System.out.println("1. Play");
- System.out.println("2. Quit");
- String raw = stdin.next();
- int response;
- try{
- response = Integer.parseInt(raw);
- } catch(Exception e) {
- response = -1;
- }
- switch(response) {
- case 1:
- case 2:
- return response;
- default:
- return doMenu();
- }
- }
- public static void doGame() {
- while(!exit) {
- cls();
- System.out.println("Wrong letters: "+usedLetters.toString());
- updateMan();
- updateWord();
- if(won) {
- System.out.println("You win!");
- waitForEnter();
- break;
- } else if(lost) {
- System.out.println("You lose!");
- waitForEnter();
- break;
- }
- System.out.println("Enter letter or guess word:");
- String in = stdin.nextLine();
- if(in != "") {
- if(in.length() > 1)
- if(checkWord(in)) { won = true; } else {
- usedLetters.delete(0, usedLetters.length());
- usedLetters.append(in);
- lost = true;
- }
- else if(in.length() == 1)
- checkLetter(in.charAt(0));
- } else {
- System.out.println("Please enter a letter or guess the word!");
- }
- }
- }
- public static void updateMan() {
- String head = " O ";
- String body = "|";
- String lArm = "\\";
- String rArm = "/";
- String lLeg = "/";
- String rLeg = "\\";
- StringBuilder man = new StringBuilder();
- if(attemptsLeft > 0) man.append(head+"\n");
- if(attemptsLeft > 2) man.append(lArm); else man.append(" ");
- if(attemptsLeft > 1) man.append(body); else man.append(" ");
- if(attemptsLeft > 3) man.append(rArm+"\n"); else man.append(" \n");
- if(attemptsLeft > 1) man.append(" "+body+" \n"); else man.append(" \n");
- if(attemptsLeft > 4) man.append(lLeg); else man.append(" ");
- if(attemptsLeft > 5) man.append(" "+rLeg); else man.append(" ");
- System.out.println(man.toString());
- }
- public static void updateWord() {
- StringBuilder phrase = new StringBuilder();
- for(char c:phraseStatus) {
- phrase.append(c+" ");
- }
- System.out.println(phrase.toString());
- }
- public static boolean checkWord(String in) {
- if(in.toLowerCase().equals(currentPhrase.toLowerCase())) {
- for(int i=0; i<currentPhrase.length(); i++) {
- phraseStatus.set(i, currentPhrase.charAt(i));
- }
- return true;
- }
- return false;
- }
- public static void checkLetter(char letter) {
- boolean found = false;
- for(int i=0; i<currentPhrase.length(); i++) {
- if(currentPhrase.charAt(i) == letter) {
- phraseStatus.set(i, currentPhrase.charAt(i));
- found = true;
- }
- }
- if(!found){
- usedLetters.append(letter);
- attemptsLeft--;
- if(attemptsLeft == 0) lost = true;
- }
- StringBuilder currentWord = new StringBuilder();
- for(char c:phraseStatus) { currentWord.append(c); }
- won = checkWord(currentWord.toString());
- }
- private static void cls() { //Quick and dirty clearscreen
- for(int i=0;i<60;i++)
- System.out.println();
- }
- private static void waitForEnter() {
- try {
- System.in.read();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement