Advertisement
Guest User

Untitled

a guest
Mar 15th, 2012
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.66 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. public class MM {
  6. static String f = "";
  7.  
  8. public static void main(String[] args) {
  9. int s = 0;
  10. String n = "";
  11. String diff = "";
  12. boolean ans = false;
  13. int[] d = new int[4];
  14. int[] pos = new int[4];
  15. pos[0] = (int) (Math.random() * 100);
  16. pos[1] = (int) (Math.random() * 100);
  17. pos[2] = (int) (Math.random() * 100);
  18. pos[3] = (int) (Math.random() * 100);
  19. f = readLine("Welcome to MasterMind by Greg Cawthorne!\nIn this game you attempt to crack a code with only the aid of minor hints and you own ingenuity!\nPress enter to begin! ");
  20. f = readLine("Would you like to play with letters (1) or numbers (2)? ");
  21. do {
  22. if (f.equals("letters") || f.equals("1")) {
  23. diff = "letters";
  24. ans = true;
  25. System.out
  26. .println("The code will be made up 4 letters of either R,G,B,Y,P,T ");
  27. } else if (f.equals("numbers") || f.equals("2")) {
  28. diff = "numbers";
  29. ans = true;
  30. System.out
  31. .println("The code will be made up of 4 numbers of either 1,2,3,4,5,6 ");
  32. } else {
  33. f = readLine("Sorry, I didn't quite get that. Would you like to play with letters (1) or numbers (2)? ");
  34. }
  35. } while (ans == false);
  36. for (int x = 0; x <= 3; x++) {
  37. if (pos[x] <= 17) {
  38. d[x] = 1;
  39. } else if (pos[x] <= 34) {
  40. d[x] = 2;
  41. } else if (pos[x] <= 51) {
  42. d[x] = 3;
  43. } else if (pos[x] <= 68) {
  44. d[x] = 4;
  45. } else if (pos[x] <= 85) {
  46. d[x] = 5;
  47. } else if (pos[x] <= 100) {
  48. d[x] = 6;
  49. }}
  50. if (diff.equals("numbers")) {
  51. n = "numbers either 1,2,3,4,5 or 6";
  52. } else if (diff.equals("letters")) {
  53. n = "letters either R,B,G,Y,P or T all caps";
  54. }
  55.  
  56. System.out.println("The computer has thought of his sequence.");
  57. if(diff == "letters"){
  58. check(d, n, s);
  59. } else if (diff == "numbers"){
  60. check2(n, d, s);
  61. }
  62. }
  63.  
  64. public static void check(int[] d, String n, int s) {
  65. int count2= 0;
  66. String r = "R";
  67. String b = "B";
  68. String g = "G";
  69. String y = "Y";
  70. String p = "P";
  71. String tq = "T";
  72. String f = "";
  73. boolean z = false;
  74. int v = 0;
  75. f = readLine("Take a guess! ");
  76.  
  77. if ((f.length() == 4)
  78. && (f.charAt(0) == r.charAt(0) || f.charAt(0) == b.charAt(0)
  79. || f.charAt(0) == g.charAt(0)
  80. || f.charAt(0) == y.charAt(0)
  81. || f.charAt(0) == p.charAt(0) || f.charAt(0) == tq
  82. .charAt(0))
  83. && (f.charAt(1) == r.charAt(0) || f.charAt(1) == b.charAt(0)
  84. || f.charAt(1) == g.charAt(0)
  85. || f.charAt(1) == y.charAt(0)
  86. || f.charAt(1) == p.charAt(0) || f.charAt(1) == tq
  87. .charAt(0))
  88. && (f.charAt(2) == r.charAt(0) || f.charAt(2) == b.charAt(0)
  89. || f.charAt(2) == g.charAt(0)
  90. || f.charAt(2) == y.charAt(0)
  91. || f.charAt(2) == tq.charAt(0) || f.charAt(2) == p
  92. .charAt(0))
  93. && (f.charAt(3) == r.charAt(0) || f.charAt(3) == b.charAt(0)
  94. || f.charAt(3) == g.charAt(0)
  95. || f.charAt(3) == y.charAt(0)
  96. || f.charAt(3) == tq.charAt(0) || f.charAt(3) == p.charAt(0))) {
  97. z = true;
  98. s++;
  99. int chin[] = new int[6];
  100. int chin2[] = new int[6];
  101. char bin[] = new char[6];
  102. bin[0] = 'R'; bin[1] = 'B'; bin[2] = 'G'; bin[3] = 'Y'; bin[4] = 'P'; bin[5] = 'T';
  103. char a[] = new char[4];
  104. int t[] = new int[4];
  105. for (int x = 0;x<4;x++){
  106. a[x] = (f.charAt(x));
  107. }
  108. for (int eye = 0; eye< 4;eye++){
  109. for (int tim = 0; tim<6;tim++){
  110. if (a[eye] == bin[tim]){
  111. t[eye] = tim +1;
  112. }}
  113. }for (int x = 0;x<4;x++){
  114. if (t[x] == 1){
  115. chin[0] = chin[0]+1;
  116. } else if (t[x] == 2){
  117. chin[1] = chin[1]+1;
  118. } else if (t[x] == 3){
  119. chin[2] = chin[2]+1;
  120. } else if (t[x] == 4){
  121. chin[3] = chin[3]+1;
  122. } else if (t[x] == 5){
  123. chin[4] = chin[4]+1;
  124. } else if (t[x] == 6){
  125. chin[5] = chin[5]+1;
  126. }
  127. if (d[x] == 1){
  128. chin2[0] = chin2[0]+1;
  129. } else if (d[x] == 2){
  130. chin2[1] = chin2[1]+1;
  131. } else if (d[x] == 3){
  132. chin2[2] = chin2[2]+1;
  133. } else if (d[x] == 4){
  134. chin2[3] = chin2[3]+1;
  135. } else if (d[x] == 5){
  136. chin2[4] = chin2[4]+1;
  137. } else if (d[x] == 6){
  138. chin2[5] = chin2[5]+1;
  139. }
  140. }
  141.  
  142. for (int x = 0; x <= 3; x++) {
  143. if (d[x] == t[x]) {
  144. v++;
  145. }
  146. }
  147.  
  148. for (int x = 0;x<6;x++){
  149. if (chin[x] <= chin2[x]){
  150. count2 = count2 + chin[x];
  151. } else {
  152. count2 = count2 + chin2[x]; }
  153. } count2 = count2 - v;
  154. }
  155. if (z == false) {
  156. System.out.println("Invalid guess. Please guess four " + n
  157. + " with no spaces. :)");
  158. } else {
  159. System.out.println("You guessed: " + f);
  160. System.out.println("There are "+v+" letters in the right place,\nand "+count2+" other letters which are right, but in the wrong place.");
  161. }
  162. if (v == 4) {
  163. System.out.print("Well done you cracked the code in " + s
  164. + " attempts!\n");
  165. do{ f = readLine("Do you want to play again? ");
  166. if (f.equals("yes") || f.equals("y") || f.equals("YES")){
  167. main(null);
  168. } else if (f.equals("no") || f.equals("n") || f.equals("NO")){
  169. System.out.println("I hope you enjoyed MasterMind by Greg Cawthorne!");
  170. System.exit(0);
  171. } else {
  172. System.out.print("Sorry I didn't quite get that. ");
  173. z = false;
  174. }} while (z == false);
  175. } check(d, n, s);
  176. }
  177.  
  178. public static void check2(String n, int[] d, int s){
  179. int count2 = 0;
  180. boolean z = false;
  181. int v = 0;
  182. String one = "1";
  183. String two = "2";
  184. String three = "3";
  185. String four = "4";
  186. String five = "5";
  187. String six = "6";
  188. f = readLine("Take a guess! ");
  189. if((f.length() == 4) && (f.charAt(0) == one.charAt(0) || f.charAt(0) == two.charAt(0) || f.charAt(0) == three.charAt(0)
  190. || f.charAt(0) == four.charAt(0) || f.charAt(0) == five.charAt(0) || f.charAt(0) == six.charAt(0))
  191. && (f.charAt(1) == one.charAt(0) || f.charAt(1) == two.charAt(0)
  192. || f.charAt(1) == three.charAt(0)
  193. || f.charAt(1) == four.charAt(0)
  194. || f.charAt(1) == five.charAt(0) || f.charAt(1) == six.charAt(0))
  195. && (f.charAt(2) == one.charAt(0) || f.charAt(2) == two.charAt(0)
  196. || f.charAt(2) == three.charAt(0)
  197. || f.charAt(2) == four.charAt(0)
  198. || f.charAt(2) == five.charAt(0) || f.charAt(2) == six.charAt(0))
  199. && (f.charAt(3) == one.charAt(0) || f.charAt(3) == two.charAt(0)
  200. || f.charAt(3) == three.charAt(0)
  201. || f.charAt(3) == four.charAt(0)
  202. || f.charAt(3) == five.charAt(0) || f.charAt(3) == six.charAt(0))){
  203. z = true;
  204. s++;
  205. int chin[] = new int[6];
  206. int chin2[] = new int[6];
  207. String a[] = new String[4];
  208. int t[] = new int[4];
  209. for (int x = 0;x<=3;x++){
  210. a[x] = String.valueOf(f.charAt(x));
  211. t[x] = Integer.parseInt(a[x]);
  212. if (t[x] == 1){
  213. chin[0] = chin[0]+1;
  214. } else if (t[x] == 2){
  215. chin[1] = chin[1]+1;
  216. } else if (t[x] == 3){
  217. chin[2] = chin[2]+1;
  218. } else if (t[x] == 4){
  219. chin[3] = chin[3]+1;
  220. } else if (t[x] == 5){
  221. chin[4] = chin[4]+1;
  222. } else if (t[x] == 6){
  223. chin[5] = chin[5]+1;
  224. }
  225. if (d[x] == 1){
  226. chin2[0] = chin2[0]+1;
  227. } else if (d[x] == 2){
  228. chin2[1] = chin2[1]+1;
  229. } else if (d[x] == 3){
  230. chin2[2] = chin2[2]+1;
  231. } else if (d[x] == 4){
  232. chin2[3] = chin2[3]+1;
  233. } else if (d[x] == 5){
  234. chin2[4] = chin2[4]+1;
  235. } else if (d[x] == 6){
  236. chin2[5] = chin2[5]+1;
  237. }
  238. }
  239. System.out.println("You guessed: " + f);
  240. for (int x = 0; x <= 3; x++) {
  241. if (d[x] == t[x]) {
  242. v++;
  243. }
  244. }
  245.  
  246. for (int x = 0;x<6;x++){
  247. if (chin[x] <= chin2[x]){
  248. count2 = count2 + chin[x];
  249. } else {
  250. count2 = count2 + chin2[x]; }
  251. } count2 = count2 - v;
  252. }
  253. if (z == false) {
  254. System.out.println("Invalid guess. Please guess four " + n
  255. + " with no spaces. :)");
  256. } else {
  257. System.out.println("There are "+v+" numbers in the right place,\nand "+count2+" other numbers which are right, but in the wrong place.");
  258. }
  259. if (v == 4) {
  260. System.out.print("Well done you cracked the code in " + s
  261. + " attempts!\n");
  262. do{ f = readLine("Do you want to play again? ");
  263. if (f.equals("yes") || f.equals("y") || f.equals("YES")){
  264. main(null);
  265. } else if (f.equals("no") || f.equals("n") || f.equals("NO")){
  266. System.out.println("I hope you enjoyed MasterMind by Greg Cawthorne!");
  267. System.exit(0);
  268. } else {
  269. System.out.print("Sorry I didn't quite get that. ");
  270. z = false;
  271. }} while (z == false);
  272. } check2(n, d, s);
  273. }
  274.  
  275.  
  276. public static String readLine(String prompt) {
  277. String input = "";
  278. System.out.print(prompt);
  279. InputStreamReader isr = new InputStreamReader(System.in);
  280. BufferedReader br = new BufferedReader(isr);
  281. try {
  282.  
  283. input = br.readLine();
  284.  
  285. } catch (IOException ioe) {
  286. }
  287. return input;
  288. }
  289. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement