title: Programming a game of Nim
body: <p>The idea for this program is to play a game of Nim. We generate a random number for the pile. The computer is randomly set as smart, which uses an algorithm to take from the pile, or in normal mode which randomly takes from the pile. The who goes first is also decided at random. So I made three smaller functions that call the turn sequence in a while loop that continues until our stack is at 1. I tested running it, and I only get the output that shows whether the computer is in smart mode or not and whose turn it is. Before I put any more effort into this program I need to know where I went wrong. Why is it not working?</p>
  
  <p>Edit: The algorithm for smart mode is a power of two minus one, IE 2^4=16-1=15, but I didn't know how to work that out mathematically with pileSize so I just used a lot of if statements.</p>
  
  <pre><code>void Nim();
  int PlayerTurn(int);
  int ComputerTurn(int);
  int SmartComputer(int);
  
  int main()
  {
      srand(time(NULL));
      Nim();
      return 0;
  }
  
  int PlayerTurn(int pileSize)
  {
      int userInput = 0;
      bool flag = true;
  
      while(flag == true)
      {
          cout &lt;&lt; "There are " &lt;&lt; pileSize &lt;&lt; " in the pile" &lt;&lt; endl;
          cout &lt;&lt; "How many do you want to take? ";
          cin &gt;&gt; userInput;
          if (userInput &gt; 1 &amp;&amp; userInput &lt; (pileSize/2))
          {
              pileSize = pileSize - userInput;
              flag = false;
          }
          else
          {
              cout &lt;&lt; "Error, that's not a valid move." &lt;&lt; endl;
          }
      }
      return pileSize;
  }
  
  int ComputerTurn(int pileSize)
  {
      cout &lt;&lt; "The computer will take from the pile. " &lt;&lt; endl;
      pileSize = pileSize - rand() % (pileSize/2);
      return pileSize;
  }
  
  int SmartComputer(int pileSize)
  {
      cout &lt;&lt; "The computer will take from the pile. " &lt;&lt; endl;
      if (pileSize&gt;63)
      {
          pileSize = 63;
      }
      else if (pileSize&gt;31&amp;&amp;pileSize&lt;63)
      {
          pileSize = 31;
      }
      else if (pileSize&gt;15&amp;&amp;pileSize&lt;31)
      {
          pileSize = 15;
      }
      else if (pileSize&gt;7&amp;&amp;pileSize&lt;15)
      {
          pileSize = 7;
      }
      else if (pileSize&gt;3&amp;&amp;pileSize&lt;7)
      {
          pileSize = 3;
      }
      else
      {
          pileSize = pileSize - rand() % (pileSize/2);
      }
      return pileSize;
  }
  
  void Nim()
  {
      int pileSize = rand()% (100-10) + 10;
      bool smartOrStupid = rand() % 2;
      if (smartOrStupid == true)
      {
          cout &lt;&lt; "The computer is in smart mode." &lt;&lt; endl;
      }
  
      bool turn = rand() % 2;
      if (turn = true)
      {
          cout &lt;&lt; "The computer will got first. " &lt;&lt; endl;
      }
      else
      {
          cout &lt;&lt; "The player will go first. " &lt;&lt; endl;
      }
      while(pileSize!=1);
      {
          if (turn = true)
          {
              if (smartOrStupid = true)
              {
                  pileSize = SmartComputer(pileSize);
                  cout &lt;&lt; pileSize;
              }
              else
              {
                  pileSize = ComputerTurn(pileSize);
                  cout &lt;&lt; pileSize;
              }
          }
          else
          {
              pileSize = PlayerTurn(pileSize);
              cout &lt;&lt; pileSize;
          }
      }
  }
  </code></pre>