21 game: Difference between revisions
m
syntax highlighting fixup automation
(Added uBasic/4tH version) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 29:
{{trans|Python: Original, with output}}
<
‘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</
{{out}}
Line 97:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<
/* 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>
=={{header|Ada}}==
<
with Ada.Numerics.Discrete_Random;
Line 426:
when Human => Computer);
end loop Play_Loop;
end Game_21;</
=={{header|Applesoft BASIC}}==
Line 437:
WAIT instead of GET in a loop.
<
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"</
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<
/* ARM assembly Raspberry PI */
/* program game21.s */
Line 695:
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
<pre>
The first move is human move.
Line 727:
=={{header|Arturo}}==
<
print " Welcome to 21 Game"
print "-----------------------------"
Line 762:
]
]
</syntaxhighlight>
{{out}}
Line 813:
=={{header|AutoHotkey}}==
<
Gui, add, Radio, vRadioC , Cake
Gui, add, Radio, vRadioE x+0 Checked, Easy
Line 897:
Loop 3
GuiControl, Enable, % "Player1_" A_Index
return</
=={{header|AWK}}==
<
BEGIN {
Line 968:
newgame();
}
}</
{{Output}}
<pre>
Line 1,050:
=={{header|bash}}==
<
alias bind_variables='
{
Line 1,185:
fi
21_Game
</syntaxhighlight>
=={{header|C}}==
<
* Game 21 - an example in C language for Rosseta Code.
*
Line 1,327:
}
}
}</
{{out}}
<pre>21 Game
Line 1,423:
=={{header|C++}}==
===Model View Controller===
<
* Game 21 - an example in C++ language for Rosseta Code.
*
Line 1,623:
}
return EXIT_SUCCESS; // dead code
}</
===Model View Presenter===
<
* Game 21 - an example in C++ language for Rosseta Code.
*
Line 1,865:
return EXIT_SUCCESS; // dead code
}
</syntaxhighlight>
{{out}}
<pre>
Line 1,940:
=={{header|C sharp|C#}}==
<
using System;
Line 2,093:
</syntaxhighlight>
=={{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.
<
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"</
{{output}}
Line 2,324:
print "Sorry, you lost"
.
.</
=={{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}}
<
kernel math math.functions math.parser multiline qw random
sequences ;
Line 2,409:
.welcome nl [ <game> do-turns ] with-return ;
MAIN: play-21-game</
{{out}}
<pre>
Line 2,450:
=={{header|F sharp|F#}}==
<
type Player =
| Computer
Line 2,516:
let run () = looper Start
</syntaxhighlight>
{{out}}
<pre>
Line 2,545:
=={{header|Fortran}}==
<
subroutine ai
Line 2,629:
end do
end do
end program main</
{{out}}
<pre>game 21 - an example in fortran language for rosseta code.
Line 2,691:
=={{Header|FreeBASIC}}==
<
#define COMP 0
randomize timer
Line 2,721:
else
print "Bad luck. The computer wins."
end if</
=={{header|Gambas}}==
<
' Gambas module file
Line 2,825:
Endif
End</
<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.
<
import (
Line 2,987:
}
}
}</
{{out}}
Line 3,093:
=={{header|Haskell}}==
The computer chooses values randomly.
<
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"</
=={{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>
g21=: summarize@play@setup ::('g21: error termination'"_)
Line 3,197:
write=: 1!:2 4: NB. unused
format=: ''&$: :([: ; (a: , [: ":&.> [) ,. '{}' ([ (E. <@}.;._1 ]) ,) ])
</syntaxhighlight>
<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.
<
import java.util.Random;
import java.util.Scanner;
Line 3,393:
}
</syntaxhighlight>
{{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.
<
<head>
Line 3,696:
</body>
</html></
=={{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.
<
function trytowin(n)
if 21 - n < 4
Line 3,783:
play21game()
</syntaxhighlight>
=={{header|Lua}}==
<
gamewon = false
running_total = 0
Line 3,836:
end
</syntaxhighlight>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
ClearAll[ComputerChoose, HumanChoose]
ComputerChoose[n_] := If[n < 18, RandomChoice[{1, 2, 3}], 21 - n]
Line 3,872:
If[runningtotal == 21, Print["You won!"]; Break[]];
];
]</
{{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.
<
# 21 game.
Line 4,004:
echo "Quitting game."
break
</syntaxhighlight>
{{out}}
Line 4,071:
=={{header|Objeck}}==
<
@quit : Bool;
@player_total : Int;
Line 4,165:
return 0;
}
}</
=={{header|Pascal|Delphi}}==
<
program Game21;
Line 4,372:
end;
end.
</syntaxhighlight>
=={{header|Perl}}==
{{trans|Raku}}
<
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"
}</
{{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.)
<!--<
<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>
<!--</
=={{header|PHP}}==
The solution as a server-side PHP7 script and HTML5.
<
<html lang="en">
Line 4,695:
?>
</body></
=={{header|Picat}}==
{{trans|Julia}}
<
main =>
Line 4,777:
NextN = playermove(N)
end.
</syntaxhighlight>
=={{header|Python}}==
Line 4,783:
{{works with|Python 2.X and 3.X}}
<
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")</
{{out}}
Line 4,978:
===Python: using tkinter===
<
''' Python 3.6.5 code using Tkinter graphical user interface.
Starting player chosen randomly. '''
Line 5,222:
g = Game(root)
root.mainloop()
</syntaxhighlight>
=={{header|Quackery}}==
<
"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 ( --> )</
{{out}}
Line 5,345:
=={{header|R}}==
<
state = 0
finished = F
Line 5,393:
turn = turn + 1
}
}</
<lang>game21()</
{{out}}
<pre>The total is now 0
Line 5,418:
You win!</pre>
<
{{out}}
<pre>The total is now 0
Line 5,429:
=={{header|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?))]))</
{{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.
<
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..."
}</
{{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.
<
/*──────────── 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</
{{out|output|text=''':'''}}
<pre>
Line 5,654:
=={{header|Ring}}==
<
# Project : 21 Game
Line 5,763:
}
}
</syntaxhighlight>
Output:<br>
[http://kepkezelo.com/images/1hyutakip6vo1t1rpypy.jpg 21 Game]
=={{header|Ruby}}==
<
# 21 Game - an example in Ruby for Rosetta Code.
Line 5,875:
end
puts 'Good bye!'
</syntaxhighlight>
=={{header|rust}}==
<
use std::io;
Line 6,103:
println!("player: {} win: {}", players[1].get_name(), players[1].wins);
}
</syntaxhighlight>
=={{header|Scala}}==
<
object Game21 {
Line 6,198:
}
}
</syntaxhighlight>
=={{header|uBasic/4tH}}==
Line 6,232:
else
print "Bad luck. The computer wins."
endif</
=={{header|Visual Basic .NET}}==
Line 6,243:
<p>
The file MainWindow.xaml.vb with Visual Basic source code.
</p><
Class MainWindow
Line 6,306:
End If
End Sub
End Class</
<p>
The file MainWindow.xaml with GUI source code written in XAML.
</p><
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Line 6,338:
</Grid>
</Window>
</syntaxhighlight>
=={{header|Vlang}}==
{{trans|Go}}
<
import rand
import rand.seed
Line 6,419:
}
}
}</
{{out}}
Line 6,474:
{{libheader|Wren-fmt}}
{{libheader|Wren-ioutil}}
<
import "/ioutil" for Input
import "random" for Random
Line 6,536:
}
round = round + 1
}</
{{out}}
|