Merge branch 'main' of https://git.bib.de/PBT3H24AFA/Lotto_WPf
This commit is contained in:
commit
1d929d5ad0
39
Lotto_Wpf/Lotto_Wpf/LottoProject.cs
Normal file
39
Lotto_Wpf/Lotto_Wpf/LottoProject.cs
Normal file
@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace LottoProject
|
||||
{
|
||||
public class Lotto
|
||||
{
|
||||
public int[] GezogeneZahlen { get; private set; } = new int[6];
|
||||
public int TrefferAnzahl { get; private set; } = 0;
|
||||
|
||||
public Lotto(int[] userZahlen)
|
||||
{
|
||||
Ziehen();
|
||||
TrefferAnzahl = Treffer(userZahlen, GezogeneZahlen);
|
||||
}
|
||||
|
||||
private void Ziehen()
|
||||
{
|
||||
Random rnd = new();
|
||||
for (int i = 0; i < GezogeneZahlen.Length; i++)
|
||||
{
|
||||
int zahl;
|
||||
do
|
||||
{
|
||||
zahl = rnd.Next(1, 50);
|
||||
}
|
||||
while (GezogeneZahlen.Contains(zahl));
|
||||
GezogeneZahlen[i] = zahl;
|
||||
}
|
||||
}
|
||||
|
||||
public int Treffer(int[] user, int[] gezogen)
|
||||
{
|
||||
return user.Count(gezogen.Contains);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,131 +1,23 @@
|
||||
<Window x:Class="LottoApp.MainWindow"
|
||||
<Window x:Class="LottoNumberBoard.MainWindow"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
Title="Lotto App" Height="600" Width="800"
|
||||
Background="#1e1e1e"
|
||||
WindowStartupLocation="CenterScreen"
|
||||
Loaded="Window_Loaded">
|
||||
Title="Lotto Number Board" Height="600" Width="400">
|
||||
<DockPanel Margin="10">
|
||||
|
||||
<Grid>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="80"/>
|
||||
<ColumnDefinition Width="*"/>
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
|
||||
<StackPanel Grid.Column="0" Background="#111" Orientation="Vertical">
|
||||
<TextBlock Text="LOTTO" FontSize="16" FontWeight="Bold"
|
||||
Foreground="White" Margin="0,20,0,20"
|
||||
HorizontalAlignment="Center"/>
|
||||
<Button Content="Home" FontSize="12" Margin="10" Background="Transparent" Foreground="White"/>
|
||||
<Button Content="Spiel" FontSize="12" Margin="10" Background="Transparent" Foreground="White"/>
|
||||
<Button Content="Geschichte" FontSize="12" Margin="10" Background="Transparent" Foreground="White"/>
|
||||
<StackPanel DockPanel.Dock="Bottom" Margin="0,10,0,0">
|
||||
<TextBlock Text="Selected Numbers:" FontWeight="Bold" FontSize="16" />
|
||||
<TextBlock x:Name="ResultTextBlock" FontSize="14" Foreground="DarkBlue" TextWrapping="Wrap"/>
|
||||
<TextBlock Text="Startgeld (€):" FontWeight="Bold" Margin="0,10,0,0"/>
|
||||
<TextBox x:Name="StartgeldTextBox" Text="2000" Width="100" Margin="0,0,0,10"/>
|
||||
<Button Content="Ziehen" Margin="0,10,0,0" Click="DrawButton_Click" Height="30"/>
|
||||
<Button Content="Simulation starten" Margin="0,10,0,0" Click="SimulationButton_Click" Height="30"/>
|
||||
</StackPanel>
|
||||
|
||||
<!-- Main -->
|
||||
<StackPanel Grid.Column="1" Margin="20">
|
||||
<TextBlock Text="Lotto " FontSize="28" FontWeight="Bold" Foreground="White" Margin="0,0,0,20"/>
|
||||
<UniformGrid x:Name="NumberGrid" Rows="7" Columns="7" Margin="0,0,0,20"/>
|
||||
<TextBlock x:Name="SelectedNumbersText" Foreground="White" FontSize="16" Margin="0,0,0,10"/>
|
||||
<ScrollViewer DockPanel.Dock="Top">
|
||||
<UniformGrid x:Name="NumberGrid" Columns="7" Rows="7" Margin="0,0,0,10"/>
|
||||
</ScrollViewer>
|
||||
|
||||
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Margin="0,10,0,0">
|
||||
<Button Content="Wahlen" Width="100" Margin="0,0,10,0" Click="QuickPick_Click"/>
|
||||
<Button Content="Loschen" Width="75" Margin="0,0,10,0" Click="Clear_Click"/>
|
||||
</StackPanel>
|
||||
</StackPanel>
|
||||
|
||||
<x:Code>
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Media;
|
||||
|
||||
List<int> selectedNumbers = new List<int>();
|
||||
|
||||
void Window_Loaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
for (int i = 1; i <= 49; i++)
|
||||
{
|
||||
Button btn = new Button
|
||||
{
|
||||
Content = i.ToString(),
|
||||
Margin = new Thickness(5),
|
||||
Background = Brushes.LightGray,
|
||||
Foreground = Brushes.Black,
|
||||
Tag = i
|
||||
};
|
||||
btn.Click += NumberButton_Click;
|
||||
NumberGrid.Children.Add(btn);
|
||||
}
|
||||
}
|
||||
|
||||
void NumberButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
Button btn = sender as Button;
|
||||
int number = (int)btn.Tag;
|
||||
|
||||
if (selectedNumbers.Contains(number))
|
||||
{
|
||||
selectedNumbers.Remove(number);
|
||||
btn.Background = Brushes.LightGray;
|
||||
}
|
||||
else if (selectedNumbers.Count < 6)
|
||||
{
|
||||
selectedNumbers.Add(number);
|
||||
btn.Background = Brushes.Orange;
|
||||
}
|
||||
|
||||
SelectedNumbersText.Text = string.Join(", ", selectedNumbers.OrderBy(n => n));
|
||||
}
|
||||
|
||||
void QuickPick_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
Random rand = new Random();
|
||||
selectedNumbers.Clear();
|
||||
var buttons = NumberGrid.Children.OfType<Button>().ToList();
|
||||
|
||||
foreach (Button btn in buttons)
|
||||
btn.Background = Brushes.LightGray;
|
||||
|
||||
while (selectedNumbers.Count < 6)
|
||||
{
|
||||
int n = rand.Next(1, 50);
|
||||
if (!selectedNumbers.Contains(n))
|
||||
selectedNumbers.Add(n);
|
||||
}
|
||||
|
||||
foreach (Button btn in buttons)
|
||||
{
|
||||
if (selectedNumbers.Contains((int)btn.Tag))
|
||||
btn.Background = Brushes.Orange;
|
||||
}
|
||||
|
||||
SelectedNumbersText.Text = string.Join(", ", selectedNumbers.OrderBy(n => n));
|
||||
}
|
||||
|
||||
void Clear_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
selectedNumbers.Clear();
|
||||
foreach (Button btn in NumberGrid.Children.OfType<Button>())
|
||||
btn.Background = Brushes.LightGray;
|
||||
|
||||
SelectedNumbersText.Text = "";
|
||||
}
|
||||
|
||||
void Submit_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (selectedNumbers.Count != 6)
|
||||
{
|
||||
MessageBox.Show("Please select exactly 6 numbers.");
|
||||
return;
|
||||
}
|
||||
|
||||
MessageBox.Show("Numbers submitted: " + string.Join(", ", selectedNumbers.OrderBy(n => n)));
|
||||
}
|
||||
]]>
|
||||
</x:Code>
|
||||
</Grid>
|
||||
</DockPanel>
|
||||
</Window>
|
||||
|
||||
|
||||
|
@ -1,24 +1,149 @@
|
||||
using System.Text;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using System.Windows.Navigation;
|
||||
using System.Windows.Shapes;
|
||||
using LottoProject;
|
||||
using System.Diagnostics;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Lotto_Wpf
|
||||
namespace LottoNumberBoard
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for MainWindow.xaml
|
||||
/// </summary>
|
||||
public partial class MainWindow : Window
|
||||
{
|
||||
private HashSet<int> selectedNumbers = new();
|
||||
|
||||
public MainWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
GenerateNumberButtons();
|
||||
}
|
||||
|
||||
private void GenerateNumberButtons()
|
||||
{
|
||||
for (int i = 1; i <= 49; i++)
|
||||
{
|
||||
Button btn = new Button
|
||||
{
|
||||
Content = i.ToString(),
|
||||
Tag = i,
|
||||
Margin = new Thickness(5)
|
||||
};
|
||||
btn.Click += NumberButton_Click;
|
||||
NumberGrid.Children.Add(btn);
|
||||
}
|
||||
}
|
||||
|
||||
private void NumberButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (sender is Button btn && int.TryParse(btn.Content.ToString(), out int number))
|
||||
{
|
||||
if (selectedNumbers.Contains(number))
|
||||
{
|
||||
selectedNumbers.Remove(number);
|
||||
btn.ClearValue(Button.BackgroundProperty);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (selectedNumbers.Count >= 6)
|
||||
{
|
||||
MessageBox.Show("Nur 6 Zahlen erlaubt!", "Hinweis", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
return;
|
||||
}
|
||||
selectedNumbers.Add(number);
|
||||
btn.Background = Brushes.LightGreen;
|
||||
}
|
||||
|
||||
UpdateResultDisplay();
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateResultDisplay()
|
||||
{
|
||||
var sorted = selectedNumbers.OrderBy(n => n);
|
||||
ResultTextBlock.Text = "Ausgewählt: " + string.Join(", ", sorted);
|
||||
}
|
||||
|
||||
private void DrawButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (selectedNumbers.Count != 6)
|
||||
{
|
||||
MessageBox.Show("Bitte wähle genau 6 Zahlen!", "Warnung", MessageBoxButton.OK, MessageBoxImage.Warning);
|
||||
return;
|
||||
}
|
||||
|
||||
int[] userZahlen = selectedNumbers.ToArray();
|
||||
Lotto lotto = new Lotto(userZahlen);
|
||||
|
||||
string gezogene = string.Join(", ", lotto.GezogeneZahlen.OrderBy(n => n));
|
||||
string getroffene = string.Join(", ", userZahlen.Intersect(lotto.GezogeneZahlen).OrderBy(n => n));
|
||||
int anzahlTreffer = lotto.TrefferAnzahl;
|
||||
|
||||
ResultTextBlock.Text += $"\nGezogene Zahlen: {gezogene}";
|
||||
ResultTextBlock.Text += $"\nTreffer: {anzahlTreffer} ({getroffene})\n";
|
||||
}
|
||||
|
||||
private async void SimulationButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (selectedNumbers.Count != 6)
|
||||
{
|
||||
MessageBox.Show("Bitte wähle genau 6 Zahlen für die Simulation!", "Warnung", MessageBoxButton.OK, MessageBoxImage.Warning);
|
||||
return;
|
||||
}
|
||||
|
||||
int[] meineZahlen = selectedNumbers.ToArray();
|
||||
int[] trefferVerteilung = new int[7];
|
||||
int meinGeld;
|
||||
if (!int.TryParse(StartgeldTextBox.Text, out meinGeld))
|
||||
{
|
||||
MessageBox.Show("Bitte gültigen Startgeldbetrag eingeben!", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
return;
|
||||
}
|
||||
|
||||
int versuche = 1000;
|
||||
|
||||
var stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
|
||||
await Task.Run(() =>
|
||||
{
|
||||
Random rnd = new();
|
||||
for (int i = 0; i < versuche; i++)
|
||||
{
|
||||
HashSet<int> zufallsZahlen = new();
|
||||
while (zufallsZahlen.Count < 6)
|
||||
zufallsZahlen.Add(rnd.Next(1, 50));
|
||||
|
||||
Lotto lotto = new Lotto(zufallsZahlen.ToArray());
|
||||
int treffer = lotto.Treffer(meineZahlen, lotto.GezogeneZahlen);
|
||||
trefferVerteilung[treffer]++;
|
||||
|
||||
meinGeld -= 5;
|
||||
switch (treffer)
|
||||
{
|
||||
case 3: meinGeld += 20; break;
|
||||
case 4: meinGeld += 400; break;
|
||||
case 5: meinGeld += 20000; break;
|
||||
case 6: meinGeld += 100000000; break;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
stopwatch.Stop();
|
||||
|
||||
StringBuilder sb = new();
|
||||
sb.AppendLine("\nSimulation abgeschlossen:");
|
||||
for (int i = 0; i < trefferVerteilung.Length; i++)
|
||||
{
|
||||
sb.AppendLine($"{i} Treffer: {trefferVerteilung[i]}x");
|
||||
}
|
||||
sb.AppendLine($"\nDauer: {stopwatch.Elapsed.TotalSeconds:F2} Sekunden");
|
||||
sb.AppendLine($"Kontostand: {meinGeld} €");
|
||||
|
||||
ResultTextBlock.Text += "\n" + sb.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user