Rock-paper-scissors: Difference between revisions
m
→{{header|Sidef}}: improved and modified the code to work with the latest version of Sidef
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:
=={{header|Sidef}}==
<lang ruby>const rps = %w(r p s);
const msg = [
"Rock breaks scissors",
Line 3,961:
"Scissors cut paper",
];
say <<"EOT";
\n>> Rock Paper Scissors <<\n
Line 3,968:
** Running score shown as <your wins>:<my wins>
EOT
var plays = 0;
var aScore = 0;
Line 3,974:
var pcf = [0,0,0]; # pcf = player choice frequency
var aChoice = 3.rand.int; # ai choice for first play is completely random
loop {
var pi = Sys.scanln("Play: ");
pi == 'q' && break;
var pChoice = rps.index(pi);
if (pChoice == -1) {
STDERR.print("Invalid input!\n");
next;
}
++pcf[pChoice]
# show result of play
">> My play:
given ((aChoice - pChoice + 3) % 3) {
when (0) { say "Tie." }
}▼
▲ "%-*s %s".printlnf(30, msg[aChoice], 'My point');
▲ "%-*s %s".printlnf(30, msg[pChoice], 'Your point');
# show score
"%-6s".printf("%d:%d" % (pScore, aScore));
# compute ai choice for next play
when (pcf[0]+pcf[1] > rn) { aChoice =
▲ aChoice = 0;
}</lang>
|