Files
TicTacToe/TicTacToe/fonctions.cpp

487 lines
10 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*====================================
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";
string style = "themes/themeDefaut.txt";
int codeConsole = 0;
fstream monFlux;
HANDLE hconsole = GetStdHandle(STD_OUTPUT_HANDLE);
char carre = 219, choix;
//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>0");
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 douvrir le fichier! " << endl;
exit(1);
}
}
//Général (plusieurs options)
void initGrid(string grid[][LARGTIC]) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
grid[i][j] = "";
}
}
}
void afficherImageTableau(std::string grid[][LARGTIC], std::string style) {
SetConsoleOutputCP(CP_UTF8);
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 douvrir 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);
}
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 > 3) {
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 > 3) {
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) {
// VÉRIFIER LE CIN D'ERREUR!!!!!
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;
}
}
bool verifFinMatch(std::string grid[][LARGTIC]) {
bool fin = false;
// Valide horizontales
for (int i = 0; i < 2; i++) {
fin = true;
for (int j = 0; j < 2; j++) {
if (grid[i][j] == "") {
fin = false;
}
else {
fin = fin && (grid[i][j] == grid[i][j + 1]);
}
}
if (fin == true) {
return fin;
}
}
// Valide verticales
for (int i = 0; i < 2; i++) {
fin = true;
for (int j = 0; j < 2; j++) {
if (grid[i][j] == "") {
fin = false;
}
else {
fin = fin && (grid[i][j] == grid[i + 1][j]);
}
}
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 {
cout << "\n\nL'ordinateur a gagne la partie!";
}
}
else {
cout << "\n\nLe joueur " << gagnant
<< " a gagne la partie!";
}
}
//1 joueur
void jeuUnJoueur(string grid[][LARGTIC]) {
bool fin;
int gagnant;
int nbJoueurs = 1;
cout << "Un joueur a ete choisi. Le joueur joue en premier.";
do {
tourJoueur(grid, 1);
gagnant = 1;
fin = verifFinMatch(grid);
if (fin != true) {
tourOrdi(grid);
gagnant = 2;
system("PAUSE>0");
fin = verifFinMatch(grid);
}
} 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() % (3);
horizontal = rand() % (3);
} 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;
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);
if (fin != true) {
tourJoueur(grid, 2);
gagnant = 2;
fin = verifFinMatch(grid);
}
} while (fin != true);
afficherFinMatch(grid, gagnant, nbJoueurs);
}
//Changer le thème
void changerLeTheme(bool& theme) {
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;
if (choix >= 'A' && choix <= 'D') {
theme = true;
}
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.";
}
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.";
}
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.";
}
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.";
}
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";
system("PAUSE>nul");
break;
default:
cout << "\n\nChoisir une option valide. \n";
break;
}
} while (choix != 'Q');
}