Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

NIM game. A NIM game consists of n > 1 piles of match sticks. Two players take t

ID: 3767421 • Letter: N

Question

NIM game. A NIM game consists of n > 1 piles of match sticks. Two players take turns removing a pile or part of a pile (at least one stick) from these piles. Nobody can remove sticks from more than 1 pile in a turn. The player getting the last stick loses.

a)Prove that (1, n) with n >= 1 is a winning configuration; i.e. if you play with one pile of 1 stick and another pile of n sticks, you can play to win.

b)Prove that (2, 2) is a losing configuration. In other words, no matter how you play, your opponent wins if she / he knows how to play.

c)Prove that (2, n) with n unequal to 2 is a winning configuration (n = 1or n > 2).

d)Is (n, n) a winning or losing configuration for n > 2? Why?

e)Show in general if you have (m, n) where m and n can be equal or different, is this a winning or losing configuration.

f)Find out if (1, 2, 3) is a winning or losing configuration. The configuration means three piles of 1, 2, and 3 sticks respectively.

g)Find out if (2, 3, 4) is a winning or losing configuration.

h)Find out if (2, 2, 2) is a winning or losing configuration

Explanation / Answer

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main ( ){
    int num, sticks, comp_choice;
   

bool winner = false;

     cout << "Welcome to the game of NIM! Please enter an integer 9 or greater "
             << "to indicate the number of sticks used in this game." << endl;
        cin >> sticks;


   while(sticks < 9){
        cout << "This is not an acceptable value. "
             << "Please enter a number 9 or greater." << endl;
        cin >> sticks;
                    }
              
      while (!winner){

           
            if(sticks >= 9){
               cout << "There are " << sticks << " sticks in the pile. "
                    << "Please enter the amount(between 1 and 4) that you would like to remove from the pile." << endl;
               cin >> num;
                           }
           
            if((num >= 1) && (num <= 4)){
               cout << "You have removed " << num << " sticks from the pile." << endl;
           
               sticks -= num;
               cout << "There are " << sticks << " left in the pile." << endl;
                              }
              
                                    if((num < 1) || (num > 4)){
              cout << "This is not an acceptable value. "
                      << "Please enter an amount between 1 and 4." << endl;
                  cin >> num;
         //If the amount of sticks reaches 0 during the user's turn, the user will be declared winner.
         if(sticks == 0){
                  winner = true;
                  cout << "Yay!!! You won the game of NIM! Congratulations!" << endl;
                       }
                                  }
            
             else if (sticks - num < 0){
                cout << "Oops! There aren't enough sticks left to take this many from the pile. "
                     << "Please enter an acceptable amount." << endl;
                 cin >> num;
                                    }
       
        else{
          srand((unsigned)time(NULL));
          comp_choice = rand() % 4;
      
        sticks -= comp_choice;
                 cout << "The computer has chosen to take away " << sticks << " sticks from the pile. "
                      << "There are " << sticks << " sticks left in the pile." << endl;
              
        if(sticks == 0){
                  winner = true;
                  cout << "Awwww darn. The computer won. Better luck next time!" << endl;
                  }
             }
        }
       
       
      

       
  
system ("PAUSE");
return 0;
}