21 game: Difference between revisions

12,732 bytes added ,  3 months ago
m
 
(12 intermediate revisions by 6 users not shown)
Line 1,048:
quit
goodbye</pre>
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="qbasic">PLAYER = 1
COMP = 0
 
sum = 0
total = 0
turn = int(rand + 0.5)
dim precomp = {1, 1, 3, 2}
 
while sum < 21
turn = 1 - turn
print "The sum is "; sum
if turn = PLAYER then
print "It is your turn."
while total < 1 or total > 3 or total + sum > 21
input "How many would you like to total? ", total
end while
else
print "It is the computer's turn."
total = precomp[sum mod 4]
print "The computer totals ", total, "."
end if
print
sum += total
total = 0
end while
 
if turn = PLAYER then
print "Congratulations. You win."
else
print "Bad luck. The computer wins."
end if</syntaxhighlight>
 
==={{header|Chipmunk Basic}}===
{{trans|Commodore BASIC}}
{{works with|Chipmunk Basic|3.6.4}}
{{works with|MSX BASIC}}
<syntaxhighlight lang="qbasic">100 rem 21 game
110 rem for rosetta code
120 '
130 rem initialization
140 l$ = chr$(157) : rem left cursor
150 dim p$(2),hc(2),ca(4) : hc(1) = 0 : hc(2) = 0 : rem players
160 ca(0) = 1 : ca(1) = 1 : ca(2) = 3 : ca(3) = 2 : rem computer answers
170 dim cn$(6) : for i = 1 to 6 : read cn$(i) : next i : rem computer names
180 def fnm(X)=(X-(INT(X/4))*4):REM modulo function
190 '
200 rem optionally set screen colors here
210 cls
220 print " 21 GAME"
230 print : print " The goal of this game is to take turns"
240 print " adding the value of either 1, 2, or 3"
250 print " to a running total. The first player"
260 print " to bring the total to 21..."
270 print : print " ... WINS THE GAME!"
280 print : gosub 1110
290 for p = 1 to 2
300 '
310 rem game setup and get players
320 for p = 1 to 2
330 print : print "Player ";p;l$;", [H]uman or [C]omputer? ";
340 k$ = inkey$ : if k$ <> "c" and k$ <> "h" then 340
350 print k$ : hc(p) = (k$ = "c")
360 print : print "Player";p;l$ "," : print "Enter your name"; : if hc(p) then goto 400
370 input p$(p)
380 next p
390 goto 420
400 gosub 1340 : print "? ";p$(p)
410 next p
420 print
430 for p = 1 to 2 : print p;l$;". ";p$(p) : next p
440 print : print "Is this correct (y/n)? ";
450 k$ = inkey$ : if k$ <> "y" and k$ <> "n" then 450
460 print k$ : if k$ = "n" then goto 290
470 print : print "Who will play first (1 or 2)? ";
480 k$ = inkey$ : if k$ < "1" or k$ > "2" then 480
490 fp = asc(k$)-48 : print k$ : print
500 print "Okay, ";p$(fp);" will play first." : print : gosub 1110
510 cls
520 '
530 rem start main game loop
540 pi = fp : rt = 0
550 print "Total so far:";rt
560 print : print p$(pi);"'s turn."
570 if hc(pi) then gosub 1240
580 if not hc(pi) then gosub 1170
590 rt = rt+ad
600 if rt = 21 then goto 680
610 if rt > 21 then print : print p$(pi);" loses by going over 21!!" : goto 700
620 pi = pi+1 : if pi > 2 then pi = 1
630 goto 550
640 print : print " ... WINS THE GAME!"
650 print : gosub 1110
660 '
670 for p = 1 to 2
680 rem winner winner chicken dinner
690 print : print "21! ";p$(pi);" wins the game!!!"
700 print : print "Would you like to play again? ";
710 k$ = inkey$ : if k$ <> "n" and k$ <> "y" then 710
720 print k$
730 if k$ = "n" then print : print "Okay, maybe another time. Bye!" : end
740 goto 200
750 print k$ : hc(p) = (k$ = "c")
760 print : print "Player";p;l$ "," : print "Enter your name"; : if hc(p) then goto 800
770 input p$(p)
780 next p
790 goto 820
800 gosub 1340 : print "? ";p$(p)
810 next p
820 print : for p = 1 to 2 : print p;l$;". ";p$(p) : next p
830 print : print "Is this correct (y/n)? ";
840 k$ = inkey$ : if k$ <> "y" and k$ <> "n" then 840
850 print k$ : if k$ = "n" then goto 660
860 print : print "Who will play first (1 or 2)? ";
870 k$ = inkey$ : if k$ < "1" or k$ > "2" then 870
880 fp = asc(k$)-48 : print k$ : print
890 print "Okay, ";p$(fp);" will play first." : print : gosub 1110
900 '
910 rem start main game loop
920 pi = fp : rt = 0
930 print chr$(147);"Total so far: ";rt
940 print : print p$(pi);"'s turn."
950 if hc(pi) then gosub 1240
960 if not hc(pi) then gosub 1170
970 rt = rt+ad
980 if rt = 21 then goto 1030
990 if rt > 21 then print : print p$(pi);" loses by going over 21!!" : goto 1050
1000 pi = pi+1 : if pi > 2 then pi = 1
1010 goto 930
1020 '
1030 rem winner winner chicken dinner
1040 print : print "21! ";p$(pi);" wins the game!!!"
1050 print : print "Would you like to play again? ";
1060 k$ = inkey$ : if k$ <> "n" and k$ <> "y" then 1060
1070 print k$
1080 if k$ = "n" then print : print "Okay, maybe another time. Bye!" : end
1090 goto 490
1100 '
1110 rem pause for keypress
1120 z$ = " Press a key to continue. "
1130 print z$
1140 k$ = inkey$ : if k$ = "" then 1140
1150 return
1160 '
1170 rem human player move
1180 print : print "How much to add,"
1190 print "1, 2, or 3 (0 to quit)"; : input ad
1200 if ad < 0 or ad > 3 then print : print "Illegal amount. Try again." : goto 1180
1210 if ad = 0 then print : print "Game was ended by ";p$(pi);"." : end
1220 return
1230 '
1240 rem computer player move
1250 print : print "Thinking...";
1260 tt = int(rnd(1)*10)
1270 for t = 1 to tt : print "."; : for i = 1 to 250 : next i : next t : print
1280 rm = fn m(rt)
1290 ad = ca(rm)
1300 print : print p$(pi);" adds ";ca(rm);l$;"."
1310 for t = 1 to 1000 : next t
1320 return
1330 '
1340 rem pick a computer name
1350 pn = int(rnd(1)*6)+1 : t$ = cn$(pn)
1360 if t$ = p$(p-1) then goto 1350
1370 p$(p) = t$
1380 return
1390 '
1400 rem some computer names to pick from
1410 data "Commodore 64","VIC-20","Commodore 128"
1420 data "PET","Plus/4","Commodore 16"</syntaxhighlight>
 
