Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class C0163_Intermediate {
- private static int difficulty = getDifficulty();
- private static String[] words = getWordList();
- private static int wordLength = getWordLength(difficulty);
- private static String[] selectedWords = new String[(int)(Math.random()*wordLength)+1];
- private static String answer;
- private static final int ATTEMPTS = 4;
- public static void main(String[] args) {
- getWordSelection();
- answer = selectedWords[(int)(Math.random()*(selectedWords.length))];
- play();
- }
- private static void play(){
- for(int i = 0; i < ATTEMPTS; i++){
- String input = "";
- boolean b = false;
- while(!b){
- System.out.print("Guess (" + (ATTEMPTS-i) + " left): ");
- input = new Scanner(System.in).next().toUpperCase();
- if(arrayContainsWord(selectedWords, input)) b = true;
- else System.out.println("Try again. That word is not valid.");
- }
- if(input.equals(answer)){
- System.out.println("You win! " + wordLength + "/" + wordLength + " guessed!");
- return;
- } else{
- int count = 0;
- for(int j = 0; j < selectedWords.length; j++){
- if(input.charAt(j) == answer.charAt(j)) count++;
- }
- System.out.println(count + "/" + wordLength + " correct");
- }
- }
- }
- private static boolean arrayContainsWord(String[] array, String word){
- for(int i = 0; i < array.length; i++){
- if(array[i] == null) return false;
- if(array[i].equals(word)) return true;
- }
- return false;
- }
- private static void getWordSelection(){
- for(int i = 0; i < selectedWords.length; i++){
- for(int j = (int)(Math.random()*words.length); j < words.length; j++){
- if(words[j].length() == wordLength && !arrayContainsWord(selectedWords, words[j])){
- selectedWords[i] = words[j].toUpperCase();
- System.out.println(selectedWords[i]);
- break;
- }
- }
- }
- }
- private static int getDifficulty(){
- boolean correct = false;
- int diff = 0;
- while(!correct){
- try{
- System.out.print("Difficulty (1-5): ");
- diff = Integer.parseInt(new Scanner(System.in).next());
- if(diff <= 5 && diff >= 1) correct = true;
- } catch(Exception e){
- correct = false;
- }
- }
- return diff;
- }
- private static String[] getWordList(){
- File file = new File("Wordlist.txt");
- ArrayList<String> t = new ArrayList<String>();
- try{
- BufferedReader buffRead = new BufferedReader(new FileReader(file));
- String line = buffRead.readLine();
- while(line != null){
- t.add(line);
- line = buffRead.readLine();
- }
- buffRead.close();
- } catch(IOException e){
- e.printStackTrace();
- }
- return t.toArray(new String[t.size()]);
- }
- private static int getWordLength(int difficulty){
- if(difficulty == 1) return 4;
- if(difficulty == 2) return (int)(Math.random()*4)+4;
- if(difficulty == 3) return (int)(Math.random()*4)+7;
- if(difficulty == 4) return (int)(Math.random()*4)+9;
- if(difficulty == 5) return (int)(Math.random()*4)+12;
- else return 7;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement