21 game: Difference between revisions

Content added Content deleted
(Added uBasic/4tH version)
m (syntax highlighting fixup automation)
Line 29: Line 29:
{{trans|Python: Original, with output}}
{{trans|Python: Original, with output}}


<lang 11l>F select_count(game_count)
<syntaxhighlight lang=11l>F select_count(game_count)
‘selects a random number if the game_count is less than 18. otherwise chooses the winning number’
‘selects a random number if the game_count is less than 18. otherwise chooses the winning number’
Int t
Int t
Line 90: Line 90:
V t = input(‘Another game?(press y to continue):’)
V t = input(‘Another game?(press y to continue):’)
I t != ‘y’
I t != ‘y’
L.break</lang>
L.break</syntaxhighlight>


{{out}}
{{out}}
Line 97: Line 97:
=={{header|AArch64 Assembly}}==
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<lang AArch64 Assembly>
<syntaxhighlight lang=AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program game21_64.s */
/* program game21_64.s */
Line 315: Line 315:
/* for this file see task include a file in language AArch64 assembly */
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
.include "../includeARM64.inc"
</syntaxhighlight>
</lang>




=={{header|Ada}}==
=={{header|Ada}}==
<lang Ada>with Ada.Text_IO;
<syntaxhighlight lang=Ada>with Ada.Text_IO;
with Ada.Numerics.Discrete_Random;
with Ada.Numerics.Discrete_Random;


Line 426: Line 426:
when Human => Computer);
when Human => Computer);
end loop Play_Loop;
end loop Play_Loop;
end Game_21;</lang>
end Game_21;</syntaxhighlight>


=={{header|Applesoft BASIC}}==
=={{header|Applesoft BASIC}}==
Line 437: Line 437:
WAIT instead of GET in a loop.
WAIT instead of GET in a loop.


<lang gwbasic> 5 DIM P$(2),HC(2),CA(4),CN$(6): READ CA(0),CA(1),CA(2),CA(3): FOR I = 1 TO 6: READ CN$(I): NEXT : DEF FN M(X) = (X - ( INT (X / 4)) * 4): DEF FN U(K) = K - (K > 95) * 32:L$ = " ":M$ = CHR$ (13): DATA 1,1,3,2
<syntaxhighlight lang=gwbasic> 5 DIM P$(2),HC(2),CA(4),CN$(6): READ CA(0),CA(1),CA(2),CA(3): FOR I = 1 TO 6: READ CN$(I): NEXT : DEF FN M(X) = (X - ( INT (X / 4)) * 4): DEF FN U(K) = K - (K > 95) * 32:L$ = " ":M$ = CHR$ (13): DATA 1,1,3,2
9 FOR PLAY = 0 TO 1
9 FOR PLAY = 0 TO 1
10 REM SET SCREEN COLORS HERE
10 REM SET SCREEN COLORS HERE
Line 473: Line 473:
240 END
240 END
1000 Z$ = " PRESS A KEY TO CONTINUE. ": PRINT SPC( 20 - INT ( LEN (Z$) / 2));: INVERSE : PRINT Z$;: NORMAL : WAIT 49152,128:K = PEEK (49168): RETURN
1000 Z$ = " PRESS A KEY TO CONTINUE. ": PRINT SPC( 20 - INT ( LEN (Z$) / 2));: INVERSE : PRINT Z$;: NORMAL : WAIT 49152,128:K = PEEK (49168): RETURN
2010 DATA "APPLE IIE","APPLE II PLUS","APPLE IIC","APPLE II","APPLE IIGS","MACINTOSH LC"</lang>
2010 DATA "APPLE IIE","APPLE II PLUS","APPLE IIC","APPLE II","APPLE IIGS","MACINTOSH LC"</syntaxhighlight>
=={{header|ARM Assembly}}==
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
{{works with|as|Raspberry Pi}}
<lang ARM Assembly>
<syntaxhighlight lang=ARM Assembly>
/* ARM assembly Raspberry PI */
/* ARM assembly Raspberry PI */
/* program game21.s */
/* program game21.s */
Line 695: Line 695:
/***************************************************/
/***************************************************/
.include "../affichage.inc"
.include "../affichage.inc"
</syntaxhighlight>
</lang>
<pre>
<pre>
The first move is human move.
The first move is human move.
Line 727: Line 727:
=={{header|Arturo}}==
=={{header|Arturo}}==


<lang rebol>print "-----------------------------"
<syntaxhighlight lang=rebol>print "-----------------------------"
print " Welcome to 21 Game"
print " Welcome to 21 Game"
print "-----------------------------"
print "-----------------------------"
Line 762: Line 762:
]
]
]
]
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 813: Line 813:


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang AutoHotkey>Gui, font, S16
<syntaxhighlight lang=AutoHotkey>Gui, font, S16
Gui, add, Radio, vRadioC , Cake
Gui, add, Radio, vRadioC , Cake
Gui, add, Radio, vRadioE x+0 Checked, Easy
Gui, add, Radio, vRadioE x+0 Checked, Easy
Line 897: Line 897:
Loop 3
Loop 3
GuiControl, Enable, % "Player1_" A_Index
GuiControl, Enable, % "Player1_" A_Index
return</lang>
return</syntaxhighlight>


=={{header|AWK}}==
=={{header|AWK}}==
<lang awk># Game 21 - an example in AWK language for Rosseta Code.
<syntaxhighlight lang=awk># Game 21 - an example in AWK language for Rosseta Code.


BEGIN {
BEGIN {
Line 968: Line 968:
newgame();
newgame();
}
}
}</lang>
}</syntaxhighlight>
{{Output}}
{{Output}}
<pre>
<pre>
Line 1,050: Line 1,050:


=={{header|bash}}==
=={{header|bash}}==
<lang bash>shopt -s expand_aliases
<syntaxhighlight lang=bash>shopt -s expand_aliases
alias bind_variables='
alias bind_variables='
{
{
Line 1,185: Line 1,185:
fi
fi
21_Game
21_Game
</syntaxhighlight>
</lang>


=={{header|C}}==
=={{header|C}}==
<lang C>/**
<syntaxhighlight lang=C>/**
* Game 21 - an example in C language for Rosseta Code.
* Game 21 - an example in C language for Rosseta Code.
*
*
Line 1,327: Line 1,327:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>21 Game
<pre>21 Game
Line 1,423: Line 1,423:
=={{header|C++}}==
=={{header|C++}}==
===Model View Controller===
===Model View Controller===
<lang cpp>/**
<syntaxhighlight lang=cpp>/**
* Game 21 - an example in C++ language for Rosseta Code.
* Game 21 - an example in C++ language for Rosseta Code.
*
*
Line 1,623: Line 1,623:
}
}
return EXIT_SUCCESS; // dead code
return EXIT_SUCCESS; // dead code
}</lang>
}</syntaxhighlight>


===Model View Presenter===
===Model View Presenter===
<lang cpp>/**
<syntaxhighlight lang=cpp>/**
* Game 21 - an example in C++ language for Rosseta Code.
* Game 21 - an example in C++ language for Rosseta Code.
*
*
Line 1,865: Line 1,865:
return EXIT_SUCCESS; // dead code
return EXIT_SUCCESS; // dead code
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,940: Line 1,940:


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
<lang csharp>// 21 Game
<syntaxhighlight lang=csharp>// 21 Game


using System;
using System;
Line 2,093: Line 2,093:




</syntaxhighlight>
</lang>
=={{header|Commodore BASIC}}==
=={{header|Commodore BASIC}}==


Written for BASIC v2 on the Commodore 64, this should be mostly compatible with all Commodore machines running any version of BASIC v2 and above. This implementation allows for one or both players to be either human or computer players. Also note that the computer player's "Thinking..." routine is a purely random delay meant to allow any human user time to process the flow of the game.
Written for BASIC v2 on the Commodore 64, this should be mostly compatible with all Commodore machines running any version of BASIC v2 and above. This implementation allows for one or both players to be either human or computer players. Also note that the computer player's "Thinking..." routine is a purely random delay meant to allow any human user time to process the flow of the game.


<lang gwbasic>1 rem 21 game
<syntaxhighlight lang=gwbasic>1 rem 21 game
2 rem for rosetta code
2 rem for rosetta code


Line 2,187: Line 2,187:
2000 rem some computer names to pick from
2000 rem some computer names to pick from
2010 data "Commodore 64","VIC-20","Commodore 128","PET"
2010 data "Commodore 64","VIC-20","Commodore 128","PET"
2020 data "Plus/4","Commodore 16"</lang>
2020 data "Plus/4","Commodore 16"</syntaxhighlight>


{{output}}
{{output}}
Line 2,324: Line 2,324:
print "Sorry, you lost"
print "Sorry, you lost"
.
.
.</lang>
.</syntaxhighlight>


=={{header|Factor}}==
=={{header|Factor}}==
A difficulty system has been implemented. The player chooses a difficulty from 1-10. There is a <code>difficulty in 10</code> chance that the computer opponent gets to move first. There is a <code>difficulty in 10</code> chance each turn that the computer opponent will make the optimal move (i.e. setting the total to a number of the form <code>4n+1</code>) as opposed to a random move. At difficulty level 10, it is impossible for the human player to win the game.
A difficulty system has been implemented. The player chooses a difficulty from 1-10. There is a <code>difficulty in 10</code> chance that the computer opponent gets to move first. There is a <code>difficulty in 10</code> chance each turn that the computer opponent will make the optimal move (i.e. setting the total to a number of the form <code>4n+1</code>) as opposed to a random move. At difficulty level 10, it is impossible for the human player to win the game.
{{works with|Factor|0.99 2020-07-03}}
{{works with|Factor|0.99 2020-07-03}}
<lang factor>USING: accessors combinators.random continuations formatting io
<syntaxhighlight lang=factor>USING: accessors combinators.random continuations formatting io
kernel math math.functions math.parser multiline qw random
kernel math math.functions math.parser multiline qw random
sequences ;
sequences ;
Line 2,409: Line 2,409:
.welcome nl [ <game> do-turns ] with-return ;
.welcome nl [ <game> do-turns ] with-return ;


MAIN: play-21-game</lang>
MAIN: play-21-game</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,450: Line 2,450:


=={{header|F sharp|F#}}==
=={{header|F sharp|F#}}==
<lang fsharp>
<syntaxhighlight lang=fsharp>
type Player =
type Player =
| Computer
| Computer
Line 2,516: Line 2,516:
let run () = looper Start
let run () = looper Start
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 2,545: Line 2,545:


=={{header|Fortran}}==
=={{header|Fortran}}==
<lang Fortran>! game 21 - an example in modern fortran language for rosseta code.
<syntaxhighlight lang=Fortran>! game 21 - an example in modern fortran language for rosseta code.


subroutine ai
subroutine ai
Line 2,629: Line 2,629:
end do
end do
end do
end do
end program main</lang>
end program main</syntaxhighlight>
{{out}}
{{out}}
<pre>game 21 - an example in fortran language for rosseta code.
<pre>game 21 - an example in fortran language for rosseta code.
Line 2,691: Line 2,691:


=={{Header|FreeBASIC}}==
=={{Header|FreeBASIC}}==
<lang freebasic>#define PLAYER 1
<syntaxhighlight lang=freebasic>#define PLAYER 1
#define COMP 0
#define COMP 0
randomize timer
randomize timer
Line 2,721: Line 2,721:
else
else
print "Bad luck. The computer wins."
print "Bad luck. The computer wins."
end if</lang>
end if</syntaxhighlight>


=={{header|Gambas}}==
=={{header|Gambas}}==
<lang gambas>
<syntaxhighlight lang=gambas>
' Gambas module file
' Gambas module file


Line 2,825: Line 2,825:
Endif
Endif


End</lang>
End</syntaxhighlight>


<pre>
<pre>
Line 2,888: Line 2,888:


To give the human player a reasonable chance whoever goes first, the computer always chooses randomly when the running total is below 18 but otherwise chooses the exact number needed to win the game.
To give the human player a reasonable chance whoever goes first, the computer always chooses randomly when the running total is below 18 but otherwise chooses the exact number needed to win the game.
<lang go>package main
<syntaxhighlight lang=go>package main


import (
import (
Line 2,987: Line 2,987:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 3,093: Line 3,093:
=={{header|Haskell}}==
=={{header|Haskell}}==
The computer chooses values randomly.
The computer chooses values randomly.
<lang haskell>import System.Random
<syntaxhighlight lang=haskell>import System.Random
import System.IO
import System.IO
import System.Exit
import System.Exit
Line 3,156: Line 3,156:
putStrLn $ (fst names) ++ " has won the game"
putStrLn $ (fst names) ++ " has won the game"
when (y == 1) $ do
when (y == 1) $ do
putStrLn $ (snd names) ++ " has won the game"</lang>
putStrLn $ (snd names) ++ " has won the game"</syntaxhighlight>
=={{header|J}}==
=={{header|J}}==
The main definition <tt>g21</tt> starts the game. J doesn't support niladic verbs. Although the <tt>y</tt> argument has no effect, it must be given. The empty literal vector <nowiki>''</nowiki> types quickly, hence often used to trigger niladic verbs. g21 does not superfluously penalize final sums exceeding 21.
The main definition <tt>g21</tt> starts the game. J doesn't support niladic verbs. Although the <tt>y</tt> argument has no effect, it must be given. The empty literal vector <nowiki>''</nowiki> types quickly, hence often used to trigger niladic verbs. g21 does not superfluously penalize final sums exceeding 21.
<syntaxhighlight lang=J>
<lang J>
g21=: summarize@play@setup ::('g21: error termination'"_)
g21=: summarize@play@setup ::('g21: error termination'"_)


Line 3,197: Line 3,197:
write=: 1!:2 4: NB. unused
write=: 1!:2 4: NB. unused
format=: ''&$: :([: ; (a: , [: ":&.> [) ,. '{}' ([ (E. <@}.;._1 ]) ,) ])
format=: ''&$: :([: ; (a: , [: ":&.> [) ,. '{}' ([ (E. <@}.;._1 ]) ,) ])
</syntaxhighlight>
</lang>
<pre>
<pre>
g21''
g21''
Line 3,257: Line 3,257:
The computer strategy is to see if a valid value will win the game. If so, that value is selected. Otherwise, a random value among the valid values is selected.
The computer strategy is to see if a valid value will win the game. If so, that value is selected. Otherwise, a random value among the valid values is selected.


<lang java>
<syntaxhighlight lang=java>
import java.util.Random;
import java.util.Random;
import java.util.Scanner;
import java.util.Scanner;
Line 3,393: Line 3,393:


}
}
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 3,538: Line 3,538:
=={{header|Javascript}}==
=={{header|Javascript}}==
The solution as a Javascript script inside a page written in HTML5. It should work with all modern browsers.
The solution as a Javascript script inside a page written in HTML5. It should work with all modern browsers.
<lang javascript><!DOCTYPE html><html lang="en">
<syntaxhighlight lang=javascript><!DOCTYPE html><html lang="en">


<head>
<head>
Line 3,696: Line 3,696:


</body>
</body>
</html></lang>
</html></syntaxhighlight>


=={{header|Julia}}==
=={{header|Julia}}==
The computer or player can always win if they go first and choose a number that brings the total to one of the following: 1, 5, 9, 13, or 17. To make things vary more, there is a choice of computer play level at the start. The computer will randomly respond with level 1 and choose a random response 1/4 of the time at level 2.
The computer or player can always win if they go first and choose a number that brings the total to one of the following: 1, 5, 9, 13, or 17. To make things vary more, there is a choice of computer play level at the start. The computer will randomly respond with level 1 and choose a random response 1/4 of the time at level 2.
<lang julia>
<syntaxhighlight lang=julia>
function trytowin(n)
function trytowin(n)
if 21 - n < 4
if 21 - n < 4
Line 3,783: Line 3,783:


play21game()
play21game()
</syntaxhighlight>
</lang>


=={{header|Lua}}==
=={{header|Lua}}==
<lang lua>
<syntaxhighlight lang=lua>
gamewon = false
gamewon = false
running_total = 0
running_total = 0
Line 3,836: Line 3,836:


end
end
</syntaxhighlight>
</lang>




=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<lang Mathematica>SeedRandom[1234];
<syntaxhighlight lang=Mathematica>SeedRandom[1234];
ClearAll[ComputerChoose, HumanChoose]
ClearAll[ComputerChoose, HumanChoose]
ComputerChoose[n_] := If[n < 18, RandomChoice[{1, 2, 3}], 21 - n]
ComputerChoose[n_] := If[n < 18, RandomChoice[{1, 2, 3}], 21 - n]
Line 3,872: Line 3,872:
If[runningtotal == 21, Print["You won!"]; Break[]];
If[runningtotal == 21, Print["You won!"]; Break[]];
];
];
]</lang>
]</syntaxhighlight>
{{out}}
{{out}}
<pre>You choose = 2
<pre>You choose = 2
Line 3,902: Line 3,902:
There is a simple strategy which allows the first player to win in any case. It consists to choose a value which gives
There is a simple strategy which allows the first player to win in any case. It consists to choose a value which gives
a running total equal to 1, 5, 9, 13 or 17. So never let the computer starts as the program uses this strategy.
a running total equal to 1, 5, 9, 13 or 17. So never let the computer starts as the program uses this strategy.
<lang Nim>
<syntaxhighlight lang=Nim>
# 21 game.
# 21 game.


Line 4,004: Line 4,004:
echo "Quitting game."
echo "Quitting game."
break
break
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 4,071: Line 4,071:


=={{header|Objeck}}==
=={{header|Objeck}}==
<lang objeck>class TwentyOne {
<syntaxhighlight lang=objeck>class TwentyOne {
@quit : Bool;
@quit : Bool;
@player_total : Int;
@player_total : Int;
Line 4,165: Line 4,165:
return 0;
return 0;
}
}
}</lang>
}</syntaxhighlight>


=={{header|Pascal|Delphi}}==
=={{header|Pascal|Delphi}}==
<lang pascal>
<syntaxhighlight lang=pascal>
program Game21;
program Game21;


Line 4,372: Line 4,372:
end;
end;
end.
end.
</syntaxhighlight>
</lang>


=={{header|Perl}}==
=={{header|Perl}}==
{{trans|Raku}}
{{trans|Raku}}
<lang perl>print <<'HERE';
<syntaxhighlight lang=perl>print <<'HERE';
The 21 game. Each player chooses to add 1, 2, or 3 to a running total.
The 21 game. Each player chooses to add 1, 2, or 3 to a running total.
The player whose turn it is when the total reaches 21 wins. Enter q to quit.
The player whose turn it is when the total reaches 21 wins. Enter q to quit.
Line 4,411: Line 4,411:
my $i = $insults[1+int rand($#insults)];
my $i = $insults[1+int rand($#insults)];
print "$i, $g is not an integer between 1 and 3...\n"
print "$i, $g is not an integer between 1 and 3...\n"
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>The 21 game. Each player chooses to add 1, 2, or 3 to a running total.
<pre>The 21 game. Each player chooses to add 1, 2, or 3 to a running total.
Line 4,436: Line 4,436:
If the computer goes first you cannot win, it will say "(you've already lost)" as soon as you have.<br>
If the computer goes first you cannot win, it will say "(you've already lost)" as soon as you have.<br>
You can run this online [http://phix.x10.mx/p2js/21Game.htm here]. (As noted below, a few improvements are in order.)
You can run this online [http://phix.x10.mx/p2js/21Game.htm here]. (As noted below, a few improvements are in order.)
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang=Phix>(phixonline)-->
<span style="color: #000080;font-style:italic;">--
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\21_Game.exw
-- demo\rosetta\21_Game.exw
Line 4,551: Line 4,551:
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</lang>-->
<!--</syntaxhighlight>-->


=={{header|PHP}}==
=={{header|PHP}}==
The solution as a server-side PHP7 script and HTML5.
The solution as a server-side PHP7 script and HTML5.
<lang PHP><!DOCTYPE html>
<syntaxhighlight lang=PHP><!DOCTYPE html>
<html lang="en">
<html lang="en">


Line 4,695: Line 4,695:
?>
?>


</body></lang>
</body></syntaxhighlight>


=={{header|Picat}}==
=={{header|Picat}}==
{{trans|Julia}}
{{trans|Julia}}
<lang Picat>import util.
<syntaxhighlight lang=Picat>import util.


main =>
main =>
Line 4,777: Line 4,777:
NextN = playermove(N)
NextN = playermove(N)
end.
end.
</syntaxhighlight>
</lang>


=={{header|Python}}==
=={{header|Python}}==
Line 4,783: Line 4,783:


{{works with|Python 2.X and 3.X}}
{{works with|Python 2.X and 3.X}}
<lang python>
<syntaxhighlight lang=python>
from random import randint
from random import randint
def start():
def start():
Line 4,845: Line 4,845:
print("Computer wins {0} game, human wins {1} games".format(c,m))
print("Computer wins {0} game, human wins {1} games".format(c,m))
t = raw_input("Another game?(press y to continue):")
t = raw_input("Another game?(press y to continue):")
r = (t=="y")</lang>
r = (t=="y")</syntaxhighlight>


{{out}}
{{out}}
Line 4,978: Line 4,978:


===Python: using tkinter===
===Python: using tkinter===
<lang python>
<syntaxhighlight lang=python>
''' Python 3.6.5 code using Tkinter graphical user interface.
''' Python 3.6.5 code using Tkinter graphical user interface.
Starting player chosen randomly. '''
Starting player chosen randomly. '''
Line 5,222: Line 5,222:
g = Game(root)
g = Game(root)
root.mainloop()
root.mainloop()
</syntaxhighlight>
</lang>


=={{header|Quackery}}==
=={{header|Quackery}}==


<lang Quackery> [ say
<syntaxhighlight lang=Quackery> [ say
"Who goes first: Computer, Player"
"Who goes first: Computer, Player"
say " or Random?" cr
say " or Random?" cr
Line 5,273: Line 5,273:
[ say "The computer won!" ]
[ say "The computer won!" ]
else [ say "You won! Well done!" ]
else [ say "You won! Well done!" ]
drop ] is play ( --> )</lang>
drop ] is play ( --> )</syntaxhighlight>


{{out}}
{{out}}
Line 5,345: Line 5,345:


=={{header|R}}==
=={{header|R}}==
<lang R>game21<-function(first = c("player","ai","random"),sleep=.5){
<syntaxhighlight lang=R>game21<-function(first = c("player","ai","random"),sleep=.5){
state = 0
state = 0
finished = F
finished = F
Line 5,393: Line 5,393:
turn = turn + 1
turn = turn + 1
}
}
}</lang>
}</syntaxhighlight>


<lang>game21()</lang>
<lang>game21()</syntaxhighlight>
{{out}}
{{out}}
<pre>The total is now 0
<pre>The total is now 0
Line 5,418: Line 5,418:
You win!</pre>
You win!</pre>


<lang R>game21(first = "ai")</lang>
<syntaxhighlight lang=R>game21(first = "ai")</syntaxhighlight>
{{out}}
{{out}}
<pre>The total is now 0
<pre>The total is now 0
Line 5,429: Line 5,429:
=={{header|Racket}}==
=={{header|Racket}}==


<lang racket>#lang racket
<syntaxhighlight lang=racket>#lang racket


(define limit 21)
(define limit 21)
Line 5,464: Line 5,464:
[(= new-total limit) (win human-turn?)]
[(= new-total limit) (win human-turn?)]
[(> new-total limit) (win (not human-turn?))]
[(> new-total limit) (win (not human-turn?))]
[else (loop new-total (not human-turn?))]))</lang>
[else (loop new-total (not human-turn?))]))</syntaxhighlight>


{{out}}
{{out}}
Line 5,513: Line 5,513:
{{works with|Rakudo|2018.09}}
{{works with|Rakudo|2018.09}}
Since there is no requirement that the computer play sensibly, it always plays a random guess so the player has some chance to win.
Since there is no requirement that the computer play sensibly, it always plays a random guess so the player has some chance to win.
<lang perl6>say qq :to 'HERE';
<syntaxhighlight lang=perl6>say qq :to 'HERE';
The 21 game. Each player chooses to add 1, 2, or 3 to a running total.
The 21 game. Each player chooses to add 1, 2, or 3 to a running total.
The player whose turn it is when the total reaches 21 wins. Enter q to quit.
The player whose turn it is when the total reaches 21 wins. Enter q to quit.
Line 5,543: Line 5,543:
print ('Yo mama,', 'Jeez,', 'Ummmm,', 'Grow up,', 'Did you even READ the instructions?').roll;
print ('Yo mama,', 'Jeez,', 'Ummmm,', 'Grow up,', 'Did you even READ the instructions?').roll;
say " $g is not an integer between 1 & 3..."
say " $g is not an integer between 1 & 3..."
}</lang>
}</syntaxhighlight>
{{out|Sample game}}
{{out|Sample game}}
<pre>The 21 game. Each player chooses to add 1, 2, or 3 to a running total.
<pre>The 21 game. Each player chooses to add 1, 2, or 3 to a running total.
Line 5,578: Line 5,578:


This REXX version allows the user to choose if the computer should go first.
This REXX version allows the user to choose if the computer should go first.
<lang rexx>/*REXX program plays the 21 game with a human, each player chooses 1, 2, or 3 which */
<syntaxhighlight lang=rexx>/*REXX program plays the 21 game with a human, each player chooses 1, 2, or 3 which */
/*──────────── is added to the current sum, the first player to reach 21 exactly wins.*/
/*──────────── is added to the current sum, the first player to reach 21 exactly wins.*/
sep= copies('─', 8); sep2= " "copies('═', 8)" " /*construct an eye─catching msg fences.*/
sep= copies('─', 8); sep2= " "copies('═', 8)" " /*construct an eye─catching msg fences.*/
Line 5,614: Line 5,614:
if bad then iterate /*Had an error? Then get another number*/
if bad then iterate /*Had an error? Then get another number*/
x= x/1; if $+x>goal then call ser "Number will cause the sum to exceed " goal': ' x
x= x/1; if $+x>goal then call ser "Number will cause the sum to exceed " goal': ' x
end /*until*/; return</lang>
end /*until*/; return</syntaxhighlight>
{{out|output|text=''':'''}}
{{out|output|text=''':'''}}
<pre>
<pre>
Line 5,654: Line 5,654:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang=ring>
# Project : 21 Game
# Project : 21 Game


Line 5,763: Line 5,763:
}
}
}
}
</syntaxhighlight>
</lang>
Output:<br>
Output:<br>
[http://kepkezelo.com/images/1hyutakip6vo1t1rpypy.jpg 21 Game]
[http://kepkezelo.com/images/1hyutakip6vo1t1rpypy.jpg 21 Game]


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang Ruby>
<syntaxhighlight lang=Ruby>
# 21 Game - an example in Ruby for Rosetta Code.
# 21 Game - an example in Ruby for Rosetta Code.


Line 5,875: Line 5,875:
end
end
puts 'Good bye!'
puts 'Good bye!'
</syntaxhighlight>
</lang>


=={{header|rust}}==
=={{header|rust}}==
<lang rust>use rand::Rng;
<syntaxhighlight lang=rust>use rand::Rng;
use std::io;
use std::io;


Line 6,103: Line 6,103:
println!("player: {} win: {}", players[1].get_name(), players[1].wins);
println!("player: {} win: {}", players[1].get_name(), players[1].wins);
}
}
</syntaxhighlight>
</lang>


