Hello Donquijota,
I have managed to get a little farther.
I moved most of the global variables to "main" with the exception of "cuadro" I put in the "iniciarPartida" function.
the prototypes now look like:
1 2 3 4
|
int RevisarGanador(std::string& cuadro);
void tablero(std::string& cuadro, std::string& jugador1, std::string& jugador2, int ronda);
int iniciarPartida(std::string& jugador1, std::string& jugador2, int ronda);
void crearArchivo(); // <--- Not worked with this yet.
|
For now in "main" in "case 1" I have used one function call to "iniciarPartida" then I notices that this function returns a value thet is never captured in "main" or used.
Starting with the function "iniciarPartida" I did this:
1 2 3 4
|
int iniciarPartida(std::string& jugador1, std::string& jugador2, int ronda)
{
const std::string constCuadro{ " 123456789" };
std::string cuadro = cCuadro;
|
The rest of the variables work well.
I made "cuadro" a std::string which will work just the same as the character array that you started with. The string makes it easier to work with as you will see.
in the while loop
while (cuenta <= 3
. be careful with using "<=". This will give you 4 rounds. I am guessing that you want 3 rounds which means you would have to use
while (cuenta < 3)
.
For the in/else if statements I did find that this works:
1 2 3 4
|
if (eleccion == 1 && cuadro[eleccion] == '1') // <--- eleccion / choice.
cuadro[eleccion] = simbolo;
else if (eleccion == 2 && cuadro[eleccion] == '2')
cuadro[eleccion] = simbolo;
|
I changed the else part this way:
1 2 3 4 5 6 7 8 9 10 11 12
|
else
{
// MANEJA EXCEPCION ANTE UN NUMERO NO POSIBLE O UN CUADRO OCUPADO
// EXCEPTION HANDLING BEFORE A NON-POSSIBLE NUMBER OR A BUSY TABLE
cout << "\n Movimiento invalido / Invalid movement ";
jugador--;
//cin.ignore(); // <--- Ignores one character or the new line whichever comes first.
// <--- Ignora un carácter o la nueva línea, lo que ocurra primero.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
cin.get();
}
|
on Line 5 I have been indenting an error message with at least 4 spaces to set it off from the rest of the output.
Line 9 is a better way of using the "cin.ignore". It is more portable and can be used by more systems. Meaning operating systems and compilers.
Line 10 is so you are not looking at a flashing cursor and wondering what to do. The whole of lines 9 - 11 tend to do the best job. and sometimes line 9 is not needed. It depends on the preceding code.
The line
i = RevisarGanador(cuadro);
passes the the string to the function to use.
Following the do/while loop I am not sure what you wanted to de with this:
1 2
|
//INVOCA A TABLERO CUANDO DEVUELVE -1 / NEVER BOARD WHEN RETURNING -1
tablero(cuadro, jugador1, jugador2, ronda);
|
Because it always calls the function. It does appear to work properly.
This is just an adjustment to the "cout" statements:
1 2 3 4 5 6 7
|
// IMPRIME GANADOR SI DEVUELVE 1
if (i == 1)
cout << " ==>\aJugador " << (--jugador?'X':'O') << " Gano / Won ";
else
// IMPRIME EMPATE EN CASO DE QUE NO SE CUMPLAN LOS REQUISITOS PARAA GANAR
// PRINT TIE IN THE EVENT THAT THE REQUIREMENTS ARE NOT FULFILLED TO WIN
cout << " ==>\aEmpate / Tie";
|
Then I changed your "cin.ignore()" to :
1 2 3
|
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
cin.get();
|
Just before the close of the while loop I have this:
1 2 3 4 5 6
|
cuadro = cCuadro; // <--- Resets the string. / Restablece la cadena.
n++;
ronda++; // <--- Added. / Adicional
//cout << cuenta; // <--- Not really needed. / Realmente no es necesario
++cuenta;
|
Defining "cuadro" as a "std::string" makes this much easier. The simple assignment on line 1 resets the board. Which is what you need in the first place.
Something you might consider is adding to this function a way of keeping track of how many rounds each player wins. Just thinking yo would define that variables in "main" and pass them to the function by reference then at the end of the program use that information to print out the stats of who won haw many rounds.
The only part I have not worked on is the "crearArchivo" function. I am not sure what you want to do with this or when. I will start working on this.
Hope that helps,
Andy