100 Days of Code (48 Part Series)
1 Day 1: Two Sum
2 Day 2: Check Prime
… 44 more parts…
3 Day 3: Hex Colour
4 Day 4: Repeating Numbers
5 Day 5: Nearest Prime Numbers
6 Day 6: Anagram Detector
7 Day 7: Poker ID
8 Day 8: Bubble Sort
9 Day 9: Password Generator
10 Day 10: Array Search
11 Day 11: Rock, Paper, Scissors
12 Day 12: List Shuffle
13 Day 13: Validate ISBN
14 Day 14: HTML Markup Generator
15 Day 15: Pascal’s Triangle
16 Day 16: Pascal’s Pyramid
17 Day 17: d20 Dice Roller
18 Day 18: Base64
19 Day 19: Vigenère cipher
20 Day 20: Seven-segment display
21 Day 21: Insertion Sort
22 Day 22: Binary-Decimal
23 Day 23: Ducci sequence
24 Day 24: Shell Sort
25 Day 25: Calculator
26 Day 26: Merge Sort
27 Day 27: Bucket Sort
28 Day 28: Counting Sort
29 Day 29: Heap Sort
30 Day 30: Quick Sort
31 Day 31: Radix Sort
32 Day 32: Shaker Sort
33 Day 33: Stooge Sort
34 Day 34: Consuming a RESTful API with React
35 Day 35: Adding Machine
36 Day 36: React Form Validation
37 Day 37: Time Format Converter
38 Day 38: React Stopwatch
39 Day 39: Blackjack Checker
40 Day 40: Password Guessing Game
41 Day 41: String Case
42 Day 42: Tip Calculator
43 Day 43: Twitter Auto-reply
44 Day 44: React Currency Converter
45 Day 45: Phonebook
46 Day 46: Sudoku Generator
47 Day 47: Binary Search Tree
48 Day 48: AVL Tree
public class PasswordGuessGame {
private static final int DIFFICULTY_MULTIPLIER = 5;
private static int guessesLeft = 4;
private static boolean gameIsWon = false;
private static char[] winningCombination;
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
System.out.println("Difficulty? (1-5)");
int difficulty = in.nextInt();
in.nextLine();
List<String> wordHints = getWords(difficulty);
Random rand = new Random();
winningCombination = wordHints.get(rand.nextInt(wordHints.size())).toCharArray();
while (notGameOver()) {
for (String hint : wordHints) {
System.out.println(hint);
}
System.out.println("Guess (" + guessesLeft + " left)?");
String nextLine = in.nextLine();
int result = compare(nextLine);
final int wordLength = difficulty*2 + DIFFICULTY_MULTIPLIER;
if (result == wordLength) {
gameIsWon = true;
} else {
guessesLeft--;
System.out.println(result+"/"+wordLength + " correct");
}
}
printWinOrLose();
}
private static boolean notGameOver() {
return guessesLeft != 0 && !gameIsWon;
}
private static void printWinOrLose() {
if(gameIsWon) {
System.out.println("You are victorious");
} else if(guessesLeft == 0){
System.out.println("You have ran out of guesses, try again");
}
}
private static int compare(String input) {
int totalCorrectCharacters = 0;
char[] inputCharArray = input.toCharArray();
if(input.length() != winningCombination.length) {
System.out.println("Please input a string of the same size");
return 0;
}
for (int i = 0; i < inputCharArray.length; i++) {
if (inputCharArray[i] == winningCombination[i]) {
totalCorrectCharacters++;
}
}
return totalCorrectCharacters;
}
private static List<String> getWords(int difficulty) throws IOException {
List<String> strings = readFile();
List<String> sameLengthStrings = getSameLengthStrings(difficulty, strings);
Set<String> randomWords = extractRandomStrings(sameLengthStrings);
return new ArrayList<String>(randomWords);
}
private static List<String> readFile() throws IOException {
List<String> strings = new ArrayList<String>();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("C:/javadev/tools/eclipse-4.3/workspace/sandbox/src/main/java/fallout/enable1.txt"));
String line = br.readLine();
while (line != null) {
line = br.readLine();
strings.add(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
br.close();
}
return strings;
}
private static Set<String> extractRandomStrings(List<String> sameLengthStrings) {
Set<String> randomWords = new HashSet<String>();
Random random = new Random();
//Return between 5 and 15 words
int setSize = random.nextInt(10) + 5;
while (randomWords.size() != setSize) {
randomWords.add(sameLengthStrings.get(random.nextInt(sameLengthStrings.size())));
}
return randomWords;
}
private static List<String> getSameLengthStrings(int difficulty, List<String> strings) {
List<String> sameLengthStrings = new ArrayList<String>();
for (String string : strings) {
if (string != null && string.length() == difficulty*2 + DIFFICULTY_MULTIPLIER) {
sameLengthStrings.add(string);
}
}
return sameLengthStrings;
}
}
Enter fullscreen mode Exit fullscreen mode
100 Days of Code (48 Part Series)
1 Day 1: Two Sum
2 Day 2: Check Prime
… 44 more parts…
3 Day 3: Hex Colour
4 Day 4: Repeating Numbers
5 Day 5: Nearest Prime Numbers
6 Day 6: Anagram Detector
7 Day 7: Poker ID
8 Day 8: Bubble Sort
9 Day 9: Password Generator
10 Day 10: Array Search
11 Day 11: Rock, Paper, Scissors
12 Day 12: List Shuffle
13 Day 13: Validate ISBN
14 Day 14: HTML Markup Generator
15 Day 15: Pascal’s Triangle
16 Day 16: Pascal’s Pyramid
17 Day 17: d20 Dice Roller
18 Day 18: Base64
19 Day 19: Vigenère cipher
20 Day 20: Seven-segment display
21 Day 21: Insertion Sort
22 Day 22: Binary-Decimal
23 Day 23: Ducci sequence
24 Day 24: Shell Sort
25 Day 25: Calculator
26 Day 26: Merge Sort
27 Day 27: Bucket Sort
28 Day 28: Counting Sort
29 Day 29: Heap Sort
30 Day 30: Quick Sort
31 Day 31: Radix Sort
32 Day 32: Shaker Sort
33 Day 33: Stooge Sort
34 Day 34: Consuming a RESTful API with React
35 Day 35: Adding Machine
36 Day 36: React Form Validation
37 Day 37: Time Format Converter
38 Day 38: React Stopwatch
39 Day 39: Blackjack Checker
40 Day 40: Password Guessing Game
41 Day 41: String Case
42 Day 42: Tip Calculator
43 Day 43: Twitter Auto-reply
44 Day 44: React Currency Converter
45 Day 45: Phonebook
46 Day 46: Sudoku Generator
47 Day 47: Binary Search Tree
48 Day 48: AVL Tree
暂无评论内容