BookmarkSubscribeRSS Feed
FriedEgg
SAS Employee

Chutes and Ladders is a board game during which one or more players, moving alternately, move their tokens along the numbered spaces on the board according to the roll of a single six-sided die; the tokens are initially off-the-board at what is effectively space zero.  Landing on a space with the bottom of a ladder promotes the token to the space at the top of said ladder.  Landing on a space at the top of a chute demotes the token to the space at the bottom of the chute.  The goal is to reach space 100, exactly; if the roll of the die would take the token past space 100, the token remains where it is and play passes to the next player.

http://artsandgamecraft.files.wordpress.com/2010/06/chutes-and-ladders-1.jpg

1. What is the minimum number of rolls required to reach space 100?

2. For a single player, what is the average number of rolls required to reach space 100?

3. For n players, what is the average number of rolls until one of the players reaches space 100 and ends the game?

You can also do this for the predecessor of the chutes and ladder game called Vaikuntapaali or Paramapada Sopanam from India in the 16th century.  It has the same basic rules however the total number of spaces and locations of the chutes/snakes and ladders are different.

http://artsandgamecraft.files.wordpress.com/2010/06/vaikuntapali.jpg

SPOILER:  Below is my obscured answer.... Don't cheat Smiley Happy

%let games=10000; *number of gamesto play;

%let players=1; *number of playersplaying;

data transitions;

input from to @@;

cards;

1 38 4 14 9 31

16 6

21 42 28 84

36 44

47 26 49 11

51 67 56 53

62 19 64 60

71 91 80 100

87 24

93 73 95 75 98 78

;

run;

data _null_;

length from to game ps rolls roll tot avg 8;

declare hash ha(dataset:'transitions');

ha.definekey('from');

ha.definedata(all:'y');

ha.definedone();

declare hash ga(ordered:'a');

ga.definekey('rolls','game');

ga.definedata('game','rolls');

ga.definedone();

do game=1 to &games*&players;

ps=0; *present space;

rolls=0;

do until(ps=100);

  roll=rand('table',1/6,1/6,1/6,1/6,1/6,1/6);

  if ps+roll<=100 then

   do;

     ps+roll;

     if ha.find(key:ps)=0 then ps=to;

   end;

  rolls+1;

end;

ga.add();

end;

declare hiter iter('ga');

tot=0;

iter.first();

put 'Shortest game:' game= rolls=;

do while(iter.next()=0);

tot+rolls;

end;

avg=tot/(&games*&players);

put 'Longest game:' game= rolls=;

put 'Average length:' avg=;

run;

I do not properly induce logic for calculating the results of games with multiple players, but logically it is not difficult to desern that generally as the number of players increases, the chance to beat the average game length increases and so the average length for more players should decrease relative to the average for a single player...


chutes-and-ladders-1[1].jpgvaikuntapali[1].jpg
4 REPLIES 4
gfarkas
Calcite | Level 5

I think your solution is missing something. When I execute it in 9.3, I get the following in my log:

NOTE: Variable from is uninitialized.

NOTE: Variable to is uninitialized.

FriedEgg
SAS Employee

This is not a real issue.  It is more of a by-product of how I chose to initialize my scalar elements for the hash object.  It you wanted to remove these notes you could do either of the following:

data _null_;

length from to game ps rolls roll tot avg 8;

declare hash ha(dataset:'transitions');

ha.definekey('from');

ha.definedata(all:'y');

ha.definedone();

call missing(from,to); *this will inititaze the variables;

*now the rest of the original code;

data _null_;

if 0 the

do;

  length from to game ps rolls roll tot avg 8;

  set transitions; *this will initialize the variables;

end;

declare hash ha(dataset:'transitions');

ha.definekey('from');

ha.definedata(all:'y');

ha.definedone();

*now the rest of the original code;

gfarkas
Calcite | Level 5

Thanks for the reply. I'm definitely a novice when it comes to using the hash object, but what you wrote makes sense in general.

FriedEgg
SAS Employee

No problem, it is why we are all here.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 2593 views
  • 0 likes
  • 2 in conversation