==={{header|IS-BASIC}}===
<syntaxhighlight lang="is-basic">100 PROGRAM "21Game.bas"
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=NOT TURN
190 SET #102:INK 3:PRINT "The sum is";SUM:SET #102:INK 1
200 IF TURN 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=RND(3)+1
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 THEN
320 PRINT "Congratulations. You win."
330 ELSE
340 PRINT "Bad luck. The computer wins."
350 END IF
360 END
370 DEF READKEY
380 DO
390 LET T$=INKEY$
400 LOOP WHILE T$>"3" OR T$<"1"
410 PRINT T$:LET READKEY=VAL(T$)
420 END DEF </syntaxhighlight>
 
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
The [[#Chipmunk Basic|Chipmunk Basic]] solution works without any changes.
 
==={{header|PureBasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="purebasic">If OpenConsole()
PLAYER.i = 1
COMP.i = 0
sum.i = 0
total.i = 0
turn.i = Random(1) + 0.5
Dim precomp.i(3)
precomp(0) = 1
precomp(1) = 1
precomp(2) = 3
precomp(3) = 2
While sum < 21
turn = 1 - turn
PrintN("The sum is " + Str(sum))
If turn = PLAYER
PrintN("It is your turn.")
While total < 1 Or total > 3 Or total + sum > 21
Print("How many would you like to total? ")
total = Val(Input())
Wend
Else
PrintN("It is the computer's turn.")
total = precomp(sum % 4)
PrintN("The computer totals " + Str(total) + ".")
EndIf
PrintN("")
sum + total
total = 0
Wend
If turn = PLAYER
PrintN("Congratulations. You win.")
Else
PrintN("Bad luck. The computer wins.")
EndIf
Input()
EndIf</syntaxhighlight>
 
==={{header|QBasic}}===
{{trans|FreeBASIC}}
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">PLAYER = 1
COMP = 0
RANDOMIZE TIMER
 
sum = 0
total = 0
turn = INT(RND + 0.5)
DIM precomp(0 TO 3)
precomp(0) = 1 : precomp(1) = 1
precomp(2) = 3 : precomp(3) = 2
 
WHILE sum < 21
turn = 1 - turn
PRINT "The sum is "; sum
IF turn = PLAYER THEN
PRINT "It is your turn."
WHILE total < 1 OR total > 3 OR total + sum > 21
INPUT "How many would you like to total? ", total
WEND
ELSE
PRINT "It is the computer's turn."
total = precomp(sum MOD 4)
PRINT "The computer totals"; total; "."
END IF
PRINT
sum = sum + total
total = 0
WEND
 
IF turn = PLAYER THEN
PRINT "Congratulations. You win."
ELSE
PRINT "Bad luck. The computer wins."
END IF</syntaxhighlight>
 
==={{header|True BASIC}}===
{{trans|QBasic}}
<syntaxhighlight lang="qbasic">LET player = 1
LET comp = 0
RANDOMIZE
LET sum = 0
LET total = 0
LET turn = INT(RND+0.5)
DIM precomp(0 TO 3)
LET precomp(0) = 1
LET precomp(1) = 1
LET precomp(2) = 3
LET precomp(3) = 2
DO WHILE sum < 21
LET turn = 1-turn
PRINT "The sum is "; sum
IF turn = player THEN
PRINT "It is your turn."
DO WHILE total < 1 OR total > 3 OR total+sum > 21
INPUT prompt "How many would you like to total? ": total
LOOP
ELSE
PRINT "It is the computer's turn."
LET total = precomp(remainder(round(sum),4))
PRINT USING "The computer totals #.": total
END IF
PRINT
LET sum = sum+total
LET total = 0
LOOP
IF turn = player THEN PRINT "Congratulations. You win." ELSE PRINT "Bad luck. The computer wins."
END</syntaxhighlight>
 
==={{header|PureBasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vb">If OpenConsole()
PLAYER.i = 1
COMP.i = 0
sum.i = 0
total.i = 0
turn.i = Random(1) + 0.5
Dim precomp.i(3)
precomp(0) = 1
precomp(1) = 1
precomp(2) = 3
precomp(3) = 2
While sum < 21
turn = 1 - turn
PrintN("The sum is " + Str(sum))
If turn = PLAYER
PrintN("It is your turn.")
While total < 1 Or total > 3 Or total + sum > 21
Print("How many would you like to total? ")
total = Val(Input())
Wend
Else
PrintN("It is the computer's turn.")
total = precomp(sum % 4)
PrintN("The computer totals " + Str(total) + ".")
EndIf
PrintN("")
sum + total
total = 0
Wend
If turn = PLAYER
PrintN("Congratulations. You win.")
Else
PrintN("Bad luck. The computer wins.")
EndIf
Input()
EndIf</syntaxhighlight>
 
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vb">PLAYER = 1
COMP = 0
 
sum = 0
total = 0
turn = int(ran() + 0.5)
dim precomp(3)
precomp(0) = 1 : precomp(1) = 1
precomp(2) = 3 : precomp(3) = 2
 
while sum < 21
turn = 1 - turn
print "The sum is ", sum
if turn = PLAYER then
print "It is your turn."
while total < 1 or total > 3 or total + sum > 21
input "How many would you like to total? " total
wend
else
print "It is the computer's turn."
total = precomp(mod(sum, 4))
print "The computer totals ", total, "."
fi
print
sum = sum + total
total = 0
wend
 
if turn = PLAYER then
print "Congratulations. You win."
else
print "Bad luck. The computer wins."
fi</syntaxhighlight>
 
=={{header|bash}}==
Line 2,289 ⟶ 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,308 ⟶ 2,715:
sleep 1
if sum mod 4 = 1
n = randomrandint 3
else
n = 4 - (sum + 3) mod 4
Line 2,325 ⟶ 2,732:
print "Sorry, you lost"
.
.
.</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Factor}}==
Line 2,544 ⟶ 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 2,777 ⟶ 3,219:
ControlSetIntegerValue( _sum, sum )
string( who ) += "+" + chr$( add or 48 ) + " "
print cr;cr;cr;string( _human );cr;cr;string( _robot )))
if sum < 21
if who == _robot then msg = @"Your turn..." else msg = @"My turn..."
Line 3,369 ⟶ 3,811:
when (y == 1) $ do
putStrLn $ (snd names) ++ " has won the game"</syntaxhighlight>
 
=={{header|IS-BASIC}}==
<syntaxhighlight lang="is-basic">100 PROGRAM "21Game.bas"
110 RANDOMIZE
120 LET SUM,ADD=0
130 LET TURN=RND(2)
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-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
340 PRINT "Bad luck. The computer wins."
350 END IF
360 END
370 DEF READKEY
380 DO
390 LET T$=INKEY$
400 LOOP WHILE T$>"3" OR T$<"1"
410 PRINT T$:LET READKEY=VAL(T$)
420 END DEF </syntaxhighlight>
 
=={{header|J}}==
Line 4,033 ⟶ 4,440:
</syntaxhighlight>
 
=={{header|Koka}}==
<syntaxhighlight lang="koka">
import std/num/random
import std/os/readline
 
effect exit
final ctl exit(): ()
 
type player
Player
Computer
 
fun other(p: player): player
match p
Player -> Computer
Computer -> Player
 
fun show(p: player): string
match p
Player -> "Player"
Computer -> "Computer"
 
fun get-selection(max-int: int)
val i = readline().trim().parse-int()
match i
Just(x) | x >= 1 && x <= max-int -> x
_ ->
println("Please enter a number between 1 and " ++ max-int.show ++ " or press q to quit")
get-selection(max-int)
 
fun play(p: player, total: int)
println("Total: " ++ total.show)
match total
21 ->
// The player who reaches 21 wins the game, which is the player in the last loop
println(p.other.show ++ " wins!")
return ()
_ -> ()
val max = if total + 3 > 21 then 21 - total else 3
match p
Player ->
println("Player's turn")
val selection = get-selection(max)
play(p.other, total + selection)
Computer ->
println("Computer's turn")
val selection = (random-int() % max) + 1
println("Computer chooses " ++ selection.show)
play(p.other, total + selection)
 
fun main()
with final ctl exit() ()
"21 is a two player game. The game is played by choosing a number (1, 2, 3) to".println
"be added to the running total. The game is won by the player whose chosen number".println
"causes the running total to reach exactly 21. The running total starts at zero.".println
"".println
"You can quit the game at any time by typing 'q'.".println
val playerGoesFirst = random-bool()
val p1 = if playerGoesFirst then Player else Computer
play(p1, 0)
</syntaxhighlight>
=={{header|Lua}}==
<syntaxhighlight lang="lua">
Line 4,085 ⟶ 4,553:
end
</syntaxhighlight>
 
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Line 6,125 ⟶ 6,592:
</syntaxhighlight>
 
=={{header|rustRust}}==
<syntaxhighlight lang="rust">use rand::Rng;
use std::io;
Line 6,722 ⟶ 7,189:
{{libheader|Wren-fmt}}
{{libheader|Wren-ioutil}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Conv
import "./ioutil" for Input
import "random" for Random
 
2,022

edits