130 views

Uploaded by Tripletsp3

NIM Game

- Water Resources In India
- Water Resources In India
- Composite Materials
- Gummy Bear Genetics
- How to Work With Code Blocks - Tutorial
- Hidden Figures: The American Dream and the Untold Story of the Black Women Mathematicians Who Helped Win the Space Race
- The Library Book
- Shoe Dog: A Memoir by the Creator of Nike
- Never Split the Difference: Negotiating As If Your Life Depended On It
- Maybe You Should Talk to Someone: A Therapist, HER Therapist, and Our Lives Revealed
- The Subtle Art of Not Giving a F*ck: A Counterintuitive Approach to Living a Good Life
- Sapiens: A Brief History of Humankind
- The Unwinding: An Inner History of the New America
- A Heartbreaking Work Of Staggering Genius: A Memoir Based on a True Story
- This Changes Everything: Capitalism vs. The Climate
- The Emperor of All Maladies: A Biography of Cancer
- Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future
- John Adams
- Grit: The Power of Passion and Perseverance
- Devil in the Grove: Thurgood Marshall, the Groveland Boys, and the Dawn of a New America

You are on page 1of 2

Exercise 02.05.02 Game of NIM: Accepts positive input only for initial total. Also, permits any "delta" -- max. amount to subtract each turn.

#include <iostream> using namespace std; int main() { int total, n; int delta = 0; // Max. number to subtract by each turn. int remainder = 0; cout << "Welcome to NIM. Pick a starting total: "; cin >> total; while (total < 1) { cout << "Enter positive integer only. " << endl; cout << "Pick starting total: "; cin >> total; } cout << "What should be the max. number to subtract? "; cin >> delta; while (delta < 1 || delta >= total) { cout << "Enter positive integer only. " << endl; cout << "Num must be less than starting total. " << endl; cout << "Pick max. number to subtract: "; cin >> delta; } while (true) { // Pick best response and print results. // Divide total by delta and use remainder // as the number to subtract by. remainder = total % (delta + 1); if (remainder > 0) { total = total - remainder; cout << "I am subtracting " << remainder << "." << endl; } else { total--; cout << "I am subtracting 1." << endl; } cout << "New total is " << total << endl; if (total == 0) { cout << "I win!" << endl; break; } // Get user s response; must be in range 1 to delta. cout << "Enter number to subtract, "; cout << "from 1 to " << delta << ": "; cin >> n; while (n < 1 || n > delta) { cout << "Input must be 1 to " << delta << ". " << endl; cout << "Re-enter: "; cin >> n;

if (total == 0) { cout << "You win!" << endl. cout << "New total is " << total << endl. return 0.} total = total . } .n. break. } } system("PAUSE").