addedMastermind

This commit is contained in:
lunarised 2019-06-19 21:30:25 +12:00
parent b76067d67a
commit f9272cea90

246
Mastermind/Mastermind.ino Normal file
View File

@ -0,0 +1,246 @@
#define RIP (inp > 570 && inp < 580)
#define YIP (inp > 780 && inp < 790)
#define GIP (inp > 840 && inp < 850)
#define BIP (inp > 928 && inp < 938)
static const int CCCP = 11;
static const int CCIP = 12;
static const int ICIP = 13;
static const int BI = 4;
static const int GI = 5;
static const int YI = 6;
static const int RI = 7;
int v1 = 0;
int v2 = 0;
int v3 = 0;
int v4 = 0;
int diff = 2;
int turnsRemaining = 0;
int master = 0;
int incorrect = 0;
int correct = 0;
int partial = 0;
bool win = false;
void setup() {
pinMode(CCCP, OUTPUT);
pinMode(CCIP, OUTPUT);
pinMode(ICIP, OUTPUT);
pinMode(BI, OUTPUT);
pinMode(GI, OUTPUT);
pinMode(YI, OUTPUT);
pinMode(RI, OUTPUT);
pinMode (A1, INPUT_PULLUP);
Serial.begin(9600);
randomSeed(analogRead(0));
}
void loop() {
// put your main code here, to run repeatedly:
int inp = analogRead(A1);
generateRandomValues();
Serial.println(master);
correct = 0;
for (int d = 0; d<diff; d++){
correct = 0;
incorrect = 0;
partial = 0;
inputValues();
checkValues();
showFeedback();
if(correct == 4){
d = diff;
win = true;
}
}
if (win){
for (int w = 0; w< 10; w++){
digitalWrite(GI, HIGH);
digitalWrite(CCCP, LOW);
delay (500);
digitalWrite(GI, LOW);
digitalWrite(CCCP, HIGH);
delay(500);
}
digitalWrite(CCCP, LOW);
}
else{
for (int l = 0; l< 10; l++){
digitalWrite(RI, HIGH);
digitalWrite(ICIP, LOW);
delay (500);
digitalWrite(RI, LOW);
digitalWrite(ICIP, HIGH);
delay(500);
}
digitalWrite(ICIP, LOW);
}
}
/* SETS THE GAME DIFFICULTY */
void difficultyState() {
diff = readPins();
turnsRemaining = (diff * 4) + 4;
}
int readPins() {
int inp = analogRead(A1);
if (RIP) {
return 0;
}
else if (YIP) {
return 1;
}
else if (GIP) {
return 2;
}
else if (BIP) {
return 3;
}
else {
return 5;
}
}
void generateRandomValues() {
master = random(0, 256);
master = 228;
}
void checkValues() {
bool v1p = false, v2p = false, v3p = false , v4p = false;
int masterProc[] {0, 0, 0, 0};
for (int i = 0; i < 8; i += 2) {
bool succrun = false;
byte b1 = bitRead(master, i);
byte b2 = bitRead(master, i + 1);
if (bitRead(b1, 0) == bitRead(v1, 0 ) && bitRead(b2, 0) == bitRead(v1, 1)) {
if (i == 0) {
correct ++;
Serial.println("First Correct");
v1p = true;
succrun = true;
}
}
if (bitRead(b1, 0) == bitRead(v2, 0) && bitRead(b2, 0) == bitRead(v2, 1)) {
if (i == 2) {
correct ++;
Serial.println("2nd Correct");
v2p = true;
succrun = true;
}
}
if (bitRead(b1, 0) == bitRead(v3, 0 ) && bitRead(b2, 0) == bitRead(v3, 1)) {
if (i == 4) {
correct ++;
Serial.println("3rd Correct");
v3p = true;
succrun = true;
}
}
if (bitRead(b1, 0) == bitRead(v4, 0) && bitRead(b2, 0) == bitRead(v4, 1)) {
if (i == 6) {
correct ++;
Serial.println("4 Correct");
v4p = true;
succrun = true;
}
}
if (succrun) {
masterProc[i / 2] = 1;
}
}
for (int j = 0; j < 4; j++) {
if (masterProc[j] == 0) {
byte b1 = bitRead(master, j * 2);
byte b2 = bitRead(master, (j * 2) + 1);
bool runFail = false;
if (!v1p && runFail == false) {
if (bitRead(b1, 0) == bitRead(v1, 0 ) && bitRead(b2, 0) == bitRead(v1, 1)) {
partial++;
v1p = true;
masterProc[j] = 1;
runFail = true;
}
}
if (!v2p && runFail == false) {
if (bitRead(b1, 0) == bitRead(v2, 0 ) && bitRead(b2, 0) == bitRead(v2, 1)) {
v2p = true;
masterProc[j] = 1;
partial++;
runFail = true;
}
}
if (!v3p && runFail == false) {
if (bitRead(b1, 0) == bitRead(v3, 0 ) && bitRead(b2, 0) == bitRead(v3, 1)) {
v3p = true;
partial++;
masterProc[j] = 1;
runFail = true;
}
}
if (!v4p && runFail == false) {
if (bitRead(b1, 0) == bitRead(v4, 0 ) && bitRead(b2, 0) == bitRead(v4, 1)) {
v4p = true;
masterProc[j] = 1;
partial++;
runFail = true;
}
}
}
}
for (int i = 0; i < 4; i++) {
if (masterProc[i] == 0) {
incorrect++;
}
}
}
void showFeedback() {
for (int i = 0; i < correct; i++) {
digitalWrite(CCCP, HIGH);
delay (500);
digitalWrite(CCCP, LOW);
delay (500);
}
for (int i = 0; i < partial; i++) {
digitalWrite(CCIP, HIGH);
delay (500);
digitalWrite(CCIP, LOW);
delay (500);
}
for (int i = 0; i < incorrect; i++) {
digitalWrite(ICIP, HIGH);
delay (500);
digitalWrite(ICIP, LOW);
delay (500);
}
}
void inputValues() {
byte input = 0;
digitalWrite(RI, HIGH);
while (readPins() == 5) {
}
v1 = readPins();
digitalWrite(RI, LOW);
delay(250);
digitalWrite(YI, HIGH);
while (readPins() == 5) {
}
v2 = readPins();
digitalWrite(YI, LOW);
delay(250);
digitalWrite(GI, HIGH);
while (readPins() == 5) {
}
v3 = readPins();
digitalWrite(GI, LOW);
delay(250);
digitalWrite(BI, HIGH);
while (readPins() == 5) {
}
v4 = readPins();
digitalWrite(BI, LOW);
delay(250);
}