Python :: Aufgabe #237 :: Lösung #5
7 Lösungen
#237
Glücksspiel Zufällige Zahl 0-9
Fortgeschrittener - Python
von re_mas
- 27.11.2019 um 18:30 Uhr
Die Aufgabenstellung ist wie folgt:
- Glücksspiel bei der eine random Zahl zwischen 0 - 9 erzeugt werden soll.
- Der Spieler hat ein Startkonto von 10.000 Punkten und kann damit einen beliebigen Teilbetrag auf die zufällig erzeugte Zahl setzen.
- Liegt er richtig bekommt er das 9 Fache seines Einsatzes als Gewinn
- Programmieren Sie ein entsprechendes Programm, welches die Eingaben von der Tastatur einliest und
die Ausgaben auf dem Bildschirm liefert. Die zu erratende Zahl kann durch einen verfügbaren Zufallsgenerator gezogen werden.
- Glücksspiel bei der eine random Zahl zwischen 0 - 9 erzeugt werden soll.
- Der Spieler hat ein Startkonto von 10.000 Punkten und kann damit einen beliebigen Teilbetrag auf die zufällig erzeugte Zahl setzen.
- Liegt er richtig bekommt er das 9 Fache seines Einsatzes als Gewinn
- Programmieren Sie ein entsprechendes Programm, welches die Eingaben von der Tastatur einliest und
die Ausgaben auf dem Bildschirm liefert. Die zu erratende Zahl kann durch einen verfügbaren Zufallsgenerator gezogen werden.
#5
von claas1007 (130 Punkte)
- 06.02.2020 um 13:54 Uhr
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication8
{
public partial class Form1 : Form
{
// Initialisierung
public int iKontostand = 10000;
Random rnd = new Random();
public int iZahlUser;
public int iZufallszahl;
public int iEinsatz;
public bool bZahlenGleich;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
txt_regeln.Text = "Glücksspiel bei der eine random Zahl zwischen 0 - 9 erzeugt wird.\r\nDer Spieler hat ein Startkonto von 10.000 Punkten und kann damit einen beliebigen Teilbetrag auf die zufällig erzeugte Zahl setzen.\r\nLiegt er richtig bekommt er das 9 Fache seines Einsatzes als Gewinn.\r\n";
txt_kontostand.Text = Convert.ToString(iKontostand);
}
private void txt_regeln_TextChanged(object sender, EventArgs e)
{
}
private void txt_kontostand_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
iZahlUser = Convert.ToInt32(txt_zahlUser.Text);
iZufallszahl = rnd.Next(0, 9);
txt_zufallszahl.Text = Convert.ToString(iZufallszahl);
vergleiche(iZufallszahl , iZahlUser);
berechne(iKontostand);
if (iEinsatz < 1)
{
this.Close();
}
if (iZahlUser < 0 && iZahlUser > 10)
{
this.Close();
}
}
public bool vergleiche(int zahl1, int zahl2)
{
if (zahl1 == zahl2)
{
bZahlenGleich = true;
}
else
{
bZahlenGleich = false;
}
return bZahlenGleich;
}
public int berechne(int zahl1)
{
if (bZahlenGleich)
{
iEinsatz = Convert.ToInt32(txt_einsatz.Text);
iKontostand = iKontostand + (iEinsatz * 9);
txt_endeSpiel.Text = "gewonnen";
}
else
{
iEinsatz = Convert.ToInt32(txt_einsatz.Text);
iKontostand = iKontostand - iEinsatz;
if (iKontostand <= 0)
{
MessageBox.Show("Sie haben verloren\nKontostand < 0");
this.Close();
}
txt_endeSpiel.Text = "verloren";
}
txt_kontostand.Text = Convert.ToString(iKontostand);
return iKontostand;
}
}
}
namespace WindowsFormsApplication8
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.txt_regeln = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.txt_einsatz = new System.Windows.Forms.TextBox();
this.txt_zahlUser = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.txt_zufallszahl = new System.Windows.Forms.TextBox();
this.label5 = new System.Windows.Forms.Label();
this.txt_kontostand = new System.Windows.Forms.TextBox();
this.txt_endeSpiel = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 18.14286F);
this.label1.ForeColor = System.Drawing.Color.Black;
this.label1.Location = new System.Drawing.Point(263, 29);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(464, 51);
this.label1.TabIndex = 0;
this.label1.Text = "Glücksspiel Zufallszahl";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 12.14286F);
this.label2.Location = new System.Drawing.Point(22, 152);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(115, 33);
this.label2.TabIndex = 1;
this.label2.Text = "Regeln:";
//
// txt_regeln
//
this.txt_regeln.Font = new System.Drawing.Font("Microsoft Sans Serif", 10.14286F);
this.txt_regeln.Location = new System.Drawing.Point(177, 152);
this.txt_regeln.Multiline = true;
this.txt_regeln.Name = "txt_regeln";
this.txt_regeln.ScrollBars = System.Windows.Forms.ScrollBars.Horizontal;
this.txt_regeln.Size = new System.Drawing.Size(841, 190);
this.txt_regeln.TabIndex = 2;
this.txt_regeln.TextChanged += new System.EventHandler(this.txt_regeln_TextChanged);
//
// label3
//
this.label3.AutoSize = true;
this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 12.14286F);
this.label3.Location = new System.Drawing.Point(22, 389);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(119, 33);
this.label3.TabIndex = 3;
this.label3.Text = "Einsatz:";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Font = new System.Drawing.Font("Microsoft Sans Serif", 12.14286F);
this.label4.Location = new System.Drawing.Point(-1, 463);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(138, 33);
this.label4.TabIndex = 4;
this.label4.Text = "Ihre Zahl:";
//
// txt_einsatz
//
this.txt_einsatz.Font = new System.Drawing.Font("Microsoft Sans Serif", 12.14286F);
this.txt_einsatz.Location = new System.Drawing.Point(177, 389);
this.txt_einsatz.Name = "txt_einsatz";
this.txt_einsatz.Size = new System.Drawing.Size(346, 40);
this.txt_einsatz.TabIndex = 5;
//
// txt_zahlUser
//
this.txt_zahlUser.Font = new System.Drawing.Font("Microsoft Sans Serif", 12.14286F);
this.txt_zahlUser.Location = new System.Drawing.Point(177, 463);
this.txt_zahlUser.Name = "txt_zahlUser";
this.txt_zahlUser.Size = new System.Drawing.Size(346, 40);
this.txt_zahlUser.TabIndex = 6;
//
// button1
//
this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F);
this.button1.Location = new System.Drawing.Point(-2, 524);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(139, 48);
this.button1.TabIndex = 7;
this.button1.Text = "Zufallszahl:";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// txt_zufallszahl
//
this.txt_zufallszahl.Font = new System.Drawing.Font("Microsoft Sans Serif", 12.14286F);
this.txt_zufallszahl.Location = new System.Drawing.Point(177, 532);
this.txt_zufallszahl.Name = "txt_zufallszahl";
this.txt_zufallszahl.Size = new System.Drawing.Size(346, 40);
this.txt_zufallszahl.TabIndex = 8;
//
// label5
//
this.label5.AutoSize = true;
this.label5.Font = new System.Drawing.Font("Microsoft Sans Serif", 18.14286F);
this.label5.Location = new System.Drawing.Point(671, 385);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(253, 51);
this.label5.TabIndex = 9;
this.label5.Text = "Kontostand:";
//
// txt_kontostand
//
this.txt_kontostand.Font = new System.Drawing.Font("Microsoft Sans Serif", 18.14286F);
this.txt_kontostand.Location = new System.Drawing.Point(673, 475);
this.txt_kontostand.Name = "txt_kontostand";
this.txt_kontostand.Size = new System.Drawing.Size(251, 55);
this.txt_kontostand.TabIndex = 10;
this.txt_kontostand.TextChanged += new System.EventHandler(this.txt_kontostand_TextChanged);
//
// txt_endeSpiel
//
this.txt_endeSpiel.Font = new System.Drawing.Font("Microsoft Sans Serif", 18.14286F);
this.txt_endeSpiel.Location = new System.Drawing.Point(38, 642);
this.txt_endeSpiel.Multiline = true;
this.txt_endeSpiel.Name = "txt_endeSpiel";
this.txt_endeSpiel.Size = new System.Drawing.Size(928, 161);
this.txt_endeSpiel.TabIndex = 11;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(11F, 24F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1079, 939);
this.Controls.Add(this.txt_endeSpiel);
this.Controls.Add(this.txt_kontostand);
this.Controls.Add(this.label5);
this.Controls.Add(this.txt_zufallszahl);
this.Controls.Add(this.button1);
this.Controls.Add(this.txt_zahlUser);
this.Controls.Add(this.txt_einsatz);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.txt_regeln);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txt_regeln;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox txt_einsatz;
private System.Windows.Forms.TextBox txt_zahlUser;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox txt_zufallszahl;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.TextBox txt_kontostand;
private System.Windows.Forms.TextBox txt_endeSpiel;
}
}
Kommentare:
Für diese Lösung gibt es noch keinen Kommentar
Seite 1 von 0
1
