Created
February 25, 2024 03:19
-
-
Save PatheticMustan/802f69d15250b55eba709f1e54c0221a to your computer and use it in GitHub Desktop.
our code from an arduino workshop done by Maxlinear
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Keypad.h> | |
const byte ROWS = 4; //four rows | |
const byte COLS = 4; //four columns | |
//define the cymbols on the buttons of the keypads | |
char hexaKeys[ROWS][COLS] = { | |
{'1','2','3','A'}, | |
{'4','5','6','B'}, | |
{'7','8','9','C'}, | |
{'*','0','#','D'} | |
}; | |
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad | |
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad | |
// generate a four digit number | |
int secretRandomNumber[4]; | |
int entry[4]; | |
int digitsEntered = 0; | |
int bulls = 0; | |
int cows = 0; | |
/** | |
* 0 - unmatched | |
* 1 - bulls | |
* 2 - cows | |
*/ | |
int bullCowChecker[8]; | |
//initialize an instance of class NewKeypad | |
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); | |
void setup(){ | |
Serial.begin(9600); | |
randomSeed(analogRead(A1)); | |
for (int i=0; i<4; i++) secretRandomNumber[i] = random(0, 9); | |
// output the answer | |
for (int i=0; i<4; i++) Serial.println(secretRandomNumber[i]); | |
Serial.println("Guess the four digit number!"); | |
} | |
void loop(){ | |
char customKey = customKeypad.waitForKey(); | |
if (customKey){ | |
Serial.print("You pressed: "); | |
Serial.println(customKey); | |
entry[digitsEntered] = customKey - 48; | |
digitsEntered++; | |
if (digitsEntered == 4) { | |
// WE DO IT ALL AT ONCE!!!!! | |
for (int i=0; i<4; i++) { | |
for (int j=0; j<4; j++) { | |
if (bullCowChecker[i] != 0) break; | |
if (bullCowChecker[4+j] != 0) continue; | |
if (entry[i] == secretRandomNumber[j]) { | |
if (i == j) { | |
bullCowChecker[i] = 1; | |
bullCowChecker[4+j] = 1; | |
} | |
} | |
} | |
} | |
for (int i=0; i<4; i++) { | |
for (int j=0; j<4; j++) { | |
if (bullCowChecker[i] != 0) break; | |
if (bullCowChecker[4+j] != 0) continue; | |
if (entry[i] == secretRandomNumber[j]) { | |
bullCowChecker[i] = 2; | |
bullCowChecker[4+j] = 2; | |
} | |
} | |
} | |
int cows = 0; | |
int bulls = 0; | |
for (int i=0; i<4; i++) { | |
Serial.println(bullCowChecker[i]); | |
if (bullCowChecker[i] == 1) bulls++; | |
if (bullCowChecker[i] == 2) cows++; | |
} | |
// print guess, bulls, cows | |
Serial.print("Your guess: "); | |
for (int i=0; i<4; i++) Serial.print(entry[i]); | |
Serial.println(); | |
Serial.print("Bulls: "); | |
Serial.println(bulls); | |
Serial.print("Cows: "); | |
Serial.println(cows); | |
digitsEntered = 0; | |
for (int i=0; i<4; i++) { | |
bullCowChecker[i] = 0; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment