Merge branch 'refac/headers' into 'main'

Refac/headers

See merge request SAE_203_2025/grA_21_Morpion_lumineaux/gra_21_morpion_lumineux_code!3
This commit is contained in:
T'JAMPENS QUENTIN p2406187 2025-06-17 07:47:45 +00:00
commit 47ce303f41
8 changed files with 677 additions and 726 deletions

106
README.md
View File

@ -1,93 +1,19 @@
# grA_21_Morpion_lumineux_code
# Morpion lumineux
## SAE 203
<p>
Quentin T'JAMPENS <br>
Giovanni JOSSERAND <br>
</p>
### Bibliothèques utilisées
## Getting started
To make it easy for you to get started with GitLab, here's a list of recommended next steps.
Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
## Add your files
- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
- [ ] [Add files using the command line](https://docs.gitlab.com/topics/git/add_files/#add-files-to-a-git-repository) or push an existing Git repository with the following command:
```
cd existing_repo
git remote add origin https://iutbg-gitlab.iutbourg.univ-lyon1.fr/sae_203_2025/grA_21_Morpion_lumineaux/gra_21_morpion_lumineux_code.git
git branch -M main
git push -uf origin main
```
## Integrate with your tools
- [ ] [Set up project integrations](https://iutbg-gitlab.iutbourg.univ-lyon1.fr/sae_203_2025/grA_21_Morpion_lumineaux/gra_21_morpion_lumineux_code/-/settings/integrations)
## Collaborate with your team
- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
- [ ] [Set auto-merge](https://docs.gitlab.com/user/project/merge_requests/auto_merge/)
## Test and Deploy
Use the built-in continuous integration in GitLab.
- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/)
- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing (SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
***
# Editing this README
When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thanks to [makeareadme.com](https://www.makeareadme.com/) for this template.
## Suggestions for a good README
Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
## Name
Choose a self-explaining name for your project.
## Description
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
## Badges
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
## Visuals
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
## Installation
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
## Usage
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
## Support
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
## Roadmap
If you have ideas for releases in the future, it is a good idea to list them in the README.
## Contributing
State if you are open to contributions and what your requirements are for accepting them.
For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
## Authors and acknowledgment
Show your appreciation to those who have contributed to the project.
## License
For open source projects, say how it is licensed.
## Project status
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
<ul>
<li> Adafruit NeoPixel v1.15.1 </li>
<li> TTP229 </li>
<li> Adafruit_MPR121 </li>
<li> Adafruit GFX Library </li>
<li> Adafruit_SH110X </li>
</ul>

View File

@ -32,7 +32,6 @@ void loop() {
readButton();
if (!isPlay) {
menu();
Serial.println("ok");
} else {
if (!local) {
if(!isWebSet){

150
localMode.cpp Normal file
View File

@ -0,0 +1,150 @@
#include "localMode.h"
#include <Arduino.h>
uint8_t color_pos = 16;
MechKey keys[] = {
{0, A0, false, Adafruit_NeoPixel(1, A0, NEO_GRB + NEO_KHZ800)},
{1, 25, false, Adafruit_NeoPixel(1, 25, NEO_GRB + NEO_KHZ800)},
{2, 34, false, Adafruit_NeoPixel(1, 34, NEO_GRB + NEO_KHZ800)},
{3, 39, false, Adafruit_NeoPixel(1, 39, NEO_GRB + NEO_KHZ800)},
{4, 36, false, Adafruit_NeoPixel(1, 36, NEO_GRB + NEO_KHZ800)},
{5, 4, false, Adafruit_NeoPixel(1, 4, NEO_GRB + NEO_KHZ800)},
{6, 14, false, Adafruit_NeoPixel(1, 14, NEO_GRB + NEO_KHZ800)},
{7, 33, false, Adafruit_NeoPixel(1, 33, NEO_GRB + NEO_KHZ800)},
{8, 15, false, Adafruit_NeoPixel(1, 15, NEO_GRB + NEO_KHZ800)},
};
const int numKeys = 9;
int grille[3][3] = {
{0, 0, 0},
{0, 0, 0},
{0, 0, 0}
};
bool btns[9] = { false, false, false, false, false, false, false, false, false };
bool joueur1 = true;
Coord getCo(int key) {
switch(key) {
case 0: return { 0, 0 };
case 1: return { 1, 0 };
case 2: return { 2, 0 };
case 3: return { 0, 1 };
case 4: return { 1, 1 };
case 5: return { 2, 1 };
case 6: return { 0, 2 };
case 7: return { 1, 2 };
case 8: return { 2, 2 };
default: return { -1, -1 };
}
}
bool victoire(int grille[3][3], int joueur) {
// Vérification des lignes et colonnes
for (int i = 0; i < 3; ++i) {
if (grille[i][0] == joueur && grille[i][1] == joueur && grille[i][2] == joueur)
return true;
if (grille[0][i] == joueur && grille[1][i] == joueur && grille[2][i] == joueur)
return true;
}
// Vérification des deux diagonales
if (grille[0][0] == joueur && grille[1][1] == joueur && grille[2][2] == joueur)
return true;
if (grille[0][2] == joueur && grille[1][1] == joueur && grille[2][0] == joueur)
return true;
return false;
}
bool allInit() {
for(int i = 0; i < 9; i++) {
if (!btns[i]) return false;
}
return true;
}
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();
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) {
if(joueur1) {
grille[c.y][c.x] = 1;
afficheGrille();
if(victoire(grille, 1)) {
Serial.println("Joueur 1 a gagne");
}
} else {
grille[c.y][c.x] = 2;
afficheGrille();
if(victoire(grille, 2)) {
Serial.println("Joueur 2 a gagne");
}
}
joueur1 = !joueur1;
}
}
}
}
lasttouched = currtouched;
}
void localModeSolo(Adafruit_MPR121& cap, uint16_t& lasttouched, uint16_t& currtouched) {
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");
}
}
}
}
}
lasttouched = currtouched;
}

View File

@ -9,10 +9,10 @@
#endif
struct MechKey {
uint8_t touchID; // Index sur le MPR121
uint8_t dataPin; // Broche de l'ESP32 vers NeoPixel
bool state; // État (toggle)
Adafruit_NeoPixel led; // Instance NeoPixel
uint8_t touchID;
uint8_t dataPin;
bool state;
Adafruit_NeoPixel led;
};
struct Coord {
@ -20,31 +20,12 @@ struct Coord {
int y;
};
uint8_t color_pos = 16;
// Création tableau global, mais pas de capteurs ici !
MechKey keys[] = {
{0, A0, false, Adafruit_NeoPixel(1, A0, NEO_GRB + NEO_KHZ800)},
{1, 25, false, Adafruit_NeoPixel(1, 25, NEO_GRB + NEO_KHZ800)},
{2, 34, false, Adafruit_NeoPixel(1, 34, NEO_GRB + NEO_KHZ800)},
{3, 39, false, Adafruit_NeoPixel(1, 39, NEO_GRB + NEO_KHZ800)},
{4, 36, false, Adafruit_NeoPixel(1, 36, NEO_GRB + NEO_KHZ800)},
{5, 4, false, Adafruit_NeoPixel(1, 4, NEO_GRB + NEO_KHZ800)},
{6, 14, false, Adafruit_NeoPixel(1, 14, NEO_GRB + NEO_KHZ800)},
{7, 33, false, Adafruit_NeoPixel(1, 33, NEO_GRB + NEO_KHZ800)},
{8, 15, false, Adafruit_NeoPixel(1, 15, NEO_GRB + NEO_KHZ800)},
};
const int numKeys = 9;
int grille[3][3] = {
{0, 0, 0},
{0, 0, 0},
{0, 0, 0}
};
bool btns[9] = { false, false, false, false, false, false, false, false, false };
bool joueur1 = true;
extern uint8_t color_pos;
extern MechKey keys[];
extern const int numKeys;
extern int grille[3][3];
extern bool btns[9];
extern bool joueur1;
Coord getCo(int key);
bool victoire(int grille[3][3], int joueur);
@ -54,135 +35,4 @@ void afficheGrille();
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
Coord getCo(int key) {
switch(key) {
case 0: return { 0, 0 };
case 1: return { 1, 0 };
case 2: return { 2, 0 };
case 3: return { 0, 1 };
case 4: return { 1, 1 };
case 5: return { 2, 1 };
case 6: return { 0, 2 };
case 7: return { 1, 2 };
case 8: return { 2, 2 };
default: return { -1, -1 };
}
}
bool victoire(int grille[3][3], int joueur) {
// Vérification des lignes et colonnes
for (int i = 0; i < 3; ++i) {
// Ligne
if (grille[i][0] == joueur && grille[i][1] == joueur && grille[i][2] == joueur)
return true;
// Colonne
if (grille[0][i] == joueur && grille[1][i] == joueur && grille[2][i] == joueur)
return true;
}
// Vérification des deux diagonales
if (grille[0][0] == joueur && grille[1][1] == joueur && grille[2][2] == joueur)
return true;
if (grille[0][2] == joueur && grille[1][1] == joueur && grille[2][0] == joueur)
return true;
// Aucun alignement trouvé
return false;
}
bool allInit() {
for(int i = 0; i < 9; i++) {
if (!btns[i]) return false;
}
return true;
}
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();
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) {
if(joueur1) {
grille[c.y][c.x] = 1;
afficheGrille();
if(victoire(grille, 1)) {
Serial.println("Joueur 1 a gagne");
}
} else {
grille[c.y][c.x] = 2;
afficheGrille();
if(victoire(grille, 2)) {
Serial.println("Joueur 2 a gagne");
}
}
joueur1 = !joueur1;
}
}
}
}
lasttouched = currtouched;
}
void localModeSolo(Adafruit_MPR121& cap, uint16_t& lasttouched, uint16_t& currtouched) {
//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");
}
}
}
}
}
lasttouched = currtouched;
}
#endif // LOCALMODE_H

