Thread: my lost loop

  1. #1
    Registered User sentienttoaster's Avatar
    Join Date
    Nov 2002
    Posts
    79

    my lost loop

    Ok, this might look familiar. I posted this code awhile ago and I was told I had an infinate loop in it. After some time of trying to revise the code, I cannot fix the problem on my own. The problem should be in the randomnumber function. I hope you can help me.

    Code:
    /*Epsilon Omega Gamma*/
    #ifndef _epsilonomegagamma_h
    #define _epsilonomegagamma_h
    
    
    
    void dealcomp(int number);
    void dealme(int number);
    bool showcomp(bool isgameover);
    void showme();
    void blackjack();
    int randnumber();
    int randnumber1;
    void newround();
    bool compturn();
    bool myturn();
    short comphand[11];
    int comphandsize, comphandvalue, compace;
    int myhand[11], myhandsize, myhandvalue, myace, cardsleft;
    int i;
    char hiddencard;
    int start();
    int number;
    bool continuegame1;
    bool continuegame2;
    void start1();
    bool isgameover;
    char newgame;
    
    
    
    
    void blackjack()
    {
    	cardsleft=52;
    	hiddencard=2;
    	for (i=0; i<11; i++)
    	{
    		comphand[i]=0;
    		myhand[i]=0;
    	}
    }
    int randnumber()
    {
    	static bool card[52];
    	if(cardsleft==52)
    	{
    		for(i=0; i<52; i++)
    		{
    			card[i]=1;
    		}
    	}
    	randnumber1=rand()%52;
    
    	cardsleft--;
    	card[randnumber1]=false;
    	return (randnumber1%13)+1;
    }
    void newround()
    {
    	bool continue1=true, continue2=true, over=false;
    	compace=0, comphand[11]=0, myace=0, myhand[11]=0;
    	for(i=0; i<11; i++)
    	{
    		comphand[i]=0;
    		myhand[i]=0;
    	}
    	cardsleft=52;
    	dealcomp(2);
    	showcomp(false);
    	dealme(2);
    	showme();
    	if(comphand[11]==21||myhand[11]==21)
    	{
    		continuegame1=false, continuegame2=false;
    	}
    	while((continuegame1||continuegame2) && comphand[11] < 22 && myhand[11] <22)
    	{
    		continuegame1=compturn();
    		if(continuegame1||continuegame2)
    		{
    			continuegame2=myturn();
    		}
    	}
    	cout<<endl;
    }
    void dealcomp(int number)
    {
    	for( ; number; number--)
    	{
    		int newcard = randnumber();
    		comphand[comphandsize] = newcard;
    		comphandsize++;
    		if(newcard == 1 && comphandvalue <=10)
    		{
    			compace++;
    			comphandvalue +=11;
    		}
    		else if(newcard > 10)
    		{
    			comphandvalue +=10;
    		}
    		else
    		{
    			comphandvalue += newcard;
    		}
    		if(comphandvalue > 21 && compace)
    		{
    			compace--;
    			comphandvalue -=10;
    		}
    	}
    }
    void dealme(int number)
    {
    	for( ; number; number--)
    	{
    		int newcard=randnumber();
    		myhand[myhandsize]=newcard;
    		myhandsize++;
    		if(newcard == 1 && myhandvalue <=10)
    		{
    			myace++;
    			myhandvalue +=11;
    		}
    		else if(newcard >10)
    		{
    			myhandvalue +=10;
    		}
    		else
    		{
    			myhandvalue +=newcard;
    		}
    		if(myhandvalue >21 && myace)
    		{
    			myace--;
    			myhandvalue -=10;
    		}
    	}
    }
    bool compturn()
    {
    	if(comphandvalue < 17 && compace || comphandvalue < 17)
    	{
    		cout<<"I will take a card."<<endl;
    		dealcomp(1);
    		showcomp(false);
    		return true;
    	}
    	cout<<"I'm not taking a card."<<endl;
    	return false;
    }
    bool myturn()
    {
    	char addcard;//='Q';
    	cout<<"Do you want a card? y = yes, n = no."<<endl;
    	cin>>addcard;
    	while(1)
    	{
    		switch(addcard)
    		{
    		case 'y':
    		case 'Y': cout<<"You take a card: "<<endl;
    			dealme(1);
    			return true;
    		case 'n':
    		case 'N': return false;
    		default: cout<<endl<<"Huh? Do you want another card or not. y = yes, n = no."<<endl<<" >"<<endl;
    			cin>>addcard;
    			break;
    		}
    	}
    }
    bool showcomp(bool isgameover)
    {
    	cout<<"My hand is: ";
    	if(!isgameover)
    	{
    		cout<<hiddencard<<" ";
    	}
    	for( int i = !isgameover; comphand[i] !=0; i++)
    	{
    		switch (comphand[i])
    		{
    			case 1: cout<<"A ";
    				break;
    			case 11: cout<<"J ";
    				break;
    			case 12: cout<<"Q ";
    				break;
    			case 13: cout<<"K ";
    				break;
    			default: cout<<comphand[i]<<" ";
    				break;
    		}
    	}
    	cout<<endl;
    	return isgameover;
    }
    void showme()
    {
    	cout<<"Your hand: ";
    	for( int i =0; myhand[i] !=0; i++)
    	{
    		switch (myhand[i])
    		{
    		case 1: cout<<" A ";
    			break;
    		case 11: cout<<" J ";
    			break;
    		case 12: cout<<" Q ";
    			break;
    		case 13: cout<<" K ";
    			break;
    		default: cout<<myhand[i]<<" ";
    			break;
    		}
    	}
    	cout<<endl;
    }
    void start1()
    {
    	blackjack();
    	randnumber();
    	newround();
    
    
    	compturn();
    	myturn();
    	showcomp(isgameover);
    	showme();
    }
    
    
    int Start()
    {
    	srand(time(0));
    	cout<<"\nBlackjack. Wanna play? y = yes, n = no.\n>";
    	cin>>newgame;
    	cout<<endl;
    	while((newgame =='y') || (newgame =='Y'))
    	{
    		switch(newgame)
    		{
    		case 'y': start1();
    		case 'Y': start1();
    			break;
    		case 'n': break;
    		case 'N': break;
    		default: cout<<endl<<"Huh? Do you wanna play Blackjack? y = yes, n = no"<<endl<<">";
    			cin>>newgame;
    			break;
    		}
    	}
    	cout<<"Have a nice day!"<<endl;
    	return 0;
    }
    #endif
    This has been a public service announcement from GOD.

    111 1111

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Initialize variable cardsleft to 52;

    cardsleft = 52;

    Kuphryn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  3. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  4. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM