4 Replies - 376 Views - Last Post: 13 January 2013 - 07:59 AM Rate Topic: -----

#1 Cha0sBG  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 167
  • Joined: 09-April 09

Ternary Conditional Operator Problem

Posted 12 January 2013 - 07:07 PM

Alright, so i wanted to make a game of tic tac toe . And i wanted to use ternary conditional operators to ease up things .. the problem is it made my life a hell hole. As much as i think it trough it doesn't add up. So my problem is it always gives the result of array[0][2] . Here is the source so it can make any sense.

class Board{
public:
	Board( )
	{	
		m_GameBoard[0][0] = 'o';
		m_GameBoard[0][1] = 'o';
		m_GameBoard[0][2] = 'x';
	}

	int CheckGameStatus( )
	{
		// 0  ---- o Wins
		// 1  ---- x Wins
		// -1 ---- continues


		//Check first row
		int Status = (m_GameBoard[0][0] && m_GameBoard[0][1] && m_GameBoard[0][2] == 'x') ?  1 : (( m_GameBoard[0][0] && m_GameBoard[0][1] && m_GameBoard[0][2] == 'o' ) ? 0 : -1);

		return Status;

	}

private:

	

private:
	
	char m_GameBoard[3][3];
	
};



Any ways on top of your head to fix this w/o switching from ternary conditional operators ?
PS: sorry for my bad way of explaing things
Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Ternary Conditional Operator Problem

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,161
  • Joined: 05-May 12

Re: Ternary Conditional Operator Problem

Posted 12 January 2013 - 07:15 PM

I don't think this code is doing what you think it is doing:
m_GameBoard[0][0] && m_GameBoard[0][1] && m_GameBoard[0][2] == 'x'



That string of code is checking if m_GameBoard[0][0] is non-zero, and m_GameBoard[0][1] is non-zero, and m_GameBoard[0][2] is 'x'.
Was This Post Helpful? 0
  • +
  • -

#3 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1075
  • View blog
  • Posts: 4,536
  • Joined: 09-June 09

Re: Ternary Conditional Operator Problem

Posted 12 January 2013 - 07:51 PM

int Status = (m_GameBoard[0][0] && m_GameBoard[0][1] && m_GameBoard[0][2] == 'x') ?  1 : (( m_GameBoard[0][0] && m_GameBoard[0][1] && m_GameBoard[0][2] == 'o' ) ? 0 : -1);


In short, don't do this. It doesn't make your code "faster" or more elegant, it just makes it confusing.

Only use the ternary operator when your conditions are readable and the return value is intuitive.

i.e.
int max = a < b ? b : a;


Was This Post Helpful? 0
  • +
  • -

#4 Cha0sBG  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 167
  • Joined: 09-April 09

Re: Ternary Conditional Operator Problem

Posted 13 January 2013 - 07:15 AM

Alright got it, i'll think of another way. Thanks
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,161
  • Joined: 05-May 12

Re: Ternary Conditional Operator Problem

Posted 13 January 2013 - 07:59 AM

What problem wheres you trying to solve when you switched to using the ternary operator? What benefit did you think you were getting?

Sent from my T-Mobile G2 using Tapatalk 2
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1