Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NIM Game ---- again

Status
Not open for further replies.

towjam

Programmer
Jun 14, 2006
4
CA
I'm trying to make this NIM game and i'm having a lot of trouble trying to get the AI going.

I want to get the computer to pick a stick(s) from a specific group.However , i don't want it to be random , thats easy and dumb.

The whole goal from this game is to leave ur opponent with the last stick.

Right now all i have is one player playing by himself.

This is the code so far:-
The display function is in my previous thread (if anybody wants to see)

==============================================================
Code:
 int intGroup[6] = {0, 1, 2, 3, 4, 5};
 int intGroupNum; 
 int intSticks;

 Display(intGroup);

while(1)
{
 
 cout << "Player one, choose a group between 1-5: ";
 cin >> intGroupNum;
 
while(intGroupNum < 1 || intGroupNum > 5 || intGroup[intGroupNum] <=0)
{
 cout << "Not a valid row, please try again (1-5): ";
 cin >> intGroupNum;
}
 
 cout << "How many will you take from group " << intGroupNum << "?";
 cin >> intSticks;
 
while(intSticks < 1 || intGroup[intGroupNum] - intSticks < 0 || intSticks < 1)
{
cout << "Not a valid amount!\n" << "Pick again: ";
cin >> intSticks;
}

 intGroup[intGroupNum] = intGroup[intGroupNum] - intSticks;

 Display(intGroup);
 Sum(intGroup);
  
////////////////////////COMPUTER//////////////////////////////////////////////// 
 
////////////////////////COMPUTER//////////////////////////////////////////////// 

}
}

============================================================

I know i'm asking for too much , so any help is aprreciated
I just started programming in c++ , i did read up on the game , there is a mathematical theory behind winning the NIM game.The only problem is that its alot harder for me to translate it into code.
 
would you be so kind to provide link to that theory (or just game rules?) Game I know under same name does not involve any groups at all.
 
Ok, never mind. Wikipedia on Nim is pretty straightforward.
It has "Mathematical theory" chapter - not overly long - and only thing you need to use it is to know how to express "binary digital sum (XOR)" in C (or C++).
And that is "^". Like, 5^2

Ok OK.
Now. You have endless loop;
in the loop
you give first move to the player;
then you should check winning (terminating condition)
(is it sum() function for??). There should be the code that breaks the loop!
then use theory and let computer choose a move.
check winnign condition again. Don't forget to break if game actually over.
-- that's all.

proceed with your coding! ;))
 
Yea , the sum() function checks for winner , thus ending the game.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top