/* This sketch plays a Mastermind-like game using the Arduino with the keyboard as input and the Serial monitor as output. Visit subsystems.us for more fun projects!! */ int code[4] = {0, 0, 0, 0}; int guess[4] = {0, 0, 0, 0}; int correctPosition = 0; // How many are in the correct position int correctNumber = 0; // How many are correct number/wrong position void setup() { Serial.begin(9600); // Start the Serial port randomSeed(analogRead(0)); gameSetup(); Serial.println("*** MASTERBRAIN ***"); Serial.println(); Serial.println("Enter 4 numbers from 0 to 5"); Serial.println("and send them to the Arduino."); Serial.println("Result shows how many are correct number/position combo."); Serial.println("and how many are a correct number."); Serial.println("You have 10 rounds to guess the code."); Serial.println(); } void loop() { Serial.println(); Serial.println("Round: Guess: Result:"); for (int i = 0; i < 10; i++) { // There are 10 rounds Serial.print(" "); Serial.print(i + 1); Serial.print(": "); getUserGuess(); if (checkUserGuess()) { // You guessed right. Say so and exit Serial.println(); Serial.println(); Serial.println("YOU ARE RIGHT !!!"); Serial.print("You guessed it on level "); Serial.println(i + 1); Serial.println(); break; } else { // You did not guess it. Display results Serial.print(guess[0]); Serial.print(" "); Serial.print(guess[1]); Serial.print(" "); Serial.print(guess[2]); Serial.print(" "); Serial.print(guess[3]); Serial.print(" "); Serial.print(correctPosition); Serial.print(" "); Serial.println(correctNumber); } } if (correctPosition !=4) { // you lost Serial.println("Sorry, you lost."); Serial.print("The correct code sequence was "); Serial.print(code[0]); Serial.print(" "); Serial.print(code[1]); Serial.print(" "); Serial.print(code[2]); Serial.print(" "); Serial.print(code[3]); } gameSetup(); Serial.println(); } void getNewCode() { for (int i = 0; i < 4; i++) { code[i] = int(random(6)); } } void getUserGuess() { while (Serial.available() == 0) {} // Wait for data int indx = 0; while (indx < 4) { while (Serial.available() > 0) { guess[indx] = Serial.read() - 48; indx += 1; } } } boolean checkUserGuess() { correctPosition = 0; correctNumber = 0; boolean usedGuess[4] = {false, false, false, false}; boolean usedCode[4] = {false, false, false, false}; // We will loop through the guess and tally the results // We will use the used matrix to track if the code position has been accounted for for (int i = 0; i < 4; i++) { // check for right position if (code[i] == guess[i]) { // the guess is correct and in the right position correctPosition += 1; usedGuess[i] = true; // Mark the guess and code position as used usedCode[i] = true; } } // Now check for right number with what is not yet used. for (int i = 0; i < 4; i++) { // i represents the guess for (int j = 0; j < 4; j++) { // j is the actual code if (!usedGuess[i] && !usedCode[j] && (code[j] == guess[i])) { correctNumber += 1; usedGuess[i] = true; usedCode[j] = true; } } } if (correctPosition == 4) return true; else return false; } void gameSetup() { randomSeed(analogRead(0)); getNewCode(); correctPosition = 0; correctNumber = 0; }