Rock-paper-scissors: Difference between revisions

Content added Content deleted
m (→‎{{header|Lasso}}: remove extraneous indenting)
m (Added Sidef)
Line 2,934: Line 2,934:
Your move? [R]ock, [P]aper, [S]cissors: ^D
Your move? [R]ock, [P]aper, [S]cissors: ^D
Bye!
Bye!
</pre>

=={{header|Sidef}}==
<lang ruby>const rps = qw(r p s);

var msg = [
"Rock breaks scissors",
"Paper covers rock",
"Scissors cut paper",
];

[
"\n>> Rock Paper Scissors <<\n",
"** Enter 'r', 'p', or 's' as your play.",
"** Enter 'q' to exit the game.",
"** Running score shown as <your wins>:<my wins>\n",
].map {_.say};

var plays = 0;
var aScore = 0;
var pScore = 0;
var pi = ''; # player input
var pcf = [0,0,0]; # pcf = player choice frequency
var aChoice = (3.rand.int); # ai choice for first play is completely random

{
"Play: ".print;

pi = Sys.scanln; # get player choice
pi == 'q' && (break);

var pChoice = rps.indexWhere{_ == pi};

if (var block = __BLOCK__; pChoice == -1) {
"Invalid input!\n".warn;
block.run;
}

pcf[pChoice]++;
plays++;

# show result of play
">> My play: %-8s".printf(rps[aChoice]);

given ((aChoice - pChoice + 3 ) % 3)
when (0) do {
"Tie.".say;
}
when (1) do {
"%-*s %s".printlnf(30, msg[aChoice], 'My point');
aScore++;
}
when (2) do {
"%-*s %s".printlnf(30, msg[pChoice], 'Your point');
pScore++;
}
end;

# show score
"%-6s".printf("%d:%d".sprintf(pScore, aScore));

# compute ai choice for next play
switch (var rn = (plays.rand.int))
case (rn < pcf[0]) do {
aChoice = 1;
}
case (pcf[0]+pcf[1] > rn) do {
aChoice = 2;
}
default {
aChoice = 0;
}
end;

__BLOCK__.run;
}.run;</ruby>

'''Output:'''
<pre>

>> Rock Paper Scissors <<

** Enter 'r', 'p', or 's' as your play.
** Enter 'q' to exit the game.
** Running score shown as <your wins>:<my wins>

Play: r
>> My play: s Rock breaks scissors Your point
1:0 Play: p
>> My play: p Tie.
1:0 Play: r
>> My play: s Rock breaks scissors Your point
2:0 Play: s
>> My play: p Scissors cut paper Your point
3:0 Play: r
>> My play: p Paper covers rock My point
3:1 Play: p
>> My play: r Paper covers rock Your point
4:1 Play: q
</pre>
</pre>