Problemconfigurable race board game

Session: Sign in to solve

Solution.txt

Signal Sprint Board

Problem

Signal Sprint is a tabletop race game with one token, a start at position 0, and a fixed finish. Some landing positions contain a shortcut or setback that moves the token to another position. The complete landing table is supplied with the board, so the same object can represent different board layouts.

Design SignalSprintBoard to move the token according to that configured table, report its position, and reset it for another race.

Requirements

  • Initialize the token. Construction sets the stored position to 0.
  • Advance the token. advance(steps) sets the stored position p to landingDestinations[p + steps].
  • Reset the token. reset() sets the stored position to 0.
  • Return the position. position() returns the stored position.
  • Preserve the position on read. Calling position() does not change the stored position.

API

SignatureReturnsBehavior
SignalSprintBoard(finish: integer, landingDestinations: integer[])Not applicableCreates a board with the supplied finish and landing configuration.
advance(steps: integer)voidApplies the configured transition.
reset()voidRestores the initial token state.
position()integerReads the current position.

Examples

Given finish = 10 and:

landingDestinations = [0, 1, 7, 3, 4, 2, 6, 4, 8, 9, 10]
StepOperationResultStored position
1position()00
2advance(2)No return value7
3position()77
4position()77
5advance(1)No return value8
6reset()No return value0
7position()00

Position 2 contains a shortcut to 7, so the first advance ends at 7. The entry at position 7 is not followed during the same move; each call applies exactly one configured transition.

Constraints

  • 2 <= finish <= 100.
  • landingDestinations has exactly finish + 1 entries.
  • landingDestinations[0] == 0 and landingDestinations[finish] == finish.
  • Every landing destination is between 0 and finish, inclusive.
  • 1 <= steps <= 6.
  • Every call to advance occurs before the stored position reaches finish and satisfies position() + steps <= finish.
  • At most 100 public method calls occur in one testcase.

Notes

The driver validates all constructor data and operation preconditions before learner code runs. Calls on one board are sequential, and inputs outside the declared domain are not passed to the public API.

PRIVATE WORKSPACE

Checking your session…

The statement is public. The editor, editorial, submissions, and saved work are private.