Bird
Raised Fist0

In a turn-based game with 5 players indexed from 0 to 4, which of the following correctly advances the turn to the next player in a circular manner?

easy🧠 Conceptual Q3 of Q15
LLD - Design — Tic-Tac-Toe Game
In a turn-based game with 5 players indexed from 0 to 4, which of the following correctly advances the turn to the next player in a circular manner?
Acurrent_player = (current_player + 1) % 5
Bcurrent_player = current_player + 1
Ccurrent_player = (current_player - 1) % 5
Dcurrent_player = current_player % 5 + 1
Step-by-Step Solution
Solution:
  1. Step 1: Understand circular indexing

    Using modulo with the number of players ensures the index wraps around after the last player.
  2. Step 2: Analyze options

    current_player = (current_player + 1) % 5 correctly increments and wraps the index. current_player = current_player + 1 can exceed the max index. current_player = (current_player - 1) % 5 decrements incorrectly. current_player = current_player % 5 + 1 can go out of bounds.
  3. Final Answer:

    current_player = (current_player + 1) % 5 -> Option A
  4. Quick Check:

    Modulo keeps index within range [OK]
Quick Trick: Use modulo to cycle player index [OK]
Common Mistakes:
MISTAKES
  • Forgetting modulo causes index overflow
  • Using subtraction instead of addition
  • Incorrect modulo placement

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes