Solving “Duck Duck Goose” in Java
The challenge
The objective of Duck, duck, goose is to walk in a circle, tapping on each player’s head until one is chosen.
Task: Given an array of Player objects (an array of associative arrays in PHP) and an index (1-based), return the name
of the chosen Player(name
is a property of Player
objects, e.g Player.name
)
Example:
The solution in Java code
Option 1 (returning the modulo
):
Option 2 (slightly longer
):
Option 3 (working through the problem
):
|
|
Option 4 (using a while
):
Test cases to validate our Java solution
|
|