Rock-paper-scissors: Difference between revisions

Content added Content deleted
m (→‎{{header|Sidef}}: replaced "indexWhere" with "index")
m (→‎{{header|Sidef}}: improved and modified the code to work with the latest version of Sidef)
Line 3,955: Line 3,955:
=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>const rps = %w(r p s);
<lang ruby>const rps = %w(r p s);
 

const msg = [
const msg = [
"Rock breaks scissors",
"Rock breaks scissors",
Line 3,961: Line 3,961:
"Scissors cut paper",
"Scissors cut paper",
];
];
 

say <<"EOT";
say <<"EOT";
\n>> Rock Paper Scissors <<\n
\n>> Rock Paper Scissors <<\n
Line 3,968: Line 3,968:
** Running score shown as <your wins>:<my wins>
** Running score shown as <your wins>:<my wins>
EOT
EOT
 

var plays = 0;
var plays = 0;
var aScore = 0;
var aScore = 0;
Line 3,974: Line 3,974:
var pcf = [0,0,0]; # pcf = player choice frequency
var pcf = [0,0,0]; # pcf = player choice frequency
var aChoice = 3.rand.int; # ai choice for first play is completely random
var aChoice = 3.rand.int; # ai choice for first play is completely random
 

loop {
loop {
var pi = Sys.scanln("Play: ");
var pi = Sys.scanln("Play: ");
pi == 'q' && break;
pi == 'q' && break;
 

var pChoice = rps.index(pi);
var pChoice = rps.index(pi);
 

if (pChoice == -1) {
if (pChoice == -1) {
STDERR.print("Invalid input!\n");
STDERR.print("Invalid input!\n");
next;
next;
}
}
 

pcf[pChoice]++;
++pcf[pChoice];
plays++;
++plays;
 

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

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

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

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