Annoyed something so simple took this long and still doesn't bloody work correctly, waaah!
package com.company;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class PasswordGame {
public String[] guessWords;
public String password;
enum difficultySettings {VERY_EASY, EASY, MEDIUM, HARD, VERY_HARD}
difficultySettings difficulty;
public int amountOfGuesses;
public PasswordGame(difficultySettings aDifficulty) {
this.difficulty = aDifficulty;
getGuessWordsAndPassword(difficulty);
}
public void gameRun() {
System.out.println("You have " + amountOfGuesses + " guess");
System.out.println("Your word list: ");
printWordList();
boolean gameWon = false;
String wordGuess;
System.out.println("Password is: " + this.password);
while (!gameWon && amountOfGuesses > 0) {
wordGuess = getGuess();
if (wordGuess.equals(password)) {
gameWon = true;
} else {
amountOfGuesses--;
System.out.println("Unlucky: " + passwordCompare(wordGuess) + " guesses left: " + amountOfGuesses);
}
}
if (gameWon) {
System.out.println("You WIN!!!");
} else {
System.out.println("You lose :(");
}
}
private String passwordCompare(String wordGuess) {
int correctChar = 0;
int correctCharAndPlace = 0;
ArrayList<Character> passWordCopy = new ArrayList<>();
for (char aChar : password.toCharArray()) { passWordCopy.add(aChar); }
ArrayList<Character> guess = new ArrayList<>();
for (char aChar : wordGuess.toUpperCase().toCharArray()) { guess.add(aChar); }
for(int i = 0; i < guess.size() && i < passWordCopy.size(); i++) {
if (guess.get(i) == passWordCopy.get(i)) {
correctChar++; correctCharAndPlace++;
} else if ( passWordCopy.contains(guess.get(i))) {
correctChar++;
passWordCopy.set(i, '+');
}
}
return "correct chracter: " + correctChar + " and in the correct place: " + correctCharAndPlace;
}
private String getGuess() {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String wordGuessed = "";
try {
wordGuessed = reader.readLine();
} catch (IOException ex) {
ex.printStackTrace();
}
return wordGuessed;
}
private void printWordList() {
for (String word : guessWords) {
System.out.println(word);
}
}
public String[] getGuessWordsAndPassword(difficultySettings aDifficulty) {
switch (aDifficulty) {
case VERY_EASY:
amountOfGuesses = 20;
populateWordsAndPassWord(10,5);
break;
case EASY:
amountOfGuesses = 15;
populateWordsAndPassWord(10,6);
break;
case MEDIUM:
amountOfGuesses = 12;
populateWordsAndPassWord(10,7);
break;
case HARD:
amountOfGuesses = 10;
populateWordsAndPassWord(10,8);
break;
case VERY_HARD:
default:
amountOfGuesses = 8;
populateWordsAndPassWord(10,9);
break;
}
return new String[]{};
}
private void populateWordsAndPassWord(int wordLength, int amountOfWords) {
ArrayList<String> filteredList = this.filteredWordList(wordLength, amountOfWords);
Collections.shuffle(filteredList);
this.guessWords = filteredList.toArray(new String[filteredList.size()]);
this.password = guessWords[new Random().nextInt(this.guessWords.length)];
}
private ArrayList<String> filteredWordList(int wordLength, int amountOfwords) {
FileReader fileReader;
BufferedReader bufferedReader;
Set<String> wordSet = new HashSet<>(); //set to filter duplicates
try {
fileReader = new FileReader("someplace\\enable1.txt");
bufferedReader = new BufferedReader(fileReader);
String word;
while ((word = bufferedReader.readLine()) != null) {
if(word.length() == wordLength) {
wordSet.add(word);
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
ArrayList<String> words = new ArrayList<>(wordSet);
ArrayList<String> sizedWords = new ArrayList<>();
for (int i = 0; i < amountOfwords; i++) {
sizedWords.add(words.get(i).toUpperCase());
}
return sizedWords;
}
}
No comments:
Post a Comment