Sign in to follow this  
perastikos1

nim game

Recommended Posts

This sounds like a homework assignment, so I won't say much.

I had to look up what 'Nim' is. It's a 2-player game about as complicated as tic tac toe (simpler, even). You've got 4 rows of stones; the rows begin with 1,2,3, and 4 pieces respectively. Each turn, a player can remove as many stones as he likes from a row. The player to take the last stone loses.

I have not implemented this game in MATLAB, but I estimate that it would only take a few minutes to do so.

What exactly is the problem?

Share this post


Link to post
Share on other sites
I summarized the game mainly for the benefit of other forum-goers; I assumed you understood it...

MATLAB syntax is very similar to C syntax. A quick google turns up this tutorial which looks reasonable. Also, when in doubt, type 'help [TOPIC]' in the MATLAB command prompt. The documentation is very good.

Share this post


Link to post
Share on other sites
Winning strategy:
1) Bit-wise XOR all the numbers of stones together; call the result x.
2) If x is 0, generate a random move.
3) Look at the largest bit set in x and pick a row that also has it set.
4) If that row had n stones, pick the move that leaves n XOR x stones in that row.

For the initial configuration that Emergent described, the only winning move is taking all stones from the row that has 4.

The more interesting problem is figuring out why this works.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this