Question: This is based on the NIM game. Nim winning position with only one pile, using recursion? The way those False or True are generated depending
This is based on the NIM game.
Nim winning position with only one pile, using recursion?
The way those False or True are generated depending on the moves[] array;
Those are the marbles that either player can take away from pile.
It's based on 2 players and playing with only one pile of marbles, where N is the number of marbles, and moves contains how many marbles each player can take at a time, so N = 0 is always false since there would not be anymore marbles. and at each N the output depends on the on the moves available. In the example provided, moves is {1,3}, so the algorithm would go as follows:
N = 8, 8 marbles in the pile
moves ={1,3};
N = 0 is always false;
N = 1 is true, because Player 1 can remove 1 marble so it would be a winning position for the player
N = 2 is false, because Player 1 can only remove 1, leaving the pile with one, which means its not a winning position for player 1, but it would be for player 2. Since then player 2 can remove 1 marble.
N = 3 is true, because Player 1 can remove 3, since 3 is in the moves array, making that position a winning position.
N = 4 is false, because Player 1 can either remove 1 or 3, but would leave the pile with either 1 or 3 marbles for player 2 to win.
N = 5 is true, because Player 1 can either remove 1 or 3, that would land either on position 2 or 4, that is a false position for player 2.
N = 6 is false, because Player 1 can either remove 1 or 3, that would land either 3, or 5, that are true, making the Player 2 get a winning position
N = 7 is true, because Player 1 can either remove 1 or 3, that would land either 6 or 4 position that are false position for player 2.
so that output would be
| N | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| Winning ? | F | T | F | T | F | T | F | T |
The true or false output would depend on the moves for example if moves array is
moves = {1,2,4}
N = 8 number of marbles
| N | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| Winning? | F | T | T | F | T | T | F | T |
So How can I generate the True or False array, using recursion? In Java.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
