@@ -17,6 +17,7 @@ using namespace std;
extern const int LARGTIC ;
string iconJ1 = " X " ;
string iconJ2 = " O " ;
int gagnant ;
string style = " themes/themeDefaut.txt " ;
int codeConsole = 0 ;
@@ -24,8 +25,10 @@ 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.
{
@@ -35,7 +38,7 @@ void ouvrirFichier(std::fstream& monFlux, std::string nomFichier) {
}
}
void fermerFichier ( std : : fstream & monFlux ) {
void fermerFichier ( std : : fstream & monFlux ) {
monFlux . close ( ) ;
}
@@ -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 ( ) ) {
@@ -87,8 +90,8 @@ void afficherImageMenu(std::fstream& monFlux) {
//G<> n<EFBFBD> ral (plusieurs options)
void initGrid ( string grid [ ] [ LARGTIC ] ) {
for ( int i = 0 ; i < 3 ; i + + ) {
for ( int j = 0 ; j < 3 ; j + + ) {
for ( int i = 0 ; i < LARGTIC ; i + + ) {
for ( int j = 0 ; j < LARGTIC ; j + + ) {
grid [ i ] [ j ] = " " ;
}
}
@@ -180,67 +183,81 @@ void afficherImageTableau(std::string grid[][LARGTIC], std::string style) {
SetConsoleOutputCP ( codeConsole ) ;
}
bool verifFinMatch ( std : : string grid [ ] [ LARGTIC ] ) {
bool verifFinMatch ( std : : string grid [ ] [ LARGTIC ] , int compteur ) {
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 ;
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;
}
}
else {
fin = fin & & ( grid [ i ] [ j ] = = grid [ i ] [ j + 1 ] ) ;
if ( fin = = true ) {
return fin ;
}
}
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 ;
// 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;
}
}
else {
fin = fin & & ( grid [ i ] [ j ] = = grid [ i + 1 ] [ j ] ) ;
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 ;
// Valide diagonale de 0 0, 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 ] ) ;
}
}
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 ;
}
}
if ( fin = = true ) {
return fin ;
}
}
@@ -251,14 +268,37 @@ void afficherFinMatch(string grid[][LARGTIC], int gagnant, int nbJoueurs) {
if ( gagnant = = 1 ) {
cout < < " \n \n Le joueur a gagne la partie! " ;
}
else {
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 {
cout < < " \n \n Le joueur " < < gagnant
< < " a gagne la partie! " ;
gagnant = temp ;
}
return gagnant ;
}
// Joueur
@@ -272,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 ;
@@ -281,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 ;
@@ -291,7 +331,7 @@ void tourJoueur(string grid[][LARGTIC], int joueur) {
horizontale - = 1 ;
while ( grid [ verticale ] [ horizontale ] = = iconJ1 | | grid [ verticale ] [ horizontale ] = = iconJ2 ) {
// V<> RIFIER LE CIN D'ERREUR!!!!!
cout < < " \n \n Veuillez choisir une case vide. \n "
< < " \n \n Entrez une coordonnee verticale : " ;
cin > > verticale ;
@@ -313,22 +353,25 @@ void tourJoueur(string grid[][LARGTIC], int joueur) {
//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>nul " ) ;
fin = verifFinMatch ( grid ) ;
compteur + + ;
fin = verifFinMatch ( grid , compteur ) ;
}
} while ( fin ! = true ) ;
gagnant = partieNulle ( grid , compteur ) ;
afficherFinMatch ( grid , gagnant , nbJoueurs ) ;
}
@@ -338,8 +381,8 @@ void tourOrdi(string grid[][LARGTIC]) {
cout < < " \n \n Tour de l'ordinateur " < < endl ;
do {
vertical = rand ( ) % ( 3 ) ;
horizontal = rand ( ) % ( 3 ) ;
vertical = rand ( ) % ( LARGTIC ) ;
horizontal = rand ( ) % ( LARGTIC ) ;
} while ( grid [ vertical ] [ horizontal ] ! = " " ) ;
grid [ vertical ] [ horizontal ] = iconJ2 ;
@@ -349,21 +392,24 @@ void tourOrdi(string grid[][LARGTIC]) {
//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 ) ;
}
@@ -483,4 +529,4 @@ void changerLeTheme() {
break ;
}
} while ( choix ! = ' Q ' ) ;
}
}