# nim game

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

## Recommended Posts

hello have someone manage to develop nim game in matlab? thanks a lot

##### Share on other sites
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 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 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 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 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.

1. 1
2. 2
3. 3
4. 4
Rutin
13
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633701
• Total Posts
3013435
×