Rock-paper-scissors: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 6,506: | Line 6,506: | ||
echo "I picked ${choices[i]} $(( computer_counts[i] )) times." |
echo "I picked ${choices[i]} $(( computer_counts[i] )) times." |
||
done</lang> |
done</lang> |
||
=={{header|Vlang}}== |
|||
Semi-translation of Go version: |
|||
<lang vlang>import rand |
|||
import os |
|||
const rps = 'rps' |
|||
const msg = [ |
|||
'Rock breaks scissors', |
|||
'Paper covers rock', |
|||
'Scissors cut paper' |
|||
] |
|||
fn main() { |
|||
println("Rock Paper Scissors") |
|||
println("Enter r, p, or s as your play. Anything else ends the game.") |
|||
println("Running score shown as <your wins>:<my wins>") |
|||
mut pi :='' // player input |
|||
mut a_score, mut p_score := 0, 0 |
|||
mut pcf := []int{len: 2, init: 0} // pcf = player choice frequency |
|||
mut a_choice := rand.intn(3) or {0} // ai choice for first play is completely random |
|||
for _ in 0..6 { |
|||
// get player choice |
|||
pi = os.input('Play: ').str() |
|||
if typeof(pi).name != 'string' || pi.len != 1 {break} |
|||
p_choice := rps.index_any(pi) |
|||
if p_choice < 0 {break} |
|||
pcf << p_choice |
|||
// show result of play |
|||
println('My play: ' + rps[a_choice].ascii_str()) |
|||
match (a_choice - p_choice + 3) % 3 { |
|||
0 {println("Tie.")} |
|||
1 {println('My point.\n' + msg[a_choice]) a_score++} |
|||
2 {println('Your point.\n' + msg[p_choice]) p_score++} |
|||
else {break} |
|||
} |
|||
// show score |
|||
println('$p_score : $a_score') |
|||
// compute ai choice for next play |
|||
rn := rand.intn(3) or {0} |
|||
match true { |
|||
rn < pcf[0] {a_choice = 2} |
|||
rn < pcf[0] + pcf[1] {a_choice = 0} |
|||
else {a_choice = rand.intn(3) or {0}} |
|||
} |
|||
} |
|||
}</lang> |
|||
{{out}} |
|||
Sample game: |
|||
<pre> |
|||
Rock Paper Scissors |
|||
Enter r, p, or s as your play. Anything else ends the game. |
|||
Running score shown as <your wins>:<my wins> |
|||
Play: p |
|||
My play: s |
|||
My point. |
|||
Scissors cut paper |
|||
0 : 1 |
|||
Play: r |
|||
My play: r |
|||
Tie. |
|||
0 : 1 |
|||
Play: s |
|||
My play: p |
|||
Your point. |
|||
Scissors cut paper |
|||
1 : 1 |
|||
Play: p |
|||
My play: p |
|||
Tie. |
|||
1 : 1 |
|||
Play: r |
|||
My play: p |
|||
My point. |
|||
Paper covers rock |
|||
1 : 2 |
|||
Play: s |
|||
My play: r |
|||
My point. |
|||
Rock breaks scissors |
|||
1 : 3 |
|||
</pre> |
|||
=={{header|Wee Basic}}== |
=={{header|Wee Basic}}== |