Okay I know this is a super newbie question so I will give you some background.
I am breaking down a tic tac toe source code to see how it works. For me that is one of the ways I learn. This piece of code is in my main and I have figured out most of it but for some reason I just can't figure that out. The main is below.
Now the thing is I was studying multi dimension arrays (very interested in that). So this code is way ahead of what I am studying so far. So, I may be way off because On line 2 I am thinking that it is a variable with 3 values??? Am I really wrong here.
:) It is declaring three ints with player getting initialized to 1.
This is exactly why it is bad practice to declare multiple variables on single line.
Outside a parameter list context, a comma-separated list of expressions will cause the compiler to fully evaluate all expressions within the list. In any context except for the aforementioned context, the right-most expression -- "choice" in your case -- will always be used; the other (s) will be ignored. In an assignment context, the rules still apply. For instance:
1 2 3 4 5
// Comma-separated list inside an assignment context:
int a(1, 2, 3); // a has the value of 3.
// Comma-separated list outside an assignment context:
1, 2, 3; // 3 will be the expression used by the compiler.
Comma-separated lists can be used to evaluate two or more similar expressions within one statement. The "for" loop supports this also. Note that the order of evaluation is undefined during a function invocation.
One of those slap yourself in the forehead moments.
Thank you all so much for the really quick response.
On to breaking down the code. I should understand it better now.
I guess I didn't recognize the syntax because I have not been declaring multiple variables on one line. But I was aware of it and should have recognized it. Thank you again.
player is equal to (1 modulus 2) but I don't know what ?1:2 means, and I can't really find it. Is this something specific to games.... or is it referring somehow to line 13 and the X and the O.
1 2 3 4 5 6 7 8
board();
player=(player%2)?1:2;
cout << "Player " << player << ", enter a number: ";
cin >> choice;
mark=(player == 1) ? 'X' : 'O';
Now the thing is I was studying multi dimension arrays (very interested in that). So this code is way ahead of what I am studying so far.
IMO a 2 dimensional array is the easiest way of doing this, because it is easy to do lots of things with nested for loops.
For that reason, IMO, the code you have is not a very good example to learn from. Quite a lot of people have done tic-tac-toe programs on this site - so I would search for one that uses a 2d array. Look for one that uses functions well, does not have too much repetition, and hopefully has meaningful variable names and is reasonably easy to understand.
The ternary ?: operator is a short cut for if-then-else:
conditional ? true statement : false statement
if the condition evaluates to true then the true statement is executed, otherwise the false statement is executed.
It is the same as:
1 2 3 4
if (conditional)
true statement;
elsefalse statement;
In the case of your example it is used with assignment as well - so the variable mark is assigned either X or O depending on the conditional.
Because I know I am jumping way ahead of the level of study I am on. The multi dimensional arrays peaked my interest a lot. This tic tac toe code sidetracked me but I learned a lot from trying to break it down. Coder777 and TheIdeasMan I will take your advise and try to find a better example.
Then back to my regular studies. See you around the forum.