IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

 C++ Discussion :

TIC TAC game


Sujet :

C++

  1. #1
    Nouveau membre du Club
    Homme Profil pro
    Deficient visuel
    Inscrit en
    Mai 2019
    Messages
    196
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 52
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Deficient visuel

    Informations forums :
    Inscription : Mai 2019
    Messages : 196
    Points : 37
    Points
    37
    Par défaut TIC TAC game
    Bonjour a toutes et a tous ,
    J'aurai voulu qu'a chaque debut de partie , la personne qui commence soit différente . J'ai fait une fonction pour que cela soit aleatoire mais je n'obtiens pas le resultat escompté : Au lieu du 'T' ou du 'L' en retour de ma fonction , j'obtiens un point d'interogation dans un hexagone.
    Ma fonction gen_random ne doit pas etre bien construite et peut etre est il possible de faire plus simple .
    Merci de votre aide .

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
     
     
    char matrix[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    char player;
    int count;
     
    char gen_random(){
       char choix[2] = {
          'L', 'T'
       };
       char res;
       for (int i = 0; i < 2; i++)
          res = res + choix[rand() % 2];
       return res;
    }
     
    void Draw()                                                                        
    {
    	cout << "     |     |     " << endl;
    	cout << "  " << matrix[0][0]  << "  |  " << matrix[0][1] << "  |  " << matrix[0][2]  << endl;
     
    	cout << "_____|_____|_____" << endl;
    	cout << "     |     |     " << endl;
     
    	cout << "  " << matrix[1][0]  << "  |  " << matrix[1][1]  << "  |  " <<matrix[1][2]  << endl;
     
    	cout << "_____|_____|_____" << endl;
    	cout << "     |     |     " << endl;
     
    	cout << "  " << matrix[2][0] << "  |  " << matrix[2][1] << "  |  " << matrix[2][2] << endl;
     
    	cout << "     |     |     " << endl << endl;
    }
     
    void Input()                                                                                   
     {
            srand(time(NULL));
            player=gen_random();
     
    	cout << "Its "<<player<<" turn's Enter the number of the field: ";  
    	int a;
    	cin >> a; 								   
     
    	if (a == 1)								   	
    		if (matrix[0][0]=='1')                                             
    			matrix[0][0] = player;
    		else
    			{
    			cout << "\nInvalid move :-( \n";                           
    				count=count-1;                                     
     
    			}
    	else if (a == 2)
    		if (matrix[0][1]=='2')
    			matrix[0][1] = player;
    		else
    			{
    			cout << "\nInvalid move :-(  \n";
    				count=count-1;
     
    			}
    	else if (a == 3)
    		if (matrix[0][2]=='3')
    			matrix[0][2] = player;
    		else
    			{
    			cout << "\nInvalid move :-(  \n";
    				count=count-1;
    			}
     
    	else if (a == 4)
    	        if (matrix[1][0]=='4')
    			matrix[1][0] = player;
    		else
    			{
    			cout << "\nInvalid move :-(  \n";
    				count=count-1;
    			}
    	else if (a == 5)
    		  if (matrix[1][1]=='5')
    			matrix[1][1] = player;
    		else
    			{
    			cout << "\nInvalid move :-(  \n";
    				count=count-1;
    			}
     
    	else if (a == 6)
      		if (matrix[1][2]=='6')
    			matrix[1][2] = player;
    		else
    			{
    			cout << "\nInvalid move :-(  \n";
    				count=count-1;
    			}
     
    	else if (a == 7)
    		  if (matrix[2][0]=='7')
    			matrix[2][0] = player;
    		else
    			{
    			cout << "\nInvalid move :-(  \n";
    				count=count-1;
    			}
     
    	else if (a == 8)
    		  if (matrix[2][1]=='8')
    			matrix[2][1] = player;
    		else
    			{
    			cout << "\nInvalid move :-(  \n";
    				count=count-1;
    			}
     
    	else if (a == 9)
    		  if (matrix[2][2]=='9')
    			matrix[2][2] = player;
    		else
    			{
    			cout << "\nInvalid move :-( \n";
    				count=count-1;
    			}
    }
     
    void cuser()                                                                      //                                          
    {
    	if (player == 'L')                                                      //condition 
    	{
     
    		player = 'T';                                                   
     
    	}
     
    	else
    	{	
    		player = 'L';
     
    	}
    }
     
    char Winner()                                                                  
    {
     
    	if (matrix[0][0] == 'L' && matrix[0][1] == 'L' && matrix[0][2] == 'L')
    		return 'L';
    	if (matrix[1][0] == 'L' && matrix[1][1] == 'L' && matrix[1][2] == 'L')
    		return 'L';
    	if (matrix[2][0] == 'L' && matrix[2][1] == 'L' && matrix[2][2] == 'L')
    		return 'L';
     
    	if (matrix[0][0] == 'L' && matrix[1][0] == 'L' && matrix[2][0] == 'L')
    		return 'L';
    	if (matrix[0][1] == 'L' && matrix[1][1] == 'L' && matrix[2][1] == 'L')
    		return 'L';
    	if (matrix[0][2] == 'L' && matrix[1][2] == 'L' && matrix[2][2] == 'L')
    		return 'L';
     
    	if (matrix[0][0] == 'L' && matrix[1][1] == 'L' && matrix[2][2] == 'L')
    		return 'L';
    	if (matrix[2][0] == 'L' && matrix[1][1] == 'L' && matrix[0][2] == 'L')
    		return 'L';
     
     
    	if (matrix[0][0] == 'T' && matrix[0][1] == 'T' && matrix[0][2] == 'T')
    		return 'T';
    	if (matrix[1][0] == 'T' && matrix[1][1] == 'T' && matrix[1][2] == 'T')
    		return 'T';
    	if (matrix[2][0] == 'T' && matrix[2][1] == 'T' && matrix[2][2] == 'T')
    		return 'T';
     
    	if (matrix[0][0] == 'T' && matrix[1][0] == 'T' && matrix[2][0] == 'T')
    		return 'T';
    	if (matrix[0][1] == 'T' && matrix[1][1] == 'T' && matrix[2][1] == 'T')
    		return 'T';
    	if (matrix[0][2] == 'T' && matrix[1][2] == 'T' && matrix[2][2] == 'T')
    		return 'T';
     
    	if (matrix[0][0] == 'T' && matrix[1][1] == 'T' && matrix[2][2] == 'T')
    		return 'T';
    	if (matrix[2][0] == 'T' && matrix[1][1] == 'T' && matrix[0][2] == 'T')
    		return 'T';
     
    	return '/';                                                                  //fun
    }
     
    int main()                                                                                     
    {
     
    	count=0;
    	cout << "*****************************************************************************\n";	
    	cout << "\n Tic Tac Game  \n\n";
    	cout << "*****************************************************************************\n\n";
    	Draw();                                                                                 
    	while (1)
    	{
    		count++;                                                                       
     		Input();
    		Draw();
    		if (Winner() == 'L')                                                      
    		{
    			cout << "_____________________________RESULT_________________________________________\n\n";
    			cout << " Player Lauralee wins! :-)\n\n";
    			break;
    		}
    		else if (Winner() == 'T')                                                       
    		{
    			cout << "_____________________________RESULT___________________________________________\n\n";
    			cout << " Player Théo wins! :-) \n\n";
    			break;
    		}
    		else if (Winner() == '/'&& count==9)                                           
    		{
    			cout << "_____________________________RESULT___________________________________________\n\n";
    			cout << "Draw please try again :-( \n\n";
    			break;
    		}
     
     
    		cuser();                                                                      
    	}
     
    	return 0;
     
    }

  2. #2
    Expert éminent
    Homme Profil pro
    Ingénieur développement matériel électronique
    Inscrit en
    Décembre 2015
    Messages
    1 565
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 60
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Ingénieur développement matériel électronique
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Décembre 2015
    Messages : 1 565
    Points : 7 648
    Points
    7 648
    Par défaut
    Bonjour,

    Ce que fait ton code entre les lignes 15 et 17 :
    Prendre un caractère non initialisé, y ajouter le code ASCII du 'T' ou bien du 'L', puis encore y ajouter le code ASCII du 'T' ou bien du 'L'. Le caractère à retourner est celui dont le code ASCII est la somme de ces 3 choses!
    Il faut faire beaucoup plus simple : choisir un caractère de code ASCII 'T' ou bien 'L'. Il faut ôter ce qui est en trop.

  3. #3
    Nouveau membre du Club
    Homme Profil pro
    Deficient visuel
    Inscrit en
    Mai 2019
    Messages
    196
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 52
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Deficient visuel

    Informations forums :
    Inscription : Mai 2019
    Messages : 196
    Points : 37
    Points
    37
    Par défaut
    Merci de ta reponse , concretement je ne vois pas trop quoi faire .
    Un autre point , je ne comprends pas le :
    return '/'; de la fonction " winner"

  4. #4
    Expert éminent
    Homme Profil pro
    Ingénieur développement matériel électronique
    Inscrit en
    Décembre 2015
    Messages
    1 565
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 60
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Ingénieur développement matériel électronique
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Décembre 2015
    Messages : 1 565
    Points : 7 648
    Points
    7 648
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    char gen_random(){
       static const char choix[2] = {
          'L', 'T'
       };
       return  choix[rand() % 2];
    }
    Si aucun gagnant, alors la fonction Winner() ne pas retourner ni 'T' no 'L' alors pourquoi pas '/'

  5. #5
    Expert confirmé

    Homme Profil pro
    Directeur de projet
    Inscrit en
    Mai 2013
    Messages
    1 329
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Directeur de projet
    Secteur : Service public

    Informations forums :
    Inscription : Mai 2013
    Messages : 1 329
    Points : 4 146
    Points
    4 146
    Par défaut Simplifier
    Bonjour,

    Citation Envoyé par chris7522 Voir le message
    ... concrètement je ne vois pas trop quoi faire .
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    inline char gen_random() {
    return rand() & 1 ? 'L' : 'T';
    }
    Plus simple ?

    Salutations
    Ever tried. Ever failed. No matter. Try Again. Fail again. Fail better. (Samuel Beckett)

  6. #6
    Nouveau membre du Club
    Homme Profil pro
    Deficient visuel
    Inscrit en
    Mai 2019
    Messages
    196
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 52
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Deficient visuel

    Informations forums :
    Inscription : Mai 2019
    Messages : 196
    Points : 37
    Points
    37
    Par défaut
    Merci a tous les deux !
    Dans ta fonction inline :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    inline char gen_random() {
    return rand() & 1 ? 'L' : 'T';
    }
    Je ne comprends pas la signification de :
    &1

  7. #7
    Nouveau membre du Club
    Homme Profil pro
    Deficient visuel
    Inscrit en
    Mai 2019
    Messages
    196
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 52
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Deficient visuel

    Informations forums :
    Inscription : Mai 2019
    Messages : 196
    Points : 37
    Points
    37
    Par défaut
    Serait il possible de faire la meme chose avec 2 string ?
    Au lieu de ' L' et 'T' , plutot : " Lals " et " Theo" .
    Il va y avoir un probleme de conversion de string a char ?
    Un tableau 2 dimension de string ca n'existe pas ?

  8. #8
    Expert éminent
    Homme Profil pro
    Ingénieur développement matériel électronique
    Inscrit en
    Décembre 2015
    Messages
    1 565
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 60
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Ingénieur développement matériel électronique
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Décembre 2015
    Messages : 1 565
    Points : 7 648
    Points
    7 648
    Par défaut
    Citation Envoyé par chris7522 Voir le message
    Serait il possible de faire la meme chose avec 2 string ?
    Au lieu de ' L' et 'T' , plutot : " Lals " et " Theo" .
    Il suffit d'utiliser des string au lieu de char.
    Citation Envoyé par chris7522 Voir le message
    Il va y avoir un probleme de conversion de string a char ?
    Ça ça n'a aucun sens, il suffit de travailler avec des string
    Citation Envoyé par chris7522 Voir le message
    Un tableau 2 dimension de string ca n'existe pas ?
    Tous les types d'objets peuvent être mis dans un tableau.

  9. #9
    Expert éminent sénior
    Homme Profil pro
    Analyste/ Programmeur
    Inscrit en
    Juillet 2013
    Messages
    4 630
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Analyste/ Programmeur

    Informations forums :
    Inscription : Juillet 2013
    Messages : 4 630
    Points : 10 556
    Points
    10 556
    Par défaut
    Citation Envoyé par chris7522 Voir le message
    Je ne comprends pas la signification de :
    C'est 1 ET BINAIRE il faut sortir les tables 1&1 = 1, 0&1 = 0, 1&0 = 0, 0&0 = 0
    Donc en gros, tu vas prendre le bit le plus à droite du nombre généré (puisque 1 = 00000000000000000001, on bourre à gauche de 0) et soit il vaut 0 (vaut faux en C/ C++) soit 1 (toute valeur différente de 0 vaut vrai en C/ C++)


    Citation Envoyé par chris7522 Voir le message
    Au lieu de ' L' et 'T' , plutot : " Lals " et " Theo" .
    Il faut tester , mais avec 1 retour/ paramètre const std::string& ou const std::string. Et en théorie avec return "Theo"; ou ret = "Theo";, les constructeurs devraient prendre en charge la partie conversion en std::string.

  10. #10
    Nouveau membre du Club
    Homme Profil pro
    Deficient visuel
    Inscrit en
    Mai 2019
    Messages
    196
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 52
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Deficient visuel

    Informations forums :
    Inscription : Mai 2019
    Messages : 196
    Points : 37
    Points
    37
    Par défaut
    J'ai tenté ca , mais ca l'fait pas :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
     
     
    char matrix[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    string player;
    int count;
     
    string gen_random(){
       const string choix[2] = {
          "LALS", "THEO"
       };
       return  choix[rand() % 2];
    }
     
    void Draw()                                                                        
    {
    	cout << "      |       |     " << endl;
    	cout << "  " << matrix[0][0]  << "   |   " << matrix[0][1] << "   |  " << matrix[0][2]  << endl;
     
    	cout << "_____ | _____ | _____" << endl;
    	cout << "      |       |     " << endl;
     
    	cout << "  " << matrix[1][0]  << "   |  " << matrix[1][1]  << "    |  " <<matrix[1][2]  << endl;
     
    	cout << "_____ | _____ | _____" << endl;
    	cout << "      |       |     " << endl;
     
    	cout << "  " << matrix[2][0] << "   |  " << matrix[2][1] << "    |  " << matrix[2][2] << endl;
     
    	cout << "      |       |     " << endl << endl;
    }
     
    void Input()                                                                                   
     {
            player=gen_random();
     
    	cout << "Its "<<player<<" turn's Enter the number of the field: ";  
    	int a;
    	cin >> a; 								   
     
    	if (a == 1)								   	
    		if (matrix[0][0]=='1')                                             
    			matrix[0][0] = player;
    		else
    			{
    			cout << "\nInvalid move :-( \n";                           
    				count=count-1;                                     
     
    			}
    	else if (a == 2)
    		if (matrix[0][1]=='2')
    			matrix[0][1] = player;
    		else
    			{
    			cout << "\nInvalid move :-(  \n";
    				count=count-1;
     
    			}
    	else if (a == 3)
    		if (matrix[0][2]=='3')
    			matrix[0][2] = player;
    		else
    			{
    			cout << "\nInvalid move :-(  \n";
    				count=count-1;
    			}
     
    	else if (a == 4)
    	        if (matrix[1][0]=='4')
    			matrix[1][0] = player;
    		else
    			{
    			cout << "\nInvalid move :-(  \n";
    				count=count-1;
    			}
    	else if (a == 5)
    		  if (matrix[1][1]=='5')
    			matrix[1][1] = player;
    		else
    			{
    			cout << "\nInvalid move :-(  \n";
    				count=count-1;
    			}
     
    	else if (a == 6)
      		if (matrix[1][2]=='6')
    			matrix[1][2] = player;
    		else
    			{
    			cout << "\nInvalid move :-(  \n";
    				count=count-1;
    			}
     
    	else if (a == 7)
    		  if (matrix[2][0]=='7')
    			matrix[2][0] = player;
    		else
    			{
    			cout << "\nInvalid move :-(  \n";
    				count=count-1;
    			}
     
    	else if (a == 8)
    		  if (matrix[2][1]=='8')
    			matrix[2][1] = player;
    		else
    			{
    			cout << "\nInvalid move :-(  \n";
    				count=count-1;
    			}
     
    	else if (a == 9)
    		  if (matrix[2][2]=='9')
    			matrix[2][2] = player;
    		else
    			{
    			cout << "\nInvalid move :-( \n";
    				count=count-1;
    			}
    }
     
    void cuser()                                                                                                              
    {
    	if (player == "LALS")                                                       
    	{
     
    		player = "THEO";                                                   
     
    	}
     
    	else
    	{	
    		player = "LALS";
     
    	}
    }
     
    char Winner()                                                                  
    {
     
    	if (matrix[0][0] == "LALS" && matrix[0][1] == "LALS" && matrix[0][2] == "LALS")
    		return "LALS";
    	if (matrix[1][0] == "LALS" && matrix[1][1] == "LALS" && matrix[1][2] == "LALS")
    		return "LALS";
    	if (matrix[2][0] == "LALS" && matrix[2][1] == "LALS" && matrix[2][2] == "LALS")
    		return "LALS";
     
    	if (matrix[0][0] == "LALS" && matrix[1][0] == "LALS" && matrix[2][0] == "LALS")
    		return "LALS";
    	if (matrix[0][1] == "LALS" && matrix[1][1] == "LALS" && matrix[2][1] == "LALS")
    		return "LALS";
    	if (matrix[0][2] == "LALS" && matrix[1][2] == "LALS" && matrix[2][2] == "LALS")
    		return "LALS";
     
    	if (matrix[0][0] == "LALS" && matrix[1][1] == "LALS" && matrix[2][2] == "LALS")
    		return "LALS";
    	if (matrix[2][0] == "LALS" && matrix[1][1] == "LALS" && matrix[0][2] == "LALS")
    		return "LALS";
     
     
    	if (matrix[0][0] == "THEO" && matrix[0][1] == "THEO" && matrix[0][2] == "THEO")
    		return "THEO";
    	if (matrix[1][0] == "THEO" && matrix[1][1] == "THEO" && matrix[1][2] == "THEO")
    		return "THEO";
    	if (matrix[2][0] == "THEO" && matrix[2][1] == "THEO" && matrix[2][2] == "THEO")
    		return "THEO";
     
    	if (matrix[0][0] == "THEO" && matrix[1][0] == "THEO" && matrix[2][0] == "THEO")
    		return "THEO";
    	if (matrix[0][1] == "THEO" && matrix[1][1] == "THEO" && matrix[2][1] == "THEO")
    		return "THEO";
    	if (matrix[0][2] == "THEO" && matrix[1][2] == "THEO" && matrix[2][2] == "THEO")
    		return "THEO";
     
    	if (matrix[0][0] == "THEO" && matrix[1][1] == "THEO" && matrix[2][2] == "THEO")
    		return "THEO";
    	if (matrix[2][0] == "THEO" && matrix[1][1] == "THEO" && matrix[0][2] == "THEO")
    		return "THEO";
     
    	return '/';                                                                  //fun
    }
     
    int main()                                                                                     
    {
     
    	count=0;
    	cout << "*****************************************************************************\n";	
    	cout << "\n Tic Tac Game  \n\n";
    	cout << "*****************************************************************************\n\n";
    	Draw();                                                                                 
    	while (1)
    	{
    		count++;                                                                       
     		Input();
    		Draw();
    		if (Winner() == "LALS")                                                      
    		{
    			cout << "_____________________________RESULT_________________________________________\n\n";
    			cout << " Player Lauralee wins! :-)\n\n";
    			break;
    		}
    		else if (Winner() == "THEO")                                                       
    		{
    			cout << "_____________________________RESULT___________________________________________\n\n";
    			cout << " Player Théo wins! :-) \n\n";
    			break;
    		}
    		else if (Winner() == '/'&& count==9)                                           
    		{
    			cout << "_____________________________RESULT___________________________________________\n\n";
    			cout << "Draw please try again :-( \n\n";
    			break;
    		}
     
     
    		cuser();                                                                      
    	}
     
    	return 0;
     
    }

  11. #11
    Expert confirmé

    Homme Profil pro
    Directeur de projet
    Inscrit en
    Mai 2013
    Messages
    1 329
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Directeur de projet
    Secteur : Service public

    Informations forums :
    Inscription : Mai 2013
    Messages : 1 329
    Points : 4 146
    Points
    4 146
    Par défaut
    Bonjour,

    Citation Envoyé par chris7522 Voir le message
    Je ne comprends pas la signification de : & 1
    "&" est l'opérateur "and" binaire (c'est à dire qu'il agit sur les bits et non sur le statut vrai/faux d'une expression). "& 1" est donc équivalent à "and" 0x00000001 et mettra à 0 tous les bits sauf le premier c'est à dire le bit de parité.

    Il en résulte que c'est équivalent à écrire % 2 (modulo 2).

    Les modulos sont les restes de divisions (opérations lentes) et, même si la plupart des compilateurs vont détecter que c'est un modulo d'une puissance de 2, 2^n, et le remplacer par un "and"(2^n-1), je préfère éviter tout risque.

    Par ailleurs est utilisé qu'en c toute expression non nulle sera considérée comme vrai, cela évite d'écrire :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    rand() & 1 == 1 ? 'L' : 'T';
    Salutations
    Ever tried. Ever failed. No matter. Try Again. Fail again. Fail better. (Samuel Beckett)

  12. #12
    Nouveau membre du Club
    Homme Profil pro
    Deficient visuel
    Inscrit en
    Mai 2019
    Messages
    196
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 52
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Deficient visuel

    Informations forums :
    Inscription : Mai 2019
    Messages : 196
    Points : 37
    Points
    37
    Par défaut
    Merci a tous !
    C'est vraiment généreux de votre part d'avoir pris le temps de toutes ces explications .

  13. #13
    Nouveau membre du Club
    Homme Profil pro
    Deficient visuel
    Inscrit en
    Mai 2019
    Messages
    196
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 52
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Deficient visuel

    Informations forums :
    Inscription : Mai 2019
    Messages : 196
    Points : 37
    Points
    37
    Par défaut
    Par contre , je n'ai pas resolu mon petit probleme . A la compilation , j'ai ceci :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    chris@chris:~/Bureau/Essai$ g++ -o essai16C -std=c++1z essai16.cpp
    essai16.cpp: In function ‘void Input()’:
    essai16.cpp:47:19: error: cannot convert ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘char’ in assignment
        matrix[0][0] = player;
                       ^~~~~~
    essai16.cpp:56:19: error: cannot convert ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘char’ in assignment
        matrix[0][1] = player;
                       ^~~~~~
    essai16.cpp:65:19: error: cannot convert ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘char’ in assignment
        matrix[0][2] = player;
                       ^~~~~~
    essai16.cpp:74:19: error: cannot convert ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘char’ in assignment
        matrix[1][0] = player;
                       ^~~~~~
    essai16.cpp:82:19: error: cannot convert ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘char’ in assignment
        matrix[1][1] = player;
                       ^~~~~~
    essai16.cpp:91:19: error: cannot convert ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘char’ in assignment
        matrix[1][2] = player;
                       ^~~~~~
    essai16.cpp:100:19: error: cannot convert ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘char’ in assignment
        matrix[2][0] = player;
                       ^~~~~~
    essai16.cpp:109:19: error: cannot convert ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘char’ in assignment
        matrix[2][1] = player;
                       ^~~~~~
    essai16.cpp:118:19: error: cannot convert ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘char’ in assignment
        matrix[2][2] = player;
                       ^~~~~~
    essai16.cpp: In function ‘char Winner()’:
    essai16.cpp:145:22: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
      if (matrix[0][0] == "LALS" && matrix[0][1] == "LALS" && matrix[0][2] == "LALS")
                          ^~~~~~
    essai16.cpp:145:48: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
      if (matrix[0][0] == "LALS" && matrix[0][1] == "LALS" && matrix[0][2] == "LALS")
                                                    ^~~~~~
    essai16.cpp:145:74: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
     x[0][0] == "LALS" && matrix[0][1] == "LALS" && matrix[0][2] == "LALS")
                                                                    ^~~~~~
    essai16.cpp:146:10: error: invalid conversion from ‘const char*’ to ‘char[-fpermissive]
       return "LALS";
              ^~~~~~
    essai16.cpp:147:22: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
      if (matrix[1][0] == "LALS" && matrix[1][1] == "LALS" && matrix[1][2] == "LALS")
                          ^~~~~~
    essai16.cpp:147:48: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
      if (matrix[1][0] == "LALS" && matrix[1][1] == "LALS" && matrix[1][2] == "LALS")
                                                    ^~~~~~
    essai16.cpp:147:74: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
     x[1][0] == "LALS" && matrix[1][1] == "LALS" && matrix[1][2] == "LALS")
                                                                    ^~~~~~
    essai16.cpp:148:10: error: invalid conversion from ‘const char*’ to ‘char[-fpermissive]
       return "LALS";
              ^~~~~~
    essai16.cpp:149:22: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
      if (matrix[2][0] == "LALS" && matrix[2][1] == "LALS" && matrix[2][2] == "LALS")
                          ^~~~~~
    essai16.cpp:149:48: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
      if (matrix[2][0] == "LALS" && matrix[2][1] == "LALS" && matrix[2][2] == "LALS")
                                                    ^~~~~~
    essai16.cpp:149:74: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
     x[2][0] == "LALS" && matrix[2][1] == "LALS" && matrix[2][2] == "LALS")
                                                                    ^~~~~~
    essai16.cpp:150:10: error: invalid conversion from ‘const char*’ to ‘char[-fpermissive]
       return "LALS";
              ^~~~~~
    essai16.cpp:152:22: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
      if (matrix[0][0] == "LALS" && matrix[1][0] == "LALS" && matrix[2][0] == "LALS")
                          ^~~~~~
    essai16.cpp:152:48: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
      if (matrix[0][0] == "LALS" && matrix[1][0] == "LALS" && matrix[2][0] == "LALS")
                                                    ^~~~~~
    essai16.cpp:152:74: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
     x[0][0] == "LALS" && matrix[1][0] == "LALS" && matrix[2][0] == "LALS")
                                                                    ^~~~~~
    essai16.cpp:153:10: error: invalid conversion from ‘const char*’ to ‘char[-fpermissive]
       return "LALS";
              ^~~~~~
    essai16.cpp:154:22: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
      if (matrix[0][1] == "LALS" && matrix[1][1] == "LALS" && matrix[2][1] == "LALS")
                          ^~~~~~
    essai16.cpp:154:48: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
      if (matrix[0][1] == "LALS" && matrix[1][1] == "LALS" && matrix[2][1] == "LALS")
                                                    ^~~~~~
    essai16.cpp:154:74: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
     x[0][1] == "LALS" && matrix[1][1] == "LALS" && matrix[2][1] == "LALS")
                                                                    ^~~~~~
    essai16.cpp:155:10: error: invalid conversion from ‘const char*’ to ‘char[-fpermissive]
       return "LALS";
              ^~~~~~
    essai16.cpp:156:22: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
      if (matrix[0][2] == "LALS" && matrix[1][2] == "LALS" && matrix[2][2] == "LALS")
                          ^~~~~~
    essai16.cpp:156:48: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
      if (matrix[0][2] == "LALS" && matrix[1][2] == "LALS" && matrix[2][2] == "LALS")
                                                    ^~~~~~
    essai16.cpp:156:74: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
     x[0][2] == "LALS" && matrix[1][2] == "LALS" && matrix[2][2] == "LALS")
                                                                    ^~~~~~
    essai16.cpp:157:10: error: invalid conversion from ‘const char*’ to ‘char[-fpermissive]
       return "LALS";
              ^~~~~~
    essai16.cpp:159:22: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
      if (matrix[0][0] == "LALS" && matrix[1][1] == "LALS" && matrix[2][2] == "LALS")
                          ^~~~~~
    essai16.cpp:159:48: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
      if (matrix[0][0] == "LALS" && matrix[1][1] == "LALS" && matrix[2][2] == "LALS")
                                                    ^~~~~~
    essai16.cpp:159:74: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
     x[0][0] == "LALS" && matrix[1][1] == "LALS" && matrix[2][2] == "LALS")
                                                                    ^~~~~~
    essai16.cpp:160:10: error: invalid conversion from ‘const char*’ to ‘char[-fpermissive]
       return "LALS";
              ^~~~~~
    essai16.cpp:161:22: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
      if (matrix[2][0] == "LALS" && matrix[1][1] == "LALS" && matrix[0][2] == "LALS")
                          ^~~~~~
    essai16.cpp:161:48: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
      if (matrix[2][0] == "LALS" && matrix[1][1] == "LALS" && matrix[0][2] == "LALS")
                                                    ^~~~~~
    essai16.cpp:161:74: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
     x[2][0] == "LALS" && matrix[1][1] == "LALS" && matrix[0][2] == "LALS")
                                                                    ^~~~~~
    essai16.cpp:162:10: error: invalid conversion from ‘const char*’ to ‘char[-fpermissive]
       return "LALS";
              ^~~~~~
    essai16.cpp:165:22: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
      if (matrix[0][0] == "THEO" && matrix[0][1] == "THEO" && matrix[0][2] == "THEO")
                          ^~~~~~
    essai16.cpp:165:48: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
      if (matrix[0][0] == "THEO" && matrix[0][1] == "THEO" && matrix[0][2] == "THEO")
                                                    ^~~~~~
    essai16.cpp:165:74: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
     x[0][0] == "THEO" && matrix[0][1] == "THEO" && matrix[0][2] == "THEO")
                                                                    ^~~~~~
    essai16.cpp:166:10: error: invalid conversion from ‘const char*’ to ‘char[-fpermissive]
       return "THEO";
              ^~~~~~
    essai16.cpp:167:22: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
      if (matrix[1][0] == "THEO" && matrix[1][1] == "THEO" && matrix[1][2] == "THEO")
                          ^~~~~~
    essai16.cpp:167:48: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
      if (matrix[1][0] == "THEO" && matrix[1][1] == "THEO" && matrix[1][2] == "THEO")
                                                    ^~~~~~
    essai16.cpp:167:74: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
     x[1][0] == "THEO" && matrix[1][1] == "THEO" && matrix[1][2] == "THEO")
                                                                    ^~~~~~
    essai16.cpp:168:10: error: invalid conversion from ‘const char*’ to ‘char[-fpermissive]
       return "THEO";
              ^~~~~~
    essai16.cpp:169:22: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
      if (matrix[2][0] == "THEO" && matrix[2][1] == "THEO" && matrix[2][2] == "THEO")
                          ^~~~~~
    essai16.cpp:169:48: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
      if (matrix[2][0] == "THEO" && matrix[2][1] == "THEO" && matrix[2][2] == "THEO")
                                                    ^~~~~~
    essai16.cpp:169:74: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
     x[2][0] == "THEO" && matrix[2][1] == "THEO" && matrix[2][2] == "THEO")
                                                                    ^~~~~~
    essai16.cpp:170:10: error: invalid conversion from ‘const char*’ to ‘char[-fpermissive]
       return "THEO";
              ^~~~~~
    essai16.cpp:172:22: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
      if (matrix[0][0] == "THEO" && matrix[1][0] == "THEO" && matrix[2][0] == "THEO")
                          ^~~~~~
    essai16.cpp:172:48: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
      if (matrix[0][0] == "THEO" && matrix[1][0] == "THEO" && matrix[2][0] == "THEO")
                                                    ^~~~~~
    essai16.cpp:172:74: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
     x[0][0] == "THEO" && matrix[1][0] == "THEO" && matrix[2][0] == "THEO")
                                                                    ^~~~~~
    essai16.cpp:173:10: error: invalid conversion from ‘const char*’ to ‘char[-fpermissive]
       return "THEO";
              ^~~~~~
    essai16.cpp:174:22: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
      if (matrix[0][1] == "THEO" && matrix[1][1] == "THEO" && matrix[2][1] == "THEO")
                          ^~~~~~
    essai16.cpp:174:48: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
      if (matrix[0][1] == "THEO" && matrix[1][1] == "THEO" && matrix[2][1] == "THEO")
                                                    ^~~~~~
    essai16.cpp:174:74: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
     x[0][1] == "THEO" && matrix[1][1] == "THEO" && matrix[2][1] == "THEO")
                                                                    ^~~~~~
    essai16.cpp:175:10: error: invalid conversion from ‘const char*’ to ‘char[-fpermissive]
       return "THEO";
              ^~~~~~
    essai16.cpp:176:22: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
      if (matrix[0][2] == "THEO" && matrix[1][2] == "THEO" && matrix[2][2] == "THEO")
                          ^~~~~~
    essai16.cpp:176:48: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
      if (matrix[0][2] == "THEO" && matrix[1][2] == "THEO" && matrix[2][2] == "THEO")
                                                    ^~~~~~
    essai16.cpp:176:74: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
     x[0][2] == "THEO" && matrix[1][2] == "THEO" && matrix[2][2] == "THEO")
                                                                    ^~~~~~
    essai16.cpp:177:10: error: invalid conversion from ‘const char*’ to ‘char[-fpermissive]
       return "THEO";
              ^~~~~~
    essai16.cpp:179:22: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
      if (matrix[0][0] == "THEO" && matrix[1][1] == "THEO" && matrix[2][2] == "THEO")
                          ^~~~~~
    essai16.cpp:179:48: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
      if (matrix[0][0] == "THEO" && matrix[1][1] == "THEO" && matrix[2][2] == "THEO")
                                                    ^~~~~~
    essai16.cpp:179:74: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
     x[0][0] == "THEO" && matrix[1][1] == "THEO" && matrix[2][2] == "THEO")
                                                                    ^~~~~~
    essai16.cpp:180:10: error: invalid conversion from ‘const char*’ to ‘char[-fpermissive]
       return "THEO";
              ^~~~~~
    essai16.cpp:181:22: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
      if (matrix[2][0] == "THEO" && matrix[1][1] == "THEO" && matrix[0][2] == "THEO")
                          ^~~~~~
    essai16.cpp:181:48: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
      if (matrix[2][0] == "THEO" && matrix[1][1] == "THEO" && matrix[0][2] == "THEO")
                                                    ^~~~~~
    essai16.cpp:181:74: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
     x[2][0] == "THEO" && matrix[1][1] == "THEO" && matrix[0][2] == "THEO")
                                                                    ^~~~~~
    essai16.cpp:182:10: error: invalid conversion from ‘const char*’ to ‘char[-fpermissive]
       return "THEO";
              ^~~~~~
    essai16.cpp: In function ‘int main()’:
    essai16.cpp:200:19: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
       if (Winner() == "LALS")
                       ^~~~~~
    essai16.cpp:206:24: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
       else if (Winner() == "THEO")
                            ^~~~~~
    chris@chris:~/Bureau/Essai$

  14. #14
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2017
    Messages
    34
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Israël

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Enseignement

    Informations forums :
    Inscription : Octobre 2017
    Messages : 34
    Points : 14
    Points
    14
    Par défaut
    Bonjour,
    A vue de nez ton tableau "matrix" est un tableau de char
    "LALS" et "THEO" sont des strings
    impossible de les comparer (par ex. matrix[0][1] ne peut pas être égal à LALS)
    Il faudrait soit retourner à la version "L" et "T"
    soit transformer matrix en tableau de strings
    Bonne journée.

  15. #15
    Expert confirmé

    Homme Profil pro
    Directeur de projet
    Inscrit en
    Mai 2013
    Messages
    1 329
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Directeur de projet
    Secteur : Service public

    Informations forums :
    Inscription : Mai 2013
    Messages : 1 329
    Points : 4 146
    Points
    4 146
    Par défaut Sale type
    Bonjour,

    Player est une string et les élements du tableau sont des caractères. Une affectation est donc impossible. On pourrait le modifier ainsi :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
     
    string matrix[3][3] = { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
    string player;
    int count;
     
    string gen_random(){
       const string choix[2] = {
          "LALS", "THEO"
       };
       return  choix[rand() % 2];
    }
     
    void Draw()                                                                        
    {
       cout << "      |       |     " << endl;
       cout << "  " << matrix[0][0]  << "   |   " << matrix[0][1] << "   |  " << matrix[0][2]  << endl;
     
       cout << "_____ | _____ | _____" << endl;
       cout << "      |       |     " << endl;
     
       cout << "  " << matrix[1][0]  << "   |  " << matrix[1][1]  << "    |  " <<matrix[1][2]  << endl;
     
       cout << "_____ | _____ | _____" << endl;
       cout << "      |       |     " << endl;
     
       cout << "  " << matrix[2][0] << "   |  " << matrix[2][1] << "    |  " << matrix[2][2] << endl;
     
       cout << "      |       |     " << endl << endl;
    }
     
    void Input()                                                                                   
     {
       player = gen_random();
       cout << "Its "<< player << " turn's Enter the number of the field: ";  
       int a, x, y;
       cin >> a;
       y = (a - 1) / 3;              // 1 2 3 4 5 6 7 8 9 -> 0 0 0 1 1 1 2 2 2
       x = a - 1 - 3 * y ;           // 1 2 3 4 5 6 7 8 9 -> 0 1 2 0 1 2 0 1 2
       if(matrix[y][x] == '0' + a)   // Peut être dire au compilateur d'accepter le faible typage du C
          matrix[y][x] = player;  
       else {
          cout << "\nInvalid move :-( \n";                           
          --count;                                     
       }
    }
     
    void cuser()                                                                                                              
    {
       if (player == "LALS") player = "THEO"; else player = "LALS";
    }
     
    string Winner()                                                                  
    {
       int i;
       for(i = 0; i < 3; ++i) {
          if (matrix[i][0] == matrix[i][1] && matrix[i][0] == matrix[i][2]) return matrix[i][0];
          if (matrix[0][i] == matrix[1][i] && matrix[0][i] == matrix[2][i]) return matrix[0][i];
       }
       for(i = 0; i < 3; i += 2)
          if (matrix[1][1] == matrix[0][i] && matrix[1][1] == matrix[2-i][2]) return matrix[1][1];
    return "/";                                                                  //fun
    }
     
    int main()                                                                                     
    {
       string whowin;
       count = 0;
       cout << "*****************************************************************************\n";	
       cout << "\n Tic Tac Game  \n\n";
       cout << "*****************************************************************************\n\n";
       Draw();                                                                                 
       while (1) 
       {
          count++;                                                                       
          Input();
          Draw();
          whowin = Winner(); 
          if(count == 9 || whowin != "/")
             cout << "_____________________________RESULT_________________________________________\n\n";
          if (whowin == "LALS")                                                      
             cout << " Player Lauralee wins! :-)\n\n";
          else if (whowin == "THEO")                                                       
             cout << " Player Théo wins! :-) \n\n";
          else if (count == 9)                                           
             cout << "Draw please try again :-( \n\n";   // Draw ???
          cuser();                                                                      
       }
       return 0;
    }
    On voit qu'il est facile de diminuer la taille tout en augmentant l'efficacité, mais est-ce que ce programme est devenu correct ? Je crains que non :
    • Le fait de mettre une chaine de caractères dans le tableau affiché casse complètement sa présentation qu'il faut revoir.
    • Le principe même de travailler avec des chaines n'est pas très bon. Même si ici cela n'a pas d'importance, c'est sur-consommateur de temps et de ressources. Un tableau d'entiers (par exemple : -1 case vide, 0 joueur 1 et 1 joueur 2) serait bien plus efficace
    • Un programme dont on ne prévoit pas la fin, (while(1), nécessitera d'être tué au niveau système.

    Ce code modifié est un brouillon rapide qui n'a pas été testé mais il devrait tomber en marche.

    Salutations
    Ever tried. Ever failed. No matter. Try Again. Fail again. Fail better. (Samuel Beckett)

  16. #16
    Nouveau membre du Club
    Homme Profil pro
    Deficient visuel
    Inscrit en
    Mai 2019
    Messages
    196
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 52
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Deficient visuel

    Informations forums :
    Inscription : Mai 2019
    Messages : 196
    Points : 37
    Points
    37
    Par défaut
    De nouveau merci pour les éclaircissements . Je pense qu'il est préférable que je revienne a la version "L" , "T" , meme si je regrette de ne pas avoir trouvé une solution .
    Bonne soirée a tous .

  17. #17
    Nouveau membre du Club
    Homme Profil pro
    Deficient visuel
    Inscrit en
    Mai 2019
    Messages
    196
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 52
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Deficient visuel

    Informations forums :
    Inscription : Mai 2019
    Messages : 196
    Points : 37
    Points
    37
    Par défaut
    J'ai quand meme voulu voir comment ca se passerai en transformant matrix en tableau de string et ca fonctionne tres bien ! Le probleme est que , lorsque par exemple le 1 est remplacé par "LALS" ou "THEO" toute mes grilles sont décalé . Dommage dans la mesure ou ca fonctionnait ...

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
     
     
    string matrix[3][3] = { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
    string player;
    int count;
     
    string gen_random(){
       string choix[2] = {
          "LALS", "THEO"
       };
       return  choix[rand() % 2];
    }
     
    void Draw()                                                                        
    {
    	cout << "       |            |     " << endl;
    	cout << matrix[0][0]  << "   |   " << matrix[0][1] << "       |  " << matrix[0][2]  << endl;
     
    	cout << "_____ | _____ | _____" << endl;
    	cout << "      |       |     " << endl;
     
    	cout << "  " << matrix[1][0]  << "   |  " << matrix[1][1]  << "    |  " <<matrix[1][2]  << endl;
     
    	cout << "_____ | _____ | _____" << endl;
    	cout << "      |       |     " << endl;
     
    	cout << "  " << matrix[2][0] << "   |  " << matrix[2][1] << "    |  " << matrix[2][2] << endl;
     
    	cout << "      |       |     " << endl << endl;
    }
     
    void Input()                                                                                   
     {
     
            player=gen_random();
     
    	cout << "Its "<<player<<" turn's Enter the number of the field: ";  
    	int a;
    	cin >> a; 								   
     
    	if (a == 1)								   	
    		if (matrix[0][0]=="1")                                             
    			matrix[0][0] = player;
    		else
    			{
    			cout << "\nInvalid move :-( \n";                           
    				count=count-1;                                     
     
    			}
    	else if (a == 2)
    		if (matrix[0][1]=="2")
    			matrix[0][1] = player;
    		else
    			{
    			cout << "\nInvalid move :-(  \n";
    				count=count-1;
     
    			}
    	else if (a == 3)
    		if (matrix[0][2]=="3")
    			matrix[0][2] = player;
    		else
    			{
    			cout << "\nInvalid move :-(  \n";
    				count=count-1;
    			}
     
    	else if (a == 4)
    	        if (matrix[1][0]=="4")
    			matrix[1][0] = player;
    		else
    			{
    			cout << "\nInvalid move :-(  \n";
    				count=count-1;
    			}
    	else if (a == 5)
    		  if (matrix[1][1]=="5")
    			matrix[1][1] = player;
    		else
    			{
    			cout << "\nInvalid move :-(  \n";
    				count=count-1;
    			}
     
    	else if (a == 6)
      		if (matrix[1][2]=="6")
    			matrix[1][2] = player;
    		else
    			{
    			cout << "\nInvalid move :-(  \n";
    				count=count-1;
    			}
     
    	else if (a == 7)
    		  if (matrix[2][0]=="7")
    			matrix[2][0] = player;
    		else
    			{
    			cout << "\nInvalid move :-(  \n";
    				count=count-1;
    			}
     
    	else if (a == 8)
    		  if (matrix[2][1]=="8")
    			matrix[2][1] = player;
    		else
    			{
    			cout << "\nInvalid move :-(  \n";
    				count=count-1;
    			}
     
    	else if (a == 9)
    		  if (matrix[2][2]=="9")
    			matrix[2][2] = player;
    		else
    			{
    			cout << "\nInvalid move :-( \n";
    				count=count-1;
    			}
    }
     
    void cuser()                                                                                                              
    {
    	if (player == "LALS")                                                       
    	{
     
    		player = "THEO";                                                   
     
    	}
     
    	else
    	{	
    		player = "LALS";
     
    	}
    }
     
    string Winner()                                                                  
    {
     
    	if (matrix[0][0] == "LALS" && matrix[0][1] == "LALS" && matrix[0][2] == "LALS")
    		return "LALS";
    	if (matrix[1][0] == "LALS" && matrix[1][1] == "LALS" && matrix[1][2] == "LALS")
    		return "LALS";
    	if (matrix[2][0] == "LALS" && matrix[2][1] == "LALS" && matrix[2][2] == "LALS")
    		return "LALS";
     
    	if (matrix[0][0] == "LALS" && matrix[1][0] == "LALS" && matrix[2][0] == "LALS")
    		return "LALS";
    	if (matrix[0][1] == "LALS" && matrix[1][1] == "LALS" && matrix[2][1] == "LALS")
    		return "LALS";
    	if (matrix[0][2] == "LALS" && matrix[1][2] == "LALS" && matrix[2][2] == "LALS")
    		return "LALS";
     
    	if (matrix[0][0] == "LALS" && matrix[1][1] == "LALS" && matrix[2][2] == "LALS")
    		return "LALS";
    	if (matrix[2][0] == "LALS" && matrix[1][1] == "LALS" && matrix[0][2] == "LALS")
    		return "LALS";
     
     
    	if (matrix[0][0] == "THEO" && matrix[0][1] == "THEO" && matrix[0][2] == "THEO")
    		return "THEO";
    	if (matrix[1][0] == "THEO" && matrix[1][1] == "THEO" && matrix[1][2] == "THEO")
    		return "THEO";
    	if (matrix[2][0] == "THEO" && matrix[2][1] == "THEO" && matrix[2][2] == "THEO")
    		return "THEO";
     
    	if (matrix[0][0] == "THEO" && matrix[1][0] == "THEO" && matrix[2][0] == "THEO")
    		return "THEO";
    	if (matrix[0][1] == "THEO" && matrix[1][1] == "THEO" && matrix[2][1] == "THEO")
    		return "THEO";
    	if (matrix[0][2] == "THEO" && matrix[1][2] == "THEO" && matrix[2][2] == "THEO")
    		return "THEO";
     
    	if (matrix[0][0] == "THEO" && matrix[1][1] == "THEO" && matrix[2][2] == "THEO")
    		return "THEO";
    	if (matrix[2][0] == "THEO" && matrix[1][1] == "THEO" && matrix[0][2] == "THEO")
    		return "THEO";
     
    	return "/";                                                                  //fun
    }
     
    int main()                                                                                     
    {
     
    	count=0;
    	cout << "*****************************************************************************\n";	
    	cout << "\n Tic Tac Game  \n\n";
    	cout << "*****************************************************************************\n\n";
    	Draw();                                                                                 
    	while (1)
    	{
    		count++;                                                                       
     		Input();
    		Draw();
    		if (Winner() == "LALS")                                                      
    		{
    			cout << "_____________________________RESULT_________________________________________\n\n";
    			cout << " Player Lauralee wins! :-)\n\n";
    			break;
    		}
    		else if (Winner() == "THEO")                                                       
    		{
    			cout << "_____________________________RESULT___________________________________________\n\n";
    			cout << " Player Théo wins! :-) \n\n";
    			break;
    		}
    		else if (Winner() == "/" && count==9)                                           
    		{
    			cout << "_____________________________RESULT___________________________________________\n\n";
    			cout << "Draw please try again :-( \n\n";
    			break;
    		}
     
     
    		cuser();                                                                      
    	}
     
    	return 0;
     
    }

  18. #18
    Rédacteur/Modérateur


    Homme Profil pro
    Network game programmer
    Inscrit en
    Juin 2010
    Messages
    7 115
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : Canada

    Informations professionnelles :
    Activité : Network game programmer

    Informations forums :
    Inscription : Juin 2010
    Messages : 7 115
    Points : 32 967
    Points
    32 967
    Billets dans le blog
    4
    Par défaut
    Ce code est très médiocre à tellement de niveaux.
    - Les variables globales n'ont aucune raison d'être
    - Ce truc gagnerait à être une classe qui représente le jeu
    - On n'utilise pas char ou std::string au petit bonheur
    - Pourquoi utiliser autant d'éléments du C dans un programme C++ ?
    - Les tableaux C devraient être remplacés par std::array
    - cstdlib et ctime sont des headers C
    - std possède des générateurs aléatoires, rand est du C
    - L'affichage n'a pas à contraindre la représentation des données
    - Y'a tellement de répétitions que clairement quelque chose cloche et un gros warning aurait dû apparaître à tes yeux
    - Le nom du jeu est tic-tac-toe, ou morpion en français

    J'ai écrit un tel truc il y a quelques mois qui pourra t'inspirer.
    Pensez à consulter la FAQ ou les cours et tutoriels de la section C++.
    Un peu de programmation réseau ?
    Aucune aide via MP ne sera dispensée. Merci d'utiliser les forums prévus à cet effet.

  19. #19
    Expert éminent sénior
    Avatar de Mat.M
    Profil pro
    Développeur informatique
    Inscrit en
    Novembre 2006
    Messages
    8 361
    Détails du profil
    Informations personnelles :
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Novembre 2006
    Messages : 8 361
    Points : 20 379
    Points
    20 379
    Par défaut
    bonsoir Bousk ne pas trop s'enflammer non plus car Mr chris7522 débute
    Comme débutant je n'aurais pas mieux fait je pense que lui.

  20. #20
    Nouveau membre du Club
    Homme Profil pro
    Deficient visuel
    Inscrit en
    Mai 2019
    Messages
    196
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 52
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Deficient visuel

    Informations forums :
    Inscription : Mai 2019
    Messages : 196
    Points : 37
    Points
    37
    Par défaut
    Ce n'est pas grave , je te remercie Bousk pour tes critiques certes un peu dur mais néanmoins constructives , ca me fait avancer .
    Ce code n'est pas le mien . Un de mes voisins développeur m'a conseillé d'essayer de comprendre ce que font les autres puis tenter de le modifier pour voir ce que ca donne meme si ca ne ressemble a rien , l'essentiel est de comprendre le fonctionnement du truc .
    Et puis pour compliquer un peu les choses , je suis déficient visuel , je ne peux pas consacrer beaucoup de temps a tout ca chaque jour , donc j'y vais a mon rythme

+ Répondre à la discussion
Cette discussion est résolue.
Page 1 sur 2 12 DernièreDernière

Discussions similaires

  1. Tic tac toe- déterminer une position gagnante
    Par shirya dans le forum Intelligence artificielle
    Réponses: 7
    Dernier message: 26/10/2010, 10h33
  2. algorithmes pour morpion/tic tac toe
    Par shirya dans le forum Algorithmes et structures de données
    Réponses: 4
    Dernier message: 17/01/2008, 02h35
  3. Tic Tac Toe : POO
    Par dword2add dans le forum Langage
    Réponses: 2
    Dernier message: 15/12/2007, 15h13
  4. Tic tac toe
    Par Invité(e) dans le forum Prolog
    Réponses: 9
    Dernier message: 17/05/2005, 23h08

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo