DATA Step, Macro, Functions and more

Play Chutes and Ladders with me

Reply
Trusted Advisor
Posts: 1,301

Play Chutes and Ladders with me

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(keySmiley Tongues)=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
Contributor
Posts: 23

Play Chutes and Ladders with me

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.

Trusted Advisor
Posts: 1,301

Play Chutes and Ladders with me

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;

Contributor
Posts: 23

Play Chutes and Ladders with me

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.

Trusted Advisor
Posts: 1,301

Play Chutes and Ladders with me

No problem, it is why we are all here.

Ask a Question
Discussion stats
  • 4 replies
  • 967 views
  • 0 likes
  • 2 in conversation