Writing a NIM game with certain limitations

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..

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
50
51
52
53
54
55
56
57
58
59
60
61
 // NIM game!
#include "stdafx.h"
#include <iostream>

using namespace 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;
}
}
}

Last edited on
What do you have problem with?
Is it writing the limitation for player?
Or producing an optimal strategy?
Producing an optimal strategy...for example with the current code I have I'm only giving the computer 2 options as opposed to ANY number from 1 to N..
Topic archived. No new replies allowed.