Newbie C++ programming,need help!
Hi,I have been trying to learn C++ programming from the book called C++ Without Fear 2nd Edition.There is a challenging exercise which I couldn't solve.Is anyone skilled enough to provide me with hint or display your source code so I can learn?Please help!
Exercise 2.5.2 For the more ambitious: Write a version that permits subtracting any number from 1 to N, where N is stipulated at the beginning of the game. For example, the user when prompted might say that each player can subtract any number from 1 to 7. Can you create an optimal computer strategy for this general case?
*In addition,you have to also prompt the user for any total number to be subtracted.
If you guys don't get it maybe a preview of the book might be helpful for you solvers.
Initially the author created a subtraction game that ensures that the computer will win almost everytime unless the user enters correctly which is 3. The computer will win if it is the first to reduce the total to zero. For example:
1. We agree to start with the number 7,and you go first.
2.You subtract 2 from the total,making it 5.
3.I subtract 2 from the total,making it 3.
4.You subtract 1 from the total,making it 2.
5.I subtract 2 from the total making it 0.I win!
Here is the source code:
Code:
using namespace std;
int main(){
int total,n;
do{
cout<<"Welcome to NIM.Pick a total (total>1): ";
cin>>total;
if(total<1){
cout<<"Please re-enter input."<<endl;
}
}while(total<1);
while(true){ //infinite loop
//Pick best response and print results.
if((total%3)==2){
total=total-2;
cout<<"I am subtracting 2."<<endl;
}else{
total--;
cout<<"I am subtracting 1."<<endl;
}
cout<<"New total is "<<total << endl;
if(total==0){
cout<<"I win!"<<endl;
break;
}
//Get user's response;must be 1 or 2.
cout<<"Enter num to subtract (1 or 2): ";
cin>>n;
while(n<1||n>2){//validation
cout<<"Input must be 1 or 2."<< endl;
cout<<"Re-enter: ";
cin>>n;
}
total=total-n;
cout<< "New total is "<<total << endl;
if(total==0){
cout<<"You win!"<<endl;
break;
}
}
system("PAUSE");
return 0;
}
Re: Newbie C++ programming,need help!
This usually works the other way around - you show us what you have done and where you get the problems, we help you from there.
Re: Newbie C++ programming,need help!
Hi,I am really glad someone has responded to my thread.I really appreciate it. The problem is I have not started in typing out the source code because I still couldn't find a way to solve it. I am still overwhelm by two factor:
1) The total is being first specified by the user. This can be any number which I find it is 'Infinite' numbers for me to cater to.
2) Subtraction ranging from 1 to N. The N can be any number which is in the sense 'Infinite' numbers.
The book is really good in teaching C++ to newbie because of its simple explanation & exercises to apply what readers have learned from the book
to help improve their programming skill.However, it somehow lack answers for the exercises provided.
I have thought initially that maybe if I have a deep understanding of the previous example I could solve this exercise. I believe the preview coding which I have shown works because it always eliminates the remainder of the total number given by the user. So in anyway, even if the user randomly subtract the total, the logic is always to elminate the remainder by checking with %3.
For example, let say the user provides a total of 10.
11%3 will give a remainder of two. So I will subtract two from it forcing it to be a multiplication of 3.
Therefore,the total now is 9.
Next,let say the user subtracts 1.
The total is 8.
8%3 will give a remainder of two.I will then subtract two from it forcing it to be a multiplication of 3.
The total is 6.
.
.
.
So do you see the pattern now?
The program is designed that it will meet its target of reaching 3 to win the game.Why so?
This I believe is also due to the program having the chance to start first in subtracting.
Hence, whenever it reaches 3, the user will subtract and the computer will win the game by subtracting
whatever is left.
Yup,but I still can't figure out the exercise though. :confused:
Do you have any idea of solving it? :)
Re: Newbie C++ programming,need help!
Quote:
Originally Posted by
bestmaster99
1) The total is being first specified by the user. This can be any number which I find it is 'Infinite' numbers for me to cater to.
You store the value in some variable and then operate on it.
Quote:
2) Subtraction ranging from 1 to N. The N can be any number which is in the sense 'Infinite' numbers.
No, it can vary from 1 to the number specified by the user which sits in the variable.
Quote:
Do you have any idea of solving it? :)
You can use the min-max algorithm. It's a classic when it comes to this particular game.
Re: Newbie C++ programming,need help!
Re: Newbie C++ programming,need help!
Re: Newbie C++ programming,need help!
I have searched online about min-max algorithm. Haha,I am new to programming.What exactly is the algorithm and how do I write the source code?
Re: Newbie C++ programming,need help!
Search again then. This algorithm is also called "minimax".