Created
September 4, 2018 02:43
-
-
Save WolfDan/0a68420155f49aeaa16445a50b34e93e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Main | |
package com.first; | |
public class Main { | |
public static void main(String[] args) { | |
HangmanConsole console = new HangmanConsole(); | |
console.startGame(); | |
} | |
} | |
// Word Provider | |
package com.first; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.Random; | |
public class WordProvider { | |
static List<String> getWords(){ | |
return Arrays.asList("animals", "programming", "god", "animation", "characters", "universe", "looser"); | |
} | |
static String getRandomWord() { | |
List words = getWords(); | |
return (String) words.get(new Random().nextInt(words.size())); | |
} | |
} | |
// HandgmanConsole | |
package com.first; | |
import java.util.Scanner; | |
public class HangmanConsole { | |
final HangmanWord word; | |
final Scanner in = new Scanner(System.in); | |
public HangmanConsole() { | |
this.word = new HangmanWord(); | |
} | |
public void startGame() { | |
printHeader(); | |
printGame(); | |
} | |
public void printHeader() { | |
System.out.println("The game is started"); | |
System.out.println("Check your challenge:"); | |
System.out.println(word.getWordProgression()); | |
printDivider(); | |
} | |
public void printGame() { | |
while (!word.wonGame() && !word.isGameOver()){ | |
System.out.print(word.getWordProgression()); | |
System.out.print("\t\t\t\t\t\t\t\t"); | |
System.out.print("Life left: "); | |
System.out.println(word.getLifeLeft()); | |
String letter = ""; | |
while (letter.length() != 1) { | |
System.out.print("Give me a letter (must be a single character) =>"); | |
letter = in.next(); | |
} | |
word.selectLetter(letter); | |
} | |
if (word.wonGame()) { | |
printDivider(); | |
System.out.print("The word is: "); | |
System.out.println(word.getWordProgression()); | |
System.out.println("YOU WON!"); | |
} | |
else{ | |
printDivider(); | |
System.out.print("The word is: "); | |
System.out.println(word.getWord()); | |
System.out.println("YOU LOSE!"); | |
} | |
} | |
public void printDivider() { | |
System.out.println("============================================"); | |
} | |
} | |
// HandmanWorld | |
package com.first; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
public class HangmanWord { | |
private static final int ALPHABET_COUNT = 26; | |
private static final int maxLifePoints = 100; | |
private final int failCoefficient; | |
private final String word; | |
private final HashMap<String, ArrayList<Integer>> wordIndex = new HashMap<>(); | |
private ArrayList<String> selectedCharacters = new ArrayList<>(); | |
// This could be reactive but for sake of simplicity we keep it like that | |
private StringBuilder wordProgression; | |
public HangmanWord() { | |
this.word = WordProvider.getRandomWord(); | |
processWord(); | |
this.failCoefficient = ALPHABET_COUNT / this.wordIndex.size() * 2; | |
this.wordProgression = new StringBuilder(new String(new char[this.word.length()]).replace("\0", "_")); | |
} | |
String getWord() {return word;} | |
public String getWordProgression() { | |
return wordProgression.toString(); | |
} | |
public int getLifeLeft() { | |
return maxLifePoints - getWrongSelectedCharacters().size() * failCoefficient; | |
} | |
public boolean isGameOver() { | |
return getWrongSelectedCharacters().size() * failCoefficient >= maxLifePoints; | |
} | |
public boolean wonGame() { | |
return wordProgression.toString().equals(word); | |
} | |
public void selectLetter(String letter) { | |
if (!isGameOver() && !selectedCharacters.contains(letter)){ | |
selectedCharacters.add(letter); | |
if (wordIndex.containsKey(letter)) { | |
for (int index : wordIndex.get(letter)) { | |
wordProgression.setCharAt(index, letter.charAt(0)); | |
} | |
} | |
} | |
} | |
private ArrayList<String> getWrongSelectedCharacters() { | |
ArrayList<String> result = new ArrayList<>(); | |
for (String character : selectedCharacters){ | |
if (!wordIndex.containsKey(character)) { | |
result.add(character); | |
} | |
} | |
return result; | |
} | |
private void processWord() { | |
String[] letters = word.split(""); | |
for (int i = 0; i < letters.length; i++) { | |
ArrayList<Integer> letterIndex = wordIndex.getOrDefault(letters[i], new ArrayList<>()); | |
letterIndex.add(i); | |
wordIndex.put(letters[i], letterIndex); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment