10
MS3830: Computer Simulation Take-Home Assignment: Snakes and Ladders Game Submitted by: 1) Vighnesh Ambetkar: NA10B051 2) B. Sandeep Reddy: NA10B052 Rules: Game is started at position “start” outside the board. Using a roll of fair die, number of places to advance from current position is decided. If the next position is at the bottom of a ladder, position is advanced along the ladder. Similarly if the next position is at the top of a snake, position is lowered along the snake. Case I When the position is 5 places or less away from finish and if number on appearing on die after roll is such that it is beyond finish, then current position is maintained without any advance. Game is said to be “finished” if final position reached is “finish” exactly (20 in case of given board) Case II If the final position is exceeding the “finish” position, then also game is said to be “finished” apart from the case where the final position is “finish”. Objective: To find expected value of N where N is number of rolls required to reach “finish” of the game: Application of fundamentals of probability and statistics: In this game number appearing on die after roll is a random variable, an integer between 1 and 6. Hence for the simulation of

Snake and Ladder problem

Embed Size (px)

DESCRIPTION

Snake and Ladder Simulation

Citation preview

Page 1: Snake and Ladder problem

MS3830: Computer Simulation

Take-Home Assignment: Snakes and Ladders Game

Submitted by: 1) Vighnesh Ambetkar: NA10B051

2) B. Sandeep Reddy: NA10B052

Rules:

Game is started at position “start” outside the board. Using a roll of fair die, number of places to advance from current position is decided. If the next position is at the bottom of a ladder, position is advanced along the ladder. Similarly

if the next position is at the top of a snake, position is lowered along the snake.

Case I

When the position is 5 places or less away from finish and if number on appearing on die after roll is such that it is beyond finish, then current position is maintained without any advance.

Game is said to be “finished” if final position reached is “finish” exactly (20 in case of given board)

Case II

If the final position is exceeding the “finish” position, then also game is said to be “finished” apart from the case where the final position is “finish”.

Objective:

To find expected value of N where N is number of rolls required to reach “finish” of the game:

Application of fundamentals of probability and statistics:

In this game number appearing on die after roll is a random variable, an integer between 1 and 6. Hence for the simulation of this game, number appearing on die after roll is generated by random number (integer) generator between 1 and 6.

This game can be analyzed using Markov Chain analysis because, at any time, the probability of events that will happen in the future is independent of what happened in the past. If a player is on grid square x of the board, the probability of what will happen on the next roll is independent on how the player got to square x.

In probability theory, the expected value refers to the value of a random variable one would “expect” to find if one could repeat the random variable process an infinite number of times and take the weighted average of the values obtained.

Page 2: Snake and Ladder problem

For this game each time a trial is conducted i.e. the game is played and number of rolls required to finish the game (N) are counted. If this experiment is carried out for very large number of times then average of number of rolls required to finish the game can be found out which gives expected value of N. Here N is a discrete random variable.

Let Ni be the number of rolls required to finish the game for the ith trial and n trials are conducted

(n is very large)

Suppose random variable N takes value N1 with probability p1, value N2 with probability p2 and so on up to value Nn with probability pn.

Then the expected value of this random variable is defined as

E[N]=N1p1+N2p2+…+Nnpn

In this case each trial is equally likely i.e.

p1=p2=…=pn=1/n

Hence weighted average is same as arithmetic mean.

This simulation is carried out using MATLAB. Number appearing on die after roll is generated by random number (integer) generator between 1 and 6. For this purpose randi() function is usedSyntax is

r = randi([imin,imax])

In our case imin=1 and imax=6

Page 3: Snake and Ladder problem

Algorithm (Case I)

n= position on board

N= number of rolls required to finish the game

G= distribution for random variable (integer) between 1 and 6

Initialize n=0, N=0

N=N+1

Generate t ~ G

n=n+t

If n>20, n=n-t

If n==3, n=11

If n==15, n=19

If n==13, n=4

If n==17, n=10

Repeat the above until n≠20

Page 4: Snake and Ladder problem

Code written in MATLAB for Case I:

for i=1:20000 n=0; %/ number on the board%/ N=0; %/ number of tries for a ith game%/ while(n~=20) N=N+1; t=randi([1,6]); %/ t is number appearing on die after each roll%/ n=n+t; if n>20 n=n-t; end if n==3 n=11; end if n==15 n=19; end if n==13 n=4; end if n==17 n=10; end end game(i)=N;endmean(game);

Page 5: Snake and Ladder problem

Output:

Expected value of N= 11.57

Histogram showing frequency of number of rolls (N) required to finish the game for 20000 trials

Page 6: Snake and Ladder problem

Algorithm (Case II)

n= position on board

N= number of rolls required to finish the game

G= distribution for random variable (integer) between 1 and 6

Initialize n=0, N=0

N=N+1

Generate t ~ G

n=n+t

If n>20, n=20

If n==3, n=11

If n==15, n=19

If n==13, n=4

If n==17, n=10

Repeat the above until n≠20

Page 7: Snake and Ladder problem

Code written in MATLAB for Case II:

for i=1:20000 n=0; %/ number on the board%/ N=0; %/ number of tries for a ith game%/ while(n~=20) N=N+1; t=randi([1,6]); %/ t is randomly generated dice number%/ n=n+t; if n>20 n=20; end if n==3 n=11; end if n==15 n=19; end if n==13 n=4; end if n==17 n=10; end end game(i)=N;endmean(game);

Page 8: Snake and Ladder problem

Output:

Expected value of N= 7.028

Histogram showing frequency of number of rolls (N) required to finish the game for 20000 trials