130
menu.cpp Normal file
View File

@ -0,0 +1,130 @@
#include "menu.h"
#include <Arduino.h>
// Variables de configuration du menu
bool local = true;
bool solo = true;
int difficulty = 1;
uint8_t btnPrevA;
uint8_t btnPrevB;
uint8_t btnA;
uint8_t btnB;
bool isMod = false;
bool isModJeu = false;
bool isDifficulty = false;
bool isPlay = false;
bool isWebSet = false;
Adafruit_SH1107 display = Adafruit_SH1107(64, 128, &Wire);
unsigned long lastDebounceTimeA = 0;
unsigned long lastDebounceTimeB = 0;
const unsigned long debounceDelay = 200;
// Assure-toi que epd_bitmap_logo soit déclaré ailleurs ou inclus ici
//extern const unsigned char epd_bitmap_logo[];
void setupMenu() {
pinMode(BUTTON_A, INPUT_PULLUP);
pinMode(BUTTON_B, INPUT_PULLUP);
btnPrevA = digitalRead(BUTTON_A);
btnPrevB = digitalRead(BUTTON_B);
display.begin(0x3C, true);
display.clearDisplay();
// display.drawBitmap(0, 0, epd_bitmap_logo, 128, 64, SH110X_WHITE);
display.display();
delay(1000);
display.clearDisplay();
display.display();
display.setRotation(1);
display.setTextColor(SH110X_WHITE);
display.setCursor(0, 0);
display.display();
}
void choixMode() {
display.clearDisplay();
display.setCursor(0, 0);
display.print("Choisissez le mode :\n");
display.print("[A] Local\n");
display.print("[B] Web\n");
display.display();
}
void choixModeJeu() {
display.clearDisplay();
display.setCursor(0, 0);
display.print("Choisissez le type :\n");
display.print("[A] Solo\n");
display.print("[B] Duo\n");
display.display();
}
void choixDifficulty() {
display.clearDisplay();
display.setCursor(0, 0);
display.print("Choisissez la \ndifficulte :\n");
display.print("[A] Debutant\n");
display.print("[B] Expert\n");
display.display();
}
void menu() {
if (!isMod) {
choixMode();
} else if (local && !isModJeu) {
choixModeJeu();
} else if (local && solo && !isDifficulty) {
choixDifficulty();
} else {
isPlay = true;
}
}
void handleButtonA() {
if (!isMod) {
local = true;
isMod = true;
} else if (!isModJeu) {
solo = true;
isModJeu = true;
} else if (solo && !isDifficulty) {
difficulty = 1;
isDifficulty = true;
isPlay = true;
}
}
void handleButtonB() {
if (!isMod) {
local = false;
isMod = true;
} else if (!isModJeu) {
solo = false;
isModJeu = true;
} else if (solo && !isDifficulty) {
difficulty = 2;
isDifficulty = true;
isPlay = true;
}
}
void readButton() {
btnA = digitalRead(BUTTON_A);
if (btnA == LOW && btnPrevA == HIGH && (millis() - lastDebounceTimeA > debounceDelay)) {
lastDebounceTimeA = millis();
handleButtonA();
}
btnPrevA = btnA;
btnB = digitalRead(BUTTON_B);
if (btnB == LOW && btnPrevB == HIGH && (millis() - lastDebounceTimeB > debounceDelay)) {
lastDebounceTimeB = millis();
handleButtonB();
}
btnPrevB = btnB;
}

