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 j'ai ce message :
chris@chris:~/Bureau/Essai$ g++ -o essai16C -std=c++1z essai16.cpp
essai16.cpp: In function ‘void Input()’:
essai16.cpp:42:16: error: invalid conversion from ‘char (*)()’ to ‘char’ [-fpermissive]
player=gen_random;
^~~~~~~~~~
chris@chris:~/Bureau/Essai$
J'ai les yeux hs pour aujourd'hui et j'ai besoin de votre aide . Ca doit etre simple pour vous , mais je vois pas . Il y avait certainement une autre facon plus simple de le faire .
Merci
#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;
}