Explain solidity>ternary operator

Choosing the Piece

Now we need to place X or O depending on whose turn it is.

The Ternary Operator:

A shorthand way to choose between two values based on a condition.

Syntax:

condition ? valueIfTrue : valueIfFalse

Example:

uint score = isWinner ? 100 : 0;

If isWinner is true, score becomes 100. Otherwise, it becomes 0.



For Our Game:

We'll use the ternary operator to pick which piece to place:

Cell piece = xTurn ? Cell.X : Cell.O;

If it's X's turn (xTurn is true), place an X. Otherwise, place an O.



Update the makeMove function to determine the piece using xTurn, then use that piece variable instead of hardcoding Cell.X.

Use ternary operator to choose piece
0%
Requirements:
Declares piece variable
Uses ternary operator with xTurn
Assigns to piece variable
Uses piece variable in board assignment