Question
Use C++ . What do you do with a pile of pebbles (little rocks)? You and your friend decide to play a game to remove
Use C++ .
What do you do with a pile of pebbles (little rocks)? You and your friend decide to play a game to remove these pebbles, one or several at a time, to prove that one of you is the Ultimate Strategist,
The game rules are:
You go first when the game starts (that is, you pick your game-winning move first)
You and your friend take turns to remove the rest
Each of you can remove 1 to 4 pebbles at each turn
The one who removes the last pebble will be the winner
Write the following function, CanIAlwaysWin(int n), to see if you are always the winner given n as your starting quantity of pebbles.
bool CanIAlwaysWin(int n)
For example:
n <= 4 returns true, you are the winner (since you can remove all 4 at once leaving your friend with none)
n = 5 returns false, you lose (no matter you remove 1, 2, 3, or 4 pebbles the last pebble will always be your friend's to remove)
n = 6, n = 7, etc., what do you think?
NOTE: you WON'T need to write a game asking two users to take turns inputting numbers. Instead, you need to implement the CanIAlwaysWin() function by returning either a true or false value. Your main() function may look like:
int main() { int n = 0; cout << "Enter a number: "; cin >> n; cout << "Can I win: " << CanIAlwaysWin(n); return 0; }
Constraints and Assumptions:
n is always positive > 0
n is an integer less than INT_MAX
This problem tests your use of basic human logics and understanding of "loop" and some if ... else conditional expressions in C++
You will implement your CanIAlwaysWin function which will be called from your console's main()
HINT: let's say n = 9, how many pebbles do you have to remove first to ensure your winning chance is 100%? Sure, you can remove 1 first, but that will leave you to win or lose depending on what your opponent's move is next. So removing 1 is not the key. How about removing 4 first? Will that guarantee your win?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started