Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   having problems with programming the NIM (http://www.programmingforums.org/showthread.php?t=12657)

wizardchik Feb 24th, 2007 5:31 PM

having problems with programming the NIM
 
Hey folks,
I'm a newbie in programming, don't know much about it. Now, I am trying to program the game called NIM, if you ever heard of it. Here is a little info

[
The game of Nim typically involves two players and a pile of matches, stones, marbles, or some other kind of objects. Each player alternately makes a move by taking one, two, or three objects from the pile. The player who takes the last object(s) loses -- and the other player wins. For example, suppose there's a pile of five matches. Player A takes two matches, leaving three. Player B then takes two matches, leaving one. Player A must take the final match, and loses. Player B wins.

]

Here is what i have so far for the source code for DOS.

#include <iostream>
using namespace std;

int current_pile(int,int);
int computerTurn(int,int);

int main (){
int num,_removeSticks;
cout<<"Welcome to NIM. Please enter an integer >=9 to start off the game.\n";
cin>>num;

/*Tests for numbers smaller than 9*/
while (num<9){
cout<<"You have entered "<<num<<" is not great than or equal to 9.\n"
<<"Try again. Please enter an integer >=9 to start off the game.\n";
cin>>num;
}

while (current_pile!=0){

cout<<"It's show time. You have "<<num<<" number of sticks to play againist the computer.\n"
<<"Enter the number of sticks, between 1 and 4, to be removed \n";
cin >>_removeSticks;

/*Tests for numbers greater than 4 or smaller than 1*/
while (num<1 && num>4){
cout<<""<<_removeSticks<<" is not bettween 1 and 4. No worries, let's try again \n";
cin >>_removeSticks;
}

cout<<""<<current_pile(num,_removeSticks)<<" sticks left.Now it's the computers turn \n";
}
system ("PAUSE");
return 0;
}


/* F U N C T I O N D E C L A R A T I O N N STARTS HERE */

/* Function that substracts the number of sticks user entered from the whole pile*/
int current_pile(int _currentPile,int _userNum){

_currentPile=_currentPile-_userNum;
return _currentPile;

}
/* Function that the computer substracts the number of sticks from the whole pile*/
int computerTurn(int _currentPile, int _compNum){
_currentPile=_currentPile-_compNum%(1+4);
return _currentPile;
}


My question is, how can I get the computer to remove sticks automatically after user finishes his turn. What I have for the computer doesn't work.

Any help will be greatly appreciated

DaWei Feb 24th, 2007 5:54 PM

You will get better responses if you read the forum's rules/faq. It's not only polite, it pays dividends in getting good responses without the thread having to run 40-50 posts.

One of the things you learn is to put your code in tags to preserve the indentation/formatting. Try that. Masochists, newbs, and the bored, will read it anyway, but it's really better to play by the rules.

This game was one of my first C programs, back in the 80's. It's really very simple. I only had a text-based glass TTY, so I represented the tokens as matches, constructed of vertical bars and letters 'o'.

I hope you aren't really using "DOS", as you call it (probably MSDOS, you mean?), and that it's merely a misapplication of the term. One would hope you mean a console-based app running under a more modern OS.

wizardchik Feb 25th, 2007 1:56 AM

Quote:

You will get better responses if you read the forum's rules/faq. It's not only polite, it pays dividends in getting good responses without the thread having to run 40-50 posts.

One of the things you learn is to put your code in tags to preserve the indentation/formatting. Try that. Masochists, newbs, and the bored, will read it anyway, but it's really better to play by the rules.

This game was one of my first C programs, back in the 80's. It's really very simple. I only had a text-based glass TTY, so I represented the tokens as matches, constructed of vertical bars and letters 'o'.

I hope you aren't really using "DOS", as you call it (probably MSDOS, you mean?), and that it's merely a misapplication of the term. One would hope you mean a console-based app running under a more modern OS.
As I said before, I am a newbie in this forum, so I will get to know the rules as I go through them. Thanks for the comments Mr.DaWei. Yes, I meant the console-based program (already learning new things about programming). Here are the rules of NIM for those who haven't played it yet.
Quote:


The game of Nim typically involves two players and a pile of matches, stones, marbles, or some other kind of objects. Each player alternately makes a move by taking one, two, or three objects from the pile. The player who takes the last object(s) loses -- and the other player wins. For example, suppose there's a pile of five matches. Player A takes two matches, leaving three. Player B then takes two matches, leaving one. Player A must take the final match, and loses. Player B wins.
So far I have reached to this point of programming it.

[HTML]#include <iostream>
using namespace std;

int current_pile(int,int);
int computerTurn(int,int);

int main (){
int num,_removeSticks;
cout<<"Welcome to NIM. Please enter an integer >=9 to start off the game.\n";
cin>>num;

/*Tests for numbers smaller than 9*/
while (num<9){
cout<<"You have entered "<<num<<" is not great than or equal to 9.\n"
<<"Try again. Please enter an integer >=9 to start off the game.\n";
cin>>num;
}

while (current_pile!=0){

cout<<"It's show time. You have "<<num<<" number of sticks to play againist the computer.\n"
<<"Enter the number of sticks, between 1 and 4, to be removed \n";
cin >>_removeSticks;

/*Tests for numbers greater than 4 or smaller than 1*/
while (num<1 && num>4){
cout<<""<<_removeSticks<<" is not bettween 1 and 4. No worries, let's try again \n";
cin >>_removeSticks;
}

cout<<""<<current_pile(num,_removeSticks)<<" sticks left.Now it's the computers turn \n";
}
system ("PAUSE");
return 0;
}


/* F U N C T I O N D E C L A R A T I O N N STARTS HERE */

/* Function that substracts the number of sticks user entered from the whole pile*/
int current_pile(int _currentPile,int _userNum){

_currentPile=_currentPile-_userNum;
return _currentPile;

}
/* Function that the computer substracts the number of sticks from the whole pile*/
int computerTurn(int _currentPile, int _compNum){
_currentPile=_currentPile-_compNum%(1+4);
return _currentPile;
}[/HTML]

Questions:
-How can I get the computer remove random #of sticks (between 1 and 4) after user turn is over?
-What is one way to identify the winner when there are no sticks left?

Thanks in advance

DaWei Feb 25th, 2007 8:03 AM

You can't have the computer remove a random number of sticks in the range 1-3, always. You have remove a number in the range 1 to the lesser of 3 or the number of sticks left (if there are two left, you can't take 3).

