@@ -15,27 +15,30 @@ BUT : Fichier de fonctions Tic Tac Toe
using namespace std ;
extern const int LARGTIC ;
string ICON J1 = " X " ;
string ICON J2 = " O " ;
str ing style = " themes/themeSea.txt " ;
string icon J1 = " X " ;
string icon J2 = " O " ;
int gagnant ;
string style = " themes/themeDefaut.txt " ;
int codeConsole = 0 ;
fstream monFlux ;
HANDLE hconsole = GetStdHandle ( STD_OUTPUT_HANDLE ) ;
char carre = 219 , choix ;
//<2F> faire : <20> galit<69> et entrer de coordonn<6E> e
//Fichiers
void ouvrirFichier ( std : : fstream & monFlux , std : : string nomFichier ) {
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 " ) ;
system ( " PAUSE>null " ) ;
exit ( 1 ) ; //On quitte imm<6D> diatement le programme
}
}
void fermerFichier ( std : : fstream & monFlux ) {
void fermerFichier ( std : : fstream & monFlux ) {
monFlux . close ( ) ;
}
@@ -49,7 +52,7 @@ void afficherMenu(char& choix) {
fermerFichier ( monFlux ) ;
// Menu
cout < < endl < < " MENU " < < endl
cout < < endl < < " MENU " < < endl
< < " ---------------------------------- " < < endl
< < " A) 1 joueur " < < endl
< < " B) 2 joueurs " < < endl
@@ -60,7 +63,7 @@ void afficherMenu(char& choix) {
cout < < endl ;
}
void afficherImageMenu ( std : : fstream & monFlux ) {
void afficherImageMenu ( std : : fstream & monFlux ) {
int repet = 0 ;
if ( monFlux ) {
while ( ! monFlux . eof ( ) ) {
@@ -86,19 +89,27 @@ void afficherImageMenu(std::fstream& monFlux) {
}
//G<> n<EFBFBD> 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 ) ;
SetConsoleOutputCP ( CP_UTF8 ) ; //affiche les <20> mojis
string emoteUp ,
emoteDown ,
icon J1,
icon J2;
emote J1,
emote J2;
// Faire une fonction pour retenir le th<74> me s<> lectionn<6E> par le joueur!!!!
ouvrirFichier ( monFlux , style ) ;
if ( monFlux ) {
while ( ! monFlux . eof ( ) ) {
monFlux > > emoteUp > > emoteDown > > icon J1 > > icon J2;
monFlux > > emoteUp > > emoteDown > > emote J1 > > emote J2;
}
monFlux . close ( ) ;
cout < < endl ;
@@ -109,6 +120,9 @@ void afficherImageTableau(std::string grid[][LARGTIC], std::string style) {
}
fermerFichier ( monFlux ) ;
// Lignes de code pour faire afficher le Tic Tac Toe une fois que les symboles ont <20> t<EFBFBD> ajout<75> 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 <20> t<EFBFBD> ajout<75> s
cout < < " " ;
for ( int i = 0 ; i < LARGTIC ; i + + ) {
@@ -125,27 +139,38 @@ void afficherImageTableau(std::string grid[][LARGTIC], std::string style) {
cout < < endl < < " " ;
for ( int i = 0 ; i < LARGTIC ; i + + ) {
cout < < " |" ;
cout < < " | " ;
for ( int j = 0 ; j < 3 ; j + + ) {
cout < < " " ;
}
cout < < " " ;
}
cout < < " |" < < endl < < " " < < k + 1 < < " " ;
cout < < " | " < < endl < < " " < < k + 1 < < " " ;
for ( int i = 0 ; i < LARGTIC ; i + + ) {
cout < < " |" < < " " < < grid [ k ] [ i ] < < " " ;
cout < < " | " < < " " ;
if ( grid [ k ] [ i ] = = " X " ) {
cout < < emoteJ1 ;
}
else if ( grid [ k ] [ i ] = = " O " ) {
cout < < emoteJ2 ;
}
else {
cout < < " " ;
}
cout < < " " ;
}
cout < < " |" < < endl < < " " ;
cout < < " | " < < endl < < " " ;
for ( int i = 0 ; i < LARGTIC ; i + + ) {
cout < < " |" ;
cout < < " | " ;
for ( int j = 0 ; j < 3 ; j + + ) {
cout < < " " ;
}
cout < < " " ;
}
cout < < " |" < < endl < < " " ;
cout < < " | " < < endl < < " " ;
}
@@ -158,6 +183,125 @@ void afficherImageTableau(std::string grid[][LARGTIC], std::string style) {
SetConsoleOutputCP ( codeConsole ) ;
}
bool verifFinMatch ( std : : string grid [ ] [ LARGTIC ] , int compteur ) {
bool fin = false ;
int max = LARGTIC * LARGTIC ;
if ( compteur = = max ) {
return true ;
}
else {
// 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 \n Le joueur a gagne la partie! " ;
}
else if ( gagnant = = 2 ) {
cout < < " \n \n L'ordinateur a gagne la partie! " ;
}
else {
cout < < " \n \n Partie nulle! " ;
}
}
else if ( nbJoueurs = = 2 ) {
if ( gagnant = = 1 ) {
cout < < " \n \n Joueur 1 a gagne la partie! " ;
}
else if ( gagnant = = 2 ) {
cout < < " \n \n Joueur 2 a gagne la partie! " ;
}
else {
cout < < " \n \n Partie nulle! " ;
}
}
}
int partieNulle ( string grid [ ] [ LARGTIC ] , int compteur ) {
int max = LARGTIC * LARGTIC ;
int temp = gagnant ;
if ( compteur = = max ) {
gagnant = 3 ;
}
else {
gagnant = temp ;
}
return gagnant ;
}
// Joueur
void tourJoueur ( string grid [ ] [ LARGTIC ] , int joueur ) {
int verticale ,
horizontale ;
@@ -168,7 +312,7 @@ void tourJoueur(string grid[][LARGTIC], int joueur) {
cout < < " \n \n Entrez une coordonnee verticale : " ;
cin > > verticale ;
while ( verticale < 1 | | verticale > 3 ) {
while ( verticale < 1 | | verticale > LARGTIC ) {
cout < < " \n \n Veuillez entrer une donnee valide. \n "
< < " \n \n Entrez une coordonnee verticale : " ;
cin > > verticale ;
@@ -177,7 +321,7 @@ void tourJoueur(string grid[][LARGTIC], int joueur) {
cout < < " \n Entrez une coordonnee horizontale : " ;
cin > > horizontale ;
while ( horizontale < 1 | | horizontale > 3 ) {
while ( horizontale < 1 | | horizontale > LARGTIC ) {
cout < < " \n \n Veuillez entrer une donnee valide. \n "
< < " \n \n Entrez une coordonnee horizontale : " ;
cin > > horizontale ;
@@ -186,121 +330,48 @@ void tourJoueur(string grid[][LARGTIC], int joueur) {
verticale - = 1 ;
horizontale - = 1 ;
while ( grid [ verticale ] [ horizontale ] = = ICON J1 | | grid [ verticale ] [ horizontale ] = = ICON J2) {
while ( grid [ verticale ] [ horizontale ] = = icon J1 | | grid [ verticale ] [ horizontale ] = = icon J2) {
cout < < " \n \n Veuillez choisir une case vide. \n "
< < " \n \n Entrez une coordonnee verticale : " ;
cin > > verticale ;
cout < < " \n Entrez une coordonnee horizontale : " ;
cin > > horizontale ;
verticale - - ;
horizontale - - ;
}
if ( joueur = = 1 ) {
grid [ verticale - 1 ] [ horizontale - 1 ] = ICON J1;
grid [ verticale ] [ horizontale ] = icon J1;
}
else {
grid [ verticale - 1 ] [ horizontale - 1 ] = ICON J2;
}
}
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 \n Le joueur a gagne la partie! " ;
}
else {
cout < < " \n \n L'ordinateur a gagne la partie! " ;
}
}
else {
cout < < " \n \n Le joueur " < < gagnant
< < " a gagne la partie! " ;
grid [ verticale ] [ horizontale ] = icon J2;
}
}
//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 + + ;
fin = verifFinMatch ( grid , compteur ) ;
if ( fin ! = true ) {
tourOrdi ( grid ) ;
gagnant = 2 ;
system ( " PAUSE>0 " ) ;
fin = verifFinMatch ( grid ) ;
system ( " PAUSE>nul " ) ;
compteur + + ;
fin = verifFinMatch ( grid , compteur ) ;
}
} while ( fin ! = true ) ;
gagnant = partieNulle ( grid , compteur ) ;
afficherFinMatch ( grid , gagnant , nbJoueurs ) ;
}
@@ -310,73 +381,151 @@ void tourOrdi(string grid[][LARGTIC]) {
cout < < " \n \n Tour de l'ordinateur " < < endl ;
do {
vertical = rand ( ) % ( 3 ) ;
horizontal = rand ( ) % ( 3 ) ;
} while ( grid [ vertical ] [ horizontal ] ! = " ") ;
grid [ vertical ] [ horizontal ] = ICONJ2 ;
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 < < " \n Deux 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 + + ;
fin = verifFinMatch ( grid , compteur ) ;
if ( fin ! = true ) {
tourJoueur ( grid , 2 ) ;
gagnant = 2 ;
fin = verifFinMatch ( grid ) ;
compteur + + ;
fin = verifFinMatch ( grid , compteur ) ;
}
} while ( fin ! = true ) ;
gagnant = partieNulle ( grid , compteur ) ;
afficherFinMatch ( grid , gagnant , nbJoueurs ) ;
}
//Changer le th<74> me
void changerLeTheme ( ) {
string grid [ LARGTIC ] [ LARGTIC ] = { ICONJ1 , ICONJ2 } ;
string grid [ LARGTIC ] [ LARGTIC ] ;
system ( " cls " ) ;
cout < < endl < < " Changer le theme du jeu Tic Tac Toe " < < endl ;
do {
cout < < endl < < " Changer le theme du jeu Tic Tac Toe " < < endl
< < " Options des styles: " < < " \n A) Ocean "
cout < < endl
< < " Options des styles: " < < " \n \n ------------------------------------- "
< < " \n A) Par defaut "
< < " \n B) Fantaisie " < < " \n C) Espace "
< < " \n D) Animaux " < < " \n Q) Revenir en arriere "
< < " \n Choisir une option: " ;
< < " \n D) Animaux " < < " \n E) Ocean " < < " \n Q) Revenir en arriere"
< < " \n \n Choisir une option: " ;
choix = toupper ( _getche ( ) ) ;
cout < < endl ;
switch ( choix ) {
case ' A ' :
style = " themes/themeSea .txt " ;
style = " themes/themeDefaut .txt " ;
afficherImageTableau ( grid , style ) ;
cout < < " \n \n Voulez vous utiliser ce theme? " ;
choix = toupper ( _getche ( ) ) ;
if ( choix = = ' O ' ) {
style = " themes/themeDefaut.txt " ;
cout < < " \n Theme choisi. \n " ;
}
else {
style = " themes/themeDefaut.txt " ;
cout < < " \n Le theme par defaut sera quand meme choisi. \n " ;
}
break ;
case ' B ' :
style = " themes/themeFantasy.txt " ;
afficherImageTableau ( grid , style ) ;
cout < < " \n \n Voulez vous utiliser ce theme? " ;
choix = toupper ( _getche ( ) ) ;
if ( choix = = ' O ' ) {
style = " themes/themeFantasy.txt " ;
cout < < " \n Theme choisi. \n " ;
}
else {
style = " themes/themeDefaut.txt " ;
cout < < " \n Le theme par defaut sera choisi. \n " ;
}
break ;
case ' C ' :
style = " themes/themeEspace.txt " ;
afficherImageTableau ( grid , style ) ;
cout < < " \n \n Voulez vous utiliser ce theme? " ;
choix = toupper ( _getche ( ) ) ;
if ( choix = = ' O ' ) {
style = " themes/themeEspace.txt " ;
cout < < " \n Theme choisi. \n " ;
}
else {
style = " themes/themeDefaut.txt " ;
cout < < " \n Le theme par defaut sera choisi. \n " ;
}
break ;
case ' D ' :
style = " themes/themeAnimaux.txt " ;
afficherImageTableau ( grid , style ) ;
cout < < " \n \n Voulez vous utiliser ce theme? " ;
choix = toupper ( _getche ( ) ) ;
if ( choix = = ' O ' ) {
style = " themes/themeAnimaux.txt " ;
cout < < " \n Theme choisi. \n " ;
}
else {
style = " themes/themeDefaut.txt " ;
cout < < " \n Le theme par defaut sera choisi. \n " ;
}
break ;
case ' E ' :
style = " themes/themeSea.txt " ;
afficherImageTableau ( grid , style ) ;
cout < < " \n \n Voulez vous utiliser ce theme? " ;
choix = toupper ( _getche ( ) ) ;
if ( choix = = ' O ' ) {
style = " themes/themeSea.txt " ;
cout < < " \n Theme choisi. \n " ;
}
else {
style = " themes/themeDefaut.txt " ;
cout < < " \n Le theme par defaut sera choisi. \n " ;
}
break ;
case ' Q ' :
cout < < " \n \n Retour au menu principal " ;
break ;
default :
cout < < " \n Choisir une option valide. \n " ;
cout < < " \n \n Choisir une option valide. \n " ;
break ;
}
} while ( choix ! = ' Q ' ) ;