Hi.
Let me start off by saying I've done no programming in my life aside from the 2 weeks of Intro to C++ class I've had so far, so I've got little idea of what I'm doing. Our assignment is to write a program for the 'game of NIM', essentially. The traditional game of NIM contains rows, but our program only has to have one row. We are allowed to assume that the user takes their turn first everytime the program is run. The user is allowed to input any number 9 or larger to indicate the number of sticks that will be in the pile. Then, the user is asked to enter a number 1-4 to indicate the number of sticks being removed from the pile. The computer then has to "take its turn" and generate a random number 1-4 to remove that number of sticks from the pile. This process is repeated until the last stick is 'picked up'. The person(or computer) to get the last stick is declared the winner. I've got the majority of the coding done, it only needs a little fine tuning. Anyone with any suggestions/notices any errors...any input is really helpful seeing as I'm basically crawling around in the dark. What I am having problems with specifically is in the comments at the end of the code.
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main ( ){
int num, sticks, comp_choice;
//This statement says that the winner is false until declared later in the program. (When there are no sticks left)
bool winner = false;
cout << "Welcome to the game of NIM! Please enter an integer 9 or greater\n"
<< "to indicate the number of sticks used in this game." << endl;
cin >> sticks;
//If the user enters a number less than 9, they will continually be prompted to enter a number
//9 or greater until they do so.
while(sticks < 9){
cout << "This is not an acceptable value.\n"
<< "Please enter a number 9 or greater." << endl;
cin >> sticks;
}
//While there is no winner, the body of the loop is run.
while (!winner){
//If the number of sticks is 9 or greater, the user will be prompted to choose the amount to remove between
//1 and 4.
if(sticks >= 9){
cout << "There are " << sticks << " sticks in the pile.\n"
<< "Please enter the amount(between 1 and 4) that you would like to remove from the pile." << endl;
cin >> num;
}
//If the number entered is a number 1 through 4, the user will be told the amount of sticks they removed
//followed by the amount of sticks left in the pile.
if((num >= 1) && (num <= 4)){
cout << "You have removed " << num << " sticks from the pile." << endl;
//This subtracts the number removed from the total number of sticks in the pile.
sticks -= num;
cout << "There are " << sticks << " left in the pile." << endl;
}
//If the number entered is less than 1 or greater than 4, the user will continually be prompted
//to enter a number from 1 to 4 until they do so.
if((num < 1) || (num > 4)){
cout << "This is not an acceptable value.\n"
<< "Please enter an amount between 1 and 4." << endl;
cin >> num;
//If the amount of sticks reaches 0 during the user's turn, the user will be declared winner.
if(sticks == 0){
winner = true;
cout << "Yay!!! You won the game of NIM! Congratulations!" << endl;
}
}
//If the user tries to enter a removable number of sticks that is larger than the number
//left in the pile, then they are prompted to enter a more acceptable amount.
else if (sticks - num < 0){
cout << "Oops! There aren't enough sticks left to take this many from the pile.\n"
<< "Please enter an acceptable amount." << endl;
cin >> num;
}
//This part of code is the computer's turn. The computer is allowed to remove a random number of sticks
//1 through 4. The amount taken away by the computer is then shown to the user, followed by the amount
//of sticks total left in the pile.
else{
srand((unsigned)time(NULL));
comp_choice = rand() % 4;
//This subtracts the computer's choice of removal sticks from the total sticks in the pile.
sticks -= comp_choice;
cout << "The computer has chosen to take away " << sticks << " sticks from the pile.\n"
<< "There are " << sticks << " sticks left in the pile." << endl;
//If the total number of sticks in the pile reaches 0 during the computer's turn, the computer is declared winner.
if(sticks == 0){
winner = true;
cout << "Awwww darn. The computer won. Better luck next time!" << endl;
}
}
}
system ("PAUSE");
return 0;
}
//The problems I'm having now are that the user is only allowed to input a number of sticks to remove once,
//when they should be prompted after every computer turn. The program is just continually subtracting the number
//first entered by the user. Also, the computer is not generating a number 1 through 4 to remove like it should be.