![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2007
Posts: 3
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#3 | ||
|
Newbie
Join Date: Feb 2007
Posts: 3
Rep Power: 0
![]() |
Quote:
Quote:
[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 |
||
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#5 | |
|
Newbie
Join Date: Feb 2007
Posts: 3
Rep Power: 0
![]() |
Quote:
[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 |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |