Advertisement
Guest User

Untitled

a guest
May 21st, 2014
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.11 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6. import java.util.Scanner;
  7.  
  8. public class C0163_Intermediate {
  9.  
  10.     private static int difficulty = getDifficulty();
  11.     private static String[] words = getWordList();
  12.     private static int wordLength = getWordLength(difficulty);
  13.     private static String[] selectedWords = new String[(int)(Math.random()*wordLength)+1];
  14.     private static String answer;
  15.     private static final int ATTEMPTS = 4;
  16.  
  17.     public static void main(String[] args) {
  18.         getWordSelection();
  19.         answer = selectedWords[(int)(Math.random()*(selectedWords.length))];
  20.         play();
  21.     }
  22.  
  23.     private static void play(){
  24.         for(int i = 0; i < ATTEMPTS; i++){
  25.             String input = "";
  26.             boolean b = false;
  27.             while(!b){
  28.                 System.out.print("Guess (" + (ATTEMPTS-i) + " left): ");
  29.                 input = new Scanner(System.in).next().toUpperCase();
  30.                 if(arrayContainsWord(selectedWords, input)) b = true;
  31.                 else System.out.println("Try again. That word is not valid.");
  32.             }
  33.             if(input.equals(answer)){
  34.                 System.out.println("You win! " + wordLength + "/" + wordLength + " guessed!");
  35.                 return;
  36.             } else{
  37.                 int count = 0;
  38.                 for(int j = 0; j < selectedWords.length; j++){
  39.                     if(input.charAt(j) == answer.charAt(j)) count++;
  40.                 }
  41.                 System.out.println(count + "/" + wordLength + " correct");
  42.             }
  43.         }
  44.     }
  45.    
  46.     private static boolean arrayContainsWord(String[] array, String word){
  47.         for(int i = 0; i < array.length; i++){
  48.             if(array[i] == null)    return false;
  49.             if(array[i].equals(word))   return true;
  50.         }
  51.         return false;
  52.     }
  53.    
  54.     private static void getWordSelection(){
  55.         for(int i = 0; i < selectedWords.length; i++){
  56.             for(int j = (int)(Math.random()*words.length); j < words.length; j++){
  57.                 if(words[j].length() == wordLength && !arrayContainsWord(selectedWords, words[j])){
  58.                     selectedWords[i] = words[j].toUpperCase();
  59.                     System.out.println(selectedWords[i]);
  60.                     break;
  61.                 }
  62.             }
  63.         }
  64.     }
  65.  
  66.     private static int getDifficulty(){
  67.         boolean correct = false;
  68.         int diff = 0;
  69.         while(!correct){
  70.             try{
  71.                 System.out.print("Difficulty (1-5): ");
  72.                 diff = Integer.parseInt(new Scanner(System.in).next());
  73.                 if(diff <= 5 && diff >= 1)  correct = true;
  74.             } catch(Exception e){
  75.                 correct = false;
  76.             }
  77.         }
  78.         return diff;
  79.     }
  80.  
  81.     private static String[] getWordList(){
  82.         File file = new File("Wordlist.txt");
  83.         ArrayList<String> t = new ArrayList<String>();
  84.         try{
  85.             BufferedReader buffRead = new BufferedReader(new FileReader(file));
  86.             String line = buffRead.readLine();
  87.             while(line != null){
  88.                 t.add(line);
  89.                 line = buffRead.readLine();
  90.             }
  91.             buffRead.close();
  92.         } catch(IOException e){
  93.             e.printStackTrace();
  94.         }
  95.         return t.toArray(new String[t.size()]);
  96.     }
  97.  
  98.     private static int getWordLength(int difficulty){
  99.         if(difficulty == 1) return 4;
  100.         if(difficulty == 2) return (int)(Math.random()*4)+4;
  101.         if(difficulty == 3) return (int)(Math.random()*4)+7;
  102.         if(difficulty == 4) return (int)(Math.random()*4)+9;
  103.         if(difficulty == 5) return (int)(Math.random()*4)+12;
  104.         else return 7;
  105.     }
  106.  
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement