Jump to content

Rock-paper-scissors: Difference between revisions

Algol 68 - Slight simplifications
m (→‎{{header|Perl 6}}: fix whitespace syntax errors)
(Algol 68 - Slight simplifications)
Line 258:
 
=={{header|ALGOL 68}}==
<lang algol68>BEGIN
{{works with|ALGOL 68G|Any - tested with release 2.6.win32}}
# rock/paper/scissors game #
<lang algol68>main: (
# counts of the number of times the player has chosen each move #
 
# we initialise each to 1 so that the total isn't zero when we are #
# rock/paper/scissors game #
# choosing the computer's first move (as in the Ada version) #
 
INT r count := 1;
# counts of the number of times the player has chosen each move #
INT p count := 1;
# we initialise each to 1 so that the total isn't zero when we are #
INT s count := 1;
# choosing the computer's first move (as in the Ada version) #
# counts of how many games the player and computer have won #
INT r count := 1;
INT pplayer count := 10;
INT scomputer count := 10;
print( ( "rock/paper/scissors", newline, newline ) );
 
# counts of how many games the player and computer have won #
INT player count := 0;
INT computer count := 0;
 
print( ( "rock/paper/scissors", newline, newline ) );
 
WHILE
 
CHAR player move;
 
# get the players move - r => rock #
# p => paper #
# s => scissors #
# q => quit #
 
WHILE
print(CHAR ( "Please enter yourplayer move (r/p/s) or q to quit: " ) );
# get the players move - r => rock, p => paper, s => scissors #
readf( ( $ a $, player move ) );
# q => quit #
read( ( newline ) );
WHILE
 
print( ( "Please playerenter your move (r/=p/s) or q to quit: "r" ) );
AND read( ( player move, newline /=) "p");
AND ( player move /= "sr"
AND player move /= "qp"
) AND player move /= "s"
AND player move /= "q"
)
DO
print( ( "Unrecognised move", newline ) )
OD;
# continue playing until the player chooses quit #
player move /= "q"
DO
print(# (decide "Unrecognisedthe computer's move", newlinebased on the player's history ) )#
CHAR computer move;
OD;
INT move count = r count + p count + s count;
 
# continue playing until the# predict player chooseswill quitplay rock if the random number #
# is in the range 0 .. rock / total #
player move /= "q"
# predict player will play paper if the random number #
 
# is in the range rock / total .. ( rock + paper ) / total #
DO
# predict player will play scissors otherwise #
 
REAL r limit = r count / move count;
# decide the computer's move based on the player's history #
REAL p limit = r limit + ( p count / move count );
 
CHAR computer REAL random move = next random;
IF random move < r limit THEN
 
INT move count # =we rpredict countthe +player pwill countchoose +rock s- count;we choose paper #
computer move := "p"
 
# predict player will playELIF rock if the random number move < p limit #THEN
# is in the range 0 .. rock / total # we predict the player will choose paper - we choose scissors #
# predict player will play paper if the randomcomputer numbermove := #"s"
# is in the range rock / total .. ( rock + paper ) / total #
# predict player will play scissors otherwise #
 
REAL r limit = r count / move count;
REAL p limit = r limit + ( p count / move count );
 
REAL random move = next random;
 
IF random move < r limit
THEN
# we predict the player will choose rock - we choose paper #
computer move := "p"
 
ELIF random move < p limit
THEN
# we predict the player will choose paper - we choose scissors #
computer move := "s"
 
ELSE
# we predict the player will choose scissors - we choose rock #
computer move := "r"
 
FI;
 
print( ( "You chose: " + player move, newline ) );
print( ( "I chose: " + computer move, newline ) );
 
IF player move = computer move
THEN
# both players chose the same - draw #
print( ( "We draw", newline ) )
 
ELSE
# players chose different moves - there is a winner #
 
BOOL player wins;
 
IF player move = "r"
THEN
# player chose rock - rock blunts scissors #
# but is wrapped by paper #
player wins := ( computer move = "s" )
 
ELIF player move = "p"
THEN
# player chose paper - paper wraps rock #
# but is cut by scissors #
player wins := ( computer move = "r" )
 
ELSE
# playerwe chosepredict scissorsthe -player scissorswill cut paperchoose scissors - we choose rock #
computer move := "r"
# but are blunted by rock #
player wins := ( computer move = "p" )
 
FI;
print( ( "You chose: " + player move, newline ) );
 
print( ( "I chose: " + computer move, newline ) );
IF player wins
IF player move = computer move THEN
# both players chose the same - draw #
player count +:= 1;
print( ( "YouWe windraw", newline ) )
 
ELSE
# players chose different moves - there is a winner #
computer count +:= 1;
print(BOOL (player "Iwins win", newline ) )=
IF player move = "r" THEN
# player chose rock - rock blunts scissors #
computer move = "s"
ELIF player move = "p" THEN
# player chose paper - paper wraps rock #
computer move = "r"
ELSE
# player chose scissors - scissors cut paper #
computer move = "p"
FI;
IF player wins THEN
player count +:= 1;
print( ( "You win", newline ) )
ELSE
computer count +:= 1;
print( ( "I win", newline ) )
FI;
print( ( "You won: "
, whole( player count , 0 )
, ", I won: "
, whole( computer count, 0 )
, newline
)
)
FI;
IF player move = "r" THEN
# player chose rock #
r count +:= 1
ELIF player move = "p" THEN
# player chose paper #
p count +:= 1
ELSE
# player chose scissors #
s count +:= 1
FI
OD;
print( ( "Thanks for a most enjoyable game", newline ) )
END</lang>
 
print( ( ( "You won: "
+ whole( player count , 0 )
+ ", I won: "
+ whole( computer count, 0 )
)
, newline
)
)
 
FI;
 
IF player move = "r"
THEN
# player chose rock #
r count +:= 1
 
ELIF player move = "p"
THEN
# player chose paper #
p count +:= 1
 
ELSE
# player chose scissors #
s count +:= 1
 
FI
 
OD;
 
print( ( "Thanks for a most enjoyable game", newline ) )
 
)
</lang>
=={{header|AutoHotkey}}==
<lang AHK>DllCall("AllocConsole")
3,048

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.