what is the alternative of (player=(player%2)?1:2;) and( mark=(player == 1) ? 'X' : 'O';) in this programme


i want to improve this code, first of all on execution of this code it produce a beep like sound,how to get rid of this and secondly how to replace these two{ (player=(player%2)?1:2;) and( mark=(player == 1) ? 'X' : 'O';) } statement with an alternative if else statement?
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
  #include<iostream>
using namespace std;
void shape();
int playertoggle();
int check();

char a[10]={'0','1','2','3','4','5','6','7','8','9'};
int n,player,i;
char mark='x';
int main()
{



do
{
		shape();
	
player=(player%2)?1:2;
		cout << "Player " << player << ", enter a number:  ";
		cin>>n;
	mark=(player == 1) ? 'X' : 'O';
	 if(n==1&&a[1]=='1')
	a[1]=mark;
	
    else if(n==2&&a[2]=='2')
	a[2]=mark;
    else if(n==3&&a[3]=='3')
	a[3]=mark;
    else if(n==4&&a[4]=='4')
	a[4]=mark;
    else if(n==5&&a[5]=='5')
	a[5]=mark;
    else if(n==6&&a[6]=='6')
	a[6]=mark;
    else if(n==7&&a[7]=='7')
	a[7]=mark;
    else if(n==8&&a[8]=='8')
	a[8]=mark;
    else if(n==9&&a[9]=='9')
	a[9]=mark;
      
		else
		{
			cout<<"Invalid move ";

			player--;
			cin.ignore();
			cin.get();
		
		}
		i=check();

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

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

	cin.ignore();
	cin.get();
	return 0;	
}




	

void shape()
{
	system("cls");
	cout<<"enter a nubmer "<<endl;
	

	cout<<"   |    "  <<  " |"<<endl;
	cout<<" "<<a[1]<<" |"<<"  "<<a[2]<<"  |"<<"  "<<a[3]<<endl;
	cout<<"___|"<<"_____|"<<"____"<<endl;
	cout<< "   |"<<"     |"<<"    "<<endl;
	cout<<" "<<a[4]<<" |"<<"  "<<a[5]<<"  |"<<"  "<<a[6]<<endl;
    cout<<"___|"<<"_____|"<<"____"<<endl;
	cout<< "   |"<<"     |"<<"     "<<endl;
	cout<<" "<<a[7]<<" |"<<"  "<<a[8]<<"  |"<<"  "<<a[9]<<endl;
	

}
int check()
{
	 if (a[1] == a[2] && a[2] == a[3])

		return 1;
	else if (a[4] == a[5] && a[5] == a[6])

		return 1;
	else if (a[7] == a[8] && a[8] == a[9])

		return 1;
	else if (a[1] == a[4] &&a[4] == a[7])

		return 1;
	else if (a[2] == a[5] && a[5] == a[8])

		return 1;
	else if (a[3] == a[6] && a[6] == a[9])

		return 1;
	else if (a[1] == a[5] && a[5] == a[9])

		return 1;
	else if (a[3] == a[5] && a[5] == a[7])

		return 1;
	else if (a[1] != '1' && a[2] != '2' && a[3] != '3' 
                    && a[4] != '4' && a[5] != '5' && a[6] != '6' 
                  && a[7] != '7' && a[8] != '8' && a[9] != '9')

return 0;
	else
		return -1;		

}
int  playertoggle()
{
	
	player=(player%2)?1:2;
return 1;
}.
{ (player=(player%2)?1:2;) and( mark=(player == 1) ? 'X' : 'O';) }

https://msdn.microsoft.com/en-us/library/e4213hs1.aspx?f=255&MSPPError=-2147217396
Those are using the 'conditional' operator. It is ternary operator (as opposed to the typical binary and unary operators). It basically follows these semantics:

(someBooleanTestCondition ? ifTrue : ifFalse)

'someBooleanTestCondition' will be tested. If it equates to 'true' then 'ifTrue' will be used, otherwise 'ifFalse' will be used. That should help you change them into the more traditional if statements.

I'm not sure why your program would make a a beeping sound.
but is there any alternative for this statement ?
imtiazsyed007 wrote:
but is there any alternative for this statement ?
JayZom wrote:
That should help you change them into the more traditional if statements.
Last edited on
Yes. The following two snippets are equivalent:
 
mark=(player == 1) ? 'X' : 'O';

1
2
3
4
if (player == 1) 
  mark = 'X';
else
  mark = 'O';

Not sure why you want to rewrite it. The ternary operator is shorter and is very clear (assuming you understand the ternary operator).

As for a beeping sound, the only thing I can think of is outputting a char that has a value of binary 7 (BEL).

Last edited on
Yes you can simplify the code. The key thing to realize is this: the way you represent the data does not have to be the same as the way you display the data.

So internally, represent the player as 0 or 1.
Externally, display the player as 1 or 2.

1
2
3
4
5
6
7
8
9
10
11
char mark[2] = {'X', 'O' };
int player;  // 0 or 1.

// Switch players:
player = ++player % 2;

// get current player's mark
mark[player];

// print the current player
cout << "player number " << player+1;

Topic archived. No new replies allowed.