21 game: Difference between revisions

m
imported>Lacika7
No edit summary
 
(6 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,951 ⟶ 2,953:
</pre>
 
=={{header|Forth}}==
<syntaxhighlight lang="forth">: READKEY
1+ BEGIN
Line 6,589 ⟶ 6,592:
</syntaxhighlight>
 
=={{header|rustRust}}==
<syntaxhighlight lang="rust">use rand::Rng;
use std::io;
2,022

edits