# Snakes and Ladders (Id-1144)

Bonus Practice Sheet Winter 2018 [17-Mar-2018 to 7-Apr-2018]

Snakes and Ladders is an ancient Indian board game. It is played between two or more players on a gameboard having numbered, gridded squares (which is in the form a matrix). The board has a number of pictures of the “ladders” and “snakes” on the board, each connecting two specific board squares. The object of the game is to navigate one’s game piece, according to die rolls, from the start (bottom square) to the finish (top square), helped or hindered by ladders and snakes respectively.

Given an m X n board, details of snakes and ladders in the board, current position of coin of player1 and sequence of dice rolls by player1, design an algorithm and write a C++ code to find the new position of coin of player1. The grids of the board are numbered from 1 to m*n. After a die roll, if the coin ends up in a square with snake / ladder start position then update position to end position of snake / ladder. Each grid in the board is given a number ‘g’ which is computed as (r-1)*n + c, where ‘r’ and ‘c’ are the row and column position of the grid.

Understand the template code and implement the functions appropriately

Input Format

Value for ‘m’

Value for ‘n’

Number of snakes in board k

Start Grid position of snake1

End Grid position of snake1

Start Grid position of snake2

End Grid position of snake2

….

Start Grid position of snake-k

End Grid position of snake-k

Number of ladders in board t

Start Grid position of ladder1

End Grid position of ladder1

Start Grid position of ladder2

End Grid position of ladder2

….

Start Grid position of ladder-t

End Grid position of ladder-t

Current row position of coin of player1

Current column position of coin of player1

Number of dice rolls

Value of roll1

Value of roll2

…

Value of roll-n

Output Format

Final row position of player1

Final column position of player1

Boundary Conditions

Assume that there will be only a maximum of ten ladders and ten snakes

The position will never go out of the board

**Please Comment** Working if the code worked to you

If you have other working codes please comment the codes enclosing with **<pre> **and** </pre>** 🙂

Example: <pre> Your Code </pre>

**C++ Code **

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | void read_Values(board &b,snakes &snake_Det,ladders &ladder_Det,position &cur_Pos,rolls &r) { int i; cin>>b.row>>b.col; cin>>snake_Det.num; for(i=0;i < snake_Det.num;i++) cin>>snake_Det.st_Grid[i]>>snake_Det.end_Grid[i]; cin>>ladder_Det.num; for(i=0;i < ladder_Det.num;i++) cin>>ladder_Det.st_Grid[i]>>ladder_Det.end_Grid[i]; cin>>cur_Pos.row>>cur_Pos.col; cin>>r.num; for(i=0;i < r.num;i++) cin>>r.roll[i]; } int find_New_Pos(board &b,snakes &snake_Det,ladders &ladder_Det,position &cur_Pos,rolls &r) { int i,j; int position=((cur_Pos.row-1)*b.row)+cur_Pos.col; for(i=0;i < r.num;i++) { position+=r.roll[i]; for(j=0;j < snake_Det.num;j++) if(position==snake_Det.st_Grid[j]) { position=snake_Det.end_Grid[j]; break; } for(j=0;j < ladder_Det.num;j++) if(position==ladder_Det.st_Grid[j]) { position=ladder_Det.end_Grid[j]; break; } } return(position); } |

**reference**only. It doesn't mean everyone to directly copy/paste those codes.