21 game: Difference between revisions

m
syntax highlighting fixup automation
(Added uBasic/4tH version)
m (syntax highlighting fixup automation)
Line 29:
{{trans|Python: Original, with output}}
 
<langsyntaxhighlight lang=11l>F select_count(game_count)
‘selects a random number if the game_count is less than 18. otherwise chooses the winning number’
Int t
Line 90:
V t = input(‘Another game?(press y to continue):’)
I t != ‘y’
L.break</langsyntaxhighlight>
 
{{out}}
Line 97:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<langsyntaxhighlight lang=AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program game21_64.s */
Line 315:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
</lang>
 
 
=={{header|Ada}}==
<langsyntaxhighlight lang=Ada>with Ada.Text_IO;
with Ada.Numerics.Discrete_Random;
 
Line 426:
when Human => Computer);
end loop Play_Loop;
end Game_21;</langsyntaxhighlight>
 
=={{header|Applesoft BASIC}}==
Line 437:
WAIT instead of GET in a loop.
 
<langsyntaxhighlight 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
10 REM SET SCREEN COLORS HERE
Line 473:
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
2010 DATA "APPLE IIE","APPLE II PLUS","APPLE IIC","APPLE II","APPLE IIGS","MACINTOSH LC"</langsyntaxhighlight>
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<langsyntaxhighlight lang=ARM Assembly>
/* ARM assembly Raspberry PI */
/* program game21.s */
Line 695:
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
</lang>
<pre>
The first move is human move.
Line 727:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang=rebol>print "-----------------------------"
print " Welcome to 21 Game"
print "-----------------------------"
Line 762:
]
]
</syntaxhighlight>
</lang>
 
{{out}}
Line 813:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight lang=AutoHotkey>Gui, font, S16
Gui, add, Radio, vRadioC , Cake
Gui, add, Radio, vRadioE x+0 Checked, Easy
Line 897:
Loop 3
GuiControl, Enable, % "Player1_" A_Index
return</langsyntaxhighlight>
 
=={{header|AWK}}==
<langsyntaxhighlight lang=awk># Game 21 - an example in AWK language for Rosseta Code.
 
BEGIN {
Line 968:
newgame();
}
}</langsyntaxhighlight>
{{Output}}
<pre>
Line 1,050:
 
