Can't figure out what means int player = 1,i,choice;

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
int main(){              // Beginning of main
	int player = 1,i,choice;        // player variable has 3 values???

	char mark;
	do
	{
		board();
		player=(player%2)?1:2;

		cout << "Player " << player << ", enter a number:  ";
		cin >> choice;

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

		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--;
			cin.ignore();
			cin.get();
		}
		i=checkwin();

		player++;
	}while(i==-1);
	board();
	if(i==1)

		cout<<"==>\aPlayer "<<--player<<" win ";
	else
		cout<<"==>\aGame draw";

	cin.ignore();
	cin.get();
	return 0;
}                   // End of main 
Last edited on
It's defining 3 int variables player, i and choice. player is initialized to 1. It could have been written on 3 lines as
1
2
3
int player = 1;
int i;
int choice;
:) 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.
closed account (zb0S216C)
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.

Edit: I read your code wrong :)

Wazzak
Last edited on
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.

Changed it to....

1
2
int player = 1;
int player i, choice;
Last edited on
So here is what I am thinking:

On line 8

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';
Last edited on
but I don't know what ?1:2 means
it's a ternary operator. Equivalent to:
1
2
3
4
if(player%2)
  player=1;
else
  player=2;


The problem of this code is that it's done by a beginner. There're better and shorter ways to do that.
@ebonygeek45 (3)

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;
else
   false 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.

HTH Good Luck
Thanks guys

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.

again thanks
Topic archived. No new replies allowed.