Hello Everyone. I was given problem.. Write a program play the following game: There are 21sticks. Two players take turns picking the sticks. Each player should take atleast 1 sticks and a maximum of 4 sticks at each turn. The player who picks thelast sticks is the winner. But encountered different problem, one of this is that when the Player 1 gives a value of 4, it's working fine... 21-4 will get 17. But when the next player gives a value of 3, it will add up to 17 which makes the MAX_STICKS to 18 that it should be 14. So far I have this code.
Any suggestions? I am totally a newbie in dev c, help would be highly be appreciated. Thanks in advance.Code:#include <cstdlib> #include <iostream> #include <string> using namespace std; int main() { const int MAX_STICKS = 21; string player1, player2; int winnerplayer; int sticks; cout << "\tWelcome to Nim!" << endl << endl; cout << "Players take turns picking up from 1 to 4 sticks from a pile of 21." << endl; cout << "Whoever picks up the last stick wins." << endl << endl; cout << "There are " << MAX_STICKS << " stick<s> left." << endl; cout << "Player 1 - how many will you take? "; cin >> sticks; do { if ( sticks < 21 ) { cout << "There are " << (MAX_STICKS - sticks) << " stick<s> left." << endl; cout << "Player 2 - how many will you take? "; cin >> sticks; cout << endl; } if ( sticks < 21 ) { cout << "There are " << (MAX_STICKS - sticks) << " stick<s> left." << endl; cout << "Player 1 - how many will you take? "; cin >> sticks; cout << endl; } }while (sticks > 0); system("PAUSE"); return EXIT_SUCCESS; }



LinkBack URL
About LinkBacks


