package de.ulbrich.patrick; import inouthelpers.*; public class Mastermind { // color definitions private static final int COLOR_RED = 0; private static final int COLOR_YELLOW = 1; private static final int COLOR_GREEN = 2; private static final int COLOR_BLUE = 3; private static final int COLOR_PINK = 4; private static final int COLOR_BROWN = 5; public static void main(String[] args) { // hardcoded slot colorpattern int slot1Color = COLOR_BLUE; int slot2Color = COLOR_PINK; int slot3Color = COLOR_BLUE; Out.println("java Mastermind"); Out.println("Rot = 0, gelb = 1, gruen = 2, blau = 3, pink = 4, braun = 5"); Out.print("Bitte Zahlen durch Leerzeichen getrennt eingeben: "); // read in user colors int userColor1 = In.readInt(); int userColor2 = In.readInt(); int userColor3 = In.readInt(); Out.print("Wertung:"); // flags indicating whether user color 1,2,3 matches perfectly boolean userColor1Matches = false; boolean userColor2Matches = false; boolean userColor3Matches = false; // flags indicating whether a slot is assigned to an user color already boolean slot1Assigned = false; boolean slot2Assigned = false; boolean slot3Assigned = false; // test if all user colors match perfectly if (userColor1 == slot1Color) { Out.print(" Schwarz"); userColor1Matches = true; slot1Assigned = true; } if (userColor2 == slot2Color) { Out.print(" Schwarz"); userColor2Matches = true; slot2Assigned = true; } if (userColor3 == slot3Color) { Out.print(" Schwarz"); userColor3Matches = true; slot3Assigned = true; } // if user color 1 didn't match perfectly, test if it matches slot 2 or 3 if (!userColor1Matches) { if (!slot2Assigned && slot2Color == userColor1) { Out.print(" Weiss"); slot2Assigned = true; } else if (!slot3Assigned && slot3Color == userColor1) { Out.print(" Weiss"); slot3Assigned = true; } } // if user color 2 didn't match perfectly, test if it matches slot 1 or 3 if (!userColor2Matches) { if (!slot1Assigned && slot1Color == userColor2) { Out.print(" Weiss"); slot1Assigned = true; } else if (!slot3Assigned && slot3Color == userColor2) { Out.print(" Weiss"); slot3Assigned = true; } } // if user color 3 didn't match perfectly, test if it matches slot 1 or 2 if (!userColor3Matches) { if (!slot1Assigned && slot1Color == userColor3) { Out.print(" Weiss"); slot1Assigned = true; } else if (!slot2Assigned && slot2Color == userColor3) { Out.print(" Weiss"); slot2Assigned = true; } } } }