Stop or Pass: Optimal Play in a Two-Player Dice Duel
Math & Bit ManipulationAuto-graded · 2 test casesLast updated
Find the optimal accept/reject rule and the game value for a symmetric alternating-roll zero-sum game, using a stationary fixed-point equation.
Two players, A and B, play a zero-sum game with a fair 21-sided die. Its faces show the integers from -10 to 10, one per face, and every face has probability 1/21. Rolls are independent.
A rolls first. When it is your turn, you roll the die, see the face value v, and then choose one of two actions:
- Accept: the game ends at once and your opponent pays you
v. Ifvis negative, you pay your opponent|v|. - Reject: nobody pays anything and the opponent takes the next turn, rolling and facing the same choice.
Turns alternate until someone accepts. Both players are risk-neutral and maximize their expected payoff. Both know the other plays optimally. There is no discounting, no cost for rejecting, and no limit on the number of turns.
Your task is to write a function that takes no arguments and returns the optimal strategy of each player together with the expected payoff to A.
Return value. A dictionary with exactly three keys:
"accept": a dictionary with keys"A"and"B". Each maps to an ascending list of integers."reject": a dictionary with keys"A"and"B", in the same format."expected_payoff_to_A": a float, the expected payoff to A under optimal play by both.
How to express each player's lists. Both lists are written from A's point of view, as the amount A receives if the game ends on that roll.
- For A, the numbers are the faces A rolls. Face
vis in"accept"if A should accept it, and in"reject"otherwise. - For B, the numbers are the payoffs to A caused by B's roll. If B rolls face
w, the corresponding number is-w. The number-wis in B's"accept"list if B should acceptw, and in B's"reject"list otherwise.
Together, each player's accept and reject lists must cover all 21 values from -10 to 10 exactly once.
If a player would be exactly indifferent between accepting and rejecting on some face, that cannot be the case for any face in this game, so no tie-break rule is needed.
Constraints
- The die has exactly the 21 faces -10, -9, ..., 10, each with probability 1/21.
- Rolls are independent and the game is zero-sum.
- Both players are risk-neutral expected-value maximizers.
- The function takes no parameters.
- Every list in the returned dictionary is sorted in ascending order.
expected_payoff_to_Ais the exact game value as a float.
Examples
Example 1
Input: alternating_die_policy()
Output: {'accept': {'A': [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'B': [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1]}, 'reject': {'A': [-10, -9, -8, -7, -6, -5, -4, -3, -2], 'B': [2, 3, 4, 5, 6, 7, 8, 9, 10]}, 'expected_payoff_to_A': 1.8}
Hints
Hint 1
Nothing separates turn 1 from turn 7 except who is rolling, so look for a single value V for 'the player about to roll'. Rejecting hands the opponent a fresh roll worth V to them, which is worth -V to you.
Hint 2
This gives the equation V = E[max(v, -V)]. You accept a face v exactly when v >= -V. Since max(v, -V) >= v, V is at least 0, so the threshold -V is at most 0.
Hint 3
Suppose you accept all faces from -k up to 10. Solve the resulting linear equation for V and check that -V falls between the accepted and rejected faces. Only one k is consistent. For B, flip the sign of each face to express it from A's side.
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
One stationary value. After a rejection, the opponent faces exactly the game you just faced, with roles swapped. There is no clock, discount or cost, so only the identity of the roller matters. Let V be the expected payoff, under optimal play, to the player who is about to roll.
After rolling v, you choose between two options:
- Accept: you get v.
- Reject: the opponent is now the roller and expects V, so by zero-sum you expect -V.
So you accept exactly when v >= -V, and
V = E[max(v, -V)].
Both players use the same rule, because whoever is rolling is "the roller". Since max(v, -V) >= v and E[v] = 0, we get V >= 0. The threshold -V is therefore non-positive.
Solving the fixed point. Assume the roller accepts the faces from index i upward in the sorted list (everything at or above some threshold) and rejects the i faces below it. With n = 21 faces:
n·V = (sum of accepted faces) + i·(-V), so V = (sum of accepted faces) / (n + i).
A candidate i is consistent only if the smallest accepted face is >= -V and the largest rejected face is < -V. Trying the candidates gives exactly one consistent one: accept faces >= -1 and reject faces <= -2. The accepted faces -1 through 10 sum to 54, and 9 faces are rejected, so
V = 54 / (21 + 9) = 9/5 = 1.8.
The threshold is -1.8. Accepting -1 is correct because rejecting would give the opponent a roll worth 1.8 to them, which costs more than paying 1.
Reporting. A's lists are the faces accepted (-1 to 10) and rejected (-10 to -2). B follows the same rule on B's own face w, accepting w >= -1. Expressed as the payoff to A (-w), B accepts -10 to 1 and rejects 2 to 10. The payoff to A at the start is V = 1.8, since A is the first roller.
Why it is correct
- The map T(V) = E[max(v, -V)] is non-increasing, so V - T(V) is strictly increasing and has exactly one root.
- T is also a contraction: |T(x) - T(y)| <= (10/21)|x - y|. So the stationary value is the true value of the infinite game, and value iteration from any start converges to it.
- Each turn ends the game with probability 12/21, so the game ends with probability 1.
- Against this rule, any other choice on a face is worth min(v, -V) <= max(v, -V), so no player gains by deviating.
Complexity
The code tries each of the n thresholds and recomputes a sum for each, giving O(n^2) time for n faces. Prefix sums reduce this to O(n). Here n = 21, so the cost is negligible. Space is O(n) for the output lists.
Reference solution
from fractions import Fraction
from typing import Dict
def alternating_die_policy() -> Dict[str, object]:
faces = list(range(-10, 11))
n = len(faces)
# Find the consistent threshold: the roller accepts faces[i:], rejects faces[:i].
# n*V = sum(accepted) + i*(-V) => V = sum(accepted) / (n + i)
value = None
threshold = None
for i in range(n):
v_i = Fraction(sum(faces[i:]), n + i)
accepts_ok = faces[i] >= -v_i
rejects_ok = (i == 0) or (faces[i - 1] < -v_i)
if accepts_ok and rejects_ok:
value = v_i
threshold = faces[i]
break
a_accept = [v for v in faces if v >= threshold]
a_reject = [v for v in faces if v < threshold]
# B uses the same rule on its own face w; report -w (payoff to A).
b_accept = sorted(-w for w in faces if w >= threshold)
b_reject = sorted(-w for w in faces if w < threshold)
return {
"accept": {"A": a_accept, "B": b_accept},
"reject": {"A": a_reject, "B": b_reject},
"expected_payoff_to_A": float(value),
}