ANI Coding Competition — Challenge 3

computer and cup of coffee - Allstate NI Coding competiton, challenge 3.

Here is our final challenge! You could win £50!

It's Snakes and Ladders this time! Once again, there are errors in the code that are placed within the code. Just identify an error in any line of code, then click the link at the bottom of the page for a chance to win £50! Fill out the form and, if you were successful, you'll be added to or prize draw. Three final winners will be drawn next Monday, will you be one of the lucky ones?

1  BEGIN # Snakes and ladders game #

2   # create the board #

3   INT max square = 100;

4   [ 1 : max square ]INT board;

5   FOR i TO max square DO board[ i ] := 0 OD;

6   board[ 4 ] := 14;

7   board[ 9 ] := 31;

8   board[ 17 ] := 7;

9   board[ 20 ] := 38;

10 board[ 28 ] := 84;

11 board[ 40 ] := 59;

12 board[ 51 ] := 67;

13 board[ 54 ] := 34;

14 board[ 62 ] := 19;

15 board[ 63 ] := 81;

16 board[ 64 ] := 60;

17 board[ 71 ] := 91;

18 board[ 87 ] := 24;

19 board[ 93 ] := 73;

20 board[ 95 ] := 75;

21 board[ 99 ] := 78;

22 # get options #

23 INT players := 0;

24 WHILE print( ( "How many players ( 1-10, 0 or less to quit ) ? " ) );

25 read( ( players, newline ) );

26 players > 10

27 DO

28 print( ( "Too many players" ) )

29 OD;

30 IF players > 0 THEN

31 # the user does want to play #

32 BOOL win on gt max := FALSE;

33 WHILE print( ( "Allow position > ", whole( max square, 0 ), " to win [y/n] ? " ) );

34 CHAR c;

35 read( ( c, newline ) );

36 win on gt max := c = "y";

37 c /= "y" AND c /= "n"

38 DO SKIP OD;

39 BOOL extra roll for 6 := FALSE;

40 WHILE print( ( "Roll again on a 6 [y/n] ? " ) );

41 CHAR c;

42 read( ( c, newline ) );

43 extra roll for 6 := c = "y";

44 c /= "y" AND c /= "n"

45 DO SKIP OD;

46 [ 1 : players ]INT position;

47 FOR i TO players DO position[ i ] := 0 OD;

48 # play #

49 BOOL game over := TRUE;

50 WHILE NOT game over DO

51 # handle each players move #

52 print( ( newline ) );

53 FOR p TO players WHILE NOT game over DO

54 WHILE

55 INT roll = ENTIER ( next random * 6 ) + 1;

56 STRING player id = "Player " + whole( p, 0 );

57 print( ( player id

58 , IF position[ p ] < 1 THEN "" ELSE " (on square " + whole( position[ p ], 0 ) + ")" FI

59 , " rolls a "

60 , whole( roll, 0 )

61 , newline

62 )

63 );

64 IF position[ p ] + roll > max square

65 AND NOT win on gt max

66 THEN

67 # the player would be off the board #

68 print( ( " ", player id, " cannot move", newline ) )

69 ELSE

70 # the player can move roll squares #

71 position[ p ] +:= roll;

72 IF position[ p ] > max square THEN position[ p ] := max square FI;

73 print( ( " and moves to square ", whole( position[ p ], 0 ) ) );

74 IF board[ position[ p ] ] > position[ p ] THEN

75 # landed on a ladder #

76 position[ p ] := board[ position[ p ] ];

77 print( ( " - which is a ladder :) ", player id, " climbs to up ", whole( position[ p ], 0 ), newline ) )

78 ELIF board[ position[ p ] ] /= 0 THEN

79 # landed on a snake #

80 position[ p ] := board[ position - 1[ p ] ];

81 print( ( " - which is a snake :( ", player id, " slides down to ", whole( position[ p ], 0 ), newline ) )

82 ELIF position[ p ] >= max square THEN

83 # the player has won #

84 print( ( " and wins the game!", newline ) );

85 game over := TRUE

86 ELSE

87 # not a ladder, snake or winning move #

88 print( ( newline ) )

89 FI

90 FI;

91 roll = 6 AND extra roll for 6 AND NOT game over

92 DO

93 print( ( player id, " can roll again...", newline ) )

94 OD

95 OD

96 OD

97 FI

48 END

Enter to win here