erst commit

This commit is contained in:
abdelaziz
2025-06-17 06:33:48 +02:00
commit c5c2a18a30
10 changed files with 922 additions and 0 deletions

25
LOTO/LOTO.sln Normal file
View File

@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.13.35931.197 d17.13
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LOTO", "LOTO\LOTO.csproj", "{B2D67C5F-CC7C-436C-B5AC-2EB33DACD937}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B2D67C5F-CC7C-436C-B5AC-2EB33DACD937}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B2D67C5F-CC7C-436C-B5AC-2EB33DACD937}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B2D67C5F-CC7C-436C-B5AC-2EB33DACD937}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B2D67C5F-CC7C-436C-B5AC-2EB33DACD937}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4AC806A9-C448-43FC-9BB1-96155BB7978B}
EndGlobalSection
EndGlobal

9
LOTO/LOTO/App.xaml Normal file
View File

@@ -0,0 +1,9 @@
<Application x:Class="LOTO.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:LOTO"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>

14
LOTO/LOTO/App.xaml.cs Normal file
View File

@@ -0,0 +1,14 @@
using System.Configuration;
using System.Data;
using System.Windows;
namespace LOTO
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}

10
LOTO/LOTO/AssemblyInfo.cs Normal file
View File

@@ -0,0 +1,10 @@
using System.Windows;
[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]

11
LOTO/LOTO/LOTO.csproj Normal file
View File

@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net9.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UseWPF>true</UseWPF>
</PropertyGroup>
</Project>

98
LOTO/LOTO/Lotto.cs Normal file
View File

@@ -0,0 +1,98 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LOTO
{
internal class Lotto
{
int[] matchCounts = new int[7];
//Random array erzeuger
public int[] RandomNumbers()
{
Random rn = new Random();
bool dupl;
int[] x = new int[6];
for (int i = 0; i < 6; i++)
{
int y = rn.Next(1, 50);
dupl = DuplicateChecker(x, y);
while (dupl == true)
{
y = rn.Next(1, 50);
dupl = DuplicateChecker(x, y);
}
x[i] = y;
}
return x;
}
//Prüft duplizierte Zahlen in einem array
public bool DuplicateChecker(int[] x, int z)
{
for (int i = 0; i < x.Length; i++)
{
if (x[i] == z)
{
return true;
}
}
return false;
}
//recursiv binary search ,der prüft eine Zahl ,ob sie in einem Array liegt.
public bool RevursivBinarySearch(int[] arry, int target, int left, int right)
{
int mid = (left + right) / 2;
if (left > right)
{
return false;
}
if (arry[mid] == target)
{
return true;
}
else if (arry[mid] < target)
{
left = mid + 1;
return RevursivBinarySearch(arry, target, left, right);
}
else
{
right = mid - 1;
return RevursivBinarySearch(arry, target, left, right);
}
}
//addiert die Zhal der arrays,der i matches haben
public int[] Stats(int count)
{
if (count > 0)
{
matchCounts[count]++;
}
return matchCounts;
}
//rechnet die Möglichkeit der Gewinn
public double Möglichkeit(int ticket)
{
//13983816 ist die Gesamtzahl der möglichen Kombinationen für Lotto 6/49 ( C(m,n) = m! / (n! × (m-n)!) )
int m = 13983816;
double möglichkeit = (double)ticket / m;
return Math.Round(möglichkeit, 12);
}
}
}

82
LOTO/LOTO/MainWindow.xaml Normal file
View File

