Sign in to follow this  

nim game

This topic is 3150 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 understand the meaning of the game but i have never use matlab

i have manage to develop it in c++ so i would like to have a help in matlab

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
i see what you mean
i am going read it

the purpose of my post was if someone can quickly help me

anyway thanks for answer

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
Sign in to follow this