21 game: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
(7 intermediate revisions by 2 users not shown)
Line 1,226:
110 RANDOMIZE
120 LET SUM,ADD=0
130 LET TURN=RND(2)-1
140 CLEAR SCREEN
150 PRINT "21 is a two player game, the game is played by choosing a number (1, 2, or 3) to be added to the running total. The game is won by the player whose chosen number causes the running total to reach exactly 21."
160 PRINT "The running total starts at zero. One player will be the computer.":PRINT
170 DO
180 LET TURN=1-NOT TURN
190 SET #102:INK 3:PRINT "The sum is";SUM:SET #102:INK 1
200 IF TURN=1 THEN
210 PRINT "It is your turn.":PRINT "How many would you like to add? (1-3): ";
220 LET ADD=READKEY
230 IF ADD>21-SUM THEN PRINT "You can only add";21-SUM
240 ELSE
250 LET ADD=4-MOD((SUM-1),4)
260 IF ADD=4 THEN LET ADD=MIN(RND(3)+1,SUM)
270 PRINT "It is the computer's turn.":PRINT "The computer adds";ADD
280 END IF
290 PRINT :LET SUM=SUM+ADD
300 LOOP WHILE SUM<21
310 IF TURN=1 THEN
320 PRINT "Congratulations. You win."
330 ELSE
Line 2,695:
=={{header|EasyLang}}==
The computer plays optimally
<syntaxhighlight lang="text">print "Who reaches 21, wins"
print "Who reaches 21, wins"
print "Do you want to begin (y/n)"
who = 1
Line 2,714 ⟶ 2,715:
sleep 1
if sum mod 4 = 1
n = randomrandint 3
else
n = 4 - (sum + 3) mod 4
Line 2,731 ⟶ 2,732:
print "Sorry, you lost"
.
.
.</syntaxhighlight>
 
=={{header|Factor}}==
Line 2,950 ⟶ 2,952:
 
</pre>
 
=={{header|Forth}}==
<syntaxhighlight lang="forth">: READKEY
1+ BEGIN
KEY DUP 27 = ABORT" Bye!"
48 - 2DUP > OVER 0 > AND IF
DUP 48 + EMIT CR SWAP DROP EXIT
THEN DROP
REPEAT
;
: 21GAME CLS
0 2 RND 1-
." 21 is a two player game." CR
." The game is played by choosing a number (1, 2 or 3) to be added to the running total. "
." The game is won by the player whose chosen number causes the running total to reach exactly 21."
." The running total starts at zero. One player will be the computer."
BEGIN
NOT
CR ." The sum is " OVER . CR
SWAP OVER IF
." How many would you like add?"
." (1-3) " 3 READKEY
ELSE
." It is the computer's turn."
4 OVER 1- 4 MOD -
DUP 4 = IF 3 RND 1+ MIN THEN
DUP CR ." Computer adds " . CR
THEN + SWAP
OVER 21 < NOT UNTIL
CR
IF ." Congratulations. You win."
ELSE ." Bad Luck. Computer wins."
THEN CR DROP
;</syntaxhighlight>
 
=={{header|Fortran}}==
Line 6,556 ⟶ 6,592:
</syntaxhighlight>
 
=={{header|rustRust}}==
<syntaxhighlight lang="rust">use rand::Rng;
use std::io;
2,023

edits