본문 바로가기

카테고리 없음

Design Snake And Ladder Game



The board would be a two dimensional array of cells. Each cell will have a optional reference to a snake or ladder. It will also mark it either head or tail. So whenever the buck stops at a cell which has a ladder start or snake head, we get the reference to the snake or ladder and do the processing to bring the buck to the appropriate cell.

What others are saying Unit Study Week free printable snakes and ladders game for kids craft activities snakes and ladders game board. Read a sight word, roll the dice and move that many spaces. Feb 27, 2018  I’d start with the data structure. In JavaScript the entire board might be an array of 100 spaces some of which lead elsewhere (e.g. Store an index). codevar board = null, null, null, 11, null, null, null, 2.; /codeAn index that points t.

OO Design for Snake and Ladder Game:
http://www.programcreek.com/2014/08/leetcode-design-snake-game-java/


http://designwithneo.blogspot.in/2013/04/snake-and-ladder.html
http://gamedev.stackexchange.com/questions/87694/modeling-object-oriented-snakes-and-ladders
https://www.safaribooksonline.com/library/view/object-oriented-programming/9789332503663/xhtml/head-0973.xhtml

Related Problems:
Given a snake and ladder board, find the minimum number of dice throws required to reach the destination or last cell from source or 1st cell. Basically, the player has total control over outcome of dice throw and wants to find out minimum number of throws required to reach last cell.

http://www.geeksforgeeks.org/snake-ladder-problem-2/
http://shirleyisnotageek.blogspot.in/2015/03/snakes-and-ladders.html

Variation:
Design a Snake Game found in Mobiles
http://massivetechinterview.blogspot.in/2016/05/design-snake-game.html


Complete Game Design in C++

  1. Players play alternately (turn by turn).
  2. A player throws a dice.
  3. His pebble is moved on the board depending on the dice value.
  4. If the pebble is at the bottom of the ladder, move it to the top of the ladder.
  5. If the pebble is at the mouth of the snake, move it to the tail of the snake.
  6. If any player reaches Home (H100) he wins. Play ends.
  7. If pebble tries to move ahead of Home, it reverses its direction. For example, from H98 two moves take pebble to home, but three moves take it to H99.

A project starts with the problem analysis. A complex problem is required to be thoroughly analysed before any design or coding starts. It is expected that “requirements gathering” is done before hand

Requirement analysis:

We may start with studying the snake and ladder board and the rules of playing and specify what the software should do:
  • A board with 100 houses, numbered from 1 to 100.
  • Numbering in a particular sequence.
  • H1 is start H100 is the Home.
  • Snakes = 11, Ladders = 11
Other details:
  • Number of players 2
  • A dice with 6 faces
  • Two color pebbles, one for each player
When a process of analysis starts, we know what we are expected to develop. In simple words, this involves two steps
  1. Identify all objects
  2. Study its interaction
Another aspect of the analysis is the goal. What the software is going to achieve? When the program is going to end?
Let us scan the problem specifications and list all possible objects:
Board, house, snake, ladder, player, dice and pebbles.
Start, home, six faces, colour (pebble), throw, move (pebble), mouth, tail, top, bottom, win and 100.
It is very easy to understand that these keywords are either attributes or methods related to objects. These are summarized in Table 22.1.
Table 22.1 Attributes and Methods Related to Objects
 
Design:

22.4.1 Design of classes

In the analysis, we have identified seven objects. Now we can group the objects:
Group 2: Dice
A careful analysis will reveal that though snakes and ladders are objects they do not move or change state. Hence, the class board should include snakes, ladders, houses, and pebbles as its integral part and not separate objects.
It leaves us at only three basic objects namely board, dice, and player.Figure 22.2shows the class diagrams.
 

22.4.2 Interaction

Now we have to study the interaction between the classes. In the game, player throws the dice. Hence, in the program player will send message “throw” to dice. Dice will turn and assume a new value. Now Dice will tell (send message) the player to calculate. After calculation player will instruct the board to move the pebble. This has been illustrated inFigure 22.3
When the pebble moves, it may land at the mouth of a snake or at the bottom of the ladder. In that case, player has to calculate again and then move the pebble accordingly. By this action, it is possible that the pebble may have reached the end. So we have to check for the end of game. Hence, the actual sequence of actions will be:
  1. Player to dice -> throw()
  2. Dice to player -> calculate1()
  3. Player to Board move_pebble()
  4. If needed { mouth of snake or base of ladder}
  5. Calculate2()
  6. Player to Board move_pebble()



 
For simplicity, Figure 22.4 shows only actions of player1. In actual program actions of player1 will be followed by identical actions by player2.
Strategy to develop complete Program:
We should start with an empty prototype say lad1.cpp1. As we progress, we should rename program files as lad2.cpp, lad3.cpp. It helps us in keeping track of development. For any reason if we are required to backtrack, it is a painless job. Now you will appreciate why the name of the main file is kept as “lad9A.cpp”.
  • Pebbles initialized with colours.
  • Players associated with pebbles.
  • Board drawn with houses, snakes, ladders, and pebbles.
  • Both pebbles at house1.
 
We know that program size is moderately large. So we have to decide on using appropriately designed header files. Since screen has to be in the graphical mode, we should include our much used file bpgraph1.h. Now we should develop header file which generates images of pebbles. We can call it as snlgraph.h. Thereafter, we should develop file board.h. It should include routines related to board. As board.h starts getting bulky, we may develop header file sn_ld.h to contain elementary things regarding snakes and ladder. File board.h can use this file. TheBox 22.3describes the General Strategy about Header Files.
BOX 22.3 GENERAL STRATEGY ABOUT HEADER FILES
Whenever we develop a header file, we should write a test routine for it. This routine should be included in main file. Main file should be compiled and executed. If we are satisfied withthe header file, the test routine should be commented out. It should not be deleted! When the trials of the program are satisfactory, only at that time these routines should be deleted and main file saved.
With this much preparation, let us see the actual solution.
Problem: Develop complete snake and ladder simulation program.
Solution: The program will be developed using strategy discussed earlier.
  • Throwing of dice will be done using random number generation.
  • To observe the movements clearly, program will be sprinkled with function delay() at many places.
  • To observe movement of pebbles with snakes and ladder a special function named beep_and_pause() will be used. As the name suggests it beeps and waits for pressing of any key. If players get tired, escape key may be pressed to quit simulation. This facility is inbuilt in both functions pause() and beep_and_pause().
 
PROGRAM 22.1 SNAKE AND LADDER
// lad9A.cpp
// snake and ladder program main file
typedef char Astring[40] ;
// co-ordinates for dispalying board
const int XX0 = 100;
const int YY0 = 30;
const int SIZE = 30;
#include“BPgraph1.h”
canvas *c1;
#include “snlgraph.h” // graphics routines
#include “snlboard.h” // board related routine
#include “sn_ld.h” // snake and ladder information
Board board(XX0,YY0);
class Player
{ int position ;
Astring name;
public:
int oldpos,newpos; // moved
void update1();
Player(Astring name1);
void calculate_1(const int k);
void calculate_2();
void check_end(int & i);
} ;
Player player1(“Ramesh”), player2(“Suresh”);
int main()
{ c1=new canvas(“snake and ladder”);
c1->draw();
c1->end_graph();
return 0;
}
void canvas::draw()
{ int end_flag = 0;
Astring msg1;
int step=0;
Dice d1;
int k;
board_graphics_init();
board.draw_board();
board.put_pebble1(1);
board.put_pebble2(1);
c1->message(“press a key to start the game”);
// delay(1000);
// beep_and_pause();
while ( ! end_flag )
{// player1 dice throw
k=d1.throw_dice();
strcpy(msg1,d1.show_dice());
message(msg1); //method from BPgraph1.h showing dice value
// dice to player
player1.calculate_1(k);
// player to dispaly
board.move_pebble1(player1.oldpos, player1.newpos);
player1.update1();
delay(300); //300
player1.calculate_2();// (snake /ladder)
board.move_pebble1(player1.oldpos, player1.newpos);
player1.update1();
player1.check_end( end_flag) ;
if (end_flag)
{ //Declare winner
message(“The winner is Player 1” );
delay(1000);
break;
}
delay(300);//300
// player2 to dice throw
k=d1.throw_dice();
strcpy(msg1,d1.show_dice());
message(msg1);
// dice to player
player2.calculate_1(k);
board.move_pebble2(player2.oldpos, player2.newpos);
player2.update1();
delay(300);
player2.calculate_2();// (snake /ladder)
board.move_pebble2(player2.oldpos, player2.newpos);
player2.update1();
player2.check_end( end_flag) ;
if(end_flag)
{ //Declare winner
message(“The winner is Player 2”);
delay(2000);
}
else
if(++step > 150)
{ message(“Too long simulation program terminated ”
end_flag =1;
}
delay(1000);
// pause(); // for debugging
}
};
void Player::calculate_1(const int k)
{ int temp;
temp = position + k ;
if( temp <= 100) position =temp;
else position = 100+100 -temp;
newpos=position;
};
void Player::calculate_2()
{ switch (position)
{ // snakes
case 46 : position = 15;beep_and_pause();break;
case 48 : position = 9; beep_and_pause();break;
case 52 : position = 11;beep_and_pause();break;
case 59 : position = 18;beep_and_pause();break;
case 64 : position = 24;beep_and_pause();break;
case 68 : position = 2; beep_and_pause();break;
case 69 : position = 33;beep_and_pause();break;
case 83 : position = 22;beep_and_pause();break;
case 89 : position = 51;beep_and_pause();break;
case 93 : position = 37;beep_and_pause();break;
case 98 : position = 13;beep_and_pause();break;
// ladders
case 8 : position = 26 ;beep_and_pause();break;
case 19 : position = 38;beep_and_pause();break;
case 21 : position = 82;beep_and_pause();break;
case 28 : position = 53;beep_and_pause();break;
case 36 : position = 57;beep_and_pause();break;
case 43 : position = 77;beep_and_pause();break;
case 50 : position = 91;beep_and_pause();break;
case 54 : position = 88;beep_and_pause();break;
case 61 : position = 99;beep_and_pause();break;
case 62 : position = 96;beep_and_pause();break;
case 66 : position = 87;beep_and_pause();break;
}
newpos = position;
// beep_and_pause(); // useful for debugging
};
void Player::check_end( int & i)
{ if (position 100) i = 1 ;
};
Player::Player(Astring name1)
{ position = 1;
oldpos = 1;
newpos = 1;
strcpy (name, name1);5};
void Player::update1()
{ oldpos = newpos;
}
 
// snlgraph.h
// routines for capturing pebbles and other graphic images
#include<string.h>
unsigned int Dsize;
void * cube2 ,*cube1;
void * blank,*blank1 , * pebble1, * pebble2;
void pause()
{ char ch1;
ch1 = getch();
if (ch1 27) exit(0);
};
void beep_and_pause()
{ char ch1;
beep();
c1->message(“press a key to continue”);
ch1 = getch();
if (ch1 27) exit(0);
}
void grab_image(int x,int y, void *cube2)
{ getimage(x, y, x+ SIZE, y+ SIZE, cube2);
}
void put_image(int x,int y, void *cube2,int z)
{ putimage(x, y, cube2, z);
};
void draw_cube( int x , int y , int z )
{ int i, j ;
for(i=x; i<x+z; i++)
for(j=y; j<;y+z; j++)
putpixel(i,j,YELLOW);
};
void draw_cube1( int x , int y , int z )
{ int i, j ;
for(i=x; i<;x+z; i++)
for(j=y; j<;y+z; j++)
putpixel(i,j,BLUE);
void draw_pebble(int x, int y, int z)
{ int i;
setcolor(z);
for(i=0;i<;10;i++)
circle(x,y,i);
};
void init();
void NtoXY(int n, int & x, int & y) ;
void move_pebble1(int oldpos,int newpos);
void move_pebble2(int oldpos,int newpos);
char str_pointer[3];
void board_graphics_init()
{ int x=100, y=100, i, j;
// calculate the size of the image
Dsize = imagesize(x, y, x+ SIZE, y+ SIZE);
// allocate memory to hold the image
cube2 = malloc(Dsize);
cube1 = malloc(Dsize);
blank = malloc(Dsize);
blank1 = malloc(Dsize);
pebble1 = malloc(Dsize);
pebble2 = malloc(Dsize);
// draw the image to be grabbed
draw_cube(x,y,SIZE);
// grab the image
grab_image(x, y, cube2);
draw_cube1(x,y,SIZE);
// grab the image
grab_image(x, y, cube1);
put_image(130, 130, cube2, COPY_PUT);
putimage(70,70, cube1,COPY_PUT);
draw_pebble (30,30,RED);
grab_image(30-11 ,30-11 , pebble2 );
draw_pebble (30,30,GREEN);
grab_image(30-11 ,30-11 , pebble1 );
putimage(100,100,pebble1,COPY_PUT);
putimage(130,130,pebble2,COPY_PUT);
pause(); // for debugging
clearviewport();
grab_image(0,0,blank);
grab_image(0,0,blank1);
};
 
//snlboard.h
#define no_rows 10
#define no_cols 10
#define TEN 10
//Snake head and tail
int SHT[11][2] = { 46,15, 48,9, 52,11, 59,18, 64,24, 68,2, 69,33,
83,22, 89,51, 93,37, 98,13 };
//Ladder top and bottom
int LTB[11][2]= { 8,26, 19,38, 21,82, 28,53, 36,57, 43,77,
50,91, 54,88, 61,99, 62,96, 66,87 } ;
int status[101];
class Board
{ int x0,y0;
int a[TEN][TEN];
public:
void draw_board();
Board( int a, int b );
void put_pebble1( int z);
void put_pebble2( int z);
void draw_ladder(int topx, int topy, int botomx,int botomy) ;
void draw_snake(int topx, int topy, int botomx,int botomy) ;
void hor_line(int tempx,int i,int lim ) ;
friend void draw_all_snakes();
friend void draw_all_ladders();
// revised
void move_pebble1(int oldpos,int newpos);
void move_pebble2(int oldpos,int newpos);
void remove_pebble1( int z) ;
void remove_pebble2( int z) ;
};
extern Board board;
void Board::draw_board()
{ int i,j; int col ;
int ax,ay; int SIZE2 =SIZE+2 ;
int SIZE1= SIZE/2;
// part1 squares
for (i=0;i<;no_rows;i++)
for (j=0;j<;no_rows;j++)
{ if ((i+j)%2 )
put_image(x0+i*(SIZE2),y0+j*(SIZE2), cube2, COPY_PUT);
else
put_image(x0+i*(SIZE2),y0+j*(SIZE2), cube1, COPY_PUT);
}
// part III numbers
col=getcolor();
setcolor(BLACK);
for( i =2;i<; 100;i++)
{ sprintf(str_pointer, “%2d”,i);
NtoXY(i,ax,ay);
outtextxy( x0+ay*(SIZE2), y0+(ax-1)*(SIZE2)+SIZE1 ,str_pointer);
}
sprintf(str_pointer, “%s”, “ST”);
NtoXY(1,ax,ay); // 1= start
outtextxy( x0+ay*(SIZE2), y0+(ax-1)*(SIZE2)+SIZE1 ,str_pointer);
sprintf(str_pointer, “%s”,”HM”);
NtoXY(100,ax,ay); // 100 = home
outtextxy( x0+ay*(SIZE2), y0+(ax-1)*(SIZE2)+SIZE1 ,str_pointer);
setcolor(col);
//part II snakes and ladders
draw_all_ladders();
draw_all_snakes();
}
void Board::put_pebble1( int z)
{ int ax,ay;
NtoXY(z,ax,ay);
if (status[z]0)
{ grab_image(x0+ay*(SIZE+2), y0+(ax-1)*(SIZE+2),cube1);
put_image(x0+ay*(SIZE+2), y0+(ax-1)*(SIZE+2),pebble1,COPY_PUT);
status[z] = 1;
}
else if (status[z]1)
{ memcpy(cube1,cube2,Dsize);
put_image(x0+ay*(SIZE+2), y0+(ax-1)*(SIZE+2),
pebble1,COPY_PUT);
status[z] = 2;
} ;
};
void Board::put_pebble2( int z)
{ int ax,ay;
NtoXY(z,ax,ay);
if (status[z]0)
{ grab_image(x0+ay*(SIZE+2),y0+(ax-1)*(SIZE+2),cube2);
put_image(x0+ay*(SIZE+2),y0+(ax-1)*(SIZE+2),pebble2,COPY_PUT);
status[z] = 1;
}
else if (status[z]1)
{ memcpy(cube2,cube1,Dsize);
put_image(x0+ay*(SIZE+2),y0+(ax-1)*(SIZE+2),pebble2,COPY_PUT);
status[z] = 2;
} ;
};
void Board::remove_pebble1( int z)
{ int ax,ay;
NtoXY(z,ax,ay);
if (status[z] 1)
{ put_image(x0+ay*(SIZE+2),y0+(ax-1)*(SIZE+2),cube1,COPY_PUT);
status[z]=0;
}
else if(status[z]2)
{ // no put
status[z]= 1;
}
};
void Board::remove_pebble2( int z)
{ int ax,ay;
void Board::remove_pebble2( int zNtoXY(z,ax,ay);
if (status[z] 1)
{ put_image(x0+ay*(SIZE+2), y0+(ax-1)*(SIZE+2),cube2,COPY_PUT);
status[z]=0;
}
else if(status[z]2)
{ // no put
status[z]= 1;
}
};
Board::Board ( int a , int b)
{ int i;
x0= a ;
y0 = b ;
for (i=0;i<;101;i++)
status[i] = 0;
}
void NtoXY(int n, int & x, int & y)
{ int x1,y1;
y1= (n-1)_N;
x1=(n-1) /TEN;
x= TEN -x1;
if ( ( x %2 1) )
y= (TEN -1) -y1;
else
y= y1;
} ;
void Board::move_pebble1(int oldpos,int newpos)
{ remove_pebble1( oldpos);
put_pebble1( newpos);
};
void Board::move_pebble2(int oldpos,int newpos)
{ remove_pebble2( oldpos);
put_pebble2( newpos);
};
class Dice
{ private:
int val;
public:
Dice();
int throw_dice();
char * show_dice();
} ;
int Dice::throw_dice()
{ int temp;
temp = 1 + random(6);
val = temp;
return temp;
};
char * Dice::show_dice()
{ //char abc[40];
Astring abc ;
sprintf(abc,“%d”, val);
return abc;
}
Dice::Dice() //int x,int y)
{ randomize();
val =1 ;
};
 
// sn_ld.h
// header files contains snake and ladder drawing routines
void Board::hor_line(int tempx,int i,int lim =5)
{ int k;
for(k=0;k<;lim;k++)
putpixel(x0+tempx+k, y0+i, GREEN );
}
int head[]={1,3,4,6,8,9,8,7,6,6,5,5,12 };
int tail[] = {1,1,2,2,2,3,3,3,4,4,5,4,5 };
void Board::draw_ladder(int topx, int topy, int botomx,int botomy)
{ int temp_col;int i,tempx;
int step = 5;
int SIZE1 = SIZE /2;
//co-ordinate adjustment for centre
topx += SIZE1; topy += SIZE1;
botomx += SIZE1; botomy +=SIZE1;
temp_col=getcolor();
setcolor(BROWN);
for(i=topy +3; i<;botomy; i=i+step )
{ tempx = topx+ (topx-botomx)*(i-topy)/( topy-botomy);
line(x0+tempx, y0+i, x0+tempx+6, y0+i);
}
line(x0+topx,y0+topy, x0+botomx, y0+botomy);
line(x0+topx+6,y0+topy, x0+botomx+6, y0+botomy);
setcolor(temp_col);
void Board::draw_snake(int topx, int topy,int botomx, int botomy)
{ int temp_col;int i,tempx,one=1;
int count;
int SIZE1 = SIZE /2;
//co-ordinate adjustment for centre
topx += SIZE1; topy += SIZE1;
botomx += SIZE1; botomy +=SIZE1;
float slope,Cslope,slopeplus,slope5; //slope for 5 pixels
float disp;
//body
slope = float(topx -botomx) / (topy-botomy);
slopeplus = slope* 0.5 ;
if (slopeplus 0.0) slopeplus =0.50/2.0;
Cslope=slope;
tempx=topx; disp=0; // 5 for head and tail
// head
const int twelve = 12;
count = 0 ;
for(i=topy ;i<;topy+twelve ;i++)
{ count++;
if (i_0)
{ Cslope= slope + slopeplus;
slopeplus= - slopeplus ;
one = -one;
};
board.hor_line(tempx -( head[count]- 5)/2 ,i,head[count]) ;
disp= disp + Cslope;
tempx = tempx + int(disp ) ;
disp = disp-int(disp);
}
// body
for(i=topy+ twelve ;i<;botomy-twelve ;i++)
{ if (i_0)
{ Cslope= slope + slopeplus;
slopeplus= - slopeplus ;
one = -one;
};
board.hor_line(tempx,i ) ;
disp= disp + Cslope;
tempx = tempx + int(disp ) ;
disp = disp-int(disp);
}
// tail
count = twelve ;
for(i=botomy-twelve ; i<;botomy ;i++)
{ --count;
if (i_0) { Cslope= slope + slopeplus;
slopeplus= - slopeplus ;
one = -one;
};
board.hor_line(tempx,i,tail[count] ) ;
disp= disp + Cslope;
tempx = tempx + int(disp ) ;
disp = disp-int(disp);
}
} ;
void draw_all_snakes()
{ int i, ax, bx;
int x1,y1,x2,y2;
int SIZE2 = SIZE + 2;
for(i=0;i<;11;i++)
{ ax= SHT[i][0] ;
NtoXY(ax,x1,y1 ); // xy reversal
bx= SHT[i][1] ;
NtoXY(bx,x2,y2);
board.draw_snake(y1*SIZE2,(x1-1)*SIZE2, y2*SIZE2, (x2-1)*SIZE2);
}
} ;
void draw_all_ladders()
{ int i, ax, bx;
int x1,y1,x2,y2;
int SIZE2 = SIZE + 2;
for(i=0;i<;11;i++)
{ ax= LTB[i][1] ;
NtoXY(ax,x1,y1 ); // xy reversal
bx= LTB[i][0] ;
NtoXY(bx,x2,y2);
board.draw_ladder(y1*SIZE2, (x1-1)*SIZE2,
y2*SIZE2, (x2-1)*SIZE2);
}
}
 
The most complicated part of the program was handling pebble movement. In normal course, we would like to use XOR mode as in any graphics application. If there was only one pebble this would have worked. With two pebbles, there is a possibility that a pebble will come to a house where there is another pebble. Now we cannot use XOR_PUT. We have to use COPY_PUT. After this, the previous pebble moves. This pebble is below the recent pebble. Removing lower pebble does not change the view. Hence the action associated with such movement is no action!

 

In this game project, I have presented the source code along with the algorithm and flowchart for the project under the name “Viper Buzz”. ‘The Viper Buzz” is a reflection of the famous classic game ‘Snakes and Ladders’. Designed as a computer version of the South Asian game, this final year project is written on C language using GNU GCC compiler on Code::Blocks IDE. It is an entertaining game that has been made user friendly with multiple user compatibility.

This snakes and ladders game is primarily designed as a semester project. It particularly aims at imparting general workable and practical knowledge about C and lets the beginners have adequate concept on programming small to big projects. It is developed with a hope to increase the programming logic and also to provide substitution to the classical game “Snake and Ladder”.

Unlike other projects, there are many files, besides the source code files, that make up this snakes and ladders project in C. You can directly download the program files of this project from the link below.

Download Snakes and Ladders Game Project in C with Source Code (Updated: Sound Files)

[sociallocker]

Download Snakes and Ladders Game Project in C with Source Code (Updated: Sound Files)

[/sociallocker]

About Snakes and Ladders Game:

The game includes the facilities such as saving and loading the game. The saved games are password protected so that only the authorized user can load the previously saved game. The snakes and ladders game project comprises of multiple functions (scroll down to view the description for each) which can be dealt at ease. Attempt has been made to present the program in user friendly environment.

Objectives, Features and Scope:

OBJECTIVES

  • To use different user defined function, to break a program into many simplified parts to deal with and to make it easier to understand the codes too.
  • To promote the use of array in general programming for simplified version of the program.
  • To apply the file handling concepts in order to retrieve the player’s data files that contains the data of the players.
  • To use minimum graphics as far as possible to make the program execute fast but also user friendly side by side.
  • To use general concept of c language to develop a simple snakes and ladders game that as a whole entertains the user.

Design a snake and ladder game in python

FEATURES

  • Compatible as multi-player game.
  • Facility to save the game.
  • Open the saved game using the password pre-defined by the user.
  • Use minimum graphics in snakes and ladders as far as possible avoiding complex codes.

SCOPE

  • Users preferring the classical games can switch to the game.
  • Beginners in computer programming can take hints via the mini project to boost up their programming techniques.
  • Can be brought in practice in training centers that offers basic programming courses.
  • Can be the best choice as stress relieving game in the busy life pattern of the people in present life.
  • Simplicity and efficient is main fact that we get using the c language, so user can execute snakes and ladders in each and every computer almost.
  • Can be milestone for the programmers trying to learn to build projects in C.

Here is a block diagram for this Snakes and Ladders game labelled as “The Viper Buzz”.

Functions Used:

Main()

This function is the main function. This function contains the control of the whole game. User can switch to resume, new game, load game, save game, tutorial and exit from this function. It calls the respective function as user’s input. In this function user can use pre-defined short cut keys too.

Save game()

This part of the program is called from the main function when user wishes to save the game. The player can provide his own password to protect the game from other users. In snakes and ladders, this function calls the password function to get the password from the user. After getting the password the name of game, the name of players, and the value of the player is stored in a file named save.dat that can be reopened further.

Loadgame()

This function enables the users to load the initially saved snakes and ladders game. In order to prevent the unauthorized use of the loaded game, there is a password system that user has to go through before he opens or loads the saved game. In order to get game loaded user has to input the game name and the password per defined or per entered. Password functions is called for password input and for matching of the password and the game name data is read from the data file save.dat.

Tutorial()

This function in the program prints the tutorial of the snakes and ladders i.e. the help of the game, color indication and so on. The function uses a data file that contains the tutorial of the game. After printing the tutorial the control is transferred to main function.

Firstscr()

This function generates the first screen appeared when the program is executed. This function includes some animations and the snakes and ladders title. This function gets control until unless any key of the key board is hit. If any key of the key board is hit the control flows to main function.

Main game()

This is the function from where all the game is executed . From this function firstly the layout of the board is called. Then the vipers and the ladders are printed. Then the dice function is called and returned value is added to respective player’s position. Then after the viper and ladder function is called to check the viper and ladder position. Then after the players are displayed at the new position. At last eofgame function is called in this snakes and ladders if the player’s value equals to 100.

Layout()

In this function, the general layout of the board is drawn as like in classical game. This function does not get control for long. As soon as it get the control it prints the layout of the board and the control goes to the main game function.

Drawviperandladder()

In this function, the vipers and ladders are drawn on the board. This function is called from the main game function of this viper buzz project. The viper are represented by the red color and the ladders are represented by the green color.

Dice()

This function is for the value of the dice as per user’s reaction. In snakes and ladders, his function returns an integer value if any key of the keyboard is hit. The value is returned to the main game function. In addition to the dice rolling, this function holds the screen and also responds to the escape key pressed. If escape key is pressed, it flows control to the main menu else the control flows to main menu.

Position()

This is the function locating the player in the board of snakes and ladders. This function prints a white space in the old position and it prints the representation character to the new location as the value of the player. The main aim of this function is to prints the player’s indicator in the respective position.

Viperandladder()

In this function the player’s position is compared with the initial position of the ladder and respective value of the player is changed only if the initial value matches. In addition to that this function checks the hit condition also. The control flows to the main game after execution of snakes and ladders.

Locate(int , int)

This function is used in our program to set the cursor position on the different location of the screen. It takes two arguments and sets the cursor position according to the arguments.

Draw()

In this snakes and ladders game project, draw() function helps to print the dice box and the data of the player on the box. It is called each time loop executes so that it overlaps the initial mesh created. This function displays the player’s current position, player’s name and turn.

Eofgame(char , int)

This is the final screen after any one of the player reaches to the position of 100. The main loop of the main game function in this snakes and ladders gets break and this function is called. In this function two arguments are passed i.e. firstly the player’s name and the player’s code. This function has the control until unless a key is pressed.

Password(char)

In this function, an address is passed as argument and entered password is returned for the pointer. In this snakes and ladders game project, the character that user input is take and the asterisks are printed in place of the character to avoid the identification. This function is called from save game and the load game.

Playfilesync(char)

This function is used in the program to play the sound of the game. This function enables the programmer to embed the sound in their console based program. This function takes the file name to be played. The file to be played should be inwav format.

Algorithm & Flowchart:

Here, individual algorithms and flowcharts have been present in a sequential manner for the functions that make up this gaming project.

1. main ()

Step 1: Start
Step 2 : Display the menu items.
Step 3: Check the user input and go to respective functions.

  • 3(a): If 1 go to main game() function
  • 3(b): if 2 go to new game
  • 3(c): If 3 go to load game
  • 3(d): If 4 go to save game
  • 3(e): If 5 go to tutorial
  • 3(f): If 6 go to step 4.

Object Oriented Design Of Snake And Ladder Game

Step 4: Stop

2. main game()

Step 1: Start
Step 2: Display the layout of the board of snakes and ladders from layout function.
Step 3: Call the dice function and store returned value to a variable(c)
Step 4: Check whether the p1 and p2 value is zero or not

  • 4(a): If yes, check the dice value
  • if it is 1 or 6 change respective player’s value to 1.
  • 4(b): If no increase the value of turn, call position function and go to step 3.

Step 5: Check whether sum of player’s value and dice(c) greater than 100 or not

  • 5(a): If yes increase value of turn and go to step 3,

Step 6: Check whether sum player’s value and dicer equals 100 or not

  • 6(a): if yes, display the player name as a winner got main.
  • 6(b): if no, add the players value and dicer.

Step 8: Call the function position to locate the player on the board.
Step 9: Check whether dice value is 1 or 6.

  • 9(a): if yes, decrease turn by 1.

Step 10: Increase the turn by 1.
Step 11: Go to step 3.
Step 12: Stop

3. New game()

Step 1: Start
Step 2: Ask user whether to start a new game or not?
Step 3: check the input character

  • 3(a): if yes,
  • Assign player1’s value, player2’s value and turn to 0
  • Ask for player1’s name and player’s name and store it to global structure of snakes and ladders.
  • Call the function main game and change the flag value.
  • 3(b): If no call the function main menu.

Step 4: Stop

4. Save game()

Step 1: Start
Step 2: check the flag value initialized as global is equal to 46.

  • 2(a): if matches to start up value (46) request user to start a new game and change the flag value.
  • 2(b): If does not matches to startup value (46)
  • Open a fie to store the data in append mode.
  • Ask user for the input of game name and password.
  • Write, player’s data, position , game name and password to the file.
  • Display game saved
  • Close the file and return to main menu.

Step 3: Stop.

5. Load game ()

Step 1: start
Step 2: Ask user for game name and password.
Step 3: Open initially saved fie from save game function in read mode.
Step 4: Compare the game name and password from user and data file.

  • 4(a): If both game name and password matches
  1. Display match found.
  2. Assign player’s data and name to respective global variables
  3. Call the main game function
  • 4(b) If match is not found go to main menu of snakes and ladders

Design And Code Snake And Ladder Game

Step 5: Stop

6. Layout ()

Step 1: Start
Step 2 : Initialize a variable (j) as 0.
Step 3: Find the quotient when j is divided by 10.
Step 4: Check the quotient value is even or not?

  • 4(a): if yes,
  • Assign the value of i as 1.
  • Display the sum of i and j.
  • Increase the value of i by 1.
  • Check the value of i whether it is less than equals to 10 or not?
  • If yes, goto step 4(a).ii
  • If no goto step 5.
  • 4(b): If no,
  • Assign the value of I as 10
  • Display the sum of I and j.
  • Decrease the value of I by 1
  • Check whether the value of i is greater than 0 or not?
  • If yes, goto step 4(b).ii
  • If no, go to step 5.

Step 5: Increase the value of j by 10.
Step 6: Check whether j if less than 100 or not?

  • 6(a): If yes, goto step 3.
  • 6(b): if no, goto step 7.

Step 7: Go to main() function of snakes and ladders.
Step 8: Stop.

7. Drawviperandladder ()

This flowchart is continued below:

Step 1: Start
Step 2: Open the data file containing the initial and final position of viper and the ladder.
Step 3: Read initial and final position of viper and ladder.
Step 4: Find the coordinates of intial and final position of the snakes and ladders and store to initial.x, initial.y, final.x and final.y.
Step 5: Initialize a variable (a) with y coordinate of initial position.a=initial.y
Step 6: Locate the Cursor position as initial.x and a.
Step 7: Display asterisk (*).
Step 8 : Increase the value of a by 1.
Step 9: Check whether the value of a is less than final.y or not?

  • 9(a): If yes, go to step 6.
  • 9(b): If no, go to step 10.

Download Snake And Ladder Game

Step 10: Initialize a variable (b) with x coordinate of initial position. b=initial.x
Step 11: Locate the Cursor position as b and initial.y.
Step 12: Display asterisk (*).
Step 13 : Increase the value of b by 1.
Step 14: Check whether the value of b is less than final.x or not?

  • 14(a): If yes, go to step 11.
  • 14(b): If no, go to step 15.

Step 15: Check whether it is end of file or not?

  • 15(a): If yes, go to step 17.
  • 15(b): if no, go to step 3.

Step 16: Go to main() function of snakes and ladders.
Step 17: Stop.

8. Dice ()

Step 1: Start.
Step 2: Initialize a variable as 1.
Step 3: Increase the value of the variable by 1.
Step 4: Check whether there is keyboard input or not?

  • 4(a): if yes, return the value of the variable as the dice value and go to main().
  • 4(b): if no, go to step 5.

Step 5: Check whether counter variable equals 6 or not?

  • 5(a): if yes, assign the value of counter as 1 and goto Step 3.
  • 5(b): if no, goto Step 3.

Step 6: Stop.

9. Position ()

Step 1: Start.
Step 2: Display white space on the initial position of the players.
Step 3: Get the new player’s position.
Step 4: Locate the X and Y coordinate of the player’s position.
Step 5: Display the respective player to the position.
Step 6: Go to the main.
Step 7: Stop.

10. Viperandladder ()

Step 1: Start.
Step 2: Open the file containing initial and the final position of the viper and ladder.
Step 3: Read initial and final position of viper and ladders.
Step 4: Check the player’s position with the initial position of the viper and ladder, whether it matches or not?

  • 4(a): If yes, Assign the value of player with the final value of viper or ladder.
  • 4(b): if no, go to Step 5.

Step 5: Check whether the position of the player 1 and the position of player 2 matches or not?

  • 5(a): If yes, Check the turn whether it is odd or not?
  • If yes, assign player 1’s position equal to 1.
  • If no, assign player 2’s position equal to 1.
  • 5(b): if no, go to step 6.

Printable Snake And Ladder Game

Step 6: Go to main().
Step 7: Stop.

How To Play Snake And Ladder Game

Also see,
Mini Project Snake Game
iSnake Multiplayer Snake Game
More Projects in C and C++

Snakes And Ladders Game Pdf

Attempts will be made to incorporate more detailed and user interface features apart from the aforementioned ones. We hope this Snakes and Ladders game project presented to you as “Viper Buzz Project” fulfills its purpose and proves helpful to anyone who might use it. The modification on the game can be done as desired. The download link has been updated with sound files for this project.