import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class MM { static String f = ""; public static void main(String[] args) { int s = 0; String n = ""; String diff = ""; boolean ans = false; int[] d = new int[4]; int[] pos = new int[4]; pos[0] = (int) (Math.random() * 100); pos[1] = (int) (Math.random() * 100); pos[2] = (int) (Math.random() * 100); pos[3] = (int) (Math.random() * 100); 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! "); f = readLine("Would you like to play with letters (1) or numbers (2)? "); do { if (f.equals("letters") || f.equals("1")) { diff = "letters"; ans = true; System.out .println("The code will be made up 4 letters of either R,G,B,Y,P,T "); } else if (f.equals("numbers") || f.equals("2")) { diff = "numbers"; ans = true; System.out .println("The code will be made up of 4 numbers of either 1,2,3,4,5,6 "); } else { f = readLine("Sorry, I didn't quite get that. Would you like to play with letters (1) or numbers (2)? "); } } while (ans == false); for (int x = 0; x <= 3; x++) { if (pos[x] <= 17) { d[x] = 1; } else if (pos[x] <= 34) { d[x] = 2; } else if (pos[x] <= 51) { d[x] = 3; } else if (pos[x] <= 68) { d[x] = 4; } else if (pos[x] <= 85) { d[x] = 5; } else if (pos[x] <= 100) { d[x] = 6; }} if (diff.equals("numbers")) { n = "numbers either 1,2,3,4,5 or 6"; } else if (diff.equals("letters")) { n = "letters either R,B,G,Y,P or T all caps"; } System.out.println("The computer has thought of his sequence."); if(diff == "letters"){ check(d, n, s); } else if (diff == "numbers"){ check2(n, d, s); } } public static void check(int[] d, String n, int s) { int count2= 0; String r = "R"; String b = "B"; String g = "G"; String y = "Y"; String p = "P"; String tq = "T"; String f = ""; boolean z = false; int v = 0; f = readLine("Take a guess! "); if ((f.length() == 4) && (f.charAt(0) == r.charAt(0) || f.charAt(0) == b.charAt(0) || f.charAt(0) == g.charAt(0) || f.charAt(0) == y.charAt(0) || f.charAt(0) == p.charAt(0) || f.charAt(0) == tq .charAt(0)) && (f.charAt(1) == r.charAt(0) || f.charAt(1) == b.charAt(0) || f.charAt(1) == g.charAt(0) || f.charAt(1) == y.charAt(0) || f.charAt(1) == p.charAt(0) || f.charAt(1) == tq .charAt(0)) && (f.charAt(2) == r.charAt(0) || f.charAt(2) == b.charAt(0) || f.charAt(2) == g.charAt(0) || f.charAt(2) == y.charAt(0) || f.charAt(2) == tq.charAt(0) || f.charAt(2) == p .charAt(0)) && (f.charAt(3) == r.charAt(0) || f.charAt(3) == b.charAt(0) || f.charAt(3) == g.charAt(0) || f.charAt(3) == y.charAt(0) || f.charAt(3) == tq.charAt(0) || f.charAt(3) == p.charAt(0))) { z = true; System.out.println(); s++; int chin[] = new int[6]; int chin2[] = new int[6]; char bin[] = new char[6]; bin[0] = 'R'; bin[1] = 'B'; bin[2] = 'G'; bin[3] = 'Y'; bin[4] = 'P'; bin[5] = 'T'; char a[] = new char[4]; int t[] = new int[4]; for (int x = 0;x<4;x++){ a[x] = (f.charAt(x)); } for (int eye = 0; eye< 4;eye++){ for (int tim = 0; tim<6;tim++){ if (a[eye] == bin[tim]){ t[eye] = tim +1; }} }for (int x = 0;x<4;x++){ if (t[x] == 1){ chin[0] = chin[0]+1; } else if (t[x] == 2){ chin[1] = chin[1]+1; } else if (t[x] == 3){ chin[2] = chin[2]+1; } else if (t[x] == 4){ chin[3] = chin[3]+1; } else if (t[x] == 5){ chin[4] = chin[4]+1; } else if (t[x] == 6){ chin[5] = chin[5]+1; } if (d[x] == 1){ chin2[0] = chin2[0]+1; } else if (d[x] == 2){ chin2[1] = chin2[1]+1; } else if (d[x] == 3){ chin2[2] = chin2[2]+1; } else if (d[x] == 4){ chin2[3] = chin2[3]+1; } else if (d[x] == 5){ chin2[4] = chin2[4]+1; } else if (d[x] == 6){ chin2[5] = chin2[5]+1; } } for (int x = 0; x <= 3; x++) { if (d[x] == t[x]) { v++; } } for (int x = 0;x<6;x++){ if (chin[x] <= chin2[x]){ count2 = count2 + chin[x]; } else { count2 = count2 + chin2[x]; } } count2 = count2 - v; } if (z == false) { System.out.println("Invalid guess. Please guess four " + n + " with no spaces. :)"); } else { System.out.println("You guessed: " + f); System.out.println("There are "+v+" letters in the right place,\nand "+count2+" other letters which are right, but in the wrong place."); } if (v == 4) { System.out.print("Well done you cracked the code in " + s + " attempts!\n"); do{ f = readLine("Do you want to play again? "); if (f.equals("yes") || f.equals("y") || f.equals("YES")){ main(null); } else if (f.equals("no") || f.equals("n") || f.equals("NO")){ System.out.println("I hope you enjoyed MasterMind by Greg Cawthorne!"); System.exit(0); } else { System.out.print("Sorry I didn't quite get that. "); z = false; }} while (z == false); } check(d, n, s); } public static void check2(String n, int[] d, int s){ int count2 = 0; boolean z = false; int v = 0; String one = "1"; String two = "2"; String three = "3"; String four = "4"; String five = "5"; String six = "6"; f = readLine("Take a guess! "); if((f.length() == 4) && (f.charAt(0) == one.charAt(0) || f.charAt(0) == two.charAt(0) || f.charAt(0) == three.charAt(0) || f.charAt(0) == four.charAt(0) || f.charAt(0) == five.charAt(0) || f.charAt(0) == six.charAt(0)) && (f.charAt(1) == one.charAt(0) || f.charAt(1) == two.charAt(0) || f.charAt(1) == three.charAt(0) || f.charAt(1) == four.charAt(0) || f.charAt(1) == five.charAt(0) || f.charAt(1) == six.charAt(0)) && (f.charAt(2) == one.charAt(0) || f.charAt(2) == two.charAt(0) || f.charAt(2) == three.charAt(0) || f.charAt(2) == four.charAt(0) || f.charAt(2) == five.charAt(0) || f.charAt(2) == six.charAt(0)) && (f.charAt(3) == one.charAt(0) || f.charAt(3) == two.charAt(0) || f.charAt(3) == three.charAt(0) || f.charAt(3) == four.charAt(0) || f.charAt(3) == five.charAt(0) || f.charAt(3) == six.charAt(0))){ z = true; System.out.println(); s++; int chin[] = new int[6]; int chin2[] = new int[6]; String a[] = new String[4]; int t[] = new int[4]; for (int x = 0;x<=3;x++){ a[x] = String.valueOf(f.charAt(x)); t[x] = Integer.parseInt(a[x]); if (t[x] == 1){ chin[0] = chin[0]+1; } else if (t[x] == 2){ chin[1] = chin[1]+1; } else if (t[x] == 3){ chin[2] = chin[2]+1; } else if (t[x] == 4){ chin[3] = chin[3]+1; } else if (t[x] == 5){ chin[4] = chin[4]+1; } else if (t[x] == 6){ chin[5] = chin[5]+1; } if (d[x] == 1){ chin2[0] = chin2[0]+1; } else if (d[x] == 2){ chin2[1] = chin2[1]+1; } else if (d[x] == 3){ chin2[2] = chin2[2]+1; } else if (d[x] == 4){ chin2[3] = chin2[3]+1; } else if (d[x] == 5){ chin2[4] = chin2[4]+1; } else if (d[x] == 6){ chin2[5] = chin2[5]+1; } } System.out.println("You guessed: " + f); for (int x = 0; x <= 3; x++) { if (d[x] == t[x]) { v++; } } for (int x = 0;x<6;x++){ if (chin[x] <= chin2[x]){ count2 = count2 + chin[x]; } else { count2 = count2 + chin2[x]; } } count2 = count2 - v; } if (z == false) { System.out.println("Invalid guess. Please guess four " + n + " with no spaces. :)"); } else { System.out.println("There are "+v+" numbers in the right place,\nand "+count2+" other numbers which are right, but in the wrong place."); } if (v == 4) { System.out.print("Well done you cracked the code in " + s + " attempts!\n"); do{ f = readLine("Do you want to play again? "); if (f.equals("yes") || f.equals("y") || f.equals("YES")){ main(null); } else if (f.equals("no") || f.equals("n") || f.equals("NO")){ System.out.println("I hope you enjoyed MasterMind by Greg Cawthorne!"); System.exit(0); } else { System.out.print("Sorry I didn't quite get that. "); z = false; }} while (z == false); } check2(n, d, s); } public static String readLine(String prompt) { String input = ""; System.out.print(prompt); InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); try { input = br.readLine(); } catch (IOException ioe) { } return input; } }