Gamified challenge · Game trees
piles[i] is the number of stones in pile i. Two players alternate; on a turn the player picks one pile and takes between 1 and maxTake stones from it (never more than it holds). The player who takes the last stone wins; a player with no stones to take has lost.
It's your turn. Write winningMove(piles, maxTake) returning [pile, count] for a move after which you win however your rival plays, or [-1, -1] if every move loses against perfect play. Any winning move is accepted. For piles = [5], maxTake = 3: take 1, leaving 4; whatever your rival takes (1-3) you take the rest of the group of 4, so [0, 1].
A minimax search over the game tree works on small tables; remembering solved positions, or spotting the pattern, handles the big ones.
This game is part of the paid plans. Sign in, then pick a plan to write your answer, watch it play out, and step through the algorithm with its guide.
Sign in with Google