Advertisement
Guest User

Daily Programmer 238 Intemediate

a guest
Oct 28th, 2015
936
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Random;
  3. import java.util.Scanner;
  4.  
  5. public class DP238M { //FO3/FNV Hacking Minigame
  6.  
  7. static ArrayList<String> dict = DictionaryLoader.loadDictionary();
  8. static Scanner sc = new Scanner(System.in);
  9.  
  10. //Terminal Difficulties: Password length - words
  11. //Very Easy: 4-5 - 5
  12. //Easy: 6-8 - 8
  13. //Average: 9-10 - 10
  14. //Hard: 11-12 - 12
  15. //Very Hard: 13-15 - 15
  16.  
  17. public static void main(String[] args){
  18. int difficulty = randInt(0,4); //0 -> 4
  19.  
  20. int wordlength = 0;
  21. int numwords = 0;
  22.  
  23. switch(difficulty){
  24. case 0:
  25. wordlength = randInt(4,5);
  26. numwords = 5;
  27. break;
  28. case 1:
  29. wordlength = randInt(6,8);
  30. numwords = 8;
  31. break;
  32. case 2:
  33. wordlength = randInt(9,10);
  34. numwords = 10;
  35. break;
  36. case 3:
  37. wordlength = randInt(11,12);
  38. numwords = 12;
  39. break;
  40. case 4:
  41. wordlength = randInt(13,15);
  42. numwords = 15;
  43. break;
  44. }
  45.  
  46. System.out.println("Difficulty: " + difficulty);
  47. System.out.println("Word Length: " + wordlength);
  48. System.out.println("Number of Words: " + numwords);
  49.  
  50. ArrayList<String> validwords = new ArrayList<String>();
  51. for(int i=0; i<dict.size(); i++){
  52. if(dict.get(i).length() == wordlength){
  53. validwords.add(dict.get(i));
  54. }
  55. }
  56. //System.out.println("Selected subset containing " + validwords.size() + " words of length " + wordlength);
  57. System.out.println();
  58.  
  59. ArrayList<String> usedwords = new ArrayList<String>();
  60. while(usedwords.size() < numwords){
  61. int w = randInt(0, validwords.size());
  62. usedwords.add(validwords.get(w));
  63. validwords.remove(w);
  64. }
  65.  
  66. int correctword = randInt(0,usedwords.size()-1);
  67. String cw = usedwords.get(correctword);
  68.  
  69.  
  70. boolean foundcorrect = false;
  71. int guesses = 4;
  72. while(!foundcorrect && guesses > 0){
  73. for(int i=0; i<5; i++){
  74. System.out.println();
  75. }
  76. /*
  77. for(int i=0; i<usedwords.size(); i++){
  78. System.out.println();
  79. System.out.print(hexAfter(i) + " " + usedwords.get(i).toUpperCase() + " (" + i + ")");
  80. if(i == correctword){
  81. //System.out.print(" <---");
  82. }
  83. }
  84. System.out.println();
  85. */
  86.  
  87. printTerminal(wordlength,numwords,guesses,usedwords);
  88.  
  89. System.out.println("Make a selection ("+0+" - "+ (usedwords.size()-1)+")");
  90. int sel = sc.nextInt();
  91.  
  92. String sw = usedwords.get(sel);
  93. int correctcounter = 0;
  94. for(int i=0; i<sw.length(); i++){
  95. if(sw.charAt(i) == cw.charAt(i))
  96. correctcounter++;
  97. }
  98.  
  99. System.out.println("> " + sw.toUpperCase());
  100. if(correctcounter == wordlength){
  101. System.out.println("> Exact match!");
  102. System.out.println("> Please wait while system is accessed.");
  103. foundcorrect = true;
  104. }
  105. else{
  106. System.out.println("> Entry denied");
  107. System.out.println("> " + correctcounter+"/"+wordlength + " correct.");
  108. }
  109.  
  110. guesses--;
  111.  
  112. }
  113. }
  114.  
  115. public static int randInt(int min, int max) {
  116. Random rand = new Random();
  117. int randomNum = rand.nextInt((max - min) + 1) + min;
  118. return randomNum;
  119. }
  120.  
  121. public static String hexAfter(int n){
  122. String hex = "0x";
  123. String add = Integer.toHexString(3000+(int)Math.pow(n, 3));
  124. while(hex.length() + add.length() < 6){
  125. hex+="0";
  126. }
  127. hex+=add.toUpperCase();
  128. return hex;
  129. }
  130.  
  131. public static void printTerminal(int wl, int nw, int ng, ArrayList<String> uw){
  132. //N ATTEMPT(S) LEFT [] [] [] []
  133. //0xF748 xxxxxxxxxxxxxxx 0xF814 xxxxxxxxxxxxxx
  134.  
  135. char[] randchars = {')','/','@','^',']',';'};
  136. int numlines = 15;
  137. int totalspaces = numlines*2;
  138. int wordposition = (int)(totalspaces/nw)+1;
  139. int linelength = 15;
  140.  
  141. System.out.println();
  142. System.out.print(ng + " ATTEMPT(S) LEFT: ");
  143. for(int i=0; i<ng; i++){
  144. System.out.print("# ");
  145. }
  146. System.out.println();
  147.  
  148. for(int i=0; i<numlines; i++){
  149. int w1 = i;
  150. int w2 = i+1;
  151.  
  152. String line = "";
  153.  
  154. //First column
  155. line+=hexAfter(i);
  156. if(w1%wordposition == 0){
  157. line+=centreTextwithRandChars(uw.get(i/wordposition).toUpperCase(),15,randchars);
  158. }
  159. else{
  160. line+=centreTextwithRandChars("",15,randchars);
  161. }
  162.  
  163. line+=" ";
  164.  
  165. //Second column
  166. line+=hexAfter(i+numlines);
  167. if(w2%wordposition == 0){
  168.  
  169. line+=centreTextwithRandChars(uw.get(((i+numlines)/wordposition)).toUpperCase(),15,randchars);
  170. }
  171. else{
  172. line+=centreTextwithRandChars("",15,randchars);
  173. }
  174.  
  175. System.out.println(line);
  176. }
  177.  
  178. }
  179.  
  180. public static String centreTextwithRandChars(String c, int length, char[] cset){
  181. String centred = "";
  182. int startchar = (length-c.length())/2;
  183. for(int i=0; i<startchar; i++){
  184. centred+=cset[randInt(0,cset.length-1)];
  185. }
  186. centred+=c;
  187. while(centred.length()<length){
  188. centred+=cset[randInt(0,cset.length-1)];
  189. }
  190.  
  191. return centred;
  192. }
  193.  
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement