Add local solo mode

This commit is contained in:
Pyramond 2025-06-16 17:01:54 +02:00
parent 15f5d46b55
commit 08de0a245c
2 changed files with 62 additions and 9 deletions

View File

@ -10,13 +10,13 @@
#include <Adafruit_NeoPixel.h> #include <Adafruit_NeoPixel.h>
// --- Capteur capacitif --- // --- Capteur capacitif ---
// Attention : Une SEULE instance globale !
Adafruit_MPR121 cap = Adafruit_MPR121(); Adafruit_MPR121 cap = Adafruit_MPR121();
uint16_t lasttouched = 0; uint16_t lasttouched = 0;
uint16_t currtouched = 0; uint16_t currtouched = 0;
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
randomSeed(analogRead(0));
if (!cap.begin(0x5B)) { if (!cap.begin(0x5B)) {
Serial.println("MPR121 not found, check wiring?"); Serial.println("MPR121 not found, check wiring?");
@ -40,8 +40,12 @@ void loop() {
loopWeb(); loopWeb();
} }
} else { } else {
// Passe cap, lasttouched, currtouched par référence ! if(solo) {
localMode(cap, lasttouched, currtouched); localModeSolo(cap, lasttouched, currtouched);
}
else {
localModeDuo(cap, lasttouched, currtouched);
}
} }
} }
} }

View File

@ -49,8 +49,10 @@ bool joueur1 = true;
Coord getCo(int key); Coord getCo(int key);
bool victoire(int grille[3][3], int joueur); bool victoire(int grille[3][3], int joueur);
bool allInit(); bool allInit();
void afficheGrille();
void localMode(Adafruit_MPR121& cap, uint16_t& lasttouched, uint16_t& currtouched); void localModeDuo(Adafruit_MPR121& cap, uint16_t& lasttouched, uint16_t& currtouched);
void localModeSolo(Adafruit_MPR121& cap, uint16_t& lasttouched, uint16_t& currtouched);
#endif // LOCALMODE_H #endif // LOCALMODE_H
@ -97,7 +99,17 @@ bool allInit() {
return true; return true;
} }
void localMode(Adafruit_MPR121& cap, uint16_t& lasttouched, uint16_t& currtouched) { void afficheGrille() {
Serial.println("==============");
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
Serial.print(grille[i][j]);
}
Serial.println();
}
}
void localModeDuo(Adafruit_MPR121& cap, uint16_t& lasttouched, uint16_t& currtouched) {
currtouched = cap.touched(); currtouched = cap.touched();
for (int i = 0; i < numKeys; i++) { for (int i = 0; i < numKeys; i++) {
@ -114,23 +126,60 @@ void localMode(Adafruit_MPR121& cap, uint16_t& lasttouched, uint16_t& currtouche
if(grille[c.y][c.x] == 0) { if(grille[c.y][c.x] == 0) {
if(joueur1) { if(joueur1) {
grille[c.y][c.x] = 1; grille[c.y][c.x] = 1;
afficheGrille();
if(victoire(grille, 1)) { if(victoire(grille, 1)) {
Serial.println("Joueur 1 a gagne"); Serial.println("Joueur 1 a gagne");
} }
} else { } else {
grille[c.y][c.x] = 2; grille[c.y][c.x] = 2;
afficheGrille();
if(victoire(grille, 2)) { if(victoire(grille, 2)) {
Serial.println("Joueur 2 a gagne"); Serial.println("Joueur 2 a gagne");
} }
} }
joueur1 = !joueur1; joueur1 = !joueur1;
} }
}
}
}
lasttouched = currtouched;
}
for(int i = 0; i < 3; i++) { void localModeSolo(Adafruit_MPR121& cap, uint16_t& lasttouched, uint16_t& currtouched) {
for(int j = 0; j < 3; j++) {
Serial.print(grille[i][j]); //Serial.println("Mode Solo Local");
currtouched = cap.touched();
for (int i = 0; i < numKeys; i++) {
uint8_t t = keys[i].touchID;
keys[i].led.setPixelColor(0, 255); // Bleu
keys[i].led.show();
if ((currtouched & _BV(t)) && !(lasttouched & _BV(t))) {
Serial.print("Touch "); Serial.print(t); Serial.println(" pressed");
btns[i] = true;
if(allInit()) {
Coord c = getCo(i);
if(grille[c.y][c.x] == 0) {
grille[c.y][c.x] = 1;
afficheGrille();
if(victoire(grille, 1)) {
Serial.println("Joueur 1 a gagne");
}
int x, y;
do {
x = random(0, 3);
y = random(0, 3);
} while(grille[y][x] != 0);
grille[y][x] = 2;
afficheGrille();
if(victoire(grille, 2)) {
Serial.println("Joueur 2 a gagne");
} }
Serial.println();
} }
} }
} }