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?
I'm stuck because I can't use a random number generator, and my code would have a long list of "if" statements which I know just can't be right...here's what I have so far but I'm only giving the computer 2 options here..
// NIM game!
#include "stdafx.h"
#include <iostream>
usingnamespace std;
int main()
{
int total, n;
cout << "Please enter a number between 1 and 7 and press ENTER:" << endl;
cin >> total;
if (total>=7 || total<=1)
{ cout << "Please enter a number between 1 and 7 and press ENTER:" << endl;
cin >> n;
}
else{
while (true) {
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;
}
cout << "Enter number to subtract between 1 and the new total: ";
cin >> n;
while (n < 1 || n > total) {
cout << "Input must be between 1 and new total." << 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;
}
}
}