• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2601
  • Last Modified:

snakes n ladders

i'm going to make a game in c++ - snakes n ladders..
first i though of making the board by plotting pixels ..
n the program will have the knowlegde of the coordiantes of each box, sankes and laddders.
to check in which box a pawn is i'll just check what are its coordinates and check in which box it lies(coordinates of each box is prefixed)
i made wach square of side 20 pixels ..
a pawn's center is always at the center of a box..
to move a pawn ahead i just added 20 to its x - axis coordinates (for horizontal movement only)

now i thought of another way but have no way how to implement it:::
       is there any way i can just draw a 16 bit image and just import it into my program ?

if you have any advice/suggestions that'd be really nice ..

  • 2
1 Solution
Sounds fun!

Drawing your board as a graphic is a good idea. How you import it into your program depends very much on your OS and what graphics library you use in C++. There are several graphics libraries for Windows, and a bunch or others for Unix/Linux.

If you're using Microsoft Visual C++, the easiest option is to create your board in the resource editor (although you might want to draw it in a better graphics program then import it into resource editor). This means it will be compiled into the .EXE and you don't need a seperate image file. Once you have that, you can use the CBitmap::LoadBitmap() method to read the resource.

Here's a quick tutorial on it: http://www.functionx.com/visualc/bitmaps/DisplayFromResource.htm

So long as you keep all your squares the same size, it's fairly easy to keep track of everything. Although the board is two dimensional, the progression of the players is one-dimensional, so you could keep track of the player's position with a single integer.

For example:

    /* Constants define board and square size. */
    const int xSquares = 10;
    const int ySquares = 10;
    const int xWidth = 20;
    const int yWidth = 20;

    /* Variable to store the current player position. If you want multiple players, use an array. */
    int playerPos = 0;

    /* Calculate the squares accross and up the board from the current position. */
    int yPos = playerPos / xSquares;
    int xPos = playerPos % xSquares;
    /* Every second row the direction reverses, so if the row isn't divisible by 2, count from end of row. */
    if (yPos % 2)
        xPos = xSquares - xPos;

    /* To get the pixel position, multiply by the square size. Assuming the zero coordinate is the top left, we need to reverse the vertical position. */
    int xPixel = xPos * xWidth;
    int yPixel = (ySquares - yPos) * yWidth;

Hopefully that gives you some ideas. For each snake and ladder you just need to store the starting squares and the ending squares inj an array. Each move, check if the player landed on a starting square, and if they did, move them to the ending square. You don't even need seperate arrays for snakes and ladders.

Note that everything is zero-based, so the finishing square is (xSquares * ySquares) - 1.

Good luck!

shilpi84Author Commented:
i m using C++ in 16 bit(dos)
Okay, everything still applies, except for the MFC parts. What compiler/class library are you using? I did quite a bit of graphics work in Borland C++ in the dim and distant past. It's also up to you what graphics mode you want to use. Mode 19 in VGA is a low-res 256-colour graphics mode that's great for DOS games. Depending on what graphics library functions are available to you, you may not have anything built in for importing bitmaps, but you might be able to do something that would read a RAW image file with no complex compression.

I actually wrote most of my own primitive graphic functions to do things like set the video mode, set up the palette colours and paint pixels specified colours. I could see if I could dig some of them out later if it's any help.

Hope this helps,

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now