Nim,

Oct 1, 2008 at 12:32am
Hi ive been trying to write a simple game based of Nim were two players take matches from a sinlge pile in turn the player left to take the last match loses... It's assumed that the user go first and that the comp goes second and wins! However i can only get the comp to win all the time when the number of matches to start the game with is 21 (n) and the max amount that can be removed is 4 (t), as one can use a constant value k = (n-1)/(t+1)+1 meaning that we can enusre that mulitpes of five are removed on each turn as no matter what the user takes we can add so the sum of the amount removed by user and comp adds to five on each go, hence user will be left with 1 in the end... Any way i've be wrecking my head the last few days trying to look for a generic solution/algorithm to solve the problem for any amount of matches (n) where up to any amount (t) less than n can be removed on each go. Ive been trying to find a solution of the form
n=k*(t+1)+1, for some constant k, which should enable me to win. When this is not the case, my program should take a random number of matches from the pile. If the user makes an error on their turn, my program should play correctly and win the game.

I would be every gratefull if someone could help me construct should an algoritm

Regards Patrick
Topic archived. No new replies allowed.