none
need a flow chart for the code

    Question

  • #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    int main()
    {
    char again;
    int  stoneCount;
    int  userTurn(int stoneinPile)      // function prototype
    int  computerTurn(int stoneinPile); // function prototype
    bool gameOver = false;
    cout << "Welcome to Nim game\n";
    cout << "You may remove 1, 2, or 3 stones at a time.\n";
    cout << "Whoever removes the last stone loses\n";
    
    stoneCount = 16;
    while (! gameOver)
       {
       stoneCount = stoneCount - userTurn(stoneCount);
       if (stoneCount == 0)
          {
          cout << "You lost!\n";
          gameOver = true; // The player lost
          }
       else 
          stoneCount = stoneCount - computerTurn(stoneCount);
       if (! gameOver && stoneCount == 0) 
          {
          cout << "You won\n";
          gameOver = true; // The player won
          }
       } // game loop
    cout << "Bye Bye!\n";
    return 0;
    } // main
    
    
    int userTurn(int cnt)
    {
    bool  done = false;
    int temp;
    while (! done)
      {
      cout << "There are " << cnt << " stones in the pile. \n";
      cout << "How many stones would you like to remove? (1, 2, or 3) ";
      cin >> temp;   // Get the number entered by the user
      if (temp > cnt)
         {
         cout << "You cannot remove more stones than there are in the pile.\n";
         }
      else if (temp < 1 || temp > 3) // 1, 2, or 3 stones can be removed only
    	{
    	cout << "Please enter 1, 2, or 3.\n";
    	}
           else
               done = true;
      } // while
    return temp;
    } // userTurn
    
    int computerTurn(int cnt)
    {
    	int temp=myRandom(3);
    	do { temp = myRandom(3); } while (temp > cnt);
    	cout << "I remove " << temp << " stones from the pile\n";
    	return temp;
    } // computerTurn
    
    
    int myRandom(int upperLimit)
    {
    srand(time(0));
    return rand() % upperLimit + 1;
    } // myRandom
    Tuesday, March 08, 2016 12:47 AM

Answers