Thread: Snake And Ladder Multiplay

  1. #1
    Registered User Oat Potter's Avatar
    Join Date
    May 2016
    Posts
    4

    Snake And Ladder Multiplay

    Code:
    #include <stdio.h>
    #include<stdlib.h>
    #include<time.h>
    int i,n;
    int dice;
    int checkPosition();
    int choice;
    int player,newPosition[6], above[6],Position[6],win;
    
    
    
    
     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 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 64 exactly\n   If you go over 64 then your imaginary piece will be moved backwards for the remainder of your dice roll\n\n Good Luck\n\n");}
          if (choice==1,2){
    
    
          printf("How Many Player Enter 1-4\n");
          scanf("%d",&player);
          printf("\nWelcome to Snakes and Ladders.\n");
          printf("\n64  63  62  61  60  59  58  57         1=Start           27=Ladder to 37\n");
          printf("49  50  51  52  53  54  55  56         4=Ladder to 35    34=Snake  to 20\n");
          printf("48  47  46  45  44  43  42  41         7=Ladder to 23    42=Snake  to 11\n");
          printf("33  34  35  36  37  38  39  40         9=Snake  to 5     46=Ladder to 53\n");
          printf("32  31  30  29  28  27  26  25        14=Ladder to 43    49=Snake  to 32\n");
          printf("17  18  19  20  21  22  23  24        17=Snake  to 13    63=Snake  to 2\n");
          printf("16  15  14  13  12  11  10   9        21=Snake  to 3     64=End\n");
          printf(" 1   2   3   4   5   6   7   8        24=Ladder to 58\n");                       /*Prints the board for user's reference*/
    
    
     if(Position[i]<64){
    for(i=1;(i<=player)||(win>=1);i++){
          printf("\n\nPlease press enter to roll Player %d\n\n",i);
          dice = toupper( getche() );
          dice=65;
          printf("\nYou have rolled a %d.\n", dice);
    
    
    
    
         Position[i]+=dice;
          printf("\nYou on  %d.\n", Position[i]);
          checkPosition();
    
    
          if (Position[i]<newPosition[i])
             {printf("\nWell done, you have landed on a ladder. You are now on space %d.",newPosition[i]);}
    
    
          if (Position[i]>newPosition[i])
             {printf("\nUnlucky, you have landed on a snake. You are now on space %d.",newPosition[i]);}
    
    
          Position[i] = newPosition[i];
    } win=1;} }
         printf("Congratulations, you have won!!!");
    
    
    getch();
    return 0;
    }
    
    
    checkPosition()
    {
    
    
         switch(Position[i])
    {
             case 4:
                  return newPosition[i] = 35;
                  break;
    
    
             case 7:
                  return newPosition[i] = 23;
                  break;
    
    
             case 9:
                  return newPosition[i] =5;
                  break;
    
    
             case 14:
                  return newPosition[i] =43;
                  break;
    
    
             case 17:
                  return newPosition[i] =13;
                  break;
    
    
             case 21:
                  return newPosition[i]=3;
                  break;
    
    
             case 24:
                  return newPosition[i] =58;
                  break;
    
    
             case 27:
                  return newPosition[i] =37;
                  break;
    
    
             case 34:
                  return newPosition[i] =20;
                  break;
    
    
             case 42:
                  return newPosition[i] =11;
                  break;
    
    
             case 46:
                  return newPosition[i] =53;
                  break;
    
    
             case 49:
                  return newPosition[i] =32;
                  break;
    
    
             case 63:
                  return newPosition[i] =2;
                  break;
    
    
             default:
                     return newPosition[i] = Position[i];
    
    
    }
    
    
    }
    It have a problem about when player 1 win this program loop again can someone help me.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The for loop seems clear:
    Code:
    for(i=1;(i<=player)||(win>=1);i++)
    There are a couple of ways this loop ends, 1) the i reaches player + 1 with no one winning, or 2) win is equal to 2 or more.
    For loops continue until their condition is false.
    So the reason the loop continues is obvious: win being 1 is not enough because 1 >= 1 is still a true statement.

    Also the loop shouldn't really start at 1 if the i is going to be used with the player array. You can access player numbers 0 through player - 1, inclusive.

    I think the loop needs to be simpler. After all you want to play until you win, not just run through all the players one time, and then stop.
    Code:
    i = 0;
    while (win != 1) {
       // game here
       if (i == player) {
          i = 0;
       }
       ++i;
    }
    This will keep playing until someone wins, and it doesn't matter how many turns all the players take.

  3. #3
    Registered User Oat Potter's Avatar
    Join Date
    May 2016
    Posts
    4
    Thank you but while I run it I use this code it auto win or I make any mistake .Sorry I newbie C .
    Code:
    if(Position[i]>64){i = 1;
    while (win != 1) {
          printf("\n\nPlease press enter to roll Player %d\n\n",i);
          dice = toupper( getche() );
          dice=5;
          printf("\nYou have rolled a %d.\n", dice);
    
    
    
    
         Position[i]+=dice;
          printf("\nYou on  %d.\n", Position[i]);
          checkPosition();
    
    
          if (Position[i]<newPosition[i])
             {printf("\nWell done, you have landed on a ladder. You are now on space %d.",newPosition[i]);}
    
    
          if (Position[i]>newPosition[i])
             {printf("\nUnlucky, you have landed on a snake. You are now on space %d.",newPosition[i]);}
    
    
          Position[i] = newPosition[i];
          if (i == player) {
          i = 1;
           }
           i++;
    } win=1;} }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Better indentation would help.
    Pick a style, and stick to it rigorously.

    Code:
      if (Position[i] > 64) {
        i = 1;
        while (win != 1) {
          printf("\n\nPlease press enter to roll Player %d\n\n", i);
          dice = toupper(getche());
          dice = 5;
          printf("\nYou have rolled a %d.\n", dice);
    
          Position[i] += dice;
          printf("\nYou on  %d.\n", Position[i]);
          checkPosition();
    
          if (Position[i] < newPosition[i]) {
            printf("\nWell done, you have landed on a ladder. You are now on space %d.",
                   newPosition[i]);
          }
          if (Position[i] > newPosition[i]) {
            printf("\nUnlucky, you have landed on a snake. You are now on space %d.",
                   newPosition[i]);
          }
    
          Position[i] = newPosition[i];
          if (i == player) {
            i = 1;
          }
          i++;
        } //!! end of while (win != 1)
        win = 1;
      }
    Now look at your while loop.
    By careful inspection, you'll see that the win=1 statement is outside of the loop.
    Therefore, the loop will never exit, since the loop cannot ever assign win=1 to make the loop exit.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User Oat Potter's Avatar
    Join Date
    May 2016
    Posts
    4
    Snake And Ladder Multiplay-capture-20160523-090905-png
    Code:
    #include <stdio.h>#include<stdlib.h>
    #include<time.h>
    int i,n;
    int dice;
    int checkPosition();
    int choice;
    int player,newPosition[6], above[6],Position[6],win;
    
    
    
    
     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 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 64 exactly\n   If you go over 64 then your imaginary piece will be moved backwards for the remainder of your dice roll\n\n Good Luck\n\n");}
          if (choice==1,2){
    
    
          printf("How Many Player Enter 1-4\n");
          scanf("%d",&player);
          printf("\nWelcome to Snakes and Ladders.\n");
          printf("\n64  63  62  61  60  59  58  57         1=Start           27=Ladder to 37\n");
          printf("49  50  51  52  53  54  55  56         4=Ladder to 35    34=Snake  to 20\n");
          printf("48  47  46  45  44  43  42  41         7=Ladder to 23    42=Snake  to 11\n");
          printf("33  34  35  36  37  38  39  40         9=Snake  to 5     46=Ladder to 53\n");
          printf("32  31  30  29  28  27  26  25        14=Ladder to 43    49=Snake  to 32\n");
          printf("17  18  19  20  21  22  23  24        17=Snake  to 13    63=Snake  to 2\n");
          printf("16  15  14  13  12  11  10   9        21=Snake  to 3     64=End\n");
          printf(" 1   2   3   4   5   6   7   8        24=Ladder to 58\n");                       /*Prints the board for user's reference*/
    
    
     if (Position[i] < 64) {
      i = 1;
      while (win != 1) {
        printf("\n\nPlease press enter to roll Player %d\n\n", i);
        dice = toupper(getche());
        dice = 5;
        printf("\nYou have rolled a %d.\n", dice);
    
    
        Position[i] += dice;
        printf("\nYou on  %d.\n", Position[i]);
        checkPosition();
    
    
        if (Position[i] < newPosition[i]) {
          printf("\nWell done, you have landed on a ladder. You are now on space %d.",
                 newPosition[i]);
        }
        if (Position[i] > newPosition[i]) {
          printf("\nUnlucky, you have landed on a snake. You are now on space %d.",
                 newPosition[i]);
        }
    
    
        Position[i] = newPosition[i];
        if (i == player) {
          i = 0;
        }
        i++;
        if (Position[i] > 64){win = 1;} }
    
    
    
    
    
    
    }
         printf("Congratulations, you have won!!!");
    
    
    getch();
    return 0;
    }
    }
    
    
    checkPosition()
    {
    
    
         switch(Position[i])
    {
             case 4:
                  return newPosition[i] = 35;
                  break;
    
    
             case 7:
                  return newPosition[i] = 23;
                  break;
    
    
             case 9:
                  return newPosition[i] =5;
                  break;
    
    
             case 14:
                  return newPosition[i] =43;
                  break;
    
    
             case 17:
                  return newPosition[i] =13;
                  break;
    
    
             case 21:
                  return newPosition[i]=3;
                  break;
    
    
             case 24:
                  return newPosition[i] =58;
                  break;
    
    
             case 27:
                  return newPosition[i] =37;
                  break;
    
    
             case 34:
                  return newPosition[i] =20;
                  break;
    
    
             case 42:
                  return newPosition[i] =11;
                  break;
    
    
             case 46:
                  return newPosition[i] =53;
                  break;
    
    
             case 49:
                  return newPosition[i] =32;
                  break;
    
    
             case 63:
                  return newPosition[i] =2;
                  break;
    
    
             default:
                     return newPosition[i] = Position[i];
    
    
    }
    
    
    }
    Excuse me again Why T^T Thank you .

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The dice is always 5. Follow the code (in your head or with a debugger) for a bit and you should see why. The first time you should notice it happens right after you start the game for real.
    Code:
    Hardware watchpoint 2: dice
    
    
    Old value = 0
    New value = 13
    main () at snake.c:42
    42          dice = 5;
    (gdb) c
    Continuing.
    Hardware watchpoint 2: dice
    
    
    Old value = 13
    New value = 5
    main () at snake.c:43
    43          printf("\nYou have rolled a %d.\n", dice);
    (gdb)
    This causes the Position array to behave strangely. This is after about 3 rolls or so, and it just keeps growing by 5 each time:
    Code:
    You have rolled a 5.
    Hardware watchpoint 3: Position[i]
    
    
    Old value = 10
    New value = 15
    main () at snake.c:47
    47          printf("\nYou on  %d.\n", Position[i]);
    (gdb) c
    Continuing.
    
    
    You on  15.
    Hardware watchpoint 3: Position[i]
    
    
    Old value = 15
    New value = 0
    main () at snake.c:65
    65          i++;
    (gdb) c
    Continuing.
    [New Thread 9444.0xb48]
    Hardware watchpoint 3: Position[i]
    
    
    Old value = 0
    New value = 15
    main () at snake.c:66
    66          if (Position[i] > 64){win = 1;} }
    (gdb) p Position[i]
    $1 = 15
    (gdb)
    The problems with this are 1) rolling 5 every time should only be a stopgap measure, 2) you have no case 5 to handle it in your switch, so the default case is what actually happens, 3) the dice roll is always added to the position, so the game is always over after a dozen rolls.

    Honestly no matter what you do, it needs to be play tested extensively. But it would be a good start to actually use random numbers in your program. I would also note that you can seed rand with a constant number to get the same number series every time (for testing). And expect more problems like these to appear. Perhaps adding dice rolls to your position is not a smart thing to do?
    Last edited by whiteflags; 05-22-2016 at 08:58 PM.

  7. #7
    Registered User Oat Potter's Avatar
    Join Date
    May 2016
    Posts
    4
    Sorry didn't tell i random dice but this code I set it to 5 for test. Thank you it's ok now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Snake Programming: Let the Snake grow
    By Snake1 in forum C Programming
    Replies: 3
    Last Post: 11-24-2015, 05:40 PM
  2. Replies: 10
    Last Post: 05-14-2013, 07:49 PM
  3. Snake & Ladder OOAD
    By audinue in forum Tech Board
    Replies: 3
    Last Post: 06-05-2009, 04:57 AM
  4. Word Ladder
    By rocketman03 in forum C++ Programming
    Replies: 0
    Last Post: 04-25-2009, 11:38 PM
  5. Output of If-else ladder not functioning
    By SWAT_LtLen in forum C++ Programming
    Replies: 2
    Last Post: 06-14-2005, 05:41 AM

Tags for this Thread