1

I was wondering if someone could help me understand some code. Its a tic tac toe program..

There is a section of the code I don't get, but seems to allow the system to distinguish between two choices. im used to saying something like this.

cout<<"enter choice.1 or 2";
cin>>choice;

but in this code they have

player=(player%2)?1:2;

and this

mark=(player == 1) ? 'X' : 'O';

Complete code

char square[10] = {'o','1','2','3','4','5','6','7','8','9'};
int checkwin();
void board();

int main()
{
    int player = 1,i,choice;
    char mark;
    clrscr();
    do
    {
        board();
        player=(player%2)?1:2; //<<--- This part I don't understand
        cout << "Player " << player << ", enter a number:  ";
        cin >> choice;
        mark=(player == 1) ? 'X' : 'O'; //<<--- This part I don't understand
        if (choice == 1 && square[1] == '1')
            square[1] = mark;
        else if (choice == 2 && square[2] == '2')
            square[2] = mark;
        else if (choice == 3 && square[3] == '3')
            square[3] = mark;
        else if (choice == 4 && square[4] == '4')
            square[4] = mark;
        else if (choice == 5 && square[5] == '5')
            square[5] = mark;
        else if (choice == 6 && square[6] == '6')
            square[6] = mark;
        else if (choice == 7 && square[7] == '7')
            square[7] = mark;
        else if (choice == 8 && square[8] == '8')
            square[8] = mark;
        else if (choice == 9 && square[9] == '9')
            square[9] = mark;
        else
        {
            cout<<"Invalid move ";
            player--;
            getch();
        }
        i=checkwin();
        player++;
    }while(i==-1);
    board();
    if(i==1)
        cout<<"==>\aPlayer "<<--player<<" win ";
    else
        cout<<"==>\aGame draw";
    getch();
    return 0;
OMGtechy
  • 7,935
  • 8
  • 48
  • 83
Darius
  • 85
  • 2
  • 4
  • 12
  • 1
    Two? Where? I count no less than nine choices. (Actually, I'd prefer choice-less solution: `if (square[choice] == '0'+choice`.) – Jongware Apr 11 '14 at 20:43
  • You can simplify your code with a switch statement. – Brandon Apr 11 '14 at 20:49
  • Jongware..sorry I was unclear.... i really meant I didnt understand... player=(player%2)?1:2;//**<<--- This part I don't understand** this part of the code...and ............mark=(player == 1) ? 'X' : 'O';** – Darius Apr 11 '14 at 20:57

4 Answers4

3

This is the conditional operator.

mark = (player == 1) ? 'X' : 'O';

is equivalent to

if(player == 1)
{
   mark = 'X';
}
else
{
   mark = 'O';
}
OMGtechy
  • 7,935
  • 8
  • 48
  • 83
  • @Jongware what do you mean? – OMGtechy Apr 11 '14 at 20:45
  • Guessing what line the OP *most probably* meant, out of the 40-or so lines he posted :) (For the record, you are probably right.) – Jongware Apr 11 '14 at 20:46
  • Also referred to as the ternary operator. Technically there are more but this is the one everyone knows. – Sean Perry Apr 11 '14 at 20:50
  • @SeanPerry I call it that too, just chose this name as it better conveys its purpose. I didn't know there was another (will google it now, thanks)! – OMGtechy Apr 11 '14 at 20:52
  • 1
    Thank you @OMGtechy....I love this site compared to other blog sites..for C++... you guys answer quickly..and don't act as if you don't have time for us little guys..who don't grasp coding quickly.. – Darius Apr 11 '14 at 20:53
  • @Darius no problem, you can't be expected to know everything instantly. If you run into any other strange looking operators, take a look at [this webpage](http://www.cplusplus.com/doc/tutorial/operators/). – OMGtechy Apr 11 '14 at 20:56
  • @SeanPerry so conditional operator is also known as Ternary Operator..I will look this up for more example to understand it and start using it in my code....I got a lot to learn....wow.. – Darius Apr 11 '14 at 20:59
  • 1
    @Darius it's known as that simply because it takes 3 operands and is the only one (that I knew of, see Sean's comment) that does; binary operators takes 2, and unary operators take 1. – OMGtechy Apr 11 '14 at 21:01
1

The assignment:

var  =  cond ? x : y;

is the same as:

if (cond) {
    var = x;
} else {
    var = y;
}
DJG
  • 6,413
  • 4
  • 30
  • 51
0

This is the Conditional Operator: http://www.eskimo.com/~scs/cclass/int/sx4eb.html

yussuf
  • 635
  • 1
  • 4
  • 18
0
player=(player%2)?1:2; //<<--- This part I don't understand

% is the modulus operator which effectively takes the remainder after devision: (How does the modulus operator work?)

As you take (player%2) the result for different player values would be:

player(1) -> 1
player(2) -> 0
player(3) -> 1
player(4) -> 0
...

Then they take this result and turn it into a real player number:

player = result ? 1 : 2;

Result will 1 non-zero (true) if player is 1,3,5... in which case they redefine player as 1. If the player is 2,4,6,... then they redefine player as 2.

Its a simple way of enforcing that the player is always 1 or 2 and never anything else.

Community
  • 1
  • 1
Chris
  • 2,655
  • 2
  • 18
  • 22