# Thread: problem with looping

1. ## 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. 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.

3. 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. 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. 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.

6. 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);```

7. 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. 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.

Popular pages Recent additions