I went to the beach at the weekend and played on one inpeticular game, the one were you pull the lever and XOO or w.e comes out, i was going to make one but thought i would ask if this was the best solution..

#include <conio.h>
#include <iostream>

using namespace std;

int main()
{
    unsigned int firstColum, secondColum, thirdColum;
    
    /////////////////////////////////////////////////////////
    
    firstColum = rand() % 2 + 1;
    
    srand ( time(NULL) );
    
    /////////////////////////////////////////////////////////
    
    secondColum = rand() % 2 + 1;
    
    srand ( time(NULL) );
    
    /////////////////////////////////////////////////////////
    
    thirdColum = rand() % 2 + 1;
    
    srand ( time(NULL) );
    
    /////////////////////////////////////////////////////////
    
    if( firstColum == 1)
    cout << "X";
         else
    cout << "O";
    
    if( secondColum == 1)
    cout << "X";
         else
    cout << "O";
    
    if( thirdColum == 1)
    cout << "X";
         else
    cout << "O";
    
    getch();
}

EDIT: I've noticed i usually get same output

>>Is this best solution?
No. srand() should only be called once during the lifetime of the program. Leave lines 12 and 14 but delete lines 18, 20, 24 and 26.

>>#include <conio.h>
That is non-standard function. If you want best solution than delete it.

line 45: delete it. use cin.get() instead.

So are you saying i should only call srand once and use one variable for random number or..?

EDIT: I think i cracked that bit!

#include <iostream>

using namespace std;

int main()
{
    unsigned int firstColum, secondColum, thirdColum;
    
    /////////////////////////////////////////////////////////
    
    srand ( time(NULL) );
    
    firstColum = rand() % 2 + 1;
    
    /////////////////////////////////////////////////////////
    
    if( firstColum == 1)
    cout << "X";
         else
    cout << "O";
    
    firstColum = rand() % 2 + 1;
    
    if( firstColum == 1)
    cout << "X";
         else
    cout << "O";
    
    firstColum = rand() % 2 + 1;
    
    if( firstColum == 1)
    cout << "X";
         else
    cout << "O";
    
    cin.get();
}

Now, since the code in blocks 17-20 are repeated three times you can use a loop

for(int i = 0; i < 3; i++)
{

firstColum = rand() % 2 + 1;
    
    if( firstColum == 1)
    cout << "X";
         else
    cout << "O";

}

Sorry for this late reply but i've changed to firstColum, secondColum and thirdColum again, i'll post code when home and i've added options like if output = XXX you win $1, i'm planning to make it $0.10 a go and the total changes etc etc :)

This article has been dead for over six months. Start a new discussion instead.