Programming Forums
User Name Password Register
 

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

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
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
 

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 12:51 AM.

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