Advertisement
Guest User

Untitled

a guest
Jun 11th, 2012
4,882
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.71 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. System.out.println();
  99. s++;
  100. int chin[] = new int[6];
  101. int chin2[] = new int[6];
  102. char bin[] = new char[6];
  103. bin[0] = 'R'; bin[1] = 'B'; bin[2] = 'G'; bin[3] = 'Y'; bin[4] = 'P'; bin[5] = 'T';
  104. char a[] = new char[4];
  105. int t[] = new int[4];
  106. for (int x = 0;x<4;x++){
  107. a[x] = (f.charAt(x));
  108. }
  109. for (int eye = 0; eye< 4;eye++){
  110. for (int tim = 0; tim<6;tim++){
  111. if (a[eye] == bin[tim]){
  112. t[eye] = tim +1;
  113. }}
  114. }for (int x = 0;x<4;x++){
  115. if (t[x] == 1){
  116. chin[0] = chin[0]+1;
  117. } else if (t[x] == 2){
  118. chin[1] = chin[1]+1;
  119. } else if (t[x] == 3){
  120. chin[2] = chin[2]+1;
  121. } else if (t[x] == 4){
  122. chin[3] = chin[3]+1;
  123. } else if (t[x] == 5){
  124. chin[4] = chin[4]+1;
  125. } else if (t[x] == 6){
  126. chin[5] = chin[5]+1;
  127. }
  128. if (d[x] == 1){
  129. chin2[0] = chin2[0]+1;
  130. } else if (d[x] == 2){
  131. chin2[1] = chin2[1]+1;
  132. } else if (d[x] == 3){
  133. chin2[2] = chin2[2]+1;
  134. } else if (d[x] == 4){
  135. chin2[3] = chin2[3]+1;
  136. } else if (d[x] == 5){
  137. chin2[4] = chin2[4]+1;
  138. } else if (d[x] == 6){
  139. chin2[5] = chin2[5]+1;
  140. }
  141. }
  142.  
  143. for (int x = 0; x <= 3; x++) {
  144. if (d[x] == t[x]) {
  145. v++;
  146. }
  147. }
  148.  
  149. for (int x = 0;x<6;x++){
  150. if (chin[x] <= chin2[x]){
  151. count2 = count2 + chin[x];
  152. } else {
  153. count2 = count2 + chin2[x]; }
  154. } count2 = count2 - v;
  155. }
  156. if (z == false) {
  157. System.out.println("Invalid guess. Please guess four " + n
  158. + " with no spaces. :)");
  159. } else {
  160. System.out.println("You guessed: " + f);
  161. System.out.println("There are "+v+" letters in the right place,\nand "+count2+" other letters which are right, but in the wrong place.");
  162. }
  163. if (v == 4) {
  164. System.out.print("Well done you cracked the code in " + s
  165. + " attempts!\n");
  166. do{ f = readLine("Do you want to play again? ");
  167. if (f.equals("yes") || f.equals("y") || f.equals("YES")){
  168. main(null);
  169. } else if (f.equals("no") || f.equals("n") || f.equals("NO")){
  170. System.out.println("I hope you enjoyed MasterMind by Greg Cawthorne!");
  171. System.exit(0);
  172. } else {
  173. System.out.print("Sorry I didn't quite get that. ");
  174. z = false;
  175. }} while (z == false);
  176. } check(d, n, s);
  177. }
  178.  
  179. public static void check2(String n, int[] d, int s){
  180. int count2 = 0;
  181. boolean z = false;
  182. int v = 0;
  183. String one = "1";
  184. String two = "2";
  185. String three = "3";
  186. String four = "4";
  187. String five = "5";
  188. String six = "6";
  189. f = readLine("Take a guess! ");
  190. if((f.length() == 4) && (f.charAt(0) == one.charAt(0) || f.charAt(0) == two.charAt(0) || f.charAt(0) == three.charAt(0)
  191. || f.charAt(0) == four.charAt(0) || f.charAt(0) == five.charAt(0) || f.charAt(0) == six.charAt(0))
  192. && (f.charAt(1) == one.charAt(0) || f.charAt(1) == two.charAt(0)
  193. || f.charAt(1) == three.charAt(0)
  194. || f.charAt(1) == four.charAt(0)
  195. || f.charAt(1) == five.charAt(0) || f.charAt(1) == six.charAt(0))
  196. && (f.charAt(2) == one.charAt(0) || f.charAt(2) == two.charAt(0)
  197. || f.charAt(2) == three.charAt(0)
  198. || f.charAt(2) == four.charAt(0)
  199. || f.charAt(2) == five.charAt(0) || f.charAt(2) == six.charAt(0))
  200. && (f.charAt(3) == one.charAt(0) || f.charAt(3) == two.charAt(0)
  201. || f.charAt(3) == three.charAt(0)
  202. || f.charAt(3) == four.charAt(0)
  203. || f.charAt(3) == five.charAt(0) || f.charAt(3) == six.charAt(0))){
  204. z = true;
  205. System.out.println();
  206. s++;
  207. int chin[] = new int[6];
  208. int chin2[] = new int[6];
  209. String a[] = new String[4];
  210. int t[] = new int[4];
  211. for (int x = 0;x<=3;x++){
  212. a[x] = String.valueOf(f.charAt(x));
  213. t[x] = Integer.parseInt(a[x]);
  214. if (t[x] == 1){
  215. chin[0] = chin[0]+1;
  216. } else if (t[x] == 2){
  217. chin[1] = chin[1]+1;
  218. } else if (t[x] == 3){
  219. chin[2] = chin[2]+1;
  220. } else if (t[x] == 4){
  221. chin[3] = chin[3]+1;
  222. } else if (t[x] == 5){
  223. chin[4] = chin[4]+1;
  224. } else if (t[x] == 6){
  225. chin[5] = chin[5]+1;
  226. }
  227. if (d[x] == 1){
  228. chin2[0] = chin2[0]+1;
  229. } else if (d[x] == 2){
  230. chin2[1] = chin2[1]+1;
  231. } else if (d[x] == 3){
  232. chin2[2] = chin2[2]+1;
  233. } else if (d[x] == 4){
  234. chin2[3] = chin2[3]+1;
  235. } else if (d[x] == 5){
  236. chin2[4] = chin2[4]+1;
  237. } else if (d[x] == 6){
  238. chin2[5] = chin2[5]+1;
  239. }
  240. }
  241. System.out.println("You guessed: " + f);
  242. for (int x = 0; x <= 3; x++) {
  243. if (d[x] == t[x]) {
  244. v++;
  245. }
  246. }
  247.  
  248. for (int x = 0;x<6;x++){
  249. if (chin[x] <= chin2[x]){
  250. count2 = count2 + chin[x];
  251. } else {
  252. count2 = count2 + chin2[x]; }
  253. } count2 = count2 - v;
  254. }
  255. if (z == false) {
  256. System.out.println("Invalid guess. Please guess four " + n
  257. + " with no spaces. :)");
  258. } else {
  259. System.out.println("There are "+v+" numbers in the right place,\nand "+count2+" other numbers which are right, but in the wrong place.");
  260. }
  261. if (v == 4) {
  262. System.out.print("Well done you cracked the code in " + s
  263. + " attempts!\n");
  264. do{ f = readLine("Do you want to play again? ");
  265. if (f.equals("yes") || f.equals("y") || f.equals("YES")){
  266. main(null);
  267. } else if (f.equals("no") || f.equals("n") || f.equals("NO")){
  268. System.out.println("I hope you enjoyed MasterMind by Greg Cawthorne!");
  269. System.exit(0);
  270. } else {
  271. System.out.print("Sorry I didn't quite get that. ");
  272. z = false;
  273. }} while (z == false);
  274. } check2(n, d, s);
  275. }
  276.  
  277.  
  278. public static String readLine(String prompt) {
  279. String input = "";
  280. System.out.print(prompt);
  281. InputStreamReader isr = new InputStreamReader(System.in);
  282. BufferedReader br = new BufferedReader(isr);
  283. try {
  284.  
  285. input = br.readLine();
  286.  
  287. } catch (IOException ioe) {
  288. }
  289. return input;
  290. }
  291. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement