GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Apr-2008, 10:14
slash24 slash24 is offline
New Member
 
Join Date: Apr 2008
Posts: 9
slash24 is on a distinguished road

need help with looping


Hello everyone..Infact i am developing a small game in C++.For now, i have to write codes such that there is a minimum of 2 players and maximum of 4 players.
Then, each player must select their color from values Red Blue Yellow and Green.
The way i have written my code is too lengthy
Im sure there is another way to do it with looping..But i dont know how..Can someone help me?
This is what i have done till now..
CPP / C++ / C Code:
if((num_players<2)) || (num_players>4)
{ cout<<"Number of players should be between 2 and 4";
  cout<<"Please try again";
  cin>>num_players;
 
switch(num_players)
{
  case 2: //if number of players is 2
  cout<<"Player 1, select your color: " ;
  cin<<p1color;
  switch(p1color) 
{case 'r':
 cout<<"player 1 is Red";
 selectred=true; //selectred is bool
 
 
case 'b':
//same as above
 
 
case 'y':
//same as above
 
 
case 'g':
//same as above
 
cout<<"Player 2, select your color: " ;
cin>>p2color;
switch(p2color)
{ case 'r':
  if(selectred==true)
{ cout<<"cannot select Red";
  cin>>p2color;
 switch(p2color)
{
 
}
 
}
 
 
 
}
  case 3:
 
//for num_players =3, i have written codes for player 1, player 2 and player 3
//i dont know how to do this without repetition
 
 
 case 4:
// same here..i had to write for player 1,2,3 and 4
 
 

as you can see it is tooo lengthy..it works perfectly..But i have to remove these repetitions..Can anyone help me please?
Last edited by admin II : 02-Apr-2008 at 04:28. Reason: Changed [CODE] to [CPP]
  #2  
Old 01-Apr-2008, 10:51
dabigmooish's Avatar
dabigmooish dabigmooish is offline
Member
 
Join Date: May 2004
Location: Baltimore (middle of Canton)
Posts: 165
dabigmooish will become famous soon enough

Re: need help with looping


There are a couple of things wrong with your code that I'm assuming are due too you typing to fast, since you said it compiles. It looks like you've got some nested switch statements you want to get rid of. Is that correct? Can you give us some more details. There are a million different ways to get rid of those switch statments. Here is something to get you started:

CPP / C++ / C Code:
int counter = 0;
char tempChar;
char playerColor[4];

while (counter < numberofPlayers)
{
	cout << "Player"<<counter+1 << "Please pick a color" << endl;
	cin >> tempChar;

	// validate that it's a valid color.
	// if it's a valid entry then
	// check that the color isn't used. 
	// Something like
	// if( counter = 0)
	// {   
	//      then its hasn't been used (first entry)
	// }
	// else
	// {
	//      check your array for that color 
	//      if it's in the array then it can't be picked 
        //      and a new color has to be chosen
        //      otherwise its an okay choice
	//  }
	playerColor[counter] = tempChar; //put the valid color in the array

	counter++; //increment your counter
}
__________________
"To argue with a person who has renounced the use of reason is like administering medicine to the dead."
-Thomas Paine
www.sullivan-county.com/deism.htm
  #3  
Old 01-Apr-2008, 11:22
slash24 slash24 is offline
New Member
 
Join Date: Apr 2008
Posts: 9
slash24 is on a distinguished road

Re: need help with looping


here is the code:
its a bit too lengthy that is why i didnt post it
CPP / C++ / C Code:
int num_players;
char p1color,p2color,p3color,p4color;
 
bool selectred,selectblue,selectyellow,selectgreen;
 
 
 
 
 
    cout<<"Select number of players: ";
    cin>>num_players;
 
    cout<<endl;
 
 
    if((num_players>=2) && (num_players<=4))
    { switch(num_players)
        { case 2: //if number of players is 2
          cout<<"Player 1,Choose your house: " <<endl;
          cout<<"R for Red" <<endl;
          cout<<"B for Blue" <<endl;
          cout<<"Y for Yellow" <<endl;
          cout<<"G for Green" <<endl;
          cout<<endl;
          cin>>p1color;
          switch(p1color)
             {case 'r':
               cout<<"Player 1 is initialised Red..." <<endl;
               selectred=true;
               break;
 
               case 'b':
               cout<<"Player 1 is initialised Blue..." <<endl;
               selectblue=true;
               break;
 
               case 'y':
               cout<<"Player 1 is initialised Yellow..." <<endl;
               selectyellow=true;
               break;
 
               case 'g':
               cout<<"Player 1 is initialised Green..." <<endl;
               selectgreen=true;
               break;
 
             }
          cout<<"Player 2,Choose your house: ";
          cout<<"R for Red" <<endl;
          cout<<"B for Blue" <<endl;
          cout<<"Y for Yellow" <<endl;
          cout<<"G for Green)" <<endl;
          cout<<endl;
          cin>>p2color;
          cout<<endl;
          switch(p2color)
          {  case 'r':
                  if(selectred==true)
                     { cout<<"Player 1 has already been initialised Red" <<endl;
                       cout<<"Please select another colour: " <<endl;
                       cout<<"B for Blue" <<endl;
                       cout<<"Y for Yellow" <<endl;
                       cout<<"G for Green" <<endl;
                       cout<<endl;
                       cin>>p2color;
                       cout<<endl;
                       switch(p2color)
                       {   case 'b':
                           cout<<"Player 2 is initialised Blue..." <<endl;
                           selectblue=true;
                           break;
 
                           case 'y':
                           cout<<"Player 2 is initialised Yellow..." <<endl;
                           selectyellow=true;
                           break;  
 
                           case 'g':
                           cout<<"Player 2 is initialised Green..." <<endl;
                           selectgreen=true;
                           break;
 
                       }
                     }
                  else
                      { cout<<"Player 2 is initialised Red..." <<endl;
                        selectred=true;
                      }      
 
             break;
 
             case 'b':
                  if(selectblue==true)
                     { cout<<"Player 1 has already been initialised Blue" <<endl;
                       cout<<"Please select another colour: " <<endl;
                       cout<<"R for Red" <<endl;
                       cout<<"Y for Yellow" <<endl;
                       cout<<"G for Green" <<endl;
                       cout<<endl;
                       cin>>p2color;
                       cout<<endl;
                       switch(p2color)
                       {   case 'r':
                           cout<<"Player 2 is initialised Red..." <<endl;
                           selectblue=true;
                           break;
 
                           case 'y':
                           cout<<"Player 2 is initialised Yellow..." <<endl;
                           selectyellow=true;
                           break;  
 
                           case 'g':
                           cout<<"Player 2 is initialised Green..." <<endl;
                           selectgreen=true;
                           break;
 
                       }
                     }
                  else
                      { cout<<"Player 2 is initialised Blue..." <<endl;
                        selectblue=true;
                      }      
 
             break;
 
             case 'y':
                  if(selectyellow==true)
                     { cout<<"Player 1 has already been initialised Yellow" <<endl;
                       cout<<"Please select another colour: " <<endl;
                       cout<<"R for Red" <<endl;
                       cout<<"B for Blue" <<endl;
                       cout<<"G for Green" <<endl;
                       cout<<endl;
                       cin>>p2color;
                       cout<<endl;
                       switch(p2color)
                       {   case 'r':
                           cout<<"Player 2 is initialised Red..." <<endl;
                           selectred=true;
                           break;
 
                           case 'b':
                           cout<<"Player 2 is initialised Blue..." <<endl;
                           selectblue=true;
                           break;  
 
                           case 'g':
                           cout<<"Player 2 is initialised Green..." <<endl;
                           selectgreen=true;
                           break;
 
                       }
                     }
                  else 
             {cout<<"Player 2 is initialised Yellow..." <<endl;
             selectyellow=true;
             }
             break;
 
          case 'g':
          if(selectgreen==true)
                     { cout<<"Player 1 has already been initialised Green" <<endl;
                       cout<<"Please select another colour: " <<endl;
                       cout<<"R for Red" <<endl;
                       cout<<"B for Blue" <<endl;
                       cout<<"G for Yellow" <<endl;
                       cout<<endl;
                       cin>>p2color;
                       cout<<endl;
                       switch(p2color)
                       {   case 'r':
                           cout<<"Player 2 is initialised Red..." <<endl;
                           break;
 
                           case 'b':
                           cout<<"Player 2 is initialised Blue..." <<endl;
                           break;  
 
                           case 'y':
                           cout<<"Player 2 is initialised Yellow..." <<endl;
                           break;
 
                       }
                     }
          else 
              {cout<<"Player 2 is initialised Green..." <<endl;
               selectgreen=true;
              }
             break;
 
          }
    break;

as you can see i have written code when num_players is 2 that is code for
player 1 and player 2..
when the num_players is 3, i rewrite these codes and then include code
for player 3 and same when num_players is 4..

i have just posted code when num_players is 2..hope i have been able to explain what i meant to say...
Last edited by admin II : 02-Apr-2008 at 04:29. Reason: Changed [CODE] to [CPP]
  #4  
Old 01-Apr-2008, 11:57
dabigmooish's Avatar
dabigmooish dabigmooish is offline
Member
 
Join Date: May 2004
Location: Baltimore (middle of Canton)
Posts: 165
dabigmooish will become famous soon enough

Re: need help with looping


Yeah, I think I got what your asking. Did you read all of my post? Does my code make sense? You could also use functions and pointers. Something like this:

CPP / C++ / C Code:
int main()
{
	...
	char p1Color, p2Color, p3Color, p4Color;
	int r = 0,b = 0,y = 0,g =0;
	
	switch(num_players)
	{
		case 2:
			getColor(1, &p1Color, &r, &b, &y, &g);
			getColor(2, &p2Color, &r, &b, &y, &g);

		break;

		case 3:
			getColor(1, &p1Color, &r, &b, &y, &g);
			getColor(2, &p2Color, &r, &b, &y, &g);
			getColor(3, &p3Color, &r, &b, &y, &g);
		
		break;

		case 4:
			getColor(1, &p1Color, &r, &b, &y, &g);
			getColor(2, &p2Color, &r, &b, &y, &g);
			getColor(3, &p3Color, &r, &b, &y, &g);
			getColor(4, &p4Color, &r, &b, &y, &g);
		break;

		default:
			//display an error message
	}


	...

}


void getColor(int num, char *color, int *r,int *b,int *y,int *g)
{

	cout <<" Player" << num << " enter choice"
	// get the choice compare it to your r,b,y,and g values (0 = not used, 1 = used
	// if it's not used, change the r,b,y, or g to a 1 and put the color into 
	// the color pointer.  If it's used prompt for another color

}

__________________
"To argue with a person who has renounced the use of reason is like administering medicine to the dead."
-Thomas Paine
www.sullivan-county.com/deism.htm
  #5  
Old 01-Apr-2008, 12:04
slash24 slash24 is offline
New Member
 
Join Date: Apr 2008
Posts: 9
slash24 is on a distinguished road

Re: need help with looping


thanks...that was of great help..But is there a way without using pointers??pointers are confusing to me...
  #6  
Old 02-Apr-2008, 05:59
dabigmooish's Avatar
dabigmooish dabigmooish is offline
Member
 
Join Date: May 2004
Location: Baltimore (middle of Canton)
Posts: 165
dabigmooish will become famous soon enough

Re: need help with looping


My first example is without pointers. It's a good idea to get used to working with pointers. They're the bread and butter of c++. This page has a pretty good introduction to pointers. On this one you can get into the nitty gritty (how pointers operate on memory, how memory is stored, etc). This is also a good page on pointers. Anyway, give my suggestions a try and if you run into any problems, post them in this thread and I'll help you out.
__________________
"To argue with a person who has renounced the use of reason is like administering medicine to the dead."
-Thomas Paine
www.sullivan-county.com/deism.htm
  #7  
Old 03-Apr-2008, 06:38
slash24 slash24 is offline
New Member
 
Join Date: Apr 2008
Posts: 9
slash24 is on a distinguished road

Re: need help with looping


Thanks..I have tried your example without pointers and it worked!!
i have another question:
for instance if i have:

Roll dice
Display result
if condition A is true.....line 1
{ if number on dice is 6
{move pawn to position 0
Decrement pawns in base
Roll dice again
Display result
Then perform line again
}
}
how do i do this with looping?
  #8  
Old 03-Apr-2008, 07:30
dabigmooish's Avatar
dabigmooish dabigmooish is offline
Member
 
Join Date: May 2004
Location: Baltimore (middle of Canton)
Posts: 165
dabigmooish will become famous soon enough

Re: need help with looping


You're going to have to be more specific. Also give it shot and post the code that is giving you trouble. I'm willing to help you, but I won't write all your code for you. Don't forget to enclose your code in [c] and [ \c++] code tags. There is a little c++ button on the format tool bar when you submit a reply. Pressing that will add the code tags to your post. It makes your post much easier to read
__________________
"To argue with a person who has renounced the use of reason is like administering medicine to the dead."
-Thomas Paine
www.sullivan-county.com/deism.htm
 
 

Recent GIDBlogFlickr uploads of IA pictures by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Looping with enum Spidy08 C++ Forum 5 02-Oct-2007 09:44
Need help with Looping Harryt123 C Programming Language 4 21-May-2006 14:45
Sample looping (do/while) Sabin044 C++ Forum 8 04-Feb-2006 11:39
ask looping likeit C Programming Language 4 26-Jul-2005 19:49
for looping prob Rosmayati C Programming Language 1 30-Jun-2004 11:39

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:50.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.