I have an assignment to make a text-based snakes and ladders game using Dev-C++. I have so far made a splash screen and a menu (with colours and sounds, pretty cool) using the winmm.a library and the switch/case construct. I have even been able to get player names with a simple character array. However, I have no idea how to instruct the system on player positions, dice spinning and player movements. I’ve been told to use random numbers for the dice, but I don’t know anything about that. I need to be able to initialize player_position for at least 2 players, have them roll the dice, move the players to a new location and determine whether there’s a snake or ladder there, then move them to a new location. I’ve been going through Daniweb for the past week looking for tips and I found some, but not enough. I need help, please, this assignment is due in 5days.
I don’t want it to be done for me, I just want to be pointed in the right direction so that I can do it myself. My friends have no problems with cheating and I figure the best way to change their minds is by setting an example. Oh yes, is there any way I can make it full-screen?
Don’t laugh, but here’s the first thing I tried in order to get the board set up:

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int board[101];
    int snake;
    int ladder;
    snake = board[5],[15],[25],[30],[45],[53],[62],[78],[81],[93],[96];
    ladder = board[7],[12],[28],[35],[40],[59],[65],[73],[88]
    cout >> snake;
        
    system("PAUSE");
    return EXIT_SUCCESS;
}

i'm sure you can see from here that i'm trying to set positions for snakes and ladders on the board so that whatever random number pops up from the dice, locations will already by marked.
Thanks.

I have an assignment to make a text-based snakes and ladders game using Dev-C++. I have so far made a splash screen and a menu (with colours and sounds, pretty cool) using the winmm.a library and the switch/case construct. I have even been able to get player names with a simple character array. However, I have no idea how to instruct the system on player positions, dice spinning and player movements. I’ve been told to use random numbers for the dice, but I don’t know anything about that. I need to be able to initialize player_position for at least 2 players, have them roll the dice, move the players to a new location and determine whether there’s a snake or ladder there, then move them to a new location. I’ve been going through Daniweb for the past week looking for tips and I found some, but not enough. I need help, please, this assignment is due in 5days.
I don’t want it to be done for me, I just want to be pointed in the right direction so that I can do it myself. My friends have no problems with cheating and I figure the best way to change their minds is by setting an example. Oh yes, is there any way I can make it full-screen?
Don’t laugh, but here’s the first thing I tried in order to get the board set up:

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int board[101];
    int snake;
    int ladder;
    snake = board[5],[15],[25],[30],[45],[53],[62],[78],[81],[93],[96];
    ladder = board[7],[12],[28],[35],[40],[59],[65],[73],[88]
    cout >> snake;
        
    system("PAUSE");
    return EXIT_SUCCESS;
}

i'm sure you can see from here that i'm trying to set positions for snakes and ladders on the board so that whatever random number pops up from the dice, locations will already by marked.
Thanks.

Lines 11 and 12 are not going to work. snake and ladder are both integers, so you can only store one integer in them. To make these lines legal, you could do something like this:

snake = board[5];
snake = board[15];
snake = board[25];
// etc.
ladder = board[7];
ladder = board[12];
ladder = board[28];
// etc.

This probably will not do what you want, but it is legal C++ code.

Line 13: cout uses the << operator, not the >> operator. cin uses the >> operator.

i'm sure you can see from here that i'm trying to set positions for snakes and ladders on the board so that whatever random number pops up from the dice, locations will already by marked.
Thanks.

I see the words "snakes" and "ladders", which are plural, but you have snake and ladder declared as single integers. Should snake and ladder be integer arrays? I assume you want a loop somewhere in this code, but I'm not exactly sure where. What do ladder and snake represent and what does board represent?

Regarding random number generation of dice roll, generally you'll want to use srand and rand:
http://www.cplusplus.com/reference/clibrary/cstdlib/srand.html
http://www.cplusplus.com/reference/clibrary/cstdlib/rand.html

srand seeds the random number generator. Call it once at the top of your program. Generally you seed the random number generator with the system clock, so you'll need the ctime (same as time.h) library.
http://www.cplusplus.com/reference/clibrary/ctime/

The following program will give ten random rolls of a six-sided die:

#include <iostream>
#include <ctime>
using namespace std;

int main ()
{
    srand (time(NULL));
    for (int i = 1; i <= 10; i++)
    {
        int diceroll = rand () % 6 + 1;
        cout << "Dice roll " << i << ": " << diceroll << endl;
    }
  
    return 0;
}

I need to be able to initialize player_position for at least 2 players, have them roll the dice, move the players to a new location and determine whether there’s a snake or ladder there, then move them to a new location.

int board[101];
    int snake;
    int ladder;
    snake = board[5],[15],[25],[30],[45],[53],[62],[78],[81],[93],[96];
    ladder = board[7],[12],[28],[35],[40],[59],[65],[73],[88]

i'm sure you can see from here that i'm trying to set positions for snakes and ladders on the board so that whatever random number pops up from the dice, locations will already by marked.
Thanks.

What you have done here isn't all that bad. You initialized an integer array with 101 elements, called board. Good. What you could do then is define a "snake" square to be 1, a "ladder" square to be 2, and a regular square to be 0 (or whatever integers you so desire). Clarification:

#define snake 1 //put this at the top of your project, with the includes

board[5] = snake;     //body of your program

Then, there are a couple things you could do to keep track of the players. One idea would be to associate the player with his/her respective board element. That is, create a "playerposn" variable. For example, player 1 starts at 0 (i.e. board[0]). So playerposn = 0. If they role a six, you increment playerposn by 6...now player 1 is at position 6 (i.e. board[6]). Then on each role you simply check if they are on a ladder/snake/regular square, and move them accordingly. To do this check, you would use a conditional statement to see if board[playerposn] is equal to 1 (since we defined the snake squares to be 1), 2 (a ladder square), or 0 (regular square). rinse and repeat (so to speak). Hopefully I didn't give too much away... I left most of the actual coding for you (because you wanted to figure it out).

And again, thats just one idea, there are tons of other ways to do it.

How do you increment playerposn by the dice value? I assume I’ll have to somehow associate every random number generated to a specific variable, which I’ll then add to player position, right? Also, how do I get it to recognise “press space to continue, any other key to exit”. I mean, what stands for space bar in syntax?

#include <iostream>
#include <cstdlib>
#include <windows.h>
#include <ctime>
#include <time.h>
using namespace std;

int main()
{
    int playerposn = 0;
    int roll;
    int dice_value;
    srand (time(NULL));
    cout << "You rolled a " << 1 + rand()% (6-1+1) << endl;
    playerposn = ++dice_value;
    cout << "You landed on tile " << playerposn << endl;
    Sleep(3000);
    for playerposn == snake;
    cout << "You stepped on a snake!!" << endl;
    playerposn = --10;
    cout >> "You dropped down to tile " << playerposn << endl;    
    {   cout << "Press '1' to roll again, any other key to exit..." << endl;
    getch() roll;
    if (roll == 1){
             main;
                  }
                  else {
         cout << "Better luck next time!!";
         break;
                         }
    }
    cout << "You rolled a " << 1 + rand()% (6-1+1) << endl;
    playerposn = ++dice_value;
    cout << "You landed on tile " << playerposn << endl;
    }
   	cout << "Press '1' to roll again, any other key to exit..." << endl;
    getch(roll);
    if (roll == 1){
             main;
                  }
                  else {
         cout << "Better luck next time!!";
         }
         return 0;
}

This shows the general idea that I want to use. I'm afraid I don't understand about setting the snake and ladder value. Anyway, the general idea is that "snakes and ladders" is a board game with 100 tiles. When a player falls on a snake head, he/she is taken down several tiles. When she lands at the bottom of a ladder, she goes up. If she reaches the 100th tile, the game is won. Which reminds me; I need the system to check on whether the new player position after rolling the dice is the 100th tile or not. I finally managed to get the dice to give different values on each roll (why that was so hard, i cant imagine) but I'm still stuck! Please help, especially with the location of snakes and ladders on the board because I still don't get that part. Also, my loop to return to the main function after the player agrees to roll the dice again doesn't seem to work.

And I've only got 3 more days to submission!

How do you increment playerposn by the dice value?

playerposn += dice_value;

I assume I’ll have to somehow associate every random number generated to a specific variable, which I’ll then add to player position, right?

Everytime you generate a random number, you should store it in your dice_value variable. Then you just use this value to increment the player position. If you have multiple players, you will need to keep track of who's turn it is, and increment the appropriate player position. That is, if it is player 1's turn, increment playerposn1, if player 2, then increment playerposn2, etc (or you could make an array of playerposns to keep things nice and neat). In any case, get one user working before you make multiple ones...

Also, how do I get it to recognise “press space to continue, any other key to exit”. I mean, what stands for space bar in syntax?

A keypress is an event that you will need to fire. There are likely several threads already on this forumn about this topic. If you can't find any, then search for kewords C++, Win32, and Keycode/Keypress/Keydown/Keyup on google.

However, due to the fact that you are fairly new to programming, I would instead suggest you just read in from the console, and have something like "Type c to continue, e to exit". Then just read in the value, do a simple error check, if it is a c, continue, if it is e, then exit. This will be much more manageable for you...

I'm afraid I don't understand about setting the snake and ladder value.

I'll try to explain again...

You can declare an integer array to simulate the boardgame "tiles":

int board[100];

Then, you can initialize the various elements in the array (which simulate tiles on the board) to be either a snake square, a ladder square, or a regular square. For example, you can associate a snake square with a value of -1, a regular square with a value of 0, and a ladder square with a value of 1.

So for example, let's say we wanted to make the first square a regular square, the second square a snake square, and the third square a ladder square. Then we code:

board[0] = 0;     //notice that the first index of an array is at 0, not 1
board[1] = 1;
board[2] = -1;

Of course, you have 100 tiles, so you will need to initialize the board with many more calls. I suggest creating a separate method to do this for you (to make your code look cleaner).

Now the playerposn variable, which you defined earlier, can be used to index into the array. So for example, at the start of the game, playerposn = 0. So to access the square the player is on, you could type:

board[0];

But, since playerposn = 0, we could use this as a general accessor:

board[playerposn];

Thus, when you increase or decrease the player position, you can just check whether or not the square the player is on contains a snake, a ladder, or neither. For example, we started at square 0 (playerposn = 0). Now let's say that we roll a 2 (the random number generator spits out 2). We increase the player position by 2, so now playerposn = 2. To check if the new square is a snake, we type:

if (board[playerposn] == -1)
{
      //Uh-oh...a snake...put your snake code here...i.e. decrease the player position
}

Now suppose that playerposn = 0, and instead we rolled a 1. We can check if the player has landed on a ladder square like this:

if (board[playerposn] == 1)
{
       //Hurray...a ladder!  put your ladder code here, i.e. increase the player position
}

Note that, if neither of these are true, then the player must currently be on a regular square...Hopefully you can figure out what to do in that case...

I need the system to check on whether the new player position after rolling the dice is the 100th tile or not.

Well, playerposn holds the position of the player, in terms of which tile he/she is on. That is, if playerposn = 0, then the player is on square 1... If playerposn = 2, the player is on square 3...If playerposn = 50, the player is on square 51...So how do you think you can find out of the player is on or beyond the 100th tile?

Please help, especially with the location of snakes and ladders on the board because I still don't get that part.

See above...hopefully it was some help...

Also, my loop to return to the main function after the player agrees to roll the dice again doesn't seem to work.

Your loop is not properly set up...the way your program flows should look something like this:

int main()
{
       //make the necessary calls to initialize all variables, the board, and
       //the random number generator...

       while (TRUE)
       {
               //do all your game stuff here
               //One way to stop the loop is if the user enters "e" (like I expain
               //above), use the "break" command, e.g.:
       }

       return 0;
}

Also, how do I get it to recognise “press space to continue, any other key to exit”. I mean, what stands for space bar in syntax?

A keypress is an event that you will need to fire. There are likely several threads already on this forumn about this topic. If you can't find any, then search for kewords C++, Win32, and Keycode/Keypress/Keydown/Keyup on google.

However, due to the fact that you are fairly new to programming, I would instead suggest you just read in from the console, and have something like "Type c to continue, e to exit". Then just read in the value, do a simple error check, if it is a c, continue, if it is e, then exit. This will be much more manageable for you...

I imagine the console read was what the instructor has in mind rather than treating a keystroke as an event. Having the user enter a space will be problematic if you use the >> operator since >> will skip over spaces. You can use cin.get http://www.cplusplus.com/reference/iostream/istream/get.html

or as n1337 suggested, pick letters like 'c' and 'e', which are not white-space and can be read in using the >> operator.

Thank you, VernonDozier!
Thank you, n1337!
I really appreciate all your help with this project and I'm proud to say that it is finally finished! I submitted it successfully yesterday.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.