Jump to content

Snake and ladder: Difference between revisions

m (Thundergnat moved page Snake And Ladder to Snake and Ladder: Follow normal task title capitalization policy)
Line 19:
<br><br>
 
=={{header|AWK}}==
<lang AWK>
# syntax: GAWK -f SNAKE_AND_LADDER.AWK [players]
# example: GAWK -f SNAKE_AND_LADDER.AWK 3
BEGIN {
players = (ARGV[1] ~ /^[0-9]+$/) ? ARGV[1] : 2
load_board()
show_board()
if (players == 0) { exit(0) }
for (i=1; i<=players; i++) {
sq_arr[i] = 1 # all players start on square 1
}
srand() # seed rand() with time of day
while (1) {
printf("\nTurn #%d\n",++turns)
for (i=1; i<=players; i++) {
ns = turn(i,sq_arr[i])
if (ns == 100) {
printf("Player %d wins after %d moves\n",i,moves)
exit(0)
}
sq_arr[i] = ns
}
}
}
function load_board() {
# index < value is a ladder
# index > value is a snake
sl_arr[ 4] = 14
sl_arr[ 9] = 31
sl_arr[17] = 7
sl_arr[20] = 38
sl_arr[28] = 84
sl_arr[40] = 59
sl_arr[51] = 67
sl_arr[54] = 34
sl_arr[62] = 19
sl_arr[63] = 81
sl_arr[64] = 60
sl_arr[71] = 91
sl_arr[87] = 24
sl_arr[93] = 73
sl_arr[95] = 75
sl_arr[99] = 78
}
function show_board( board_arr,i,pos,row,L,S) {
PROCINFO["sorted_in"] = "@ind_num_asc"
# populate board with cell numbers
for (i=1; i<=100; i++) {
board_arr[i] = i
}
# add in Snakes & Ladders
for (i in sl_arr) {
board_arr[sl_arr[i]] = board_arr[i] = (i+0 < sl_arr[i]) ? sprintf("L%02d",++L) : sprintf("S%02d",++S)
}
# print board
print("board: L=ladder S=snake")
for (row=10; row>=1; row--) {
if (row ~ /[02468]$/) {
pos = row * 10
for (i=1; i<=10; i++) {
printf("%5s",board_arr[pos--])
}
}
else {
pos = row * 10 - 9
for (i=1; i<=10; i++) {
printf("%5s",board_arr[pos++])
}
}
printf("\n")
}
}
function turn(player,square, position,roll) {
while (1) {
moves++
roll = int(rand() * 6) + 1 # roll die
printf("Player %d on square %d rolls a %d",player,square,roll)
if (square + roll > 100) {
printf(" but cannot move\n")
}
else {
square += roll
printf(" and moves to square %d\n",square)
if (square == 100) {
return(100)
}
position = (square in sl_arr) ? sl_arr[square] : square
if (square < position) {
printf("Yay! Landed on a ladder so climb up to %d\n",position)
}
else if (position < square) {
printf("Oops! Landed on a snake so slither down to %d\n",position)
}
square = position
}
if (roll < 6) {
return(square)
}
printf("Rolled a 6 so roll again\n")
}
}
</lang>
<p>Sample command and output:</p>
<pre>
GAWK -f SNAKE_AND_LADDER.AWK 3
 
board: L=ladder S=snake
100 S08 98 97 96 S07 94 S06 92 L08
L07 82 83 L04 85 86 S05 88 89 90
80 79 S08 77 76 S07 74 S06 72 L08
61 S03 L07 S04 65 66 L06 68 69 70
S04 L05 58 57 56 55 S02 53 52 L06
41 42 43 44 45 46 47 48 49 50
L05 39 L03 37 36 35 S02 33 32 L02
21 22 23 S05 25 26 27 L04 29 30
L03 S03 18 S01 16 15 L01 13 12 11
1 2 3 L01 5 6 S01 8 L02 10
 
Turn #1
Player 1 on square 1 rolls a 4 and moves to square 5
Player 2 on square 1 rolls a 5 and moves to square 6
Player 3 on square 1 rolls a 3 and moves to square 4
Yay! Landed on a ladder so climb up to 14
 
Turn #2
Player 1 on square 5 rolls a 2 and moves to square 7
Player 2 on square 6 rolls a 6 and moves to square 12
Rolled a 6 so roll again
Player 2 on square 12 rolls a 3 and moves to square 15
Player 3 on square 14 rolls a 2 and moves to square 16
 
Turn #3
Player 1 on square 7 rolls a 5 and moves to square 12
Player 2 on square 15 rolls a 1 and moves to square 16
Player 3 on square 16 rolls a 1 and moves to square 17
Oops! Landed on a snake so slither down to 7
 
...
 
Turn #44
Player 1 on square 98 rolls a 5 but cannot move
Player 2 on square 47 rolls a 2 and moves to square 49
Player 3 on square 89 rolls a 5 and moves to square 94
 
Turn #45
Player 1 on square 98 rolls a 2 and moves to square 100
Player 1 wins after 155 moves
</pre>
=={{header|C}}==
{{trans|C++}}
477

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.