A game of at most 10 in C++
This is the problem:
There are two players A and B. Player A starts and chooses a number in the
range 1 to 10. Players take turn and in each step add a number in the
range 1 to 10. The player who reaches 100 first wins.
In the program, player A is the user and player B is the computer. Besides
that the computer must force a win whenever possible. For instance, if the
player A reaches 88 at some point, the computer must choose 89, as this is
the only way to force a win.
My attempt:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int PlayerA = 0,PlayerB = 0,sum = 0;
do
{
cout << "Please enter a number(1 between 10):" << endl;
cin >> PlayerA;
sum +=PlayerA;
cout << sum << endl;
system("pause");
PlayerB = (100-sum)%10 + 1;
sum +=PlayerB;
cout << sum << endl;
system("pause");
}while(sum < 100);
}
The problem i face is how do I do the forcing on the part of the computer.
Need some guidance on that. Welcome to suggestions as well
No comments:
Post a Comment