Archived

This topic is now archived and is closed to further replies.

Can anyone help me with the NIM Game ?

This topic is 5150 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

Hello everyone I''m a new member and i am trying to program the NIM Game in C++ but i have a little difficulty. First, let me explain how i must make the game. I have to make the player choose between 11 and 31 sticks for each game, and then make him choose how many sticks he and the cpu can take by turn (it has to be between 3 and half of the numbers of sticks he chose earlier). Also, i ABSOLUTELY have to make the computer find a way to win, a strategy so he''ll never lose. Thats where im stuck.. (btw, im French speaking so dont mind the french words, but i''ve translated the "cout" for you). Here''s where i''m stuck in my code #include <iostream> using namespace std; int main () { unsigned maxNbSticks, maxSticksByTurn, minSticksByTurn = 1, score, nbSticksTaken, unsigned nbSticksTakenByCPU, nbSticksRemaining; cout << "Welcome to the NIM Game!" << endl << endl; cout << "With how many sticks will we play ? (Min 11 Max 31) "; cin >> maxNbSticks; while(maxNbSticks < 11 || maxNbSticks > 31) { cout << "\nInvalid number !" << endl << endl; cout << "With how many sticks will we play ? (Min 11 Max 31) "; cin >> maxNbSticks; } cout << "\nMaximum sticks by turn ? "; cout << "(Min 3 Max " << maxNbSticks / 2 << ") "; cin >> priseMax; while(maxSticksByTurn < 3 || maxSticksByTurn > maxNbSticks/2) { cout << "\nInvalid number !" << endl << endl; cout << "Maximum sticks by turn ? "; cout << "(Min 3 Max " << maxNbSticks / 2 << ") "; cin >> maxSticksByTurn; } cout << "\We will then play with " << maxNbSticks << " sticks "; cout << "and we''ll take from 1 to " << maxSticksByTurn << " sticks by turn !" << endl << endl; while (nbSticksRemaining != 0) { cout << "\nThere are presently " << maxNbSticks << " sticks remaining" << endl; cout << "\nHow many sticks do you take ? "; cin >> nbSticksTaken; } return 0; } So i''m stuck there...i know what the strategy is, but now how to implement it. The strategy is that the Computer should make "rows" of sticks and divide them by the same number, to make blocks, and always "finish" the block so that the player is always stuck with the last stick. Here''s an exemple: Suppose the player chooses 13 sticks to play with, and a maximum number of 3 sticks by turn. The Computer then tells to himself "Ok, we have 13 sticks, so if we remove the last one, we have 12. 12 can be divided by 3 blocks of 4 sticks, so i can do it like this: [ / / / / ] [ / / / / ] [ / / / / ] / If the player takes 2 sticks, then the Computer takes 2, to fisnish the first block. If the player chose 3 instead, then the Computer takes 1. So the computer will ALWAYS win this way. What i''m stucks with is "how to tell this strategy to the computer for EVERY combination possible. Can anyone help me ?

Share this post


Link to post
Share on other sites

This topic is 5150 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.

Guest
This topic is now closed to further replies.