166
menu.h
View File

@ -1,150 +1,42 @@
#ifndef MENU_H
#define MENU_H
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
// Variables de configuration du menu
extern bool local;
extern bool solo;
extern int difficulty;
bool local = true;
bool solo = true;
int difficulty = 1;
extern uint8_t btnPrevA;
extern uint8_t btnPrevB;
extern uint8_t btnA;
extern uint8_t btnB;
uint8_t btnPrevA;
uint8_t btnPrevB;
uint8_t btnA;
uint8_t btnB;
bool isMod = false;
bool isModJeu = false;
bool isDifficulty = false;
bool isPlay = false;
bool isWebSet = false;
extern bool isMod;
extern bool isModJeu;
extern bool isDifficulty;
extern bool isPlay;
extern bool isWebSet;
#define BUTTON_A 15
#define BUTTON_B 32
Adafruit_SH1107 display = Adafruit_SH1107(64, 128, &Wire);
unsigned long lastDebounceTimeA = 0;
unsigned long lastDebounceTimeB = 0;
const unsigned long debounceDelay = 200;
extern Adafruit_SH1107 display;
void setupMenu() {
pinMode(BUTTON_A, INPUT_PULLUP);
pinMode(BUTTON_B, INPUT_PULLUP);
extern unsigned long lastDebounceTimeA;
extern unsigned long lastDebounceTimeB;
extern const unsigned long debounceDelay;
btnPrevA = digitalRead(BUTTON_A);
btnPrevB = digitalRead(BUTTON_B);
void setupMenu();
void choixMode();
void choixModeJeu();
void choixDifficulty();
void menu();
display.begin(0x3C, true);
display.clearDisplay();
display.drawBitmap(0, 0, epd_bitmap_logo, 128, 64, SH110X_WHITE);
display.display();
delay(1000);
display.clearDisplay();
display.display();
void handleButtonA();
void handleButtonB();
void readButton();
display.setRotation(1);
display.setTextColor(SH110X_WHITE);
display.setCursor(0, 0);
display.display();
}
void choixMode() {
display.clearDisplay();
display.setCursor(0, 0);
display.print("Choisissez le mode :\n");
display.print("[A] Local\n");
display.print("[B] Web\n");
display.display();
}
void choixModeJeu() {
display.clearDisplay();
display.setCursor(0, 0);
display.print("Choisissez le type :\n");
display.print("[A] Solo\n");
display.print("[B] Duo\n");
display.display();
}
void choixDifficulty() {
display.clearDisplay();
display.setCursor(0, 0);
display.print("Choisissez la \ndifficulte :\n");
display.print("[A] Debutant\n");
display.print("[B] Expert\n");
display.display();
}
void menu() {
if (!isMod) {
choixMode();
} else if (local && !isModJeu) {
choixModeJeu();
} else if (local && solo && !isDifficulty) {
choixDifficulty();
} else {
isPlay = true;
}
}
void handleButtonA() {
if (!isMod) {
local = true;
isMod = true;
} else if (!isModJeu) {
solo = true;
isModJeu = true;
} else if (solo && !isDifficulty) {
difficulty = 1;
isDifficulty = true;
isPlay = true;
}
}
void handleButtonB() {
if (!isMod) {
local = false;
isMod = true;
} else if (!isModJeu) {
solo = false;
isModJeu = true;
} else if (solo && !isDifficulty) {
difficulty = 2;
isDifficulty = true;
isPlay = true;
}
}
void readButton(){
btnA = digitalRead(BUTTON_A);
if (btnA == LOW && btnPrevA == HIGH && (millis() - lastDebounceTimeA > debounceDelay)) {
lastDebounceTimeA = millis();
handleButtonA();
}
btnPrevA = btnA;
btnB = digitalRead(BUTTON_B);
if (btnB == LOW && btnPrevB == HIGH && (millis() - lastDebounceTimeB > debounceDelay)) {
lastDebounceTimeB = millis();
handleButtonB();
}
btnPrevB = btnB;
}
#endif // MENU_H

328
web.cpp Normal file
View File

@ -0,0 +1,328 @@
#include "web.h"
#include <Arduino.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
// Utilisateurs du réseau
const char *ssid = "morpion-SAE";
const char *password = "12345678";
NetworkServer server(80);
void setupWeb() {
Serial.println();
Serial.println("Configuring access point...");
if (!WiFi.softAP(ssid, password)) {
log_e("Soft AP creation failed.");
while (1);
}
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.begin();
Serial.println("Server started");
display.clearDisplay();
display.setCursor(0, 0);
display.println("Connectez vous a :");
display.print("Nom : ");
display.println(ssid);
display.print("Mdp : ");
display.println(password);
display.println();
display.print("Puis accedez a \nl'adresse ");
display.print(myIP);
display.display();
}
void webGame(NetworkClient &c){
c.println(R"rawliteral(
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Morpion Duo/Solo</title>
<style>
body {
margin: 0; padding: 0;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: #121212;
color: #eee;
display: flex;
flex-direction: column;
height: 100vh;
align-items: center;
justify-content: center;
}
h1 { margin-bottom: 0.2em; }
.menu {
margin-bottom: 10px;
display: flex;
gap: 15px;
align-items: center;
}
button.recommencer {
background-color: #6200ea;
border: none;
color: white;
padding: 12px 25px;
font-size: 1.2em;
border-radius: 5px;
cursor: pointer;
box-shadow: 0 4px 6px rgba(0,0,0,0.3);
transition: background-color 0.3s ease;
}
button.recommencer:hover {
background-color: #3700b3;
}
select {
font-size: 1.1em;
padding: 6px 10px;
border-radius: 5px;
border: none;
background-color: #333;
color: #eee;
cursor: pointer;
box-shadow: 0 2px 4px rgba(0,0,0,0.5);
transition: background-color 0.3s ease;
}
select:hover {
background-color: #555;
}
.status {
margin-bottom: 15px;
font-size: 1.3em;
height: 1.6em;
color: #bb86fc;
font-weight: bold;
}
.grid {
display: grid;
grid-template-columns: repeat(3, 100px);
grid-template-rows: repeat(3, 100px);
gap: 8px;
user-select: none;
margin-bottom: 15px;
}
.cell {
background-color: #1f1f1f;
border-radius: 10px;
display: flex;
justify-content: center;
align-items: center;
font-size: 3.5em;
font-weight: bold;
cursor: pointer;
transition: background-color 0.3s ease, transform 0.15s ease;
box-shadow: 0 3px 6px rgba(0,0,0,0.5);
}
.cell:hover {
background-color: #3700b3;
transform: scale(1.05);
}
.blue { color: #2196F3; }
.red { color: #f44336; }
.winner {
background-color: #03dac6 !important;
color: #000 !important;
animation: pulse 1s infinite;
}
@keyframes pulse {
0%, 100% { box-shadow: 0 0 10px 5px #03dac6; }
50% { box-shadow: 0 0 20px 10px #03dac6; }
}
</style>
</head>
<body>
<h1>Morpion</h1>
<div class="menu">
<label for="modeSelect">Mode :</label>
<select id="modeSelect">
<option value="duo" selected>Duo (2 joueurs)</option>
<option value="solo">Solo (contre IA)</option>
</select>
</div>
<div class="status">Joueur 1 (Bleu) à jouer</div>
<div class="grid">
<div class="cell" id="0"></div>
<div class="cell" id="1"></div>
<div class="cell" id="2"></div>
<div class="cell" id="3"></div>
<div class="cell" id="4"></div>
<div class="cell" id="5"></div>
<div class="cell" id="6"></div>
<div class="cell" id="7"></div>
<div class="cell" id="8"></div>
</div>
<button class="recommencer">Recommencer</button>
<script>
const modeSelect = document.getElementById('modeSelect');
const status = document.querySelector('.status');
const cells = document.querySelectorAll('.cell');
const combinaisons = [
[0,1,2], [3,4,5], [6,7,8],
[0,3,6], [1,4,7], [2,5,8],
[0,4,8], [2,4,6]
];
let joueur = 1;
let etat = [0,0,0,0,0,0,0,0,0];
let jeuFini = false;
function verifieVictoire(j) {
for (let c of combinaisons) {
if (etat[c[0]] === j && etat[c[1]] === j && etat[c[2]] === j) {
return c;
}
}
return null;
}
function declareVictoire(cellsIndices) {
jeuFini = true;
for (let i of cellsIndices) {
cells[i].classList.add('winner');
}
status.textContent = `Le joueur ${joueur} a gagné !`;
}
function declareMatchNul() {
jeuFini = true;
status.textContent = "Match nul !";
}
function updateStatus() {
if (!jeuFini) {
if (modeSelect.value === 'duo') {
status.textContent = `Joueur ${joueur} (${joueur === 1 ? 'Bleu' : 'Rouge'}) à jouer`;
} else {
status.textContent = joueur === 1 ? 'Votre tour (Bleu)' : 'Tour de l\'IA (Rouge)';
}
}
}
function iaPlay() {
if (jeuFini) return;
let libres = [];
etat.forEach((v, i) => { if (v === 0) libres.push(i); });
if (libres.length === 0) return;
let choix = libres[Math.floor(Math.random() * libres.length)];
etat[choix] = 2;
let cell = cells[choix];
cell.textContent = 'O';
cell.classList.add('red');
let vic = verifieVictoire(2);
if (vic) {
joueur = 2;
declareVictoire(vic);
} else if (!etat.includes(0)) {
declareMatchNul();
} else {
joueur = 1;
updateStatus();
}
}
function resetGame() {
jeuFini = false;
joueur = 1;
etat.fill(0);
cells.forEach(c => {
c.textContent = '';
c.classList.remove('blue', 'red', 'winner');
});
updateStatus();
if (modeSelect.value === 'solo' && joueur === 2) {
iaPlay();
}
}
cells.forEach((cell, index) => {
cell.addEventListener('click', () => {
if (jeuFini || etat[index] !== 0) return;
if (modeSelect.value === 'duo') {
etat[index] = joueur;
cell.textContent = joueur === 1 ? 'X' : 'O';
cell.classList.add(joueur === 1 ? 'blue' : 'red');
let vic = verifieVictoire(joueur);
if (vic) {
declareVictoire(vic);
} else if (!etat.includes(0)) {
declareMatchNul();
} else {
joueur = joueur === 1 ? 2 : 1;
updateStatus();
}
} else {
if (joueur !== 1) return;
etat[index] = 1;
cell.textContent = 'X';
cell.classList.add('blue');
let vic = verifieVictoire(1);
if (vic) {
declareVictoire(vic);
} else if (!etat.includes(0)) {
declareMatchNul();
} else {
joueur = 2;
updateStatus();
setTimeout(iaPlay, 700);
}
}
});
});
document.querySelector('button.recommencer').addEventListener('click', resetGame);
modeSelect.addEventListener('change', resetGame);
updateStatus();
</script>
</body>
</html>
)rawliteral");
}
void webSolo(NetworkClient &c) {
c.println("<!DOCTYPE html>");
c.println("<html>");
c.println("<head>");
c.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
c.println("</head>");
c.println("<body>");
c.println("<div class='grid'>");
c.println("Pas encore commence !");
c.println("</div>");
c.println("</body>");
c.println("</html>");
}
void loopWeb() {
NetworkClient client = server.accept();
if (client) {
Serial.println("New Client.");
String currentLine = "";
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
if (c == '\n') {
if (currentLine.length() == 0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
webGame(client);
client.println();
break;
} else {
currentLine = "";
}
} else if (c != '\r') {
currentLine += c;
}
}
}
client.stop();
Serial.println("Client Disconnected.");
}
}

350
web.h
View File

@ -1,344 +1,20 @@
#ifndef WEBSERVER_H
#define WEBSERVER_H
#include <WiFi.h>
#include <NetworkClient.h>
#include <WiFiAP.h>
const char *ssid = "morpion-SAE";
const char *password = "12345678";
NetworkServer server(80);
NetworkClient client = server.accept();
// SSID & mot de passe du réseau WiFi
extern const char *ssid;
extern const char *password;
void setupWeb(){
Serial.println();
Serial.println("Configuring access point...");
if (!WiFi.softAP(ssid, password)) {
log_e("Soft AP creation failed.");
while (1);
}
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.begin();
// Serveur et client web
extern NetworkServer server;
Serial.println("Server started");
void setupWeb();
void loopWeb();
void webGame(NetworkClient &c);
void webSolo(NetworkClient &c);
display.clearDisplay();
display.setCursor(0, 0);
display.println("Connectez vous a :");
display.print("Nom : ");
display.println(ssid);
display.print("Mdp : ");
display.println(password);
display.println();
display.print("Puis accedez a \nl'adresse ");
display.print(myIP);
display.display();
}
void webGame(NetworkClient &c){
c.println(R"rawliteral(
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Morpion Duo/Solo</title>
<style>
body {
margin: 0; padding: 0;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: #121212;
color: #eee;
display: flex;
flex-direction: column;
height: 100vh;
align-items: center;
justify-content: center;
}
h1 { margin-bottom: 0.2em; }
.menu {
margin-bottom: 10px;
display: flex;
gap: 15px;
align-items: center;
}
button.recommencer {
background-color: #6200ea;
border: none;
color: white;
padding: 12px 25px;
font-size: 1.2em;
border-radius: 5px;
cursor: pointer;
box-shadow: 0 4px 6px rgba(0,0,0,0.3);
transition: background-color 0.3s ease;
}
button.recommencer:hover {
background-color: #3700b3;
}
select {
font-size: 1.1em;
padding: 6px 10px;
border-radius: 5px;
border: none;
background-color: #333;
color: #eee;
cursor: pointer;
box-shadow: 0 2px 4px rgba(0,0,0,0.5);
transition: background-color 0.3s ease;
}
select:hover {
background-color: #555;
}
.status {
margin-bottom: 15px;
font-size: 1.3em;
height: 1.6em;
color: #bb86fc;
font-weight: bold;
}
.grid {
display: grid;
grid-template-columns: repeat(3, 100px);
grid-template-rows: repeat(3, 100px);
gap: 8px;
user-select: none;
margin-bottom: 15px;
}
.cell {
background-color: #1f1f1f;
border-radius: 10px;
display: flex;
justify-content: center;
align-items: center;
font-size: 3.5em;
font-weight: bold;
cursor: pointer;
transition: background-color 0.3s ease, transform 0.15s ease;
box-shadow: 0 3px 6px rgba(0,0,0,0.5);
}
.cell:hover {
background-color: #3700b3;
transform: scale(1.05);
}
.blue { color: #2196F3; }
.red { color: #f44336; }
.winner {
background-color: #03dac6 !important;
color: #000 !important;
animation: pulse 1s infinite;
}
@keyframes pulse {
0%, 100% { box-shadow: 0 0 10px 5px #03dac6; }
50% { box-shadow: 0 0 20px 10px #03dac6; }
}
</style>
</head>
<body>
<h1>Morpion</h1>
<div class="menu">
<label for="modeSelect">Mode :</label>
<select id="modeSelect">
<option value="duo" selected>Duo (2 joueurs)</option>
<option value="solo">Solo (contre IA)</option>
</select>
</div>
<div class="status">Joueur 1 (Bleu) à jouer</div>
<div class="grid">
<div class="cell" id="0"></div>
<div class="cell" id="1"></div>
<div class="cell" id="2"></div>
<div class="cell" id="3"></div>
<div class="cell" id="4"></div>
<div class="cell" id="5"></div>
<div class="cell" id="6"></div>
<div class="cell" id="7"></div>
<div class="cell" id="8"></div>
</div>
<button class="recommencer">Recommencer</button>
<script>
const modeSelect = document.getElementById('modeSelect');
const status = document.querySelector('.status');
const cells = document.querySelectorAll('.cell');
const combinaisons = [
[0,1,2], [3,4,5], [6,7,8],
[0,3,6], [1,4,7], [2,5,8],
[0,4,8], [2,4,6]
];
let joueur = 1;
let etat = [0,0,0,0,0,0,0,0,0];
let jeuFini = false;
function verifieVictoire(j) {
for (let c of combinaisons) {
if (etat[c[0]] === j && etat[c[1]] === j && etat[c[2]] === j) {
return c;
}
}
return null;
}
function declareVictoire(cellsIndices) {
jeuFini = true;
for (let i of cellsIndices) {
cells[i].classList.add('winner');
}
status.textContent = `Le joueur ${joueur} a gagné !`;
}
function declareMatchNul() {
jeuFini = true;
status.textContent = "Match nul !";
}
function updateStatus() {
if (!jeuFini) {
if (modeSelect.value === 'duo') {
status.textContent = `Joueur ${joueur} (${joueur === 1 ? 'Bleu' : 'Rouge'}) à jouer`;
} else {
status.textContent = joueur === 1 ? 'Votre tour (Bleu)' : 'Tour de l\'IA (Rouge)';
}
}
}
function iaPlay() {
if (jeuFini) return;
let libres = [];
etat.forEach((v, i) => { if (v === 0) libres.push(i); });
if (libres.length === 0) return;
let choix = libres[Math.floor(Math.random() * libres.length)];
etat[choix] = 2;
let cell = cells[choix];
cell.textContent = 'O';
cell.classList.add('red');
let vic = verifieVictoire(2);
if (vic) {
joueur = 2;
declareVictoire(vic);
} else if (!etat.includes(0)) {
declareMatchNul();
} else {
joueur = 1;
updateStatus();
}
}
function resetGame() {
jeuFini = false;
joueur = 1;
etat.fill(0);
cells.forEach(c => {
c.textContent = '';
c.classList.remove('blue', 'red', 'winner');
});
updateStatus();
if (modeSelect.value === 'solo' && joueur === 2) {
iaPlay();
}
}
cells.forEach((cell, index) => {
cell.addEventListener('click', () => {
if (jeuFini || etat[index] !== 0) return;
if (modeSelect.value === 'duo') {
etat[index] = joueur;
cell.textContent = joueur === 1 ? 'X' : 'O';
cell.classList.add(joueur === 1 ? 'blue' : 'red');
let vic = verifieVictoire(joueur);
if (vic) {
declareVictoire(vic);
} else if (!etat.includes(0)) {
declareMatchNul();
} else {
joueur = joueur === 1 ? 2 : 1;
updateStatus();
}
} else {
if (joueur !== 1) return;
etat[index] = 1;
cell.textContent = 'X';
cell.classList.add('blue');
let vic = verifieVictoire(1);
if (vic) {
declareVictoire(vic);
} else if (!etat.includes(0)) {
declareMatchNul();
} else {
joueur = 2;
updateStatus();
setTimeout(iaPlay, 700);
}
}
});
});
document.querySelector('button.recommencer').addEventListener('click', resetGame);
modeSelect.addEventListener('change', resetGame);
updateStatus();
</script>
</body>
</html>
)rawliteral");
}
void webSolo(NetworkClient &c){
c.println("<!DOCTYPE html>");
c.println("<html>");
c.println("<head>");
c.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
c.println("</head>");
c.println("<body>");
c.println("<div class='grid'>");
c.println("Pas encore commence !");
c.println("</div>");
c.println("</body>");
c.println("</html>");
}
void loopWeb(){
NetworkClient client = server.accept();
if (client) {
Serial.println("New Client.");
String currentLine = "";
while (client.connected()) {
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c);
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
// the content of the HTTP response follows the header:
webGame(client);
client.println();
break;
} else { // if you got a newline, then clear currentLine:
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
client.stop();
Serial.println("Client Disconnected.");
}
}
#endif // WEBSERVER_H