Add anti cheat

This commit is contained in:
Gabriel Augendre 2022-03-07 17:28:34 +01:00
parent 0319cff4a8
commit 480ff2d603
2 changed files with 95 additions and 41 deletions

View file

@ -5,19 +5,19 @@
#define LCD_COLS 16 #define LCD_COLS 16
#define LCD_ROWS 2 #define LCD_ROWS 2
#define PLAYER_A 2 #define PLAYER_GREEN 2
#define PLAYER_B 3 #define PLAYER_RED 3
#define TIE 0 #define TIE 1
LiquidCrystal lcd(9, 8, 4, 5, 6, 7); LiquidCrystal lcd(9, 8, 4, 5, 6, 7);
unsigned int scoreA = 0; int scoreGreen = 0;
unsigned int scoreB = 0; int scoreRed = 0;
void setup() { void setup() {
pinMode(LED_BUILTIN, OUTPUT); pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW); digitalWrite(LED_BUILTIN, LOW);
pinMode(PLAYER_A, INPUT_PULLUP); pinMode(PLAYER_GREEN, INPUT_PULLUP);
pinMode(PLAYER_B, INPUT_PULLUP); pinMode(PLAYER_RED, INPUT_PULLUP);
randomSeed(analogRead(0)); randomSeed(analogRead(0));
lcd.begin(LCD_COLS, LCD_ROWS); lcd.begin(LCD_COLS, LCD_ROWS);
} }
@ -27,29 +27,56 @@ void loop() {
displayCountdown(); displayCountdown();
delay(1000); delay(1000);
byte randomWait = random(6, 32); byte randomWait = random(6, 32);
byte cheating = 0;
lcd.clear(); lcd.clear();
for (byte i = 0; i < randomWait; i++) { for (byte i = 0; i < randomWait; i++) {
if (i == LCD_COLS) { if (i == LCD_COLS) {
lcdSecondLine(); lcdSecondLine();
} }
lcd.print("."); lcd.print(".");
delay(500);
}
lcd.clear();
lcd.print("Maintenant !");
const byte winner = computeWinner(); cheating = isCheating();
lcd.clear(); if (cheating) {
if (winner == PLAYER_A) { break;
lcd.print("Bravo joueur A !"); }
scoreA += 1;
} }
else if (winner == PLAYER_B) { lcd.clear();
lcd.print("Bravo joueur B !"); if (cheating) {
scoreB += 1; lcd.print("Pas de triche !");
lcdSecondLine();
if (cheating == PLAYER_GREEN) {
lcd.print("Joueur vert: -1");
scoreGreen -= 1;
}
else if (cheating == PLAYER_RED) {
lcd.print("Joueur rouge: -1");
scoreRed -= 1;
}
else {
lcd.print("Joueurs : -1");
scoreGreen -= 1;
scoreRed -= 1;
}
} }
else { else {
lcd.print("Egalite"); lcd.print("Maintenant !");
const byte winner = computeWinner();
lcd.clear();
if (winner == PLAYER_GREEN) {
lcd.print("Bravo joueur");
lcdSecondLine();
lcd.print("vert !");
scoreGreen += 1;
}
else if (winner == PLAYER_RED) {
lcd.print("Bravo joueur");
lcd.print("rouge !");
scoreRed += 1;
}
else {
lcd.print("Egalite");
}
} }
delay(4000); delay(4000);
printScore(); printScore();
@ -76,53 +103,79 @@ void displayDigit(const char *digit) {
} }
byte computeWinner() { byte computeWinner() {
int playerA = digitalRead(PLAYER_A); int playerGreen = digitalRead(PLAYER_GREEN);
int playerB = digitalRead(PLAYER_B); int playerRed = digitalRead(PLAYER_RED);
while (playerA == HIGH && playerB == HIGH) { while (playerGreen == HIGH && playerRed == HIGH) {
// Wait for a button press // Wait for a button press
playerA = digitalRead(PLAYER_A); playerGreen = digitalRead(PLAYER_GREEN);
playerB = digitalRead(PLAYER_B); playerRed = digitalRead(PLAYER_RED);
} }
if (playerA == LOW && playerB == HIGH) { if (playerGreen == LOW && playerRed == HIGH) {
return PLAYER_A; return PLAYER_GREEN;
} }
if (playerA == HIGH && playerB == LOW) { if (playerGreen == HIGH && playerRed == LOW) {
return PLAYER_B; return PLAYER_RED;
} }
else { else {
return TIE; return TIE;
} }
} }
byte isCheating() {
int playerGreen = digitalRead(PLAYER_GREEN);
int playerRed = digitalRead(PLAYER_RED);
int timer = 0;
int maxTimer = 500;
while (timer < maxTimer && playerGreen == HIGH && playerRed == HIGH) {
// Wait for a button press
playerGreen = digitalRead(PLAYER_GREEN);
playerRed = digitalRead(PLAYER_RED);
timer += 10;
delay(10);
}
if (playerGreen == LOW && playerRed == HIGH) {
return PLAYER_GREEN;
}
else if (playerGreen == HIGH && playerRed == LOW) {
return PLAYER_RED;
}
else if (playerGreen == LOW && playerRed == LOW) {
return TIE;
}
else {
return 0;
}
}
void lcdSecondLine() { void lcdSecondLine() {
lcd.setCursor(0, 1); lcd.setCursor(0, 1);
} }
void printScore() { void printScore() {
lcd.clear(); lcd.clear();
lcd.print("A: "); lcd.print("Vert: ");
lcd.print(scoreA); lcd.print(scoreGreen);
lcdSecondLine(); lcdSecondLine();
lcd.print("B: "); lcd.print("Rouge: ");
lcd.print(scoreB); lcd.print(scoreRed);
delay(2000); delay(2000);
} }
void waitForReady() { void waitForReady() {
lcd.clear(); lcd.clear();
lcd.print("A: Commencer"); lcd.print("Vert: Commencer");
lcdSecondLine(); lcdSecondLine();
lcd.print("B: Scores"); lcd.print("Rouge: Scores");
int playerA = digitalRead(PLAYER_A); int playerGreen = digitalRead(PLAYER_GREEN);
int playerB = digitalRead(PLAYER_B); int playerRed = digitalRead(PLAYER_RED);
while (playerA == HIGH && playerB == HIGH) { while (playerGreen == HIGH && playerRed == HIGH) {
// Wait for a button press // Wait for a button press
playerA = digitalRead(PLAYER_A); playerGreen = digitalRead(PLAYER_GREEN);
playerB = digitalRead(PLAYER_B); playerRed = digitalRead(PLAYER_RED);
} }
if (playerB == LOW) { if (playerRed == LOW) {
printScore(); printScore();
waitForReady(); waitForReady();
} }

View file

@ -8,6 +8,7 @@
void displayCountdown(); void displayCountdown();
void displayDigit(const char *digit); void displayDigit(const char *digit);
byte computeWinner(); byte computeWinner();
byte isCheating();
void lcdSecondLine(); void lcdSecondLine();
void printScore(); void printScore();
void waitForReady(); void waitForReady();