Buch Cover Buch Cover Buch Cover Buch Cover

Web-Code: - Webcode Help

Casino (Würfel 2) (Simulationen)

Gegeben sind drei ungewöhnlich nummerierte Spielwürfel mit den Farben Blau, Rot und Gelb. Die drei Würfel sind nicht wie üblich mit 1 bis 6 nummeriert sondern besitzen die folgende Nummerierung:

  • Blauer Würfel mit den Zahlen 5, 7, 8, 9, 10, 18
  • Roter Würfel mit den Zahlen 2, 3, 4, 15, 16, 17
  • Gelber Würfel mit den Zahlen 1, 6, 11, 12, 13, 14

In einem Spiel wird jeweils mit zwei Würfeln gespielt. Es gewinnt der Wurf mit der höheren Augenzahl. Finden Sie den besten Würfel: Simulieren Sie dreimal 1 000 000 Spiele, indem Sie zunächst 1 000 000-mal BLAU gegen ROT, danach 1 000 000-mal BLAU gegen GELB und zuletzt noch ebenso oft ROT gegen GELB antreten lassen.

Folgende Funktion kann dabei von Nutzen sein.

anzahlSiege(a: string, b: string, anzahlSpiele: integer): integer

Dabei bezeichnen a und b einfach die Würfel, was auch mit einer Zahl (integer) zu bewältigen ist.

0 Kommentare

Bitte melde dich an um einen Kommentar abzugeben

5 Lösung(en)

import java.util.HashMap;

/**
 * Simuliere eigenartig nummerierte Würfel im Spiel gegeneinander (Aufgabe: Casino)
 * @author Philipp Gressly (phi@gressly.ch)
 */
/*
 * History: first Implementation: Sep 20, 2010
 * Bugs   :
 */
public class Casino {
  public static void main(String[] args) {
    new Casino().top(); }
 
  
  HashMap<String, int[]> wuerfel= new HashMap<String, int[]>();
  
  void top() {
    init();
    int blauRot  = anzahlSiege("BLAU", "ROT",  1000000);
    int rotGelb  = anzahlSiege("ROT",  "GELB", 1000000);
    int gelbBlau = anzahlSiege("GELB", "BLAU", 1000000); 
    System.out.println("Blau siegt über rot: " + blauRot   /  10000.0 + "%"); 
    System.out.println("Rot siegt über gelb: " + rotGelb   /  10000.0 + "%");
    System.out.println("Gelb siegt über blau: " + gelbBlau /  10000.0 + "%");
  }
  
  void init() {
    int[] BLAU = {5, 7, 8, 9, 10, 18};
    wuerfel.put("BLAU", BLAU);
    int[] ROT  = {2, 3, 4, 15, 16, 17};
    wuerfel.put("ROT", ROT);
    int[] GELB = {1, 6, 11, 12, 13, 14};
    wuerfel.put("GELB", GELB);
  }
  
  int anzahlSiege(String a, String b, int anzahlSpiele) {
    int[] wuerfelA = wuerfel.get(a);
    int[] wuerfelB = wuerfel.get(b);
    return anzahlSiege(wuerfelA, wuerfelB, anzahlSpiele);
  }
  
  int anzahlSiege(int[] wuerfelA, int[] wuerfelB, int anzahlSpiele) {
    int anzahlSiegeAGegenB = 0;
    while(anzahlSpiele > 0) {
      if(einzelSieg(wuerfelA, wuerfelB)) {
        anzahlSiegeAGegenB = anzahlSiegeAGegenB + 1;
      }
      anzahlSpiele = anzahlSpiele - 1;
    }
    return anzahlSiegeAGegenB;
  }

  /**
   * wahr, wenn Würfel A in einem Zusfallsspiel gegen Würfel B gewinnt.
   */
  boolean einzelSieg(int[] wuerfelA, int[] wuerfelB) {
    int seiteA = zufall(0, 5);
    int seiteB = zufall(0, 5);
    int wertA  = wuerfelA[seiteA];
    int wertB  = wuerfelB[seiteB];
    return wertA > wertB;
  }

  /**
   * Zufallszahl von min bis max.
   */
  int zufall(int min, int max) {
    return (int) (Math.random() * (max - min + 1)) + min;
  } 
  
  
} // end of class Casino
                
import random, collections

blau = [5, 7, 8, 9, 10, 18]
rot  = [2, 3, 4, 15, 16, 17]
gelb = [1, 6, 11, 12, 13, 14]


def wuerfeln(farbe1,farbe2,n):   
    wuerfe = []
    
    while n > 0:   
        wurf = random.randint(1,6)
        f1=(farbe1[wurf-1])
        wurf = random.randint(1,6)
        f2=(farbe2[wurf-1])
        if f1 > f2:
            wuerfe.append('erste Farbe')    
        else:
            wuerfe.append('zweite Farbe')
        n -= 1
    
    c = collections.Counter()
    for i in wuerfe:
        c[i] += 1
    
    print(c)


wuerfeln(blau, rot, 1000000)
wuerfeln(rot, gelb, 1000000)
wuerfeln(gelb,blau, 1000000)

                

Lösung von: Alex Groeg (Freies Lernen)

function Die(name, pips) {
  this.name = name;
  this.pips = pips;
  this.wins = 0;
  this.roll = function() {
    return this.pips[Math.floor(Math.random() * 6)];
  }
}

let blue = new Die('BLAU', [5, 7, 8, 9, 10, 18]),
    red = new Die('ROT', [2, 3, 4, 15, 16, 17]),
    yellow = new Die('GELB', [1, 6, 11, 12, 13, 14]);

function match(die1, die2) {
  die1.wins = 0; die2.wins = 0;
  for (let i = 1; i <= 1e6; i++)
    if (die1.roll() > die2.roll()) die1.wins++;
    else die2.wins++;
  let ranks = (die1.wins > die2.wins) ? [die1, die2] : [die2, die1];
  console.log (`
    ${ranks[0].name} schlägt ${ranks[1].name} (${ranks[0].wins} : ${ranks[1].wins})
  `); 
}

match(blue, red);
match(blue, yellow);
match(red, yellow);                                          // lissalanda@gmx.at
                

Lösung von: Lisa Salander (Heidi-Klum-Gymnasium Bottrop)

// NET 6.x | C# 10.x

var games = new List<Game>() {
    new (new (DC.blue), new (DC.red), 1_000_000),
    new (new (DC.blue), new (DC.yellow), 1_000_000),
    new (new (DC.red), new (DC.yellow), 1_000_000)
};

foreach (var g in games) {
    g.RollDice();
    Console.WriteLine(g);
}

public enum DC { blue, red, yellow }

public record class Game(Dice D1, Dice D2, int Rounds) {
    public void RollDice() {
        for (var i = 0; i < Rounds; i++) {
            if (D1.Points > D2.Points) D1.Wins++;
            else D2.Wins++;
        }
    }
    public override string ToString() => $"{D1.Color}: {D1.Wins} | {D2.Color}: {D2.Wins}";
}
public record class Dice(DC Color) {
    private int[] GetDice => Color switch {
        DC.blue => new int[] { 5, 7, 8, 9, 10, 18 },
        DC.red => new int[] { 2, 3, 4, 15, 16, 1 },
        DC.yellow => new int[] { 1, 6, 11, 12, 13, 14 },
        _ => Array.Empty<int>()
    };
    public int Points => GetDice[new Random().Next(0, GetDice.Length)];
    public int Wins { get; set; }
}
                

Lösung von: Jens Kelm (@JKooP)

// C++ 14 | VS-2022
#include <iostream>
#include <vector>

constexpr size_t ROUNDS{ 1'000'000 };
constexpr size_t DICE_SIDES{ 6 };
const char* DICE_COLORS[] = { "blue", "red", "yellow" };
size_t DICE_BLUE[]{ 5, 7, 8, 9, 10, 18 };
size_t DICE_RED[]{ 2, 3, 4, 15, 16, 1 };
size_t DICE_YELLOW[]{ 1, 6, 11, 12, 13, 14 };

enum class DC { blue, red, yellow };

class DICE {
private:
    DC _color;
    size_t _points{ 0 }, _wins{ 0 };
public:
    DICE(DC color) : _color{ color } {}
    const char* get_color() { return DICE_COLORS[(int)_color]; }
    size_t* get_dice() {
        switch (_color) {
        case DC::blue: return DICE_BLUE;
        case DC::red: return DICE_RED;
        default: return DICE_YELLOW;
        }
    };
    const size_t get_wins() { return _wins; }
    void set_wins() { _wins++; }
    const size_t get_points() { return get_dice()[rand() % DICE_SIDES]; }
};

class GAME {
private:
    DICE _d1, _d2;
public:
    GAME(DICE d1, DICE d2) : _d1{ d1 }, _d2{ d2 } {}
    void roll_dice() {
        for (size_t i{ 0 }; i < ROUNDS; i++) {
            if (_d1.get_points() > _d2.get_points()) _d1.set_wins();
            else _d2.set_wins();
        }
    }
    void print_result() {
        std::cout << _d1.get_color() << ": " << _d1.get_wins() << " | " << _d2.get_color() << ": " << _d2.get_wins() << "\n";
    }
};

int main() {
    srand((int)time(nullptr));
    std::vector<GAME> games{ { DC::blue, DC::red }, { DC::blue, DC::yellow }, { DC::red, DC::yellow } };
    for (auto game : games) {
        game.roll_dice();
        game.print_result();
    }
}
                

Lösung von: Jens Kelm (@JKooP)

Verifikation/Checksumme:

Jeder der drei Würfel hat einen "Schwächeren", der (bei genügend vielen Würfen) geschlagen werden kann: BLAU schlägt ROT, ROT schlägt GELB und GELB schlägt BLAU (als Casino-Betreiber lassen Sie den Spieler den Würfel zuerst auswählen und nehmen daraufhin selbst den besseren).

Aktionen

Bewertung

Durchschnittliche Bewertung:

Eigene Bewertung:
Bitte zuerst anmelden

Meta

Zeit: 2
Schwierigkeit: k.A.
Webcode: 5n2a-jfjb
Autor: Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch)

Download PDF

Download ZIP

Zu Aufgabenblatt hinzufügen