=={{header|bash}}==
<langsyntaxhighlight lang=bash>shopt -s expand_aliases
alias bind_variables='
{
Line 1,185:
fi
21_Game
</syntaxhighlight>
</lang>
 
=={{header|C}}==
<langsyntaxhighlight lang=C>/**
* Game 21 - an example in C language for Rosseta Code.
*
Line 1,327:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>21 Game
Line 1,423:
=={{header|C++}}==
===Model View Controller===
<langsyntaxhighlight lang=cpp>/**
* Game 21 - an example in C++ language for Rosseta Code.
*
Line 1,623:
}
return EXIT_SUCCESS; // dead code
}</langsyntaxhighlight>
 
===Model View Presenter===
<langsyntaxhighlight lang=cpp>/**
* Game 21 - an example in C++ language for Rosseta Code.
*
Line 1,865:
return EXIT_SUCCESS; // dead code
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,940:
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang=csharp>// 21 Game
 
using System;
Line 2,093:
 
 
</syntaxhighlight>
</lang>
=={{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.
 
<langsyntaxhighlight lang=gwbasic>1 rem 21 game
2 rem for rosetta code
 
Line 2,187:
2000 rem some computer names to pick from
2010 data "Commodore 64","VIC-20","Commodore 128","PET"
2020 data "Plus/4","Commodore 16"</langsyntaxhighlight>
 
{{output}}
Line 2,324:
print "Sorry, you lost"
.
.</langsyntaxhighlight>
 
=={{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.
{{works with|Factor|0.99 2020-07-03}}
<langsyntaxhighlight lang=factor>USING: accessors combinators.random continuations formatting io
kernel math math.functions math.parser multiline qw random
sequences ;
Line 2,409:
.welcome nl [ <game> do-turns ] with-return ;
 
MAIN: play-21-game</langsyntaxhighlight>
{{out}}
<pre>
Line 2,450:
 
=={{header|F sharp|F#}}==
<langsyntaxhighlight lang=fsharp>
type Player =
| Computer
Line 2,516:
let run () = looper Start
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,545:
 
=={{header|Fortran}}==
<langsyntaxhighlight lang=Fortran>! game 21 - an example in modern fortran language for rosseta code.
 
subroutine ai
Line 2,629:
end do
end do
end program main</langsyntaxhighlight>
{{out}}
<pre>game 21 - an example in fortran language for rosseta code.
Line 2,691:
 
=={{Header|FreeBASIC}}==
<langsyntaxhighlight lang=freebasic>#define PLAYER 1
#define COMP 0
randomize timer
Line 2,721:
else
print "Bad luck. The computer wins."
end if</langsyntaxhighlight>
 
=={{header|Gambas}}==
<langsyntaxhighlight lang=gambas>
' Gambas module file
 
Line 2,825:
Endif
 
End</langsyntaxhighlight>
 
<pre>
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.
<langsyntaxhighlight lang=go>package main
 
import (
Line 2,987:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 3,093:
=={{header|Haskell}}==
The computer chooses values randomly.
<langsyntaxhighlight lang=haskell>import System.Random
import System.IO
import System.Exit
Line 3,156:
putStrLn $ (fst names) ++ " has won the game"
when (y == 1) $ do
putStrLn $ (snd names) ++ " has won the game"</langsyntaxhighlight>
=={{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.
<syntaxhighlight lang=J>
<lang J>
g21=: summarize@play@setup ::('g21: error termination'"_)
 
Line 3,197:
write=: 1!:2 4: NB. unused
format=: ''&$: :([: ; (a: , [: ":&.> [) ,. '{}' ([ (E. <@}.;._1 ]) ,) ])
</syntaxhighlight>
</lang>
<pre>
g21''
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.
 
<langsyntaxhighlight lang=java>
import java.util.Random;
import java.util.Scanner;
Line 3,393:
 
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,538:
=={{header|Javascript}}==
The solution as a Javascript script inside a page written in HTML5. It should work with all modern browsers.
<langsyntaxhighlight lang=javascript><!DOCTYPE html><html lang="en">
 
<head>
Line 3,696:
 
</body>
</html></langsyntaxhighlight>
 
=={{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.
<langsyntaxhighlight lang=julia>
function trytowin(n)
if 21 - n < 4
Line 3,783:
 
play21game()
</syntaxhighlight>
</lang>
 
=={{header|Lua}}==
<langsyntaxhighlight lang=lua>
gamewon = false
running_total = 0
Line 3,836:
 
end
</syntaxhighlight>
</lang>
 
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight lang=Mathematica>SeedRandom[1234];
ClearAll[ComputerChoose, HumanChoose]
ComputerChoose[n_] := If[n < 18, RandomChoice[{1, 2, 3}], 21 - n]
Line 3,872:
If[runningtotal == 21, Print["You won!"]; Break[]];
];
]</langsyntaxhighlight>
{{out}}
<pre>You choose = 2
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
a running total equal to 1, 5, 9, 13 or 17. So never let the computer starts as the program uses this strategy.
<langsyntaxhighlight lang=Nim>
# 21 game.
 
Line 4,004:
echo "Quitting game."
break
</syntaxhighlight>
</lang>
 
{{out}}
Line 4,071:
 
=={{header|Objeck}}==
<langsyntaxhighlight lang=objeck>class TwentyOne {
@quit : Bool;
@player_total : Int;
Line 4,165:
return 0;
}
}</langsyntaxhighlight>
 
=={{header|Pascal|Delphi}}==
<langsyntaxhighlight lang=pascal>
program Game21;
 
Line 4,372:
end;
end.
</syntaxhighlight>
</lang>
 
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang=perl>print <<'HERE';
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.
Line 4,411:
my $i = $insults[1+int rand($#insults)];
print "$i, $g is not an integer between 1 and 3...\n"
}</langsyntaxhighlight>
{{out}}
<pre>The 21 game. Each player chooses to add 1, 2, or 3 to a running total.
Line 4,436:
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.)
<!--<langsyntaxhighlight lang=Phix>(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\21_Game.exw
Line 4,551:
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</langsyntaxhighlight>-->
 
=={{header|PHP}}==
The solution as a server-side PHP7 script and HTML5.
<langsyntaxhighlight lang=PHP><!DOCTYPE html>
<html lang="en">
 
Line 4,695:
?>
 
</body></langsyntaxhighlight>
 
=={{header|Picat}}==
{{trans|Julia}}
<langsyntaxhighlight lang=Picat>import util.
 
main =>
Line 4,777:
NextN = playermove(N)
end.
</syntaxhighlight>
</lang>
 
=={{header|Python}}==
Line 4,783:
 
{{works with|Python 2.X and 3.X}}
<langsyntaxhighlight lang=python>
from random import randint
def start():
Line 4,845:
print("Computer wins {0} game, human wins {1} games".format(c,m))
t = raw_input("Another game?(press y to continue):")
r = (t=="y")</langsyntaxhighlight>
 
{{out}}
Line 4,978:
 
===Python: using tkinter===
<langsyntaxhighlight lang=python>
''' Python 3.6.5 code using Tkinter graphical user interface.
Starting player chosen randomly. '''
Line 5,222:
g = Game(root)
root.mainloop()
</syntaxhighlight>
</lang>
 
=={{header|Quackery}}==
 
<langsyntaxhighlight lang=Quackery> [ say
"Who goes first: Computer, Player"
say " or Random?" cr
Line 5,273:
[ say "The computer won!" ]
else [ say "You won! Well done!" ]
drop ] is play ( --> )</langsyntaxhighlight>
 
{{out}}
Line 5,345:
 
=={{header|R}}==
<langsyntaxhighlight lang=R>game21<-function(first = c("player","ai","random"),sleep=.5){
state = 0
finished = F
Line 5,393:
turn = turn + 1
}
}</langsyntaxhighlight>
 
<lang>game21()</langsyntaxhighlight>
{{out}}
<pre>The total is now 0
Line 5,418:
You win!</pre>
 
<langsyntaxhighlight lang=R>game21(first = "ai")</langsyntaxhighlight>
{{out}}
<pre>The total is now 0
Line 5,429:
=={{header|Racket}}==
 
<langsyntaxhighlight lang=racket>#lang racket
 
(define limit 21)
Line 5,464:
[(= new-total limit) (win human-turn?)]
[(> new-total limit) (win (not human-turn?))]
[else (loop new-total (not human-turn?))]))</langsyntaxhighlight>
 
{{out}}
Line 5,513:
{{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.
<langsyntaxhighlight lang=perl6>say qq :to 'HERE';
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.
Line 5,543:
print ('Yo mama,', 'Jeez,', 'Ummmm,', 'Grow up,', 'Did you even READ the instructions?').roll;
say " $g is not an integer between 1 & 3..."
}</langsyntaxhighlight>
{{out|Sample game}}
<pre>The 21 game. Each player chooses to add 1, 2, or 3 to a running total.
Line 5,578:
 
This REXX version allows the user to choose if the computer should go first.
<langsyntaxhighlight 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.*/
sep= copies('─', 8); sep2= " "copies('═', 8)" " /*construct an eye─catching msg fences.*/
Line 5,614:
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
end /*until*/; return</langsyntaxhighlight>
{{out|output|text=''':'''}}
<pre>
Line 5,654:
 
=={{header|Ring}}==
<langsyntaxhighlight lang=ring>
# Project : 21 Game
 
Line 5,763:
}
}
</syntaxhighlight>
</lang>
Output:<br>
[http://kepkezelo.com/images/1hyutakip6vo1t1rpypy.jpg 21 Game]
 
=={{header|Ruby}}==
<langsyntaxhighlight lang=Ruby>
# 21 Game - an example in Ruby for Rosetta Code.
 
Line 5,875:
end
puts 'Good bye!'
</syntaxhighlight>
</lang>
 
=={{header|rust}}==
<langsyntaxhighlight lang=rust>use rand::Rng;
use std::io;
 
Line 6,103:
println!("player: {} win: {}", players[1].get_name(), players[1].wins);
}
</syntaxhighlight>
</lang>
 
=={{header|Scala}}==
<langsyntaxhighlight lang=Scala>
object Game21 {
 
Line 6,198:
}
}
</syntaxhighlight>
</lang>
 
=={{header|uBasic/4tH}}==
Line 6,232:
else
print "Bad luck. The computer wins."
endif</langsyntaxhighlight>
=={{header|Visual Basic .NET}}==
 
Line 6,243:
<p>
The file MainWindow.xaml.vb with Visual Basic source code.
</p><langsyntaxhighlight lang=vbnet>' Game 21 in VB.NET - an example for Rosetta Code
 
Class MainWindow
Line 6,306:
End If
End Sub
End Class</langsyntaxhighlight>
<p>
The file MainWindow.xaml with GUI source code written in XAML.
</p><langsyntaxhighlight lang=xml><Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Line 6,338:
</Grid>
</Window>
</syntaxhighlight>
</lang>
 
=={{header|Vlang}}==
{{trans|Go}}
<langsyntaxhighlight lang=vlang>import os
import rand
import rand.seed
Line 6,419:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 6,474:
{{libheader|Wren-fmt}}
{{libheader|Wren-ioutil}}
<langsyntaxhighlight lang=ecmascript>import "/fmt" for Conv
import "/ioutil" for Input
import "random" for Random
Line 6,536:
}
round = round + 1
}</langsyntaxhighlight>
 
{{out}}
10,327

edits