C++ Without Fear Exercise 2.5.2

Hello, I been working on this exercise out of C++ without Fear Second Edition. It's a game called NIM where you are agaisnt the computer trying to be the first one to get to 0 or lower. I am stuck on this part of the exercise.

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 straegy for this general case?

I was able to get the user to choose a number from 1 - (whatever number they want) But I am having difficulty trying to set up the computer to subtract more than just 2 or 1. How would I go about setting up the computer to use the numbers the user has chosen to the computer can use the number in the range of 1 - x

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>

using namespace std;

int main()
{
    int n, total, x;

    cout << "Welcome to Nim! Pick a starting total." << endl;
    cin >> total;
    while (total < 1){
        cout << "Starting total must be higher than 0." << endl;
        cout << "Re-enter starting total: ";
        cin >> total;
    }
    cout << "Enter a a range of numbers you wish to use to subtract 1 - ?" << endl;
    cin >> x;
    cout << "Your available numbers are 1 - " << x << endl;
    while (true){
        if ((total % 3) == 2){
            total -= 2;
            cout << "I am subtracting 2." << endl; //<--- Need to do something with this block of code.
        } else {
            total--;
            cout << "I am subtracting 1." << endl;
        }
        cout << "New total is " << total << endl;
        if (total <= 0){
            cout << "I WIN!!!" << endl;
            break;
        }

        cout << "Enter a number to subtract (1 - " << x << "): ";
        cin >> n;
        while(n < 1 || n > x){
            cout << "Input must be a 1 or 2." << endl;
            cout << "Re-enter: ";
            cin >> n;
        }

        total -= n;
        cout << "New total is " << total << endl;
        if (total <= 0){
            cout << "You WIN!!!" << endl;
            break;
        }
    }
    return 0;
}


Thanks for any help you have to offer.
I don't really understand what you're trying to get the computer to do, but to get it to choose a number in a range, you can use...
rand() % (x + 1) + 1
With x being the x the user inputted in your code. This makes the computer choose a random number from 1 - x.
I tried that and I the computer is still only subtracting by a 1 or a 2. hmmmmmm
1
2
3
4
5
6
7
if ((total % 3) == 2){
            total -= 2;
            cout << "I am subtracting 2." << endl; //<--- Need to do something with this block of code.
        } else {
            total--;
            cout << "I am subtracting 1." << endl;
        }


So when the computer gets a remainder of 2 it needs to subtract an even number from the total and anything else needs to subtract by an odd number. Make sense?
Topic archived. No new replies allowed.