@@ -0,0 +1,82 @@
<Window x:Class="LOTO.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Lotto Spiel" Height="600" Width="800"
Background="White" Foreground="Black"
WindowStartupLocation="CenterScreen">
<Grid Margin="20">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!-- Eingabe Bereich -->
<StackPanel Grid.Row="0" Margin="0,0,0,20">
<Label Content="Wie viele Tickets möchten Sie kaufen?" FontWeight="Bold"/>
<TextBox x:Name="txtTicketCount" Width="100" HorizontalAlignment="Left" Margin="0,5,0,10"/>
<Label Content="Sollen die Tickets zufällig ausgefüllt werden?" FontWeight="Bold"/>
<StackPanel Orientation="Horizontal" Margin="0,5,0,10">
<RadioButton x:Name="rbAllesRandom" Content="Ja (Alle zufällig)" Margin="0,0,20,0" IsChecked="True"/>
<RadioButton x:Name="rbMixed" Content="Nein (Für jedes Ticket wählen)"/>
</StackPanel>
<Button x:Name="btnSpielStarten" Content="Spiel starten" Width="120" Height="30"
HorizontalAlignment="Left" Click="BtnSpielStarten"
Background="White" BorderBrush="Black" BorderThickness="1"/>
</StackPanel>
<!-- Manuelle Eingabe Bereich (versteckt) -->
<StackPanel x:Name="ManuellPanelInput" Grid.Row="1" Visibility="Collapsed" Margin="0,0,0,20">
<Label x:Name="aktuellesLabelticket" Content="" FontWeight="Bold"/>
<Label Content="Wie möchten Sie dieses Ticket ausfüllen:"/>
<StackPanel Orientation="Horizontal" Margin="0,5,0,10">
<RadioButton x:Name="rbManuell" Content="Manuell" Margin="0,0,20,0" IsChecked="True"/>
<RadioButton x:Name="rbTicketRandom" Content="Zufällig"/>
</StackPanel>
<!-- Manuelle Zahleneingabe -->
<StackPanel x:Name="ZahlPanelInput" Margin="0,10,0,10">
<Label Content="Geben Sie Ihre Zahlen ein (1-49):"/>
<WrapPanel>
<StackPanel Orientation="Horizontal" Margin="0,0,10,5">
<Label Content="1:" Width="20"/>
<TextBox x:Name="txt1" Width="40" Margin="5,0"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,0,10,5">
<Label Content="2:" Width="20"/>
<TextBox x:Name="txt2" Width="40" Margin="5,0"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,0,10,5">
<Label Content="3:" Width="20"/>
<TextBox x:Name="txt3" Width="40" Margin="5,0"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,0,10,5">
<Label Content="4:" Width="20"/>
<TextBox x:Name="txt4" Width="40" Margin="5,0"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,0,10,5">
<Label Content="5:" Width="20"/>
<TextBox x:Name="txt5" Width="40" Margin="5,0"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,0,10,5">
<Label Content="6:" Width="20"/>
<TextBox x:Name="txt6" Width="40" Margin="5,0"/>
</StackPanel>
</WrapPanel>
</StackPanel>
<Button x:Name="btnSubmitTicket" Content="Ticket bestätigen" Width="140" Height="30"
HorizontalAlignment="Left" Click="BtnBestätigen"
Background="White" BorderBrush="Black" BorderThickness="1"/>
</StackPanel>
<!-- Ergebnisse Bereich -->
<ScrollViewer Grid.Row="3" Background="Black" Foreground="White" Margin="0,10,0,10">
<TextBlock x:Name="txtErgebnisse" Padding="10" FontFamily="Consolas" FontSize="12"
TextWrapping="Wrap" Background="Black" Foreground="White"/>
</ScrollViewer>
</Grid>
</Window>

View File

