Rock-paper-scissors: Difference between revisions

Content added Content deleted
Line 6,510: Line 6,510:
Semi-translation of Go version:
Semi-translation of Go version:


<syntaxhighlight lang="vlang">import rand
<syntaxhighlight lang="vlang">
import rand
import os
import os


const rps = 'rps'
const (
rps = 'rps'
const msg = [
msg = [
'Rock breaks scissors',
'Rock breaks scissors',s
'Paper covers rock',
'Paper covers rock',
'Scissors cut paper'
'Scissors cut paper'
]
]
)


fn main() {
fn main() {
Line 6,529: Line 6,532:
mut a_choice := rand.intn(3) or {0} // ai choice for first play is completely random
mut a_choice := rand.intn(3) or {0} // ai choice for first play is completely random
for _ in 0..6 {
for _ in 0..6 {
// get player choice
// get player choice
pi = os.input('Play: ').str()
pi = os.input('Play: ').str()
if typeof(pi).name != 'string' || pi.len != 1 {break}
if typeof(pi).name != 'string' || pi.len != 1 {break}
p_choice := rps.index_any(pi)
p_choice := rps.index_any(pi)
if p_choice < 0 {break}
if p_choice < 0 {break}
Line 6,546: Line 6,549:
println('$p_score : $a_score')
println('$p_score : $a_score')
// compute ai choice for next play
// compute ai choice for next play
rn := rand.intn(3) or {0}
rn := rand.intn(3) or {0}
match true {
match true {
rn < pcf[0] {a_choice = 2}
rn < pcf[0] {a_choice = 2}
rn < pcf[0] + pcf[1] {a_choice = 0}
rn < pcf[0] + pcf[1] {a_choice = 0}
else {a_choice = rand.intn(3) or {0}}
else {a_choice = rand.intn(3) or {0}}
}
}
}
}
}
}</syntaxhighlight>
</syntaxhighlight>


{{out}}
{{out}}