Chuck-a-Luck (Simulationen)
Simuliere das Spiel Chuck-a-luck
Bei diesem Spiel bezahlt der Spieler pro Runde Euro 1.- als Einsatz. Er darf nun nacheinander drei Würfel werfen. Zeigt mindestens ein Würfel eine 6, so erhält er zunächst den Einsatz zurück.
Zudem erhält er für jede geworfene 6 Euro 1.- als Gewinn ausbezahlt. Liegt keine 6, so verliert er den Einsatz.
Starten Sie mit einem Kapital von Euro 1'000.- und simulieren Sie 1000 Runden.
1 Kommentare (ansehen)
29 Lösung(en) (ansehen)
- java
- ruby
- python
- pl1
- vb
- cpp
- generic
- cpp
- python
- pascal
- python
- php
- python
- csharp
- javascript
- scala
- python
- abap
- cpp
- vb
- c
- python
- csharp
- csharp
- cpp
- csharp
- cpp
- python
- vb
01.
import
java.util.Scanner;
02.
03.
/**
04.
* Simuliere das Spiel Chuck-a-luck
05.
* Der Spieler bezahlt CHF 1.- als Einsatz.
06.
* Er darf nacheinander drei Würfel werfen.
07.
* Zeigt mindestens ein Würfel eine 6, so erhält
08.
* er den Einsatz zurück.
09.
* Zudem erhält er für jede geworfene 6 CHF 1.- als
10.
* Gewinn ausbezahlt.
11.
* Liegt keine 6, so verliert er den Einsatz.
12.
*
13.
* @author Philipp Gressly (phi AT gressly DOT ch)
14.
*/
15.
public
class
Chuck_A_Luck {
16.
public
static
void
main(String[] args) {
17.
new
Chuck_A_Luck().top();
18.
}
19.
void
top() {
20.
int
einsatz =
1
;
21.
int
kapital = einlesen(
"Startkapital"
);
22.
int
simulationen = einlesen(
"Anzahl Simulationen"
);
23.
for
(
int
s =
0
; s < simulationen; s++) {
24.
kapital = kapital - einsatz;
25.
int
gewinn = simuliere(einsatz);
26.
kapital = kapital + gewinn;
27.
}
28.
System.out.println(
"Es bleiben: "
+ kapital);
29.
}
30.
31.
int
simuliere(
int
einsatz) {
32.
int
wurf1 = wirf();
33.
int
wurf2 = wirf();
34.
int
wurf3 = wirf();
35.
int
anz6 = zaehleAnzahlSechser(wurf1, wurf2, wurf3);
36.
if
(
1
== anz6)
return
einsatz +
1
;
37.
if
(
2
== anz6)
return
einsatz +
2
;
38.
if
(
3
== anz6)
return
einsatz +
3
;
39.
else
return
0
;
// Kein Gewinn
40.
}
41.
42.
int
zaehleAnzahlSechser(
int
wurf1,
int
wurf2,
int
wurf3) {
43.
int
anz =
0
;
44.
if
(
6
== wurf1) anz = anz +
1
;
45.
if
(
6
== wurf2) anz = anz +
1
;
46.
if
(
6
== wurf3) anz = anz +
1
;
47.
return
anz;
48.
}
49.
50.
int
wirf() {
51.
return
(
int
) ((Math.random() *
6
) +
1
);
52.
}
53.
54.
55.
Scanner sc =
new
Scanner(System.in);
56.
int
einlesen(String wert) {
57.
System.out.println(
"Bitte "
+ wert +
" eingeben: "
);
58.
return
sc.nextInt();
59.
}
60.
61.
}
// end of class Chuck_A_Luck
Lösung von: Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch)
01.
#!/usr/bin/ruby
02.
03.
# @autor Philipp Gressly Freimann
04.
# 2011 März
05.
# Chuch-a-Luck
06.
07.
def
einlesen(wert)
08.
print
"Bitte #{wert} eingeben:"
09.
# Dezimalzahl (real) einlesen:
10.
return
STDIN
.gets.to_f
11.
end
12.
13.
def
simuliere(einsatz)
14.
wurf1 = wirf()
15.
wurf2 = wirf()
16.
wurf3 = wirf()
17.
anz6 = zaehleAnzahlSechser(wurf1, wurf2, wurf3)
18.
if
(
1
== anz6):
return
einsatz +
1
end
19.
if
(
2
== anz6):
return
einsatz +
2
end
20.
if
(
3
== anz6):
return
einsatz +
3
end
21.
return
0
# Kein Gewinn
22.
end
23.
24.
def
wirf()
25.
return
1
+ rand(
6
)
26.
end
27.
28.
def
zaehleAnzahlSechser(wurf1, wurf2, wurf3)
29.
anz =
0
30.
if
(
6
== wurf1): anz = anz +
1
end
31.
if
(
6
== wurf2): anz = anz +
1
end
32.
if
(
6
== wurf3): anz = anz +
1
end
33.
return
anz
34.
end
35.
36.
# START
37.
38.
einsatz =
1
39.
kapital = einlesen(
"Startkapital"
)
40.
simulationen = einlesen(
"Simulationen"
)
41.
42.
# starte simulation:
43.
aktuelleSimulation =
1
44.
while
(aktuelleSimulation <= simulationen)
45.
kapital = kapital - einsatz
46.
gewinn = simuliere(einsatz)
47.
kapital = kapital + gewinn
48.
aktuelleSimulation = aktuelleSimulation +
1
;
49.
end
50.
puts
"Es verbleiben #{kapital} Euro."
Lösung von: Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch)
01.
import
random
02.
s
=
1000
# Startkapital = 1000 Euro
03.
for
n
in
range (
1000
):
# 1000 Runden
04.
a
=
random.randint(
1
,
6
)
05.
b
=
random.randint(
1
,
6
)
06.
c
=
random.randint(
1
,
6
)
07.
s
=
s
-
1
08.
if
a
=
=
6
or
b
=
=
6
or
c
=
=
6
:
09.
s
=
s
+
1
10.
if
a
=
=
6
:
11.
s
=
s
+
1
12.
if
b
=
=
6
:
13.
s
=
s
+
1
14.
if
c
=
=
6
:
15.
s
=
s
+
1
16.
print
"Sie haben noch"
, s,
"Euro"
Lösung von: Name nicht veröffentlicht
01.
/*--- Programm-Parameter ---*/
02.
DCL
KO_RUNDEN
BIN
FIXED
(
31
)
VALUE
(
1000
);
03.
DCL
KO_ANZWURF
BIN
FIXED
(
31
)
VALUE
(
3
);
04.
DCL
ZW_KAPITAL
BIN
FIXED
(
31
)
INIT
(
1000
)
STATIC
;
05.
06.
/*--- Interne Variablen ---*/
07.
DCL
IND_RUNDE
BIN
FIXED
(
31
)
INIT
(
0
);
08.
DCL
IND_WURF
BIN
FIXED
(
31
)
INIT
(
0
);
09.
DCL
IND_TREFFER
BIN
FIXED
(
31
)
INIT
(
0
);
10.
DCL
ZAHL
(
KO_ANZWURF
)
BIN
FIXED
(
31
)
INIT
(
0
,
0
,
0
);
11.
DCL
ZW_RUNDE
PIC
'ZZZ9'
INIT
(
0
);
12.
DCL
ZW_ZAHL
PIC
'9'
INIT
(
0
);
13.
DCL
ZW_GEWINNP
PIC
'9'
INIT
(
0
);
14.
DCL
ZW_KAPITALP
PIC
'ZZZ9'
INIT
(
ZW_KAPITAL
);
15.
16.
/*--- Programm ---*/
17.
DO
IND_RUNDE
=
1
TO
KO_RUNDEN
;
18.
IND_TREFFER
=
0
;
/* Init pro Runde */
19.
ZW_KAPITAL
-=
1
;
/* Einsatz = 1.- */
20.
ZW_RUNDE
=
IND_RUNDE
;
21.
PUT
SKIP
EDIT
(
'Runde '
!!
TRIM
(
ZW_RUNDE
))(
A
);
22.
PUT
EDIT
(
'. Ihre Zahlen: '
)(
A
);
23.
DO
IND_WURF
=
1
TO
KO_ANZWURF
;
24.
ZAHL
(
IND_WURF
) =
TRUNC
((
RANDOM
()*
6
)+
1
);
25.
ZW_ZAHL
=
ZAHL
(
IND_WURF
);
26.
PUT
EDIT
(
ZW_ZAHL
!!
' '
)(
A
);
27.
IF
(
ZAHL
(
IND_WURF
)) =
6
THEN
IND_TREFFER
+=
1
;
28.
END
;
/* IND_WURF = 1 TO KO_ANZWURF */
29.
SELECT
(
IND_TREFFER
);
30.
WHEN
(
0
)
PUT
SKIP
LIST
(
' > Sorry, leider nichts gewonnen.'
);
31.
WHEN
(
1
,
2
,
3
)
DO
;
32.
ZW_GEWINNP
=
IND_TREFFER
;
33.
PUT
SKIP
LIST
(
' > Cool, du hast soeben '
!!
ZW_GEWINNP
!!
34.
' CHF gewonnen'
);
35.
ZW_KAPITAL
+= (
IND_TREFFER
+
1
);
/* Plus Einsatz zurück */
36.
END
;
37.
OTHER
DO
;
38.
PUT
SKIP
LIST
(
'FEHLER: Mehr als 3 Zahlen gezogen.'
);
39.
PUT
SKIP
LIST
(
'Bitte Programm prüfen.'
);
40.
IND_RUNDE
=
KO_RUNDEN
;
41.
END
;
42.
END
;
/* SELECT(IND_TREFFER); */
43.
ZW_KAPITALP
=
ZW_KAPITAL
;
44.
PUT
SKIP
LIST
(
' > Dein Kapital: '
!!
ZW_KAPITALP
!!
' CHF'
);
45.
END
;
/* IND_RUNDE = 1 TO KO_RUNDEN */
46.
PUT
SKIP
LIST
(
'=== ENDE ==='
);
Lösung von: Valentin Marolf (AXA)
01.
'Solution for VBA
02.
03.
'Spieleinstellungen
04.
Const
Runden
As
Integer
= 1000
'Anzahl Runden
05.
Const
StartKapital
As
Integer
= 1000
'Startkapital in CHF
06.
Const
Einsatz
As
Integer
= 1
'Einsatz in €
07.
Const
Gewinn
As
Integer
= 1
'Gewinn in €
08.
Const
Gewinnziffer
As
Byte
= 6
'Gewinn bei Ziffer 6
09.
Const
Wuerfel
As
Byte
= 3
'Anzahl Wuerfel pro Wurf
10.
11.
12.
'Aufrufende Prozedur
13.
Sub
Chuck_a_Luck()
14.
15.
'Variablen
16.
Dim
i
As
Long
'Schleifenvariable
17.
Dim
Kasse
As
Long
'Aktuelles Vermögen des Spielers
18.
19.
'Initialisierung
20.
Kasse = StartKapital * 0.88
'1CHF=0,88€
21.
22.
'Routine
23.
For
i = 1
To
Runden
24.
Kasse = (Kasse - Einsatz) + (Werfen * Gewinn)
25.
Next
i
26.
27.
Debug.Print Kasse
28.
29.
End
Sub
30.
31.
'Gibt false zurück, wenn keine Sechs geworfen wurde
32.
Private
Function
Werfen()
As
Byte
33.
'Variablen
34.
Dim
i
As
Byte
'Schleifenvariable
35.
Dim
count
As
Byte
'Trefferzahl
36.
37.
'Initialisierung
38.
count = 0
39.
Randomize
40.
41.
'Routine
42.
For
i = 1
To
Wuerfel
43.
If
Int(6 * Rnd + 1) = Gewinnziffer
Then
44.
count = count + 1
45.
End
If
46.
Next
i
47.
48.
'Rückgabe
49.
Werfen = count
50.
51.
End
Function
Lösung von: Felix Reinhold ()
01.
//Simulation von Chuck-a-Luck
02.
03.
#include <iostream>
04.
#include <stdlib.h>
05.
06.
int
wuerfeln();
07.
08.
int
main(
void
) {
09.
int
einsatz, runden;
10.
std::cout <<
"Wie viel Geld: "
;
11.
std::cin >> einsatz;
12.
std::cout <<
"Wie viele Runden: "
;
13.
std::cin >> runden;
14.
for
(
int
i = 0; i < runden; i++) {
15.
einsatz--;
16.
int
treffer = 0;
17.
for
(
int
j = 0; j < 3; j++) {
18.
if
((
rand
() % 6 + 1) == 6) {
19.
treffer++;
20.
}
21.
}
22.
einsatz += treffer;
23.
if
(einsatz == 0) {
24.
std::cout <<
"Sie haben nach der "
<< i <<
" Runde kein Geld mehr!!"
<< std::endl;
25.
break
;
26.
}
27.
}
28.
std::cout <<
"\n\n"
;
29.
std::cout <<
"Sie haben noch: "
<< einsatz <<
"\n\n"
;
30.
system
(
"pause"
);
31.
return
0;
32.
}
Lösung von: Name nicht veröffentlicht
01.
;Programmiersprache: PureBasic, Version
4.60
02.
03.
OpenConsole()
04.
05.
PrintN(
"Wie hoch ist das Startkapital? (Euro)"
)
06.
kapital = Val(Input())
07.
PrintN(
"Wie viele Runden sollen simuliert werden?"
)
08.
runden = Val(Input())
09.
10.
For i =
0
To runden
11.
PrintN(
"Runde Nummer "
+Str(i)+
", Kapital: "
+Str(kapital))
12.
wurf1 = Random(
5
)+
1
13.
wurf2 = Random(
5
)+
1
14.
wurf3 = Random(
5
)+
1
15.
16.
kapital = kapital -
1
17.
PrintN(
"Zahle Einsatz! - 1 Euro!"
)
18.
19.
If wurf1 =
6
Or wurf2 =
6
Or wurf3 =
6
20.
kapital = kapital+
1
21.
PrintN(
"Ein Würfel zeigt eine 6! + 1 Euro!"
)
22.
If wurf1 =
6
23.
kapital = kapital+
1
24.
PrintN(
"Wurf 1 ist eine 6! + 1 Euro!"
)
25.
EndIf
26.
If wurf2 =
6
27.
kapital = kapital+
1
28.
PrintN(
"Wurf 2 ist eine 6! + 1 Euro!"
)
29.
EndIf
30.
If wurf3 =
6
31.
kapital = kapital+
1
32.
PrintN(
"Wurf 3 ist eine 6! + 1 Euro!"
)
33.
EndIf
34.
Else
35.
PrintN(
"Verloren! kein Würfel zeigt eine 6!"
)
36.
EndIf
37.
38.
39.
Next i
40.
41.
PrintN(
"Der Endstand nach "
+Str(runden)+
" Runden beträgt "
+Str(kapital)+
" Euro."
)
42.
Input()
Lösung von: Ich Bins (tubs)
01.
// Copyright 2012, Christian J. Kiewiet.
02.
//
03.
///////////////////////////////////////////////////////////////////////////////
04.
// Lösung zur Aufgabenstellung ``Chuck-a-Luck (Simulationen)''
05.
//
06.
// ``Simuliere das Spiel Chuck-a-luck. Bei diesem Spiel bezahlt der Spieler pro
07.
// Runde Euro 1.- als Einsatz. Er darf nun nacheinander drei Würfel werfen.
08.
// Zeigt mindestens ein Würfel eine 6, so erhält er zunächst den Einsatz
09.
// zurück. Zudem erhält er für jede geworfene 6 Euro 1.- als Gewinn ausbezahlt.
10.
// Liegt keine 6, so verliert er den Einsatz. Starten Sie mit einem Kapital
11.
// von CHF 1'000.- und simulieren Sie 1000 Runden.''
12.
///////////////////////////////////////////////////////////////////////////////
13.
#include <cstdlib>
14.
#include <iostream>
15.
16.
class
ChuckALuck {
17.
public
:
18.
/** Seeds the PRNG with the current time in seconds. */
19.
inline
ChuckALuck () {
srand
(
time
(0)); }
20.
21.
/** Performs |kSimulations| dice rolls with three dice. Every every dice
22.
* showing six, the player's capital is increased by 1. Returns the
23.
* capital after all simulations, aborts on depletion of capital. */
24.
int
Simulate() {
25.
int
capital(kInitialCapital);
26.
for
(
int
i = kSimulations; i-- && capital--; )
27.
for
(
int
i = 3, j = 0; i--; j +=
static_cast
<
int
>((
rand
() % 6) == 5),
28.
capital += j);
29.
return
capital;
30.
}
31.
32.
private
:
33.
static
const
int
kInitialCapital = 1000;
34.
static
const
int
kSimulations = 1000;
35.
};
36.
37.
int
main(
int
argc,
char
* argv[]) {
38.
ChuckALuck game;
39.
game.Simulate();
40.
}
Lösung von: Christian Kiewiet ()
01.
#!/usr/bin/python3
02.
04.
05.
#zufallsgenerator importieren, initialisieren
06.
import
random
07.
random.seed()
08.
09.
#Starteinsatz 1000€
10.
einsatz
=
1000
11.
print
(
"Dein Starteinsatz beträgt 1000€ gespielt werden 1000 Runden"
)
12.
runde
=
0
13.
while
not
runde
=
=
1000
:
14.
15.
#pro Runde 1€ Einsatz
16.
einsatz
=
einsatz
-
1
17.
18.
#Nacheinander 3 Würfel werfen
19.
wuerfel1
=
random.randint(
1
,
6
)
20.
wuerfel2
=
random.randint(
1
,
6
)
21.
wuerfel3
=
random.randint(
1
,
6
)
22.
runde
=
runde
+
1
23.
24.
#zeigt 1 Würfel eine 6 -> Einsatz zurück
25.
if
wuerfel1
=
=
6
:
26.
einsatz
=
einsatz
+
1
27.
28.
#für jede weitere 6 1€ gewinn
29.
if
wuerfel2
=
=
6
:
30.
einsatz
=
einsatz
+
1
31.
if
wuerfel3
=
=
6
:
32.
einsatz
=
einsatz
+
1
33.
print
(runde,
"wurden gespielt"
, einsatz,
"€ habe ich noch"
)
Lösung von: Name nicht veröffentlicht
01.
program
ChuckALuck (input, output);
02.
{ Bei diesem Spiel bezahlt der Spieler pro Runde Euro 1.- als Einsatz. Er darf nun nacheinander drei Würfel werfen. Zeigt mindestens ein Würfel eine 6, so erhält er zunächst den Einsatz zurück.
03.
Zudem erhält er für jede geworfene 6 Euro 1.- als Gewinn ausbezahlt. Liegt keine 6, so verliert er den Einsatz.
04.
Starten Sie mit einem Kapital von 1'000.- und simulieren Sie 1000 Runden. }
05.
06.
const
07.
STARTWERT =
1000
;
08.
RUNDEN =
1000
;
09.
WURFANZAHL =
3
;
10.
WUERFELSEITEN =
6
;
11.
12.
var
13.
Geld,
14.
i, j:
integer
;
15.
bankrott,
// wird gesetzt wenn einem das Geld ausgeht.
16.
sechsGeworfen:
boolean
;
// wird gesetzt wenn man diese Runde 6 gew. hat.
17.
18.
begin
19.
{ initialisieren }
20.
Randomize;
{ Zufallszahlengenerator initialisieren }
21.
Geld := STARTWERT;
22.
bankrott :=
false
;
23.
i :=
1
;
24.
25.
{ "Simulation": }
26.
while
(i <= RUNDEN)
and
(
not
bankrott)
do
27.
begin
28.
i := i +
1
;
29.
Geld := Geld -
1
;
30.
sechsGeworfen :=
false
;
31.
32.
for
j :=
1
to
WURFANZAHL
do
33.
if
(Random(WUERFELSEITEN) +
1
) = WUERFELSEITEN
then
34.
begin
35.
Geld := Geld +
1
;
36.
sechsGeworfen :=
true
;
37.
end
;
{ if (Random(WUERFELSEITEN) + 1) = WUERFELSEITEN then }
38.
39.
if
sechsGeworfen
then
40.
Geld := Geld +
1
;
41.
42.
if
Geld =
0
then
43.
bankrott :=
true
;
44.
end
;
{ while (i <= RUNDEN) and (not bankrott) }
45.
46.
{ Endausgabe: }
47.
if
bankrott
then
48.
writeln
(
'Sie sind leider vor dem Ende bankrott gegangen'
)
49.
else
50.
writeln
(
'Rest: '
, Geld);
51.
52.
end
.
{ ChuckALuck }
Lösung von: Patrick Reif ()
01.
# Python 3.4.2
02.
03.
from
random
import
randint
04.
05.
kapital
=
1000
06.
07.
for
i
in
range(
1001
):
08.
ergebnisse
=
list()
09.
kapital
-
=
1
10.
for
wurf
in
range(
3
):
11.
augenzahl
=
randint(
1
,
6
)
12.
ergebnisse.append(augenzahl)
13.
14.
# Mindestens eine 6: Einsatz zurueck
15.
if
6
in
ergebnisse:
16.
kapital
+
=
1
17.
18.
# Fuer jede 6 den Gewinn ausbezahlen
19.
for
i
in
ergebnisse:
20.
if
i
=
=
6
:
21.
kapital
+
=
1
22.
23.
print
(
'Kapital nach 1000 Runden:'
, kapital)
Lösung von: Hilli Hilli ()
01.
<?php
02.
03.
/**
04.
* Chuck-a-Luck
05.
* @author Lukas Müller
06.
*/
07.
08.
$wuerfe
=
array
(rand(1, 6),rand(1, 6),rand(1, 6));
09.
$treffer_mittelwert
=
array
();
10.
$kapital
= 1000;
11.
for
(
$i
= 0;
$i
<=
$kapital
;
$i
++){
12.
$wuerfe
=
array
(rand(1, 6),rand(1, 6),rand(1, 6));
13.
$zaehler
= 0;
14.
for
(
$h
= 0;
$h
<=3;
$h
++){
15.
if
(
$wuerfe
[
$h
] == 6){
16.
$zaehler
++;
17.
}
18.
}
19.
$treffer_mittelwert
[
$i
] =
$zaehler
;
20.
switch
(
$zaehler
){
21.
case
0:
22.
$kapital
--;
23.
break
;
24.
case
1:
25.
$kapital
++;
26.
break
;
27.
case
2:
28.
$kapital
+= 2;
29.
break
;
30.
case
3:
31.
$kapital
+= 3;
32.
break
;
33.
}
34.
}
35.
echo
"<p>Durchschnittlich hat man in einer Runde "
.
round
(
array_sum
(
$treffer_mittelwert
)/
count
(
$treffer_mittelwert
), 3) .
" sechser. <br />"
;
36.
echo
"Das Endkapital beträgt $kapital.-</p>"
;
37.
?>
Lösung von: Lukas Müller (Informatik Studium)
01.
import
random
02.
runde
=
0
03.
guthaben
=
1000
04.
05.
while
runde <
1000
:
06.
guthaben
-
=
1
07.
wurfliste
=
[]
08.
versuch
=
0
09.
while
versuch <
3
:
10.
wurf
=
random.randint(
1
,
6
)
11.
wurfliste.append(wurf)
12.
versuch
+
=
1
13.
if
6
in
wurfliste:
14.
guthaben
+
=
1
15.
16.
sechs_gewuerfelt
=
wurfliste.count(
6
)
17.
guthaben
+
=
sechs_gewuerfelt
#für jede gewürfelte sechs gibt es einen Euro
18.
runde
+
=
1
19.
20.
print
(guthaben)
Lösung von: Py Thon ()
01.
using
System;
02.
03.
namespace
Chuck_a_luck {
04.
class
Program {
05.
static
void
Main() {
06.
Random rnd =
new
Random();
07.
int
runde = 1, kapital = 1000;
08.
09.
for
(;
10.
runde <= 1000 && kapital > 0;
11.
runde++) {
12.
13.
kapital--;
14.
15.
for
(
int
i = 0; i < 3; i++)
16.
if
(rnd.Next(1, 7) == 6) kapital++;
17.
}
18.
19.
Console.WriteLine(
"Das Spiel wurde in Runde {0} mit {1} Euro Kapital beendet"
, --runde, kapital);
20.
21.
Console.ReadKey(
true
);
22.
}
23.
}
24.
}
Lösung von: Marcel Kapma ()
01.
var
balance = 1000,
02.
i = 1;
03.
04.
function
roll() {
05.
var
j = 1;
06.
balance--;
07.
for
(j; j <= 3; j++)
08.
if
(Math.round((Math.random() * 6) + 1) == 6) balance++;
09.
}
10.
11.
for
(i; i <= 1000; i++) roll();
12.
console.log(
"Guthaben nach 1000 Runden: "
+ balance +
" €"
);
Lösung von: Lisa Salander (Heidi-Klum-Gymnasium Bottrop)
01.
object
ChuckaLuckSimu
extends
App {
02.
var
Kapital
:
Int
=
1000
03.
var
inti
=
scala.util.Random
04.
var
counter
:
Int
=
0
05.
var
random
_
1
=
inti.nextInt(
7
)
06.
var
random
_
2
=
inti.nextInt(
7
)
07.
var
random
_
3
=
inti.nextInt(
7
)
08.
while
(counter !
=
1000
){
09.
Kapital
=
Kapital -
1
10.
if
(random
_
1
==
0
){
11.
random
_
1
=
inti.nextInt(
7
)
12.
}
13.
else
if
(random
_
2
==
0
){
14.
random
_
2
=
inti.nextInt(
7
)
15.
}
16.
else
if
(random
_
3
==
1000
){
17.
random
_
3
=
inti.nextInt(
7
)
18.
}
19.
else
{
20.
if
(random
_
1
==
6
){
21.
Kapital
=
Kapital +
1
22.
}
23.
if
(random
_
2
==
6
){
24.
Kapital
=
Kapital +
2
25.
}
26.
if
(random
_
3
==
6
){
27.
Kapital
=
Kapital +
3
28.
}
29.
random
_
1
=
inti.nextInt(
7
)
30.
random
_
2
=
inti.nextInt(
7
)
31.
random
_
3
=
inti.nextInt(
7
)
32.
counter
=
counter +
1
33.
}
34.
}
35.
println(
"Dein Kapital jetzt beträgt "
+ Kapital )
36.
}
Lösung von: Name nicht veröffentlicht
01.
import
random
02.
random.seed ()
03.
04.
Versuche
=
1000
05.
Kapital
=
1000
06.
07.
while
Versuche !
=
0
:
08.
09.
Kapital
-
=
1
10.
11.
x1
=
random.randint(
1
,
6
)
12.
if
(x1)
=
=
6
:
13.
Kapital
+
=
1
14.
print
(
"juhhu"
)
15.
16.
x2
=
random.randint(
1
,
6
)
17.
if
(x2)
=
=
6
:
18.
Kapital
+
=
1
19.
20.
x3
=
random.randint(
1
,
6
)
21.
if
(x3)
=
=
6
:
22.
Kapital
+
=
1
23.
24.
Versuche
-
=
1
25.
26.
print
(Kapital)
Lösung von: La Ku ()
REPORT WUERFELN.
DATA: lv_geld TYPE int4 VALUE 1000,
lv_wurf1 TYPE i,
lv_wurf2 TYPE i,
lv_wurf3 TYPE i.
DO 1000 TIMES.
lv_geld = lv_geld - 1.
CALL FUNCTION 'AKB_RANDOM'
EXPORTING
MIN = 1
MAX = 6
IMPORTING
RND = lv_wurf1.
CALL FUNCTION 'AKB_RANDOM'
EXPORTING
MIN = 1
MAX = 6
IMPORTING
RND = lv_wurf2.
CALL FUNCTION 'AKB_RANDOM'
EXPORTING
MIN = 1
MAX = 6
IMPORTING
RND = lv_wurf3.
IF ( lv_wurf1 <> 6 AND lv_wurf2 <> 6 AND lv_wurf3 <> 6 ).
ELSE.
lv_geld = lv_geld + 1.
If ( lv_wurf1 = 6 ).
lv_geld = lv_geld + 1.
ENDIF.
IF ( lv_wurf2 = 6 ).
lv_geld = lv_geld + 1.
ENDIF.
IF ( lv_wurf3 = 6 ).
lv_geld = lv_geld + 1.
ENDIF.
ENDIF.
ENDDO.
WRITE:/ lv_geld.
Lösung von: Name nicht veröffentlicht
01.
#include "stdafx.h"
02.
#include <iostream>
03.
#include <time.h>
04.
05.
using
namespace
std;
06.
07.
int
main()
08.
{
09.
srand
(
time
(NULL));
10.
int
kapital = 1000;
11.
int
ergebnis;
12.
13.
for
(
int
i = 1; i < 1000; i++)
14.
{
15.
kapital--;
16.
for
(
int
j = 1; j <= 3; j++)
17.
{
18.
ergebnis =
rand
() % 6;
19.
20.
if
(ergebnis == 5)
21.
{
22.
kapital++;
23.
}
24.
}
25.
}
26.
cout << kapital << endl;
27.
28.
system
(
"PAUSE"
);
29.
}
Lösung von: Name nicht veröffentlicht
01.
Private
Sub
CommandButton1_Click()
02.
Dim
einsatz
As
Integer
03.
Dim
result
As
Integer
04.
Dim
guthaben
As
Integer
05.
Dim
sechser
As
Integer
06.
07.
08.
guthaben = 1000
09.
einsatz = 1
10.
11.
12.
For
k = 0
To
1000
13.
guthaben = guthaben - einsatz
14.
sechser = 0
15.
For
i = 0
To
2
16.
result = Int((6 * Rnd) + 1)
17.
If
result = 6
Then
18.
guthaben = guthaben + einsatz
19.
sechser = sechser + 1
20.
End
If
21.
22.
If
sechser >= 2
Then
23.
guthaben = guthaben + einsatz
24.
End
If
25.
Next
26.
Next
27.
End
Sub
Lösung von: Ozan Gümüstas ()
01.
#include <stdio.h>
02.
#include <time.h>
03.
#include <stdlib.h>
04.
05.
06.
void
main(){
07.
08.
int
runde = 1;
09.
int
geld = 1000;
10.
int
wuerfel1, wuerfel2, wuerfel3;
11.
srand
(
time
(NULL));
12.
13.
while
(runde <= 1000){
14.
wuerfel1 =
rand
() % 6 + 1;
15.
wuerfel2 =
rand
() % 6 + 1;
16.
wuerfel3 =
rand
() % 6 + 1;
17.
if
(wuerfel1 != 6 && wuerfel2 != 6 && wuerfel3 != 6){
18.
geld = geld - 1;
19.
printf
(
"%d.Sie haben keine 6 gewuerfelt. Sie haben nun %d Euro.\n"
,runde,geld);
20.
runde++;
21.
}
22.
if
(wuerfel1 == 6 && wuerfel2 != 6 && wuerfel3 != 6){
23.
printf
(
"%d.Sie haben eine 6 gewuerfelt. Sie haben nun %d Euro.\n"
,runde,geld);
24.
runde++;
25.
}
26.
if
(wuerfel1 != 6 && wuerfel2 == 6 && wuerfel3 != 6){
27.
printf
(
"%d.Sie haben eine 6 gewuerfelt. Sie haben nun %d Euro.\n"
,runde,geld);
28.
runde++;
29.
}
30.
if
(wuerfel1 != 6 && wuerfel2 != 6 && wuerfel3 == 6){
31.
printf
(
"%d.Sie haben eine 6 gewuerfelt. Sie haben nun %d Euro.\n"
,runde,geld);
32.
runde++;
33.
}
34.
if
(wuerfel1 == 6 && wuerfel2 == 6 && wuerfel3 != 6){
35.
geld = geld + 2;
36.
printf
(
"%d.Sie haben zwei 6 gewuerfelt. Sie haben nun %d Euro.\n"
,runde,geld);
37.
runde++;
38.
}
39.
if
(wuerfel1 == 6 && wuerfel2 != 6 && wuerfel3 == 6){
40.
geld = geld + 2;
41.
printf
(
"%d.Sie haben zwei 6 gewuerfelt. Sie haben nun %d Euro.\n"
,runde,geld);
42.
runde++;
43.
}
44.
if
(wuerfel1 != 6 && wuerfel2 == 6 && wuerfel3 == 6){
45.
geld = geld + 2;
46.
printf
(
"%d.Sie haben zwei 6 gewuerfelt. Sie haben nun %d Euro.\n"
,runde,geld);
47.
runde++;
48.
}
49.
if
(wuerfel1 == 6 && wuerfel2 == 6 && wuerfel3 == 6){
50.
geld = geld + 3;
51.
printf
(
"%d.Sie haben drei 6 gewuerfelt. Sie haben nun %d Euro.\n"
,runde,geld);
52.
runde++;
53.
}
54.
}
55.
}
Lösung von: Fynn Koch (keine)
01.
from
random
import
randint
02.
kapital
=
1000
03.
04.
for
i
in
range(
1000
):
05.
sechserEnthalten
=
False
06.
kapital
-
=
1
07.
for
j
in
range(
0
,
3
):
08.
wurf
=
randint(
1
,
6
)
09.
if
wurf
=
=
6
:
10.
kapital
+
=
1
# jede 6 einen Taler
11.
if
not
sechserEnthalten:
# Einsatz zurück bei der ersten 6, einmalig
12.
kapital
+
=
1
13.
sechserEnthalten
=
True
14.
15.
print
(kapital)
Lösung von: Peter Pan (Home Office)
01.
using
System;
02.
using
System.Collections.Generic;
03.
using
System.Linq;
04.
using
System.Text;
05.
using
System.Threading.Tasks;
06.
07.
namespace
ChuckLuck
08.
{
09.
class
Program
10.
{
11.
static
void
Main(
string
[] args)
12.
{
13.
Random rnd =
new
Random();
14.
int
würfel1;
15.
int
würfel2;
16.
int
würfel3;
17.
int
euro = 1000;
18.
int
runden = 1;
19.
20.
21.
for
(; runden <= 1000; runden++)
22.
{
23.
würfel1 = rnd.Next(1,7);
24.
würfel2 = rnd.Next(1,7);
25.
würfel3 = rnd.Next(1,7);
26.
int
gv = 0;
27.
28.
if
(würfel1 == 6)
29.
{
30.
euro++;
31.
gv++;
32.
}
33.
34.
if
(würfel2 == 6)
35.
{
36.
euro++;
37.
gv++;
38.
}
39.
40.
if
(würfel3 == 6)
41.
{
42.
euro++;
43.
gv++;
44.
}
45.
euro--;
46.
gv--;
47.
if
(euro == 0)
48.
{
49.
break
;
50.
}
51.
Console.WriteLine($
"Runde {runden}\n1.Würfel = {würfel1}\n2.Würfel = {würfel2}\n3.Würfel = {würfel3}\nGewinn/Verlust = {gv} Euro\n"
);
52.
53.
}
54.
Console.WriteLine(
"Das Spiel wurde nach {0} Runden mit {1} Euro beendet."
,runden-1,euro);
55.
Console.ReadKey();
56.
}
57.
}
58.
}
Lösung von: Chrischi Leif (S&N Datentechnik)
01.
// NET 6.x | C# 10.x | VS-2022
02.
03.
var balance = 1_000;
04.
var rounds = 1_000;
05.
06.
for
(
int
i = 0; i < rounds; i++) {
07.
balance--;
08.
var d = Enumerable.Range(0, 3).Select(x =>
new
Random().Next(1, 7)).Count(x => x == 6);
09.
if
(d != 0) balance += d + 1;
10.
if
(balance < 0) {
11.
Console.WriteLine(
"Leider alles verloren!"
);
12.
return
;
13.
}
14.
}
15.
Console.WriteLine($
"Guthaben: {balance} Euro"
);
Lösung von: Jens Kelm (@JKooP)
01.
// C++ 17
02.
03.
#include <iostream>
04.
05.
int
main() {
06.
srand
(
time
(nullptr));
07.
auto balance{ 1'000 };
08.
auto rounds{ 1'000 };
09.
10.
for
(auto i{ 0 }; i < rounds; i++) {
11.
balance--;
12.
auto d{ 0 };
13.
for
(auto k{ 0 }; k < 3; k++)
14.
d += (
rand
() % 6 + 1 == 6);
15.
if
(d != 0) balance += d + 1;
16.
if
(balance <= 0) {
17.
std::cout <<
"Leider alles verloren!"
;
18.
break
;
19.
}
20.
}
21.
std::cout <<
"Guthaben: "
<< balance <<
" Euro\n"
;
22.
}
Lösung von: Jens Kelm (@JKooP)
01.
using
System;
02.
03.
namespace
ChuckAluck
04.
{
05.
class
Program
06.
{
07.
static
void
Main(
string
[] args)
08.
{
09.
Random r =
new
Random();
10.
string
endText =
""
;
11.
ushort
budget = 1000, rounds = 1000, gain = 0, roundPrice = 1;
12.
bool
returnBudget =
false
;
13.
int
draw = 0;
14.
15.
for
(
ushort
i = 1; i <= rounds; i++) {
16.
budget -= roundPrice;
17.
returnBudget =
true
;
18.
19.
Console.WriteLine(
"\nRunde {0}"
, i);
20.
for
(
byte
b = 1; b < 4; b++) {
21.
draw = r.Next(1, 7);
22.
Console.WriteLine(
"Würfel Nr. {0}. Gewürfelt: {1}"
, b, draw);
23.
24.
if
(draw == 6) {
25.
gain++;
26.
if
(returnBudget) {
27.
budget++;
28.
returnBudget =
false
;
29.
}
30.
}
31.
}
32.
if
(budget <= 0) {
33.
endText = $
"\nNach {i} Runden haben Sie kein Geld mehr"
;
34.
break
;
35.
}
36.
37.
endText = $
"\nSie haben {i} lang gespielt, {gain} Euro Gewinn gemacht und verlassen das Chuck-a-luck mit {budget + gain} Euro."
;
38.
}
39.
40.
Console.WriteLine(endText);
41.
Console.ReadKey();
// endl
42.
}
43.
}
44.
}
Lösung von: Name nicht veröffentlicht
01.
#include <iostream>
02.
#include <stdlib.h> /* srand, rand */
03.
#include <time.h> /* time */
04.
using
namespace
std;
05.
06.
07.
int
main(){
08.
srand
(
time
(NULL));
09.
string endText =
""
;
10.
short
budget = 1000, rounds = 1000, gain = 0, roundPrice = 1;
11.
bool
returnBudget =
true
;
12.
int
draw = 0;
13.
14.
for
(
short
i = 1; i <= rounds; i++) {
15.
if
(budget <= 0) {
16.
cout <<
"Sie haben "
<< i <<
" Runden gespielt und haben kein Geld mehr."
;
17.
return
0;
18.
}
19.
20.
budget -= roundPrice;
21.
returnBudget =
true
;
22.
23.
for
(
short
b = 0; b < 4; b++) {
24.
draw =
rand
() % 6 + 1;
25.
26.
if
(draw == 6) {
27.
gain++;
28.
if
(returnBudget) {
29.
budget++;
30.
returnBudget =
false
;
31.
}
32.
}
33.
}
34.
}
35.
36.
cout <<
"Sie haben "
<< rounds <<
" Runden lang gespielt, "
<< gain <<
" Euro Gewinn gemacht und verlassen das Chuck-a-luck mit "
<< (budget + gain);
37.
}
Lösung von: Name nicht veröffentlicht
01.
from
random
import
randint
02.
euro
=
1000
03.
for
a
in
range(
1000
):
04.
euro
-
=
1
;pruef
=
euro
05.
for
a
in
range(
3
):
06.
wurf
=
randint(
1
,
6
)
07.
if
wurf
=
=
6
: euro
+
=
1
08.
if
euro!
=
pruef: euro
+
=
1
09.
print
(euro)
Lösung von: rob ert (tub)
01.
' VBA
02.
03.
Sub
Main()
04.
Dim
i
As
Integer
, k
As
Integer
, d
As
Integer
, balance
As
Integer
05.
Const
ROUNDS
As
Integer
= 1000
06.
Randomize
07.
balance = 1000
08.
09.
For
i = 1
To
ROUNDS
10.
balance = balance - 1
11.
d = 0
12.
For
k = 1
To
3
13.
d = d + IIf(
CInt
(Rnd() * 6 + 1) = 6, 1, 0)
14.
Next
15.
If
d <> 0
Then
balance = balance + d + 1
16.
If
balance <= 0
Then
17.
Debug.Print
"Leider alles verloren!"
18.
Exit
For
19.
End
If
20.
Next
21.
22.
Debug.Print
"Guhaben: "
& balance &
"Euro"
23.
24.
End
Sub
Lösung von: Jens Kelm (@JKooP)
Verifikation/Checksumme: (ansehen)
Pro Spiel verliert man im Durchschnitt ca. 7.87 Cent (das entspricht einem Erwartungswert von 17/216 für das Casino, also für den Spielbetreiber).
Nach 1000 Simulationen sollten bei einem Startkapital von Euro 1000 noch ca. 920 Euro als Kapital übrig bleiben.
Kommentare (1)
Es gibt nicht einfach für jede 6 einen Taler.
Für die erste 6 bekommt man ihn doppelt.