@@ -0,0 +1,249 @@
using System.Windows;
using System.Windows.Controls;
namespace LOTO
{
public partial class MainWindow : Window
{
//new operator von Lotto class
private Lotto lotto = new Lotto();
private List<int[]> tickets = new List<int[]>();
private int ticketsZahl = 0;
private int aktuellesTicketIndex = 0;
public MainWindow()
{
InitializeComponent();
// Wenn dieses Optionsfeld angeklickt ist, wird diese Methode ausgeführt
rbMixed.Checked += RbMixed_Checked;
rbAllesRandom.Checked += RbAllesRandom_Checked;
rbManuell.Checked += RbManuell_Checked;
rbTicketRandom.Checked += RbTicketRandom_Checked;
}
private void RbMixed_Checked(object sender, RoutedEventArgs e)
{
// Mixed mode wird in SpielStarten behandelt
}
private void RbAllesRandom_Checked(object sender, RoutedEventArgs e)
{
//Das Panel wird ausgeblendet,wenn allesrandom radio button ausgewählt wird.
ManuellPanelInput.Visibility = Visibility.Collapsed;
}
private void RbManuell_Checked(object sender, RoutedEventArgs e)
{
//macht ZahlPanel sichbar ,wenn Manuell radio button ausgewählt wird
ZahlPanelInput.Visibility = Visibility.Visible;
}
private void RbTicketRandom_Checked(object sender, RoutedEventArgs e)
{
//macht den Panel von Zahlen unsihtbar,wenn radio button von Ticket random ausgeqählt wird.
ZahlPanelInput.Visibility = Visibility.Collapsed;
}
//Event Handler von Button der Spiel Starten
private void BtnSpielStarten(object sender, RoutedEventArgs e)
{
ticketsZahl = Convert.ToInt32(txtTicketCount.Text);
if (ticketsZahl <= 0)
{
MessageBox.Show("Bitte geben Sie eine gültige Ticketanzahl ein.", "Ungültige Eingabe", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
aktuellesTicketIndex = 0;
btnSpielStarten.IsEnabled = false;
if (rbAllesRandom.IsChecked == true)
{
// All random tickets
for (int i = 0; i < ticketsZahl; i++)
{
tickets.Add(lotto.RandomNumbers());
}
Process();
}
else
{
// Mixed mode
ShowTicketInput();
}
}
private void ShowTicketInput()
{
if (aktuellesTicketIndex < ticketsZahl)
{
ManuellPanelInput.Visibility = Visibility.Visible;
aktuellesLabelticket.Content = $"Ticket {aktuellesTicketIndex + 1} von {ticketsZahl}";
// Vorherige Eingaben löschen
txt1.Text = txt2.Text = txt3.Text = txt4.Text = txt5.Text = txt6.Text = "";
}
else
{
//wenn alle Tickets gefüllt werden,start prozessing und macht den Panel von Inputs unsichtbar.
ManuellPanelInput.Visibility = Visibility.Collapsed;
Process();
}
}
private void BtnBestätigen(object sender, RoutedEventArgs e)
{
if (rbTicketRandom.IsChecked == true)
{
// Random ticket
tickets.Add(lotto.RandomNumbers());
}
else
{
// Manual ticket
int[] numbers = new int[6];
TextBox[] textBoxes = { txt1, txt2, txt3, txt4, txt5, txt6 };
// Prüft jeder gegebene zahl
for (int i = 0; i < 6; i++)
{
int number = Convert.ToInt32(textBoxes[i].Text);
// Validation
if (number < 1 || number > 49)
{
MessageBox.Show($"Bitte geben Sie eine gültige Nummer (1-49) für die Position {i + 1} ein.", "Ungültige Eingabe", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
// Prüft Duplikation
if (lotto.DuplicateChecker(numbers, number))
{
MessageBox.Show("Bitte geben Sie 6 verschiedene Zahlen ein.", "Doppelte Nummern", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
// Addiert Zahl zu Array
numbers[i] = number;
}
// Gültig Array wird in die List hinzugefügt
tickets.Add(numbers);
}
// Index wird aktualisiert
aktuellesTicketIndex++;
// Ruft die Method wieder auf,um ein anderes Panel zu erzeugen.
ShowTicketInput();
}
//Hiere verwenden wir result als ein container,der alles text ,die wird erzeugt und zeigt speichert.
private void Process()
{
ManuellPanelInput.Visibility = Visibility.Collapsed;
// erzeugen GewinnZahlen
int[] winZahlen = lotto.RandomNumbers();
// Ergebnise zeigen
string result = "";
result += "The winning numbers are: " + string.Join(", ", winZahlen) + "\n\n";
// Alle tickets zeigen
for (int i = 0; i < tickets.Count; i++)
{
result += $"Ticket {i + 1}: " + string.Join(", ", tickets[i]) + "\n";
}
result += "\n";
// vergleichen und die ergebnisse zeigen
for (int i = 0; i < tickets.Count; i++)
{
result += VergleichTicket(tickets[i], winZahlen, i + 1) + "\n";
}
// Wahrscheinlichkeit zeigen
result += "\n" + GetStatistics() + "\n";
result += $"Die Warscheinlichkeit von {ticketsZahl} tickets its: {lotto.Möglichkeit(ticketsZahl):F10}\n";
txtErgebnisse.Text = result;
}
//vergleichen wir jeder Ticket mit dem Gewinnarray
private string VergleichTicket(int[] userNumbers, int[] winNumbers, int ticketNumber)
{
//array sortieren ,um recursiv binary search zu verwenden.
Array.Sort(winNumbers);
int count = 0;
string result = "";
for (int i = 0; i < 6; i++)
{
//count von Matches steigt,wenn die Method true gibt aus.
if (lotto.RevursivBinarySearch(winNumbers, userNumbers[i], 0, winNumbers.Length - 1))
{
count++;
}
}
//wenn ein Ticket hat 6 matches ,dass heißt,dass der Spieler gewonnen hat.
if (count == 6)
{
result = "**************************************************\n";
result += "You are a winner sir, sie haben 6 matches\n";
result += $"Sie Haben mit Ticket {ticketNumber} gewonnen\n";
result += "**************************************************";
}
else
{
//zeigt ,wie viel matches die Ticket hat.
result = $"Ticket {ticketNumber} hat {count} matches\n";
}
lotto.Stats(count);
return result;
}
private string GetStatistics()
{
string stats = "";
//Ruft Stats Method auf ,um die MathchCounts zu bekommen.(Stats(0) ruft Daten ab, ohne die Zähler zu erhöhen),
int[] matchCounts = lotto.Stats(0);
for (int i = 0; i < 7; i++)
{
if (matchCounts[i] > 0)
{
stats += $"Es Gibt {matchCounts[i]} tickets ,die {i} matches haben \n";
}
}
return stats;
}
}
}