Files
TicTacToe/TicTacToe/fonctions.cpp
2025-11-21 12:22:29 -05:00

204 lines
4.2 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;
fstream monFlux;
HANDLE hconsole = GetStdHandle(STD_OUTPUT_HANDLE);
char carre = 219, choix;
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();
}
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);
}
}
void afficherImageTableau(std::string grid[][LARGTIC]) {
SetConsoleOutputCP(CP_UTF8);
string emoteUp,
emoteLine,
emoteDown;
ouvrirFichier(monFlux, "themes/themeSea.txt");
if (monFlux) {
while (!monFlux.eof()) {
monFlux >> emoteUp >> emoteLine >> emoteDown;
}
monFlux.close();
cout << endl;
}
else {
cout << "ERREUR : Impossible douvrir le fichier! " << endl;
exit(1);
}
fermerFichier(monFlux);
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 << emoteLine;
for (int j = 0; j < 3; j++) {
cout << " ";
}
}
cout << emoteLine << endl << " " << k + 1 << " ";
for (int i = 0; i < LARGTIC; i++) {
cout << emoteLine << " " << grid[k][i] << " ";
}
cout << emoteLine << endl << " ";
for (int i = 0; i < LARGTIC; i++) {
cout << emoteLine;
for (int j = 0; j < 3; j++) {
cout << " ";
}
}
cout << emoteLine << endl << " ";
}
for (int i = 0; i < LARGTIC; i++) {
for (int j = 0; j < 2; j++) {
cout << emoteDown << " ";
}
}
cout << endl;
}
bool verifFinMatch(std::string grid[][LARGTIC]) {
bool fin = true;
// Valide horizontales
for (int i = 0; i < 2; i++) {
fin = true;
for (int j = 0; j < 2; j++) {
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++) {
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++) {
fin = fin && grid[i][i] == grid[i + 1][i + 1];
if (fin == true) {
return fin;
}
}
// Valide diagonale 02, 11, 20
for (int i = 2; i > 0; i--) {
fin = fin && grid[i][i] == grid[i - 1][i - 1];
if (fin == true) {
return fin;
}
}
}
//Menu
void afficherMenu(char& choix) {
system("cls");
//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) Montrer l'historique des scores" << endl
<< "D) Changer le style du jeu" << endl
<< "Q) Quitter" << endl << endl
<< "Quel est votre choix? ";
choix = toupper(_getche());
cout << endl;
}
void initialiserGrid(string grid[][LARGTIC]) {
}
void jeuDeuxJoueurs(string grid[][LARGTIC]) {
bool fin;
cout << endl << "Deux joueurs a ete choisi. Les tours seront l'un a la suite de l'autre." << endl << endl;
do {
cout << "Tour du joueur" << endl;
afficherImageTableau(grid);
system("PAUSE");
fin = verifFinMatch(grid);
// la condition doit être une série de 3 chiffres
} while (fin = true);
}