=={{header|Scala}}==
=={{header|Scala}}==
<lang Scala>
<syntaxhighlight lang=Scala>
object Game21 {
object Game21 {


Line 6,198: Line 6,198:
}
}
}
}
</syntaxhighlight>
</lang>


=={{header|uBasic/4tH}}==
=={{header|uBasic/4tH}}==
Line 6,232: Line 6,232:
else
else
print "Bad luck. The computer wins."
print "Bad luck. The computer wins."
endif</lang>
endif</syntaxhighlight>
=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==


Line 6,243: Line 6,243:
<p>
<p>
The file MainWindow.xaml.vb with Visual Basic source code.
The file MainWindow.xaml.vb with Visual Basic source code.
</p><lang vbnet>' Game 21 in VB.NET - an example for Rosetta Code
</p><syntaxhighlight lang=vbnet>' Game 21 in VB.NET - an example for Rosetta Code


Class MainWindow
Class MainWindow
Line 6,306: Line 6,306:
End If
End If
End Sub
End Sub
End Class</lang>
End Class</syntaxhighlight>
<p>
<p>
The file MainWindow.xaml with GUI source code written in XAML.
The file MainWindow.xaml with GUI source code written in XAML.
</p><lang xml><Window x:Class="MainWindow"
</p><syntaxhighlight lang=xml><Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Line 6,338: Line 6,338:
</Grid>
</Grid>
</Window>
</Window>
</syntaxhighlight>
</lang>


=={{header|Vlang}}==
=={{header|Vlang}}==
{{trans|Go}}
{{trans|Go}}
<lang vlang>import os
<syntaxhighlight lang=vlang>import os
import rand
import rand
import rand.seed
import rand.seed
Line 6,419: Line 6,419:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 6,474: Line 6,474:
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
{{libheader|Wren-ioutil}}
{{libheader|Wren-ioutil}}
<lang ecmascript>import "/fmt" for Conv
<syntaxhighlight lang=ecmascript>import "/fmt" for Conv
import "/ioutil" for Input
import "/ioutil" for Input
import "random" for Random
import "random" for Random
Line 6,536: Line 6,536:
}
}
round = round + 1
round = round + 1
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}