mercredi 22 juin 2016

Trouble with Nim Game using C++

So when I compile this program I'm not getting any error or warning messages but when I run the program it doesn't get past the stage of announcing the initial amount of beans. Does anyone have advice? Here's my code, any help is greatly appreciated!

    //Comp 11
    //HW 3

    #include <iostream>
    #include <cstdlib>
    #include <time.h>
    using namespace std;

    int main ()

    {

    int guess, beans;
    bool player_1 = false;
    cout << "Welcome to Nim. I'll flip a coin. Enter 1 for heads and 0 for tails. n";
    cin >> guess;

    srand(time(NULL));
    int flip = rand()% 2;

    if ((guess == 0 && flip == 0) || (guess == 1 && flip == 1))
    { 
      player_1 = true;
      cout << "You go first. n";
    }
      else 
      {
        player_1 = false;
        cout << "I go first. n";
      }

    int i;
    for (i = 0; i < 3; i++)
    {
      cout << "Game " << i << ". n"; 

      beans = rand() % 25 + 5;
      cout << "There are " << beans << " beans. n";
      int beans_taken;
      int beans_comp_takes = 0;

      while (beans > 0);
      {
       if (player_1 == true)
       {
       do
       {
        cout << "Do you want to take 1, 2, or 3 beans? n";
        cin >> beans_taken;
      } while (beans_taken < 1 && beans_taken > 3);

       do
       {
        cout << "That's too many beans! Try again. How many beans do you 

want to take? n";
        cin >> beans_taken;
      } while ((beans_taken > beans) || (beans_taken >= 1 && beans_taken <= 3));

      beans -= beans_taken; //All of this was the person's part

      cout << "There are " << beans << "beans left. n"; //Now we have this many left

      while ((beans_comp_takes >= 1) && (beans_comp_takes < beans))
      {
        int beans_comp_takes = rand ()% 3;
        cout << "I will take " << beans_comp_takes << "beans. n";
        beans -= beans_comp_takes;
        cout << "There are" << beans << "beans left. n";
      }
       }

      if (player_1 == false)
      {
        while ((beans_comp_takes >= 1) && (beans_comp_takes < beans))
      {
        int beans_comp_takes = rand ()% 3;
        cout << "I will take " << beans_comp_takes << "beans. n";
        beans -= beans_comp_takes;
        cout << "There are" << beans << "beans left. n";
      }

       do
       {
        cout << "Do you want to take 1, 2, or 3 beans? n";
        cin >> beans_taken;
      } while (beans_taken < 1 && beans_taken > 3);

       do
       {
        cout << "That's too many beans! Try again. How many beans do you want to take? n";
        cin >> beans_taken;
      } while ((beans_taken > beans) || (beans_taken >= 1 && beans_taken <= 3));

      beans -= beans_taken; //All of this was the person's part

      cout << "There are " << beans << "beans left. n"; //Now we have this many left
      }
      }
    } 

  //SO NOW WE HAVE NO BEANS LEFT, how do we say who the winner is?
  int p_one_wins = 0; 

  if ((player_1 == true) && (beans == 0))
     cout << "Computer wins this round!! n";

  if ((player_1 == false) && (beans == 0))
    p_one_wins += 1;
    cout << "You win this round!! n";

  //Now three rounds have occurred, how do we determine winner?
  if (p_one_wins >= 2)
    cout << "Congratulations, you win! n";
  else
    cout << "Computer wins, better luck next time! n";

      return 0;
    }

Aucun commentaire:

Enregistrer un commentaire