Rebol3 Code Examplex
Pig the dice game
A simple risk-and-reward dice game where you roll to build a turn score, but rolling a 1 wipes out that turn’s points.
Rebol [
title: "Rosetta code: Pig the dice game"
file: %Pig_the_dice_game.r3
url: https://rosettacode.org/wiki/Pig_the_dice_game
]
pig-dice-game: function [
"Play a two-or-more player game of Pig dice"
players [integer!] "Number of players"
max-score [integer!] "Score needed to win"
][
safe-score: array/initial players 0 ;; banked scores per player
score: 0 ;; current turn's unbanked score
player: 1 ;; current player
forever [
print ajoin ["Player " player ": (" safe-score/:player ", " score ") Rolling? (Y)"]
switch/default wait-key [
#"y" [ ;; roll
rolled: random 6
print [" Rolled" rolled]
either rolled = 1 [
print [
" Bust! you lose" score
"but still keep your previous" safe-score/:player
newline
]
score: 0 ;; lose turn score
player: player % players + 1
][
score: score + rolled ;; accumulate
]
]
#"n" space [ ;; bank and pass
safe-score/:player: safe-score/:player + score
if safe-score/:player >= max-score [break]
print [" Sticking with" safe-score/:player newline]
score: 0
player: player % players + 1
]
][
print "Game interupted!" ;; any other key quits
exit
]
]
print ["^/Player" player "wins with a score of" safe-score/:player]
]
pig-dice-game 2 20Output:
Player 1: (0, 0) Rolling? (Y)
Rolled 4
Player 1: (0, 4) Rolling? (Y)
Rolled 3
Player 1: (0, 7) Rolling? (Y)
Rolled 3
Player 1: (0, 10) Rolling? (Y)
Rolled 1
Bust! you lose 10 but still keep your previous 0
Player 2: (0, 0) Rolling? (Y)
Rolled 2
Player 2: (0, 2) Rolling? (Y)
Rolled 2
Player 2: (0, 4) Rolling? (Y)
Rolled 1
Bust! you lose 4 but still keep your previous 0
Player 1: (0, 0) Rolling? (Y)
Rolled 1
Bust! you lose 0 but still keep your previous 0
Player 2: (0, 0) Rolling? (Y)
Rolled 6
Player 2: (0, 6) Rolling? (Y)
Rolled 6
Player 2: (0, 12) Rolling? (Y)
Rolled 3
Player 2: (0, 15) Rolling? (Y)
Rolled 2
Player 2: (0, 17) Rolling? (Y)
Sticking with 17
Player 1: (0, 0) Rolling? (Y)
Rolled 1
Bust! you lose 0 but still keep your previous 0
Player 2: (17, 0) Rolling? (Y)
Rolled 2
Player 2: (17, 2) Rolling? (Y)
Rolled 6
Player 2: (17, 8) Rolling? (Y)
Player 2 wins with a score of 25