You need a variable, sticksLeft. You need to compare sticksLeft to 3 and generate a random number in the range 1-3 or 1-sticksLeft, accordingly. When sticks are removed, you subtract that number from sticksLeft. When sticksLeft is zero, the game is over.

Obviously, after you've subtracted the player's number, it's the computer's turn. When the computer is through, it's the player's turn. That cycle gets broken by "game over".

wizardchik Feb 25th, 2007 9:49 PM

Quote:

Originally Posted by DaWei (Post 124381)
You can't have the computer remove a random number of sticks in the range 1-3, always. You have remove a number in the range 1 to the lesser of 3 or the number of sticks left (if there are two left, you can't take 3).

You need a variable, sticksLeft. You need to compare sticksLeft to 3 and generate a random number in the range 1-3 or 1-sticksLeft, accordingly. When sticks are removed, you subtract that number from sticksLeft. When sticksLeft is zero, the game is over.

Obviously, after you've subtracted the player's number, it's the computer's turn. When the computer is through, it's the player's turn. That cycle gets broken by "game over".

Folks, I took a total different direction in my code. Instead of using local variables, I used global ones. The entire game works fine when a user wins and loses. But, but when the user loses, the program is not closing, it's starting over everything. I have tried to work around the loop, but still, it's not working. Here is the code
[HTML]
#include <iostream>
using namespace std;
int playSticks,removeSticks, compRemove;

//Function Declaration start here
void userWelcome();
int userInput1();
int userInput2();
int compTurn();


int main ()
{
userWelcome();
userInput1();

//the core loop of the game
do{
userInput2();
compTurn();
} while (playSticks>=0);
//return 0;
}


/* F U N C T I O N D E F I N I T I O N S S T A R T S H E R E */

void userWelcome(){
//Friendly user welcome
using namespace std;
cout<<"Weclome to the inteletual game of NIM.\n";
return;
}

int userInput1(){

do {
cout << "Please enter total number of sticks to be removed"<<endl;
cin >> playSticks;
} while (playSticks<=8);

return playSticks;
}


int userInput2()
{
cout<<"Please enter the amount of sticks to be removed. \n";
do{
cin>>removeSticks;
}while ((removeSticks>=5) || (removeSticks<=0));

playSticks=playSticks-removeSticks;
cout<<"There are "<<playSticks<<" left. \n";
if (playSticks==0)
{cout << "You win"<<endl;}
system ("PAUSE");
return 0;}

else {};
return playSticks;
}

int compTurn()
{
if (playSticks%5==0)

{
playSticks=playSticks-1;
cout<<"Computer removed 1. There are "<<playSticks<<" left \n";
return playSticks;
}

else
{
compRemove=playSticks%5;
playSticks=playSticks-compRemove;
cout<<"Computer removed "<<compRemove<<". There are "<<playSticks<<" left \n";}

if (playSticks==0)
{
cout << "You lost"<<endl;
system ("PAUSE");
return 0;

}
else {} ;

return playSticks;

}
[/HTML]

What else would you suggest to improve stylistically etc..?
Thank you


All times are GMT -5. The time now is 10:45 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC