21 game: Difference between revisions

m
Automated syntax highlighting fixup (second round - minor fixes)
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 29:
{{trans|Python: Original, with output}}
 
<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’
Int t
Line 97:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang=AArch64"aarch64 Assemblyassembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program game21_64.s */
Line 319:
 
=={{header|Ada}}==
<syntaxhighlight lang=Ada"ada">with Ada.Text_IO;
with Ada.Numerics.Discrete_Random;
 
Line 437:
WAIT instead of GET in a loop.
 
<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
10 REM SET SCREEN COLORS HERE
Line 476:
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang=ARM"arm Assemblyassembly">
/* ARM assembly Raspberry PI */
/* program game21.s */
Line 727:
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">print "-----------------------------"
print " Welcome to 21 Game"
print "-----------------------------"
Line 813:
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang=AutoHotkey"autohotkey">Gui, font, S16
Gui, add, Radio, vRadioC , Cake
Gui, add, Radio, vRadioE x+0 Checked, Easy
Line 900:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk"># Game 21 - an example in AWK language for Rosseta Code.
 
BEGIN {
Line 1,050:
 
=={{header|bash}}==
<syntaxhighlight lang="bash">shopt -s expand_aliases
alias bind_variables='
{
Line 1,188:
 
=={{header|C}}==
<syntaxhighlight lang=C"c">/**
* Game 21 - an example in C language for Rosseta Code.
*
Line 1,423:
=={{header|C++}}==
===Model View Controller===
<syntaxhighlight lang="cpp">/**
* Game 21 - an example in C++ language for Rosseta Code.
*
Line 1,626:
 
===Model View Presenter===
<syntaxhighlight lang="cpp">/**
* Game 21 - an example in C++ language for Rosseta Code.
*
Line 1,940:
 
=={{header|C sharp|C#}}==
<syntaxhighlight lang="csharp">// 21 Game
 
using System;
Line 2,098:
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.
 
<syntaxhighlight lang="gwbasic">1 rem 21 game
2 rem for rosetta code
 
Line 2,289:
=={{header|EasyLang}}==
The computer plays optimally
<syntaxhighlight lang="text">print "Who reaches 21, wins"
print "Do you want to begin (y/n)"
if input = "n"
Line 2,329:
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}}
<syntaxhighlight lang="factor">USING: accessors combinators.random continuations formatting io
kernel math math.functions math.parser multiline qw random
sequences ;
Line 2,450:
 
=={{header|F sharp|F#}}==
<syntaxhighlight lang="fsharp">
type Player =
| Computer
Line 2,545:
 
=={{header|Fortran}}==
<syntaxhighlight lang=Fortran"fortran">! game 21 - an example in modern fortran language for rosseta code.
 
subroutine ai
Line 2,691:
 
=={{Header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">#define PLAYER 1
#define COMP 0
randomize timer
Line 2,724:
 
=={{header|Gambas}}==
<syntaxhighlight lang="gambas">
' Gambas module file
 
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.
<syntaxhighlight lang="go">package main
 
import (
Line 3,093:
=={{header|Haskell}}==
The computer chooses values randomly.
<syntaxhighlight lang="haskell">import System.Random
import System.IO
import System.Exit
Line 3,159:
=={{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"j">
g21=: summarize@play@setup ::('g21: error termination'"_)
 
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.
 
<syntaxhighlight lang="java">
import java.util.Random;
import java.util.Scanner;
Line 3,538:
=={{header|Javascript}}==
The solution as a Javascript script inside a page written in HTML5. It should work with all modern browsers.
<syntaxhighlight lang="javascript"><!DOCTYPE html><html lang="en">
 
<head>
Line 3,700:
=={{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.
<syntaxhighlight lang="julia">
function trytowin(n)
if 21 - n < 4
Line 3,786:
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">
gamewon = false
running_total = 0
Line 3,840:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang=Mathematica"mathematica">SeedRandom[1234];
ClearAll[ComputerChoose, HumanChoose]
ComputerChoose[n_] := If[n < 18, RandomChoice[{1, 2, 3}], 21 - n]
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.
<syntaxhighlight lang=Nim"nim">
# 21 game.
 
Line 4,071:
 
=={{header|Objeck}}==
<syntaxhighlight lang="objeck">class TwentyOne {
@quit : Bool;
@player_total : Int;
Line 4,168:
 
=={{header|Pascal|Delphi}}==
<syntaxhighlight lang="pascal">
program Game21;
 
Line 4,376:
=={{header|Perl}}==
{{trans|Raku}}
<syntaxhighlight 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,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.)
<!--<syntaxhighlight lang=Phix"phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\21_Game.exw
Line 4,555:
=={{header|PHP}}==
The solution as a server-side PHP7 script and HTML5.
<syntaxhighlight lang=PHP"php"><!DOCTYPE html>
<html lang="en">
 
Line 4,699:
=={{header|Picat}}==
{{trans|Julia}}
<syntaxhighlight lang=Picat"picat">import util.
 
main =>
Line 4,783:
 
{{works with|Python 2.X and 3.X}}
<syntaxhighlight lang="python">
from random import randint
def start():
Line 4,978:
 
===Python: using tkinter===
<syntaxhighlight lang="python">
''' Python 3.6.5 code using Tkinter graphical user interface.
Starting player chosen randomly. '''
Line 5,226:
=={{header|Quackery}}==
 
<syntaxhighlight lang=Quackery"quackery"> [ say
"Who goes first: Computer, Player"
say " or Random?" cr
Line 5,345:
 
=={{header|R}}==
<syntaxhighlight lang=R"r">game21<-function(first = c("player","ai","random"),sleep=.5){
state = 0
finished = F
Line 5,395:
}</syntaxhighlight>
 
<syntaxhighlight lang="text">game21()</syntaxhighlight>
{{out}}
<pre>The total is now 0
Line 5,418:
You win!</pre>
 
<syntaxhighlight lang=R"r">game21(first = "ai")</syntaxhighlight>
{{out}}
<pre>The total is now 0
Line 5,429:
=={{header|Racket}}==
 
<syntaxhighlight lang="racket">#lang racket
 
(define limit 21)
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.
<syntaxhighlight lang=perl6"raku" line>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,578:
 
This REXX version allows the user to choose if the computer should go first.
<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.*/
sep= copies('─', 8); sep2= " "copies('═', 8)" " /*construct an eye─catching msg fences.*/
Line 5,654:
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
# Project : 21 Game
 
Line 5,768:
 
=={{header|Ruby}}==
<syntaxhighlight lang=Ruby"ruby">
# 21 Game - an example in Ruby for Rosetta Code.
 
Line 5,878:
 
=={{header|rust}}==
<syntaxhighlight lang="rust">use rand::Rng;
use std::io;
 
Line 6,106:
 
=={{header|Scala}}==
<syntaxhighlight lang=Scala"scala">
object Game21 {
 
Line 6,202:
=={{header|uBasic/4tH}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="text">dim @p(4)
 
push 1, 1, 3, 2
Line 6,243:
<p>
The file MainWindow.xaml.vb with Visual Basic source code.
</p><syntaxhighlight lang="vbnet">' Game 21 in VB.NET - an example for Rosetta Code
 
Class MainWindow
Line 6,309:
<p>
The file MainWindow.xaml with GUI source code written in XAML.
</p><syntaxhighlight 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,342:
=={{header|Vlang}}==
{{trans|Go}}
<syntaxhighlight lang="vlang">import os
import rand
import rand.seed
Line 6,474:
{{libheader|Wren-fmt}}
{{libheader|Wren-ioutil}}
<syntaxhighlight lang="ecmascript">import "/fmt" for Conv
import "/ioutil" for Input
import "random" for Random
10,333

edits