Explain solidity>require

Input Validation

require() checks if a condition is true. If false, it stops the function and reverts all changes.

Syntax:

require(condition, "Error message");

Example:

require(msg.sender == playerX, "Not your turn");

This checks if the caller is playerX. If not, it shows "Not your turn" and stops.

Note: == means "is equal to" – it checks if two values are the same.



Let's add validation to check if a board position is empty before allowing a move.

Write: require(board[4] == Cell.Empty, "Cell already taken");

Best practice: Put require checks BEFORE state changes. While require will revert everything if it fails (even changes made earlier), checking first saves gas and follows the "checks-effects-interactions" security pattern.

Add validation to makeMove
0%
Requirements:
Has require statement
Checks board[4] == Cell.Empty
Includes error message
Require comes before state change