Two bullet roulette: Difference between revisions

Added FreeBASIC
(Added FreeBASIC)
 
(7 intermediate revisions by 3 users not shown)
Line 453:
=={{header|EasyLang}}==
{{trans|C}}
<syntaxhighlight lang="text">
len cyl[] 6
proc rshift . .
Line 469:
proc load . .
while cyl[1] = 1
call rshift
.
cyl[1] = 1
call rshift
.
proc spin . .
lim = randomrandint 6
for i = 1 to lim - 1
call rshift
.
.
procfunc fire . shot .
shot = cyl[1]
call rshift
return shot
.
procfunc method m[] . shot .
call unload
shot = 0
for m in m[]
if m = 1
call load
elif m = 2
call spin
elif m = 3
callif fire shot= 1
if shot = return 1
break 1
.
.
.
return 0
.
method$[] = [ "load" "spin" "fire" ]
Line 504:
n = 100000
for i = 1 to n
callsum += method m[] shot
sum += shot
.
for i = 1 to len m[]
Line 512 ⟶ 511:
print "-> " & 100 * sum / n & "% death"
.
call test [ 1 2 1 2 3 2 3 ]
call test [ 1 2 1 2 3 3 ]
call test [ 1 1 2 3 2 3 ]
call test [ 1 1 2 3 3 ]
</syntaxhighlight>
 
Line 567 ⟶ 566:
Method <load, load, spin, fire, fire> produces 49.841% deaths.
</pre>
 
=={{header|FreeBASIC}}==
{{trans|Wren}}
<syntaxhighlight lang="vbnet">Type Revolver
cylinder(0 To 5) As Integer
End Type
 
Sub rshift(r As Revolver)
Dim t As Integer = r.cylinder(5)
For i As Integer = 4 To 0 Step -1
r.cylinder(i + 1) = r.cylinder(i)
Next i
r.cylinder(0) = t
End Sub
 
Sub unload(r As Revolver)
For i As Integer = 0 To 5
r.cylinder(i) = 0
Next i
End Sub
 
Sub load(r As Revolver)
While r.cylinder(0) <> 0
rshift(r)
Wend
r.cylinder(0) = -1
rshift(r)
End Sub
 
Sub spin(r As Revolver)
For i As Integer = 1 To Int(Rnd * 6) + 1
rshift(r)
Next i
End Sub
 
Function fire(r As Revolver) As Integer
Dim As Integer shot = r.cylinder(0)
rshift(r)
Return shot
End Function
 
Function method(r As Revolver, s As String) As Integer
unload(r)
For i As Integer = 1 To Len(s)
Dim c As String = Mid(s, i, 1)
If c = "L" Then
load(r)
Elseif c = "S" Then
spin(r)
Elseif c = "F" Then
If fire(r) <> 0 Then Return 1
End If
Next i
Return 0
End Function
 
Function mstring(s As String) As String
Dim As String l = ""
For i As Integer = 1 To Len(s)
Dim As String c = Mid(s, i, 1)
If c = "L" Then
l &= "load, "
Elseif c = "S" Then
l &= "spin, "
Elseif c = "F" Then
l &= "fire, "
End If
Next i
Return Left(l, Len(l) - 2)
End Function
 
Dim As Revolver rev
Dim As Integer tests = 100000
Dim As String methods(0 To 3) = {"LSLSFSF", "LSLSFF", "LLSFSF", "LLSFF"}
 
For m As Integer = 0 To 3 'In methods
Dim sum As Integer = 0
For t As Integer = 1 To tests
sum += method(rev, methods(m))
Next t
Print mstring(methods(m)), " produces "; sum * 100.0 / tests; "% deaths."
Next m
 
Sleep</syntaxhighlight>
{{out}}
<pre>load, spin, load, spin, fire, spin, fire produces 55.385% deaths.
load, spin, load, spin, fire, fire produces 58.204% deaths.
load, load, spin, fire, spin, fire produces 55.372% deaths.
load, load, spin, fire, fire produces 50.052% deaths.</pre>
 
=={{header|Go}}==
Line 1,229 ⟶ 1,317:
55.74% deaths for scenario Load, Load, Spin, Fire, Spin, Fire.
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 0 ..< 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[len(cylinder) - 1]
copy(cylinder[1:], cylinder[0:])
cylinder[0] = t
}
unload :: proc() {
cylinder = false // array programming
}
load :: proc() {
for cylinder[0] {
rshift()
}
cylinder[0] = true
rshift()
}
spin :: proc() {
data: []int = {1, 2, 3, 4, 5, 6}
lim := rand.choice(data[:])
for i in 0 ..< 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")
}
}
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}}==
Line 1,744 ⟶ 1,921:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "random" for Random
import "./fmt" for Fmt
 
var Rand = Random.new()
2,122

edits