Two bullet roulette: Difference between revisions
Content added Content deleted
Space Ghost (talk | contribs) (Two Bullet Roulette Task Programmed in Odin) |
|||
Line 1,229: | Line 1,229: | ||
55.74% deaths for scenario Load, Load, Spin, Fire, Spin, Fire. |
55.74% deaths for scenario Load, Load, Spin, Fire, Spin, Fire. |
||
50.14% deaths for scenario Load, Load, Spin, Fire, Fire.</pre> |
50.14% deaths for scenario Load, Load, Spin, Fire, Fire.</pre> |
||
=={{header|Odin}}== |
|||
<syntaxhighlight lang="Go"> |
|||
/* imports */ |
|||
import "core:fmt" |
|||
import "core:strings" |
|||
import "core:math/rand" |
|||
/* globals */ |
|||
cylinder := [6]bool{} |
|||
/* main block */ |
|||
main :: proc() { |
|||
rand.set_global_seed(42) |
|||
tests := 100000 |
|||
sequence := [?]string{"LSLSFSF", "LSLSFF", "LLSFSF", "LLSFF"} |
|||
for m in sequence { |
|||
sum := 0 |
|||
for t in 1 ..= tests { |
|||
sum += method(m) |
|||
} |
|||
pc: f64 = cast(f64)sum * 100 / cast(f64)tests |
|||
fmt.printf("%-40s produces %6.3f%% deaths.\n", mstring(m), pc) |
|||
} |
|||
} |
|||
/* definitions */ |
|||
rshift :: proc() { |
|||
t := cylinder[5] |
|||
for i := 4; i >= 0; i -= 1 { |
|||
cylinder[i + 1] = cylinder[i] |
|||
} |
|||
cylinder[0] = t |
|||
} |
|||
unload :: proc() { |
|||
for i in 0 ..< 6 { |
|||
cylinder[i] = false |
|||
} |
|||
} |
|||
load :: proc() { |
|||
for cylinder[0] { |
|||
rshift() |
|||
} |
|||
cylinder[0] = true |
|||
rshift() |
|||
} |
|||
spin :: proc() { |
|||
data: []int = {1, 2, 3, 4, 5, 6} |
|||
lim := 1 + rand.choice(data[:]) |
|||
for i in 1 ..< lim { |
|||
rshift() |
|||
} |
|||
} |
|||
fire :: proc() -> bool { |
|||
shot := cylinder[0] |
|||
rshift() |
|||
return shot |
|||
} |
|||
method :: proc(s: string) -> int { |
|||
unload() |
|||
for character in s { |
|||
switch character { |
|||
case 'L': |
|||
load() |
|||
case 'S': |
|||
spin() |
|||
case 'F': |
|||
if fire() { |
|||
return 1 |
|||
} |
|||
} |
|||
} |
|||
return 0 |
|||
} |
|||
mstring :: proc(s: string) -> string { |
|||
l: [dynamic]string |
|||
for character in s { |
|||
switch character { |
|||
case 'L': |
|||
append(&l, "load") |
|||
case 'S': |
|||
append(&l, "spin") |
|||
case 'F': |
|||
append(&l, "fire") |
|||
} |
|||
} |
|||
sep: string : ", " |
|||
return strings.join(l[:], ", ") |
|||
} |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
load, spin, load, spin, fire, spin, fire produces 55.771% deaths. |
|||
load, spin, load, spin, fire, fire produces 58.313% deaths. |
|||
load, load, spin, fire, spin, fire produces 55.487% deaths. |
|||
load, load, spin, fire, fire produces 49.972% deaths. |
|||
</pre> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |