1 Replies - 297 Views - Last Post: 07 February 2012 - 08:07 PM Rate Topic: -----

Topic Sponsor:

#1 Codemonkey00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 21-September 10

The game of nim

Posted 07 February 2012 - 07:39 PM

Good evening all i'm new to the c++ programming language and i'm having trouble with my while loops. i have my code pretty much figured out and i have a few more things to do to make this program work. Looping if the heap choice is not valid( not 1 2 or 3), looping if the amount is greater than the number within the heap, if the amount entered <=0. and for some reason i'm hitting an infinite loop on the second while loop. Can anyone help me understand what i did wrong to create this infinite loop?


#include<iostream>

#include<time.h>

using namespace std;

int main (){

    srand((unsigned int)time(0));

    int amount, a, b, c,ch,heap,current;

    char checkRow=' ';
	bool loose = false;
	bool legalvalue = true;

     a= rand()%100+10;

     b= rand()%100+10;

     c= rand()%100+10;

    cout<< "Welcome to the Game of Nim!\n";

    while (a>0 || b>0 || c>0)
    {
		
        cout<< "Heap 1  Heap 2  Heap 3\n"

            << "----------------------\n"

            <<  "   "<<a<<"    "<<b<<"     "<< c <<"\n";
		
		//need a loop here to check whether the user enters a heap choice correctly
		//{
		
	
			  cout<< "Which Heap will you choose?\n";

			  cin>>heap;
		    
			 	//while ( legalvalue = false) // this produces an infinate loop
	//  {
		   	if (heap == 1)
			{
				cout << "You have chosen Heap 1\n";
				heap = a;
					checkRow = 'a';
					legalvalue = true;
			}
			
			else if (heap ==2)
            {
				cout << "You have chosen Heap 2\n";
				heap = b;
				checkRow = 'b';
				legalvalue = true;
			}

			else if (heap == 3)
			{
				cout << " You have chosen Heap 3\n";
				heap = c;
				checkRow = 'c';
				legalvalue = true;
			}
			else 
			{
					cout << "You have entered an invalid heap choice please try again";
					legalvalue = false;
			}
	 //  }//end of while loop for heap choices
		
       
			cout<< "How many marbles will you draw?\n";

			cin>> amount;

			if (amount>heap/2)
			{

				cout<< "That is not a legal move.\n";
				legalvalue = false;

			}
			else if (amount <= 0)
			{
				legalvalue = false;
				cout<< "that is not a legal move.\n";
			}
			else if (heap == 1)
			{
				cout << "that is not a valid choice sir/madam!!!";
				legalvalue = false;
			}
	
			else
			{
				legalvalue = true;
			}           
			
			if (amount>0 && amount<=a/2)
			{
				current=heap-amount;

				if(checkRow=='a')
					a-=amount;
						
				else if (checkRow=='b')
				{
                      

						b-=amount;
				}

				else if (checkRow=='c') 
				{

				c-=amount;
				}

				cout<< "There are  "<<current<<"  marbles in this heap now.\n";

			}
			if (a == 0 && b == 0 && c == 0 )
			{
				bool loose = true;
				cout << " You have lost ha ha ha";
				system ("pause");
			}
			else 
			{
			cout << " the computer will now take his turn \n";

			}
			// computers turn....this spot needs help!!!!!!!// work on this tonight rawt 
		if (a > 0 || b > 0 || c > 0) 
		{
						cout << " Computer Chooses Random Heap\n";
						ch = rand ()% 3 + 1;//computers heap choice
						cout <<" Computer Chooses Heap Number  " << ch << "\n";
						if (ch ==1)
						{
					        if (a == 1)//in these statments ive found that the program will break if the computer decides to divide by zero
							{
								a --;
							
							amount = rand () %  a/2 + 1;
							cout << " The computer has taken   " << amount <<   " marbels from the pile \n";
							a -= amount;//if heap is 1 then take 1 away
							}
					
						
						
						}
						else if (ch ==2)
						{
							if (b == 1)
							{
								b--;
							}
							amount = rand () %  b/2 + 1;
					                cout << " The computer has taken   " << amount <<   " marbels from the pile \n";
							b -= amount;
							
						}
						else if (ch ==3)
						{
							if (c == 1)
							{
								c--;
							}
							amount = rand () %  c/2 + 1;
							cout << " The computer has taken   " << amount <<   " marbels from the pile \n";
							c -= amount;
						}

						if (a == 0 && b == 0 && c == 0 )
						{
							cout << " Computer Looses";
							 system ("pause");

						}


					
					
	//}


    }

    //cin.get();cin.get();

    //return 0;

}
}




Is This A Good Question/Topic? 0
  • +

Replies To: The game of nim

#2 r.stiltskin  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1812
  • View blog
  • Posts: 4,891
  • Joined: 27-December 05

Re: The game of nim

Posted 07 February 2012 - 08:07 PM

I don't see a second while loop, and when I run it there doesn't seem to be an infinite loop. Can you be more specific about your problem?

edit: Oh, I see it's commented out. I'll look again.

OK -- the problem there is that you're doing an assignment in
while(legalvalue = false)
instead of a logical test
while(legalvalue == false)

This post has been edited by r.stiltskin: 07 February 2012 - 08:13 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1