Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 24th, 2007, 5:31 PM   #1
wizardchik
Newbie
 
Join Date: Feb 2007
Posts: 3
Rep Power: 0 wizardchik is on a distinguished road
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
wizardchik is offline   Reply With Quote
Old Feb 24th, 2007, 5:54 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Feb 25th, 2007, 1:56 AM   #3
wizardchik
Newbie
 
Join Date: Feb 2007
Posts: 3
Rep Power: 0 wizardchik is on a distinguished road
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
wizardchik is offline   Reply With Quote
Old Feb 25th, 2007, 8:03 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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".
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Feb 25th, 2007, 9:49 PM   #5
wizardchik
Newbie
 
Join Date: Feb 2007
Posts: 3
Rep Power: 0 wizardchik is on a distinguished road
Quote:
Originally Posted by DaWei View Post
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
wizardchik is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Does Programming Make You Smarter? Sane Coder's Corner Lounge 43 Oct 2nd, 2005 6:12 AM
MIT's Metaphor For Software Programming Infinite Recursion Other Programming Languages 2 Jun 12th, 2005 6:42 AM
Problems with C programming in Visual Studio .NET 2003 debugger1 C 4 Jun 3rd, 2005 7:03 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:57 PM.

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