Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 11th, 2008, 12:58 AM
Registered User
 
Join Date: May 2008
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Tic Tac Toe Help!

Alright, I have made a Tic Tac Toe Game. The thing is, the computer doesn't corresponds well, with random number. And whenever someone wins a game, the program exits itself, without saying the cout's. Please
help me. T-T

# include <iostream>
# include <ctime>
# include <cstdlib>
using namespace std;

char TABLERO [3][3];
char SIMBOLO;
char COMPUTADORA;
int Movida;
int ContadorTur = 1;
int Ganador;
void setMovida(int Movida, char TIPO);
void inicializarTablero();
void seTranco();
int quienGano();
void jugadaDelaComputadora();
int cojeRANDOMS();
void jugadaDelUsuario();
void printTablero();
bool ESPACIO = true;
bool ESPACIOLIBRE = true;
bool espacioDeVerdadLibre(int EspacioNum);



int main(){


    cout << " --------------------------------------" << endl;
    cout << " *---Bienvenido al Mega Tic Tac Toe---*" << endl;
    cout << " --------------------------------------" << endl;

    int ORDEN;

    inicializarTablero();
    cout << "Favor de entrar el simbolo que desea ser, X o O? "<<endl;
    cin >> SIMBOLO;

    if (SIMBOLO == 'x' || SIMBOLO == 'X')
    {
        SIMBOLO = 'X';
        COMPUTADORA ='O';

    }
    else if (SIMBOLO == 'o' || SIMBOLO == 'O')
    {
        SIMBOLO = 'O';
        COMPUTADORA = 'X';
    }
     else
    {
        cout << "Bien" << endl;
    }
    cout << "\nPresiona 1 para ser numero 1 sino presiona 2: ";
    cin >> ORDEN;
    while (ORDEN !=1 && ORDEN !=2)
    {
        cout << "\n\nEso no es na, trata otra vez";
        cout << "\nPresiona 1 para ser numero 1 sino presiona 2: ";
        cin >> ORDEN;
    }
        cout << "Usuario: " << SIMBOLO << "\tTurno: " << ORDEN << "\nCompu: " << COMPUTADORA << "\tTurno: " << (3 - ORDEN);
        printTablero();
        if (ORDEN == 1)
    {
        while (ContadorTur < 10)
        {
            if ((ContadorTur % 2) == 0)
            {
                jugadaDelaComputadora();
                quienGano();
            }
            else
            {
                jugadaDelUsuario();
                quienGano();
            }
        }
    }
    else if(ORDEN == 2)
    {
        while (ContadorTur < 10)
        {
            if ((ContadorTur % 2) == 0)
            {
                jugadaDelUsuario();
                quienGano();
            }
            else
            {
                jugadaDelaComputadora();
                quienGano();
            }
        }
        }

            if (Ganador == 1)
            {

        cout << "Usuario Gano el juego, WEPA!" << endl;
    }
    else if (Ganador == 2)
    {
        cout << "La computadora gano, perdededor!!" << endl;
    }
    else if (Ganador == 0)
    {
        cout << "Empate!!" << endl;
    }
    else
    {
        cout << " Error" << endl;
    }

    return 0;
}

void inicializarTablero()
{
    TABLERO[0][0] = '1';
    TABLERO[0][1] = '2';
    TABLERO[0][2] = '3';
    TABLERO[1][0] = '4';
    TABLERO[1][1] = '5';
    TABLERO[1][2] = '6';
    TABLERO[2][0] = '7';
    TABLERO[2][1] = '8';
    TABLERO[2][2] = '9';
}

void printTablero(){
    cout << "\n\n\t" << TABLERO[0][0] << "|" << TABLERO[0][1] << "|" << TABLERO[0][2] << endl;
    cout << "\t-+-+-" << endl;
    cout << "\t" << TABLERO[1][0] << "|" << TABLERO[1][1] << "|" << TABLERO[1][2] << endl;
    cout << "\t-+-+-" << endl;
    cout << "\t" << TABLERO[2][0] << "|" << TABLERO[2][1] << "|" << TABLERO[2][2] << endl << "\n";

}

void jugadaDelUsuario()
{
    ContadorTur++;
    cout << "Sigue la leyenda, y entra a donde te quieres mover: ";
    cin >> Movida;
    ESPACIO = espacioDeVerdadLibre(Movida);
    while (Movida != 1 && Movida != 2 && Movida != 3 && Movida != 4 && Movida != 5 && Movida != 6 && Movida != 7 && Movida != 8 && Movida != 9 && ESPACIOLIBRE == false)
    {
        cout << "\nNo se puede, intenta otra ves: ";
        cin >> Movida;

    }
    setMovida (Movida, SIMBOLO);
    Ganador = quienGano();

}
void setMovida (int Movida, char TIPO)
{
    switch (Movida)
    {
        case 1:
            TABLERO[0][0] = TIPO;
            break;
        case 2:
            TABLERO[0][1] = TIPO;
            break;
        case 3:
            TABLERO[0][2] = TIPO;
            break;
        case 4:
            TABLERO[1][0] = TIPO;
            break;
        case 5:
            TABLERO[1][1] = TIPO;
            break;
        case 6:
            TABLERO[1][2] = TIPO;
            break;
        case 7:
            TABLERO[2][0] = TIPO;
            break;
        case 8:
            TABLERO[2][1] = TIPO;
            break;
        case 9:
            TABLERO[2][2] = TIPO;
            break;

}
}
void jugadaDelaComputadora()
{
   int RANDOMSmovida = 1;

   ContadorTur++;

   while (ESPACIOLIBRE == false)
   {
        RANDOMSmovida = cojeRANDOMS();


        ESPACIOLIBRE = espacioDeVerdadLibre(RANDOMSmovida);
   }

   setMovida(RANDOMSmovida, COMPUTADORA);
   cout << "Movio la Computadora: ";
   printTablero();
}

int cojeRANDOMS()
{
srand((unsigned)time(0));
int RANDOMS;

RANDOMS = (rand()%9)+1;

while (1)
{
if (RANDOMS < 1)
{
    RANDOMS = (rand()%9)+1;
}
else if (RANDOMS > 9)
{
    RANDOMS = (rand()%9)+1;
}
else
{
    break;
}
}

return RANDOMS;
}
int quienGano ()
{
    if(TABLERO [0][0] == SIMBOLO && TABLERO[0][1] == SIMBOLO && TABLERO[0][2] == SIMBOLO)
    {
        ContadorTur = 10;
        return 1;
    }
    else if (TABLERO [1][0] == SIMBOLO && TABLERO[1][1] == SIMBOLO && TABLERO[1][2] == SIMBOLO)
    {
        ContadorTur = 10;
        return 1;
    }
    else if (TABLERO [2][0] == SIMBOLO && TABLERO[2][1] == SIMBOLO && TABLERO[2][2] == SIMBOLO)
    {
        ContadorTur = 10;
        return 1;
    }
    else if (TABLERO [0][0] == SIMBOLO && TABLERO[1][0] == SIMBOLO && TABLERO[2][0] == SIMBOLO)
    {
        ContadorTur = 10;
        return 1;
    }
    else if (TABLERO [0][1] == SIMBOLO && TABLERO[1][1] == SIMBOLO && TABLERO[2][1] == SIMBOLO)
    {
        ContadorTur = 10;
        return 1;
    }
    else if (TABLERO [0][2] == SIMBOLO && TABLERO[1][2] == SIMBOLO && TABLERO[2][2] == SIMBOLO)
    {
        ContadorTur = 10;
        return 1;
    }
    else if (TABLERO [0][0] == SIMBOLO && TABLERO[1][1] == SIMBOLO && TABLERO[2][2] == SIMBOLO)
    {
        ContadorTur = 10;
        return 1;
    }
    else if (TABLERO [2][0] == SIMBOLO && TABLERO[1][1] == SIMBOLO && TABLERO[0][2] == SIMBOLO)
    {
        ContadorTur = 10;
        return 1;
    }
    else if (TABLERO [0][0] == COMPUTADORA && TABLERO[0][1] == COMPUTADORA && TABLERO[0][2] == COMPUTADORA)
    {
        ContadorTur = 10;
        return 2;
    }
    else if (TABLERO [1][0] == COMPUTADORA && TABLERO[1][1] == COMPUTADORA && TABLERO[1][2] == COMPUTADORA)
    {
        ContadorTur = 10;
        return 2;
    }
    else if (TABLERO [2][0] == COMPUTADORA && TABLERO[2][1] == COMPUTADORA && TABLERO[2][2] == COMPUTADORA)
    {
        ContadorTur = 10;
        return 2;
    }
    else if (TABLERO [0][0] == COMPUTADORA && TABLERO[1][0] == COMPUTADORA && TABLERO[2][0] == COMPUTADORA)
    {
        ContadorTur = 10;
        return 2;
    }
    else if (TABLERO [0][1] == COMPUTADORA && TABLERO[1][1] == COMPUTADORA && TABLERO[2][1] == COMPUTADORA)
    {
        ContadorTur = 10;
        return 2;
    }
    else if (TABLERO [0][2] == COMPUTADORA && TABLERO[1][2] == COMPUTADORA && TABLERO[2][2] == COMPUTADORA)
    {
        ContadorTur = 10;
        return 2;
    }
    else if (TABLERO [0][0] == COMPUTADORA && TABLERO[1][1] == COMPUTADORA && TABLERO[2][2] == COMPUTADORA)
    {
        ContadorTur = 10;
        return 2;
    }
    else if (TABLERO [2][0] == COMPUTADORA && TABLERO[1][1] == COMPUTADORA && TABLERO[0][2] == COMPUTADORA)
    {
        ContadorTur = 10;
        return 2;
    }
    else
        return 0;
}
bool espacioDeVerdadLibre (int EspacioNum)
{
   switch(EspacioNum)
   {
    case 1:
        if (TABLERO[0][0] == '1')
        return true;
        else
        return false;
    case 2:
        if (TABLERO[0][1] == '2')
        return true;
        else
        return false;
    case 3:
        if (TABLERO[0][2] == '3')
        return true;
        else
        return false;
    case 4:
        if (TABLERO[1][0] == '4')
        return true;
        else
        return false;
    case 5:
        if (TABLERO[1][1] == '5')
        return true;
        else
        return false;
    case 6:
        if (TABLERO[1][2] == '6')
        return true;
        else
        return false;
    case 7:
        if (TABLERO[2][0] == '7')
        return true;
        else
        return false;
    case 8:
        if (TABLERO[2][1] == '8')
        return true;
        else
        return false;
    case 9:
        if (TABLERO[2][2] == '9')
        return true;
        else
        return false;
    default:
        return false;
    }
}


Reply With Quote
  #2 (permalink)  
Old July 9th, 2008, 05:29 PM
Friend of Wrox
 
Join Date: Mar 2008
Location: So Cal is good enough for you, California, USA.
Posts: 133
Thanks: 15
Thanked 1 Time in 1 Post
Send a message via ICQ to iceman90289 Send a message via AIM to iceman90289
Default

the problem is simple. look at the end of your main function. there is basically a cout statement nested in an if-then-else statement. you need to add this before the closing bracket in main:
while(true)
{

}

this keeps your program running. if you want, you can use system("PAUSE");
what its doing is ending because all instructions are already executed, when you call a function, program flow moves down that function, and at the end of the function it goes back to where it was called and the same applies to main, once the program flow reaches "return 0" in your main function, your program is considered over.

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off




All times are GMT -4. The time now is 05:40 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.