Thread: problem with looping

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    11

    problem with looping

    hi again,

    I was given a problem which goes like this.

    Token-Tug-O-Warrior is a new board game played by two competitors on
    a one-dimensional lattice structure. The home locations for
    player1 and player2 are located at the first and last lattice positions, respectively.

    //_________________________________________________
    During initialisation, a heart-shaped token placed at the centre
    location of the lattice structure. Each player then takes turns throwing
    a six-sided die and moving the token by the requisite number of positions in
    the direction of his/her home location. If a player throws a six (6)
    then he is rewarded with another throw. The winner of the game is the
    player who manages to “bring the bacon home”. One constraint on
    winning is that the boundaries of the lattice must be respected.
    That is, if a player needs a throw of three to enter the home location,
    and any number greater than this (excluding a six) is thrown, then
    that player must forfeit a turn.
    /_________________________________________________

    The problem I am having, is if the person throws a 6, it constantly goes to forfeit a turn.

    what i did is that below the line

    Code:
    die=GetRand(MIN,MAX);
    I placed
    Code:
    while(die == 6)
    {
        printf("Player 1:Please Press Enter to roll the dice");
    			getch();
    			printf("\n");
    			printf("You just rolled a %d ",die); //displays what you rolled
    			printf("\n");
    			player1=player1_move_spaces(game_board,die,player2); //calls function then assigns value to player1
    			printf("\n");
    			printf("\n");
    }
    I didnt include the above in my code.
    I dont how to correct the problem so if there is anyone who can lend a hand, i would greatly apprciate it.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Shouldn't you actually have something in there that rolls the dice? Otherwise, you're going to just sit in an infinite loop, because die never changes in your loop.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    11
    hi quzah;

    You were absolutely right it did continue to loop. So i decided to do my while loop like this and i dont understand why it keeps going to the forfeit section, i traced it through and to me it should have worked

    Code:
    die=GetRand(MIN,MAX);//represents the spinning of the dice
    while(die != 6)
    {
        printf("Player 1:Please Press Enter to roll the dice");
    			getch();
    			printf("\n");
    			printf("You just rolled a %d ",die); //displays what you rolled
    			printf("\n");
    			player1=player1_move_spaces(game_board,die,player2  ); //calls function then assigns value to player1
    			printf("\n");
    			printf("\n");
    if(die==6)
    
    {
        printf("Player 1:Please Press Enter to roll the dice");
    			getch();
    			printf("\n");
    			printf("You just rolled a %d ",die); //displays what you rolled
    			printf("\n");
    			player1=player1_move_spaces(game_board,die,player2  ); //calls function then assigns value to player1
    			printf("\n");
    			printf("\n");
    }
    }

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    11
    hi again,
    I ended the loop below the area for player 2 and i still dont understand why it keeps looping for player 1??

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Because you aren't paying attention still. You need to change the dice each time through the loop. Or something. I'm not even sure what it is you're trying to do. Again, inside the loop, the dice never changes. Therefore, it simply loops on the current dice number over and over and over. (forever)

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    This is where do-while comes in handy:
    Code:
    do
    {
      // Get a random number for the die roll
    
      // if die roll would take player past end of board, print forfeit message and 'break' out of do-while loop
    
      // print "you moved x spaces" message
    } while(die == 6);
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    11
    hi,
    i did the following for my loop

    Code:
    printf("Player one must play first"); //prompts player one to play first
    	do//first do
       {
    		do //second do
    		{
    			printf("\n"); //goes on the next line
    			printf("\n");
    			printf("\n");
    			if(player2!=24) //player 2 is not equal to 24, then you enter the while loop
    			{
    				//die=GetRand(MIN,MAX); //assigns the result of rolling the dice to die
    				printf("Player 1:Please Press Enter to roll the dice");
    				die=GetRand(MIN,MAX);
    				getch();
    				printf("\n");
    				printf("You just rolled a %d ",die); //displays what you rolled
    				printf("\n");
    				player1=player1_move_spaces(game_board,die,player2); //calls function then assigns value to player1
    				printf("\n");
    				printf("\n");
    
    				if(die==6)
    				{
    					do//third do
    					{
    						printf("Player 1:Please Press Enter to roll the dice");
    						die=GetRand(MIN,MAX);
    						getch();
    						printf("\n");
    						printf("You just rolled a %d ",die); //displays what you rolled
    						printf("\n");
    						player1=player1_move_spaces(game_board,die,player2); //calls function then assigns value to player1
    						printf("\n");
    												
    					}//end third do
    					while(die == 6);
    				}
    But Now, if a player spins a 6, they do not get another chance. It just prompts the user to spin the dice for player2..................

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're overcomplicating this whole issue. See how easy it really is?
    Code:
    while no winner
        dice = throwdice()
        moveplayer( dice, player )
        if( dice != six )
            player = otherplayer
    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  2. looping problem
    By chris285 in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2005, 11:03 AM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  5. Looping problem
    By sketchit in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 02:19 PM