Thread: Snakes and ladders board game

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    5

    Snakes and ladders board game

    Hi, I am extremely new to C programming but am trying to create a snakes and ladders game. This is what I have so far:

    Code:
    #include <stdio.h>
    #include<stdlib.h>
    #include<time.h>
    
    
    int dice;
    
    
    main()
    {
          
          printf ("Welcome 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*/
    
    
    
    
          printf("\nType and press enter to roll\n");
          scanf("%d", &dice);
          Dice();
          
          printf("You have rolled a %d", dice);
          
    getch();
    return 0;
    }
    
    
    Dice()
    {
          srand(time(NULL));
          dice=((rand()%6)+1);
          
    }
    As you an see, it's pretty basic. It just prints out a board and then comes up with a random number between 1 and 6 for the dice.

    I now need to find a way to add that dice roll number on to an existing number to tell the player where they have landed. If they land on one of the snakes or ladders then they will move to a different space. It will then roll again and this new roll will have to add onto their previous one.

    Can anybody help?
    Thanks

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    srand() should be used just ONCE in your program, way up near the top, (just after any variables are set).

    You don't need a dice() function, because a roll of the dice is just a single line of code. You should have a prototype for any function that follows after main(), posted before main().

    I'm not familiar with the game, so no help here on setting it up. Games need a big "gaming loop" to iterate around, while play is continuing.

    Set up the board, initialize everything you need, and then have a do while loop that goes around the play, (or a while loop)

    Code:
    int game =1;
    
    //everything to initialize for 1 game
    while(game) {
    
        //all the code to play one game in here
        if(game is over) {
            print who won, and congrats and set game=0
       }
    }
    //game is over.
    Last edited by Adak; 11-12-2012 at 01:41 PM.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    In addition to Adak's comments, here is one possible algorithm.

    - Declare a variable in "main()" that keeps track of the players "position."
    - Roll the die, and add it to the current position.
    - Print any applicable output based on the new position.
    - Then pass the new position value to another function, called perhaps "checkPosition()".
    - In that function, check to see if the new position causes a change in position (perhaps using a "switch()" statement). If so, return the modified position to "main()", updating the position variable again if needed.
    - Print any applicable output based on the adjusted position.
    - Repeat.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    5
    Cheers for your help guys, I'll see what I can do

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    5
    Ok, so I've got this, bear with me I know it's a bit messy right now:

    Code:
    #include <stdio.h>
    #include<stdlib.h>
    #include<time.h>
    
    
    int dice;
    
    
    main()
    {
          srand(time(NULL));
                
          printf ("Welcome 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*/
    
    
    
    
          printf("\nType and press enter to roll\n");
          scanf("%d", &dice);
          dice=((rand()%6)+1);
          
          printf("You have rolled a %d\n", dice);
          
          int x=0;
          int y; 
    
    
          x+=dice;
          
          printf("You are on space %d\n", x);
        
          checkPosition();
          if (x!=y)
          printf("You are now on space %d", x);
          
    getch();
    return 0;
    }
    
    
    checkPosition()
    {
          int x;
          int y;
          if (x=4){
             y=35;}
          else
              if (x=7){
                 y=23;}
          else
              if (x=9){
                 y=5;}
          else
              if (x=14){
                 y=43;}
          else
              if (x=17){
                 y=13;}
          else
              if (x=21){
                 y=3;}
          else
              if (x=24){
                 y=58;}
          else
              if (x=27){
                 y=37;}
          else
              if (x=34){
                 y=20;}
          else
              if (x=42){
                 y=11;}
          else
              if (x=46){
                 y=53;}
          else
              if (x=49){
                 y=32;}
          else
              if (x=64){
                 y=2;}
          else
              x=y;
    }
    But it doesn't work. When I get a 4 it doesn't change position to 35. Any help?

    Thanks

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Neaten it up first, before proceeding.

    - You need to declare your function before "main()" if you define it after.

    Code:
    // function declaration
    returnType functionName(dataType Variable);  // <--- note semicolon
    
    
    int main(void)
    {
        // ...
        return 0;
    }
    
    // function definition
    returnType functionName(dataType Variable)  // <--- note NO semicolon
    {
        // ...
    }
    - You're mixing up the assignment operator and the equality operator

    Code:
    if(x = 21)  // WRONG!
    
    if(x == 21)  // right!
    - The 'x' and 'y' variables in "main()" are different than the 'x' and 'y' variables in "checkPosition()." Read up on passing values to, and returning values from, functions (link) before going any further. What you have there is not correct.

    You are, however, following our advice very well, which is nice to see!

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Not
    if(x=4)

    Correct is
    if(x==4)

    Correct all the comparisons to =='s. One = is for assignment, but comparison is ==.

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    5
    Thanks again guys. I know i'm pretty awful at this so far but I am learning so thanks.

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    5
    So this is how I have gotten on:

    Code:
    
    #include <stdio.h>
    #include<stdlib.h>
    #include<time.h>
    
    
    int dice;
    int checkPosition();
    int Position, newPosition, above;
    int choice;
    
    
    
    
    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("\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*/
    
    
    
    
    
    
    do
    {
          printf("\n\nPlease press enter to roll\n\n");
          dice = toupper( getche() );
          dice=((rand()%6)+1);
          printf("\nYou have rolled a %d.\n", dice);
    
    
          Position+=dice;
          
          if(Position >64)
               {above = (Position-64);
               Position = (64 - above);}  
    
    
          
          printf("\nYou have landed on space %d.\n", Position);
    
    
          
          checkPosition();
          
          if (Position<newPosition)
             {printf("\nWell done, you have landed on a ladder. You are now on space %d.", newPosition);}
          
          if (Position>newPosition)
             {printf("\nUnlucky, you have landed on a snake. You are now on space %d.", newPosition);}
          
          Position = newPosition;
          
       
    } while(Position<64);
         
         printf("Congratulations, you have won!!!");
         }
               
    getch();
    return 0;
    }
    
    
    checkPosition()
    {    
         switch(Position)
    {
             case 4:
                  return newPosition = 35;
                  break;
                                 
             case 7:
                  return newPosition = 23;
                  break;
             
             case 9:
                  return newPosition =5;
                  break;
                  
             case 14:
                  return newPosition =43;
                  break;
                  
             case 17:
                  return newPosition =13;
                  break;
                  
             case 21:
                  return newPosition =3;
                  break;
                  
             case 24:
                  return newPosition =58;
                  break;
                  
             case 27:
                  return newPosition =37;
                  break;
                  
             case 34:
                  return newPosition =20;
                  break;
                  
             case 42:
                  return newPosition =11;
                  break;
                  
             case 46:
                  return newPosition =53;
                  break;
                  
             case 49:
                  return newPosition =32;
                  break;
                  
             case 63:
                  return newPosition =2;
                  break;
                  
             default:
                    return newPosition = Position;
                     }
    
    
    }
    It plays the game and finishes when you reach the end. I think that's it. Is there anything I could do better or a way of making it neater. Any feedback would be appreciated as I am trying to learn as well as having to make this programme.

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Board Game
    By Tiago in forum C Programming
    Replies: 4
    Last Post: 04-10-2010, 09:33 AM
  2. word ladders
    By AJOHNZ in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2009, 12:19 AM
  3. Snakes on a Plane
    By SlyMaelstrom in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 03-25-2006, 11:23 PM
  4. chutes and ladders
    By ckeener in forum C++ Programming
    Replies: 13
    Last Post: 03-17-2005, 01:45 PM
  5. Board Game
    By Govtcheez in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 08-17-2001, 12:29 PM

Tags for this Thread