move back from 100

I took the code from my mate and edit them.
when i roll number 6 on dice from 96 to 100 then it will move forward to 100 then back to 98 but it go to number 100 and end the game.
here the code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
 
int dice;
int checkPosition();
int Position, newPosition, above;
int choice;
 
int main()
{
      srand(time(NULL));
             
      printf("Would you like to:\n    1)Read the rules\n    2)Play the game\n\n");
      scanf("%d", &choice);
       
      if (choice==1){printf("\nThe rules are simple: \n   You will need press enter to roll the die\n   You are then told which space you have landed on\n   If you land on a snake you will move down the board\n   If you land on a ladder then you will move up it\n   In order to win you must land on 100 exactly\n   If you go over 100 then your imaginary piece will be moved backwards for the remainder of your dice roll\n\n Good Luck\n\n");}
      if (choice==2){
       
       
      printf("\nWelcome to Snakes and Ladders.\n");

	  printf("\n100 99 98 97 96 95 94 93 92 91	1=Start				27=Ladder to 37\n");
	  printf("	81 82 83 84 85 86 87 88 89 90		81=Snake to 63		54=Ladder to 71\n");
      printf("	80 79 78 77 76 75 74 73 72 71		68=Ladder to 84		92=Snake to 78\n");
      printf("	61 62 63 64 65 66 67 68 69 70		4=Ladder to 35		34=Snake  to 20\n");
      printf("	60 59 58 57 56 55 54 53 52 51		7=Ladder to 23		42=Snake  to 11\n");
      printf("	41 42 43 44 45 46 47 48 49 50		46=Ladder to 53		16=Snake to 5\n");
      printf("	40 39 38 37 36 35 34 33 32 31		14=Ladder to 43		49=Snake  to 32\n");
      printf("	21 22 23 24 25 26 27 28 29 30		17=Snake to 13		63=Snake  to 33\n");
      printf("	20 19 18 17 16 15 14 13 12 11		21=Snake to 3		60= Snake to 43\n"); 
      printf("	1  2  3  4  5  6  7  8  9  10		24=Ladder to 58		100=End\n");              
 
 
 
 
 
 
do
{
      printf("\n\nPlease press enter on your keyboard to roll\n\n");
      dice = toupper( getche() );
      dice=((rand()%6)+1);
      printf("\nYou have rolled a %d.\n", dice);
 
 
      Position+=dice;
       
      if(Position >101)
           {above = (Position-101);
           Position = (101 - above);}  
 
 
       
      printf("\nYou have landed on space %d.\n", Position);
 
 
       
      checkPosition();
       
      if (Position<newPosition)
         {printf("\nHow lucky, you have landed on a ladder. You are now on space %d.", newPosition);} //tell the user that the user has landed on ladders and will move to the new number
       
      if (Position>newPosition)
         {printf("\nHow unlucky, you have landed on a snake. You are now on space %d.", newPosition);} //tell the user that the user has landed on snakes and will move to the new number
       
      Position = newPosition;
       
    
} while(Position<100);
      
     printf("Congratulations, you have won!!!"); // if the user has reach to number 100 then the user win the game
     }
            
getch();
return 0;
}
 
Last edited on
Consider this: You are on 96 and roll 6. Your position is now 102
1
2
3
4
5
if(Position >101)
{
  above = (Position-101);
  Position = (101 - above);
}  
if(102 > 101) // True
{
  above = 102 - 101, // = 1
  Position = 101 - 1; // = 100
}


You loop stops when Position >= 100
So Position is 96, and you rolled a 6.

Position+=dice; Position is now 102
if(Position >101) 102 IS more than 101, so...
{above = (Position-101); above is 1
Position = (101 - above);} and Position is now 100.

Try this:

1
2
3
if(Position >100) // You don't care about being above 101 - you care about being above 100
{above = (Position-100);
  Position = (100 - above);}  


Topic archived. No new replies allowed.