526 lines
11 KiB
C++
526 lines
11 KiB
C++
/*====================================
|
||
AUTEURS : Léa Dezothez et Solène Baillargeon
|
||
PROJET : Final
|
||
NOM DU FICHIER : fonctions.cpp
|
||
DATE : 2025-11-14
|
||
BUT : Fichier de fonctions Tic Tac Toe
|
||
====================================*/
|
||
|
||
#include "fonctions.h"
|
||
#include <iostream>
|
||
#include <conio.h>
|
||
#include <Windows.h>
|
||
#include <iomanip>
|
||
#include <fstream>
|
||
using namespace std;
|
||
|
||
extern const int LARGTIC;
|
||
string iconJ1 = "X";
|
||
string iconJ2 = "O";
|
||
int gagnant;
|
||
string style = "themes/themeDefaut.txt";
|
||
int codeConsole = 0;
|
||
|
||
fstream monFlux;
|
||
HANDLE hconsole = GetStdHandle(STD_OUTPUT_HANDLE);
|
||
char carre = 219, choix;
|
||
|
||
//À faire : égalité et entrer de coordonnée
|
||
|
||
//Fichiers
|
||
void ouvrirFichier(std::fstream& monFlux, std::string nomFichier) {
|
||
monFlux.open(nomFichier, ios::in);
|
||
if (!monFlux) //On vérifie si le flux est "faux"/non fonctionnel.
|
||
{
|
||
cout << "ERREUR: Impossible d'ouvrir le fichier!" << endl;
|
||
system("PAUSE>null");
|
||
exit(1); //On quitte immédiatement le programme
|
||
}
|
||
}
|
||
|
||
void fermerFichier(std::fstream& monFlux) {
|
||
monFlux.close();
|
||
}
|
||
|
||
//Menu
|
||
void afficherMenu(char& choix) {
|
||
system("cls");
|
||
codeConsole = GetConsoleOutputCP();
|
||
//T I C T A C T O E
|
||
ouvrirFichier(monFlux, "menuTitre.txt");
|
||
afficherImageMenu(monFlux);
|
||
fermerFichier(monFlux);
|
||
|
||
// Menu
|
||
cout << endl << "MENU" << endl
|
||
<< "----------------------------------" << endl
|
||
<< "A) 1 joueur" << endl
|
||
<< "B) 2 joueurs" << endl
|
||
<< "C) Choisir un theme" << endl
|
||
<< "Q) Quitter" << endl << endl
|
||
<< "Quel est votre choix? ";
|
||
choix = toupper(_getche());
|
||
cout << endl;
|
||
}
|
||
|
||
void afficherImageMenu(std::fstream& monFlux) {
|
||
int repet = 0;
|
||
if (monFlux) {
|
||
while (!monFlux.eof()) {
|
||
monFlux >> repet;
|
||
if (repet == 99) {
|
||
cout << endl;
|
||
}
|
||
else if (repet == 0) {
|
||
cout << " ";
|
||
}
|
||
else {
|
||
SetConsoleTextAttribute(hconsole, 15);
|
||
cout << carre << carre;
|
||
}
|
||
}
|
||
monFlux.close();
|
||
cout << endl;
|
||
}
|
||
else {
|
||
cout << "ERREUR : Impossible d’ouvrir le fichier! " << endl;
|
||
exit(1);
|
||
}
|
||
}
|
||
|
||
//Général (plusieurs options)
|
||
void initGrid(string grid[][LARGTIC]) {
|
||
for (int i = 0; i < LARGTIC; i++) {
|
||
for (int j = 0; j < LARGTIC; j++) {
|
||
grid[i][j] = "";
|
||
}
|
||
}
|
||
}
|
||
|
||
void afficherImageTableau(std::string grid[][LARGTIC], std::string style) {
|
||
SetConsoleOutputCP(CP_UTF8); //affiche les émojis
|
||
string emoteUp,
|
||
emoteDown,
|
||
emoteJ1,
|
||
emoteJ2;
|
||
|
||
// Faire une fonction pour retenir le thème sélectionné par le joueur!!!!
|
||
|
||
ouvrirFichier(monFlux, style);
|
||
if (monFlux) {
|
||
while (!monFlux.eof()) {
|
||
monFlux >> emoteUp >> emoteDown >> emoteJ1 >> emoteJ2;
|
||
}
|
||
monFlux.close();
|
||
cout << endl;
|
||
}
|
||
else {
|
||
cout << "ERREUR : Impossible d’ouvrir le fichier! " << endl;
|
||
exit(1);
|
||
}
|
||
fermerFichier(monFlux);
|
||
|
||
// Lignes de code pour faire afficher le Tic Tac Toe une fois que les symboles ont été ajoutés
|
||
cout << "Joueur 1: " << emoteJ1 << " Joueur 2: " << emoteJ2 << endl << endl;
|
||
|
||
// Lignes de code pour faire afficher le Tic Tac Toe une fois que les symboles ont été ajoutés
|
||
cout << " ";
|
||
for (int i = 0; i < LARGTIC; i++) {
|
||
cout << " " << i + 1 << " ";
|
||
}
|
||
cout << " " << endl << " ";
|
||
for (int k = 0; k < LARGTIC; k++) {
|
||
|
||
for (int i = 0; i < LARGTIC; i++) {
|
||
for (int j = 0; j < 2; j++) {
|
||
cout << emoteUp << " ";
|
||
}
|
||
}
|
||
cout << endl << " ";
|
||
|
||
for (int i = 0; i < LARGTIC; i++) {
|
||
cout << "|";
|
||
for (int j = 0; j < 3; j++) {
|
||
cout << " ";
|
||
}
|
||
cout << " ";
|
||
}
|
||
cout << "|" << endl << " " << k + 1 << " ";
|
||
|
||
|
||
for (int i = 0; i < LARGTIC; i++) {
|
||
cout << "|" << " ";
|
||
if (grid[k][i] == "X") {
|
||
cout << emoteJ1;
|
||
}
|
||
else if (grid[k][i] == "O") {
|
||
cout << emoteJ2;
|
||
}
|
||
else {
|
||
cout << " ";
|
||
}
|
||
cout << " ";
|
||
}
|
||
cout << "|" << endl << " ";
|
||
|
||
for (int i = 0; i < LARGTIC; i++) {
|
||
cout << "|";
|
||
for (int j = 0; j < 3; j++) {
|
||
cout << " ";
|
||
}
|
||
cout << " ";
|
||
}
|
||
cout << "|" << endl << " ";
|
||
|
||
}
|
||
|
||
for (int i = 0; i < LARGTIC; i++) {
|
||
for (int j = 0; j < 2; j++) {
|
||
cout << emoteDown << " ";
|
||
}
|
||
}
|
||
cout << endl;
|
||
SetConsoleOutputCP(codeConsole);
|
||
}
|
||
|
||
bool verifFinMatch(std::string grid[][LARGTIC], int compteur) {
|
||
bool fin = false;
|
||
int max = LARGTIC * LARGTIC;
|
||
if (compteur == max) {
|
||
return true;
|
||
}
|
||
else {
|
||
compteur++;
|
||
// Valide horizontales 00, 01, 02 || 10, 11, 12 || 20, 21, 22
|
||
for (int i = 0; i < LARGTIC; i++) {
|
||
fin = true;
|
||
for (int j = 0; j < LARGTIC-1; j++) {
|
||
if (grid[i][j] == "") {
|
||
fin = false;
|
||
}
|
||
else {
|
||
fin = fin && (grid[i][j] == grid[i][j + 1]);
|
||
//cout << "comparaison de " << i << "," << j << " et " << i << "," << j+1 << endl;
|
||
}
|
||
}
|
||
if (fin == true) {
|
||
return fin;
|
||
}
|
||
}
|
||
if (fin == true) {
|
||
return fin;
|
||
}
|
||
|
||
// Valide verticales 00, 10, 20 || 01, 11, 21 || 02, 12, 22
|
||
for (int i = 0; i < LARGTIC; i++) {
|
||
fin = true;
|
||
for (int j = 0; j < LARGTIC-1; j++) {
|
||
if (grid[j][i] == "") {
|
||
fin = false;
|
||
}
|
||
else {
|
||
fin = fin && (grid[j][i] == grid[j+1][i]);
|
||
//cout << "comparaison de" << j << "," << i << " et " << j + 1 << "," << i << endl;
|
||
}
|
||
}
|
||
if (fin == true) {
|
||
return fin;
|
||
}
|
||
}
|
||
if (fin == true) {
|
||
return fin;
|
||
}
|
||
|
||
// Valide diagonale de 00, 11, 22
|
||
fin = true;
|
||
for (int i = 0; i < 2; i++) {
|
||
if (grid[i][i] == "") {
|
||
fin = false;
|
||
}
|
||
else {
|
||
fin = fin && (grid[i][i] == grid[i + 1][i + 1]);
|
||
}
|
||
}
|
||
if (fin == true) {
|
||
return fin;
|
||
}
|
||
|
||
// Valide diagonale 02, 11, 20
|
||
fin = true;
|
||
for (int i = 2; i > 0; i--) {
|
||
if (grid[i][i] == "") {
|
||
fin = false;
|
||
}
|
||
else {
|
||
fin = fin && (grid[i][i] == grid[i - 1][i - 1]);
|
||
}
|
||
}
|
||
|
||
if (fin == true) {
|
||
return fin;
|
||
}
|
||
}
|
||
}
|
||
|
||
void afficherFinMatch(string grid[][LARGTIC], int gagnant, int nbJoueurs) {
|
||
afficherImageTableau(grid, style);
|
||
|
||
if (nbJoueurs == 1) {
|
||
if (gagnant == 1) {
|
||
cout << "\n\nLe joueur a gagne la partie!";
|
||
}
|
||
else if (gagnant == 2) {
|
||
cout << "\n\nL'ordinateur a gagne la partie!";
|
||
}
|
||
else {
|
||
cout << "\n\nPartie nulle!";
|
||
}
|
||
}
|
||
else {
|
||
if (gagnant == 3) {
|
||
cout << "\n\nPartie nulle!";
|
||
}
|
||
else {
|
||
cout << "\n\nLe joueur " << gagnant
|
||
<< " a gagne la partie!";
|
||
}
|
||
}
|
||
}
|
||
|
||
int partieNulle(string grid[][LARGTIC], int compteur) {
|
||
int max = LARGTIC * LARGTIC;
|
||
if (compteur == max) {
|
||
gagnant = 3;
|
||
return gagnant;
|
||
}
|
||
}
|
||
|
||
// Joueur
|
||
void tourJoueur(string grid[][LARGTIC], int joueur) {
|
||
int verticale,
|
||
horizontale;
|
||
|
||
cout << "\n\nTour du joueur " << joueur << endl;
|
||
afficherImageTableau(grid, style);
|
||
|
||
cout << "\n\nEntrez une coordonnee verticale : ";
|
||
cin >> verticale;
|
||
|
||
while (verticale < 1 || verticale > LARGTIC) {
|
||
cout << "\n\nVeuillez entrer une donnee valide.\n"
|
||
<< "\n\nEntrez une coordonnee verticale : ";
|
||
cin >> verticale;
|
||
}
|
||
|
||
cout << "\nEntrez une coordonnee horizontale : ";
|
||
cin >> horizontale;
|
||
|
||
while (horizontale < 1 || horizontale > LARGTIC) {
|
||
cout << "\n\nVeuillez entrer une donnee valide.\n"
|
||
<< "\n\nEntrez une coordonnee horizontale : ";
|
||
cin >> horizontale;
|
||
}
|
||
|
||
verticale -= 1;
|
||
horizontale -= 1;
|
||
|
||
while (grid[verticale][horizontale] == iconJ1 || grid[verticale][horizontale] == iconJ2) {
|
||
|
||
cout << "\n\nVeuillez choisir une case vide.\n"
|
||
<< "\n\nEntrez une coordonnee verticale : ";
|
||
cin >> verticale;
|
||
cout << "\nEntrez une coordonnee horizontale : ";
|
||
cin >> horizontale;
|
||
|
||
verticale--;
|
||
horizontale--;
|
||
}
|
||
|
||
if (joueur == 1) {
|
||
grid[verticale][horizontale] = iconJ1;
|
||
}
|
||
else {
|
||
grid[verticale][horizontale] = iconJ2;
|
||
}
|
||
}
|
||
|
||
//1 joueur
|
||
void jeuUnJoueur(string grid[][LARGTIC]) {
|
||
bool fin;
|
||
int gagnant;
|
||
int nbJoueurs = 1;
|
||
int compteur = 0;
|
||
|
||
cout << "Un joueur a ete choisi. Le joueur joue en premier.";
|
||
|
||
do {
|
||
tourJoueur(grid, 1);
|
||
gagnant = 1;
|
||
fin = verifFinMatch(grid, compteur);
|
||
gagnant = partieNulle(grid, compteur);
|
||
if (fin != true) {
|
||
tourOrdi(grid);
|
||
gagnant = 2;
|
||
system("PAUSE>nul");
|
||
fin = verifFinMatch(grid, compteur);
|
||
}
|
||
} while (fin != true);
|
||
afficherFinMatch(grid, gagnant, nbJoueurs);
|
||
}
|
||
|
||
void tourOrdi(string grid[][LARGTIC]) {
|
||
int vertical;
|
||
int horizontal;
|
||
|
||
cout << "\n\nTour de l'ordinateur" << endl;
|
||
do {
|
||
vertical = rand() % (LARGTIC);
|
||
horizontal = rand() % (LARGTIC);
|
||
} while (grid[vertical][horizontal] != "");
|
||
|
||
grid[vertical][horizontal] = iconJ2;
|
||
afficherImageTableau(grid, style);
|
||
}
|
||
|
||
//2 joueurs
|
||
void jeuDeuxJoueurs(string grid[][LARGTIC]) {
|
||
bool fin;
|
||
int gagnant;
|
||
int nbJoueurs = 2;
|
||
int compteur = 0;
|
||
|
||
cout << "\nDeux joueurs a ete choisi. Les tours seront l'un a la suite de l'autre.\n\n";
|
||
|
||
do {
|
||
tourJoueur(grid, 1);
|
||
gagnant = 1;
|
||
fin = verifFinMatch(grid, compteur);
|
||
compteur++;
|
||
gagnant = partieNulle(grid, compteur);
|
||
if (fin != true) {
|
||
tourJoueur(grid, 2);
|
||
gagnant = 2;
|
||
fin = verifFinMatch(grid, compteur);
|
||
compteur++;
|
||
}
|
||
} while (fin != true);
|
||
afficherFinMatch(grid, gagnant, nbJoueurs);
|
||
}
|
||
|
||
//Changer le thème
|
||
void changerLeTheme() {
|
||
string grid[LARGTIC][LARGTIC];
|
||
system("cls");
|
||
cout << endl << "Changer le theme du jeu Tic Tac Toe" << endl;
|
||
do {
|
||
cout << endl
|
||
<< "Options des styles: " << "\n\n-------------------------------------"
|
||
<< "\nA) Par defaut"
|
||
<< "\nB) Fantaisie" << "\nC) Espace"
|
||
<< "\nD) Animaux" << "\nE) Ocean" << "\nQ) Revenir en arriere"
|
||
<< "\n\nChoisir une option: ";
|
||
choix = toupper(_getche());
|
||
cout << endl;
|
||
|
||
switch (choix) {
|
||
case 'A':
|
||
style = "themes/themeDefaut.txt";
|
||
afficherImageTableau(grid, style);
|
||
|
||
cout << "\n\nVoulez vous utiliser ce theme? ";
|
||
choix = toupper(_getche());
|
||
|
||
if (choix == 'O') {
|
||
style = "themes/themeDefaut.txt";
|
||
cout << "\nTheme choisi. \n";
|
||
}
|
||
|
||
else {
|
||
style = "themes/themeDefaut.txt";
|
||
cout << "\nLe theme par defaut sera quand meme choisi. \n";
|
||
}
|
||
|
||
break;
|
||
|
||
case 'B':
|
||
style = "themes/themeFantasy.txt";
|
||
afficherImageTableau(grid, style);
|
||
|
||
cout << "\n\nVoulez vous utiliser ce theme? ";
|
||
choix = toupper(_getche());
|
||
|
||
if (choix == 'O') {
|
||
style = "themes/themeFantasy.txt";
|
||
cout << "\nTheme choisi. \n";
|
||
}
|
||
|
||
else {
|
||
style = "themes/themeDefaut.txt";
|
||
cout << "\nLe theme par defaut sera choisi.\n";
|
||
}
|
||
break;
|
||
|
||
case 'C':
|
||
style = "themes/themeEspace.txt";
|
||
afficherImageTableau(grid, style);
|
||
|
||
cout << "\n\nVoulez vous utiliser ce theme? ";
|
||
choix = toupper(_getche());
|
||
|
||
if (choix == 'O') {
|
||
style = "themes/themeEspace.txt";
|
||
cout << "\nTheme choisi. \n";
|
||
}
|
||
|
||
else {
|
||
style = "themes/themeDefaut.txt";
|
||
cout << "\nLe theme par defaut sera choisi.\n";
|
||
}
|
||
break;
|
||
|
||
case 'D':
|
||
style = "themes/themeAnimaux.txt";
|
||
afficherImageTableau(grid, style);
|
||
|
||
cout << "\n\nVoulez vous utiliser ce theme? ";
|
||
choix = toupper(_getche());
|
||
|
||
if (choix == 'O') {
|
||
style = "themes/themeAnimaux.txt";
|
||
cout << "\nTheme choisi. \n";
|
||
}
|
||
|
||
else {
|
||
style = "themes/themeDefaut.txt";
|
||
cout << "\nLe theme par defaut sera choisi.\n";
|
||
}
|
||
break;
|
||
|
||
case 'E':
|
||
style = "themes/themeSea.txt";
|
||
afficherImageTableau(grid, style);
|
||
|
||
cout << "\n\nVoulez vous utiliser ce theme? ";
|
||
choix = toupper(_getche());
|
||
|
||
if (choix == 'O') {
|
||
style = "themes/themeSea.txt";
|
||
cout << "\nTheme choisi. \n";
|
||
}
|
||
|
||
else {
|
||
style = "themes/themeDefaut.txt";
|
||
cout << "\nLe theme par defaut sera choisi.\n";
|
||
}
|
||
break;
|
||
|
||
case 'Q':
|
||
cout << "\n\nRetour au menu principal";
|
||
break;
|
||
|
||
default:
|
||
cout << "\n\nChoisir une option valide. \n";
|
||
break;
|
||
}
|
||
} while (choix != 'Q');
|
||
} |