/* chutes and ladders - tct - 3/30/15 http://primepuzzle.com/tc/chutes_and_ladders.html */ #include #include #include int rnd(int i) {return rand()%i+1;} int main(void) { int pos,roll,moves; srand((unsigned)time(NULL)); int board[101]={00, 38,00,00,00,14,00,00,00,31,00, 00,00,00,00,00,06,00,00,00,00, 42,00,00,00,00,00,00,84,00,00, 00,00,00,00,00,44,00,00,00,00, 00,00,00,00,00,00,26,00,11,00, 67,00,00,00,00,53,00,00,00,00, 00,19,00,60,00,00,00,00,00,00, 91,00,00,00,00,00,00,00,00,100, 00,00,00,00,00,00,24,00,00,00, 00,00,73,00,75,00,00,78,00,00}; roll=rnd(6);pos=roll;moves=1; printf("\n\nRoll: %d",roll); printf("\nYou advance to square %d",pos); while (pos!=100) { // play game if (board[pos]pos) { // hit bottom of ladder printf("\nGreat! You landed on a ladder at %d and climb to square %d",pos,board[pos]); pos=board[pos];++moves; } if (pos!=100) { roll=rnd(6); while (pos+roll>100) roll=rnd(6); // roll again if off board pos+=roll;++moves; printf("\nRoll: %d",roll); printf("\nYou advance to square %d",pos); }} printf("\n\nYou reached the goal in %d moves!\n",moves); printf("\n");exit(1); }