Files
WIR-MACHEN-ALLE-ARM/Casino/Slots.xaml.cs
T
2026-09-13 15:59:13 +02:00

357 lines
9.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
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.Shapes;
namespace Casino
{
/// <summary>
/// Interaktionslogik für Slots.xaml
/// </summary>
public partial class Slots : Window
{
private List<Image> _bilder;
private Random _random = new Random();
Database db = new Database();
private List<Byte[]> AlleBilder;
ObservableCollection<User> users = new ObservableCollection<User>();
Audioplayer music = new Audioplayer();
Audioplayer audio = new Audioplayer();
private int[,] currentSlot = new int[3, 6];
const decimal DREI = 1.25m;
const decimal VIER = 0.75m;
const decimal FUENF = 2.25m;
const decimal SECHS = 6.75m;
const decimal X_MUSTER = 13.00m;
const decimal RAND = 18.00m;
const decimal X_UND_RAND = 25.00m;
const decimal FULLBOARD = 50.00m;
private int highestWin = 5;
public Slots()
{
InitializeComponent();
users = db.GetUser();
_bilder = new List<Image>
{
Bild_11, Bild_12, Bild_13, Bild_14, Bild_15, Bild_16,
Bild_21, Bild_22, Bild_23, Bild_24, Bild_25, Bild_26,
Bild_31, Bild_32, Bild_33, Bild_34, Bild_35, Bild_36
};
AlleBilder = new List<Byte[]>
{
db.GetCardImage(2),db.GetCardImage(3),db.GetCardImage(4),
db.GetCardImage(5),db.GetCardImage(6),db.GetCardImage(7),
db.GetCardImage(8),db.GetCardImage(9),db.GetCardImage(10),
};
Konto.Text = users[MainWindow.currentUser].Geld.ToString();
music.PlayLoopingSound(db.GetSound(2));
}
private void LoadCard(Image imageControl, int bildId)
{
byte[] img = AlleBilder.ElementAt(bildId);
imageControl.Source = ByteArrayToImage(img);
}
private BitmapImage ByteArrayToImage(byte[] bytes)
{
BitmapImage image = new BitmapImage();
using (MemoryStream ms = new MemoryStream(bytes))
{
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.StreamSource = ms;
image.EndInit();
}
return image;
}
private async Task Spin()
{
for (int i = 0; i < 15; i++)
{
for (int j = 5; j>=0;j--)
{
int randomBild = _random.Next(0, AlleBilder.Count);
if (i == 0)
{
currentSlot[0,j] = randomBild;
LoadCard(_bilder[j], randomBild);
}
else if (i == 1)
{
currentSlot[1, j] = currentSlot[0, j];
LoadCard(_bilder[j + 6], currentSlot[0,j]);
currentSlot[0, j] = randomBild;
LoadCard(_bilder[j], randomBild);
}
else
{
currentSlot[2, j] = currentSlot[1, j];
LoadCard(_bilder[j + 12], currentSlot[1, j]);
currentSlot[1, j] = currentSlot[0, j];
LoadCard(_bilder[j + 6], currentSlot[0, j]);
currentSlot[0, j] = randomBild;
LoadCard(_bilder[j], randomBild);
}
}
await Task.Delay(150);
}
}
private async void Spin(object sender, RoutedEventArgs e)
{
int einsatz = Convert.ToInt32(einsatzFeld.Text);
users[MainWindow.currentUser].Geld -= einsatz;
Konto.Text = users[MainWindow.currentUser].Geld.ToString();
await Spin();
users[MainWindow.currentUser].Geld += CheckWin(currentSlot, einsatz);
Konto.Text = users[MainWindow.currentUser].Geld.ToString();
db.UpdateGeld(
users[MainWindow.currentUser].ID,
users[MainWindow.currentUser].Geld);
/* catch (NullReferenceException)
{
MessageBox.Show(
"Bitte ein Betrag eingeben",
"Fehlermeldung 101",
MessageBoxButton.OK,
MessageBoxImage.Information);
}*/
}
private void Hauptmenue(object sender, RoutedEventArgs e)
{
music.Stop();
Close();
}
public decimal CheckWin(int[,] board, decimal einsatz)
{
decimal multiplikator = 0m;
// Waagerecht
for (int row = 0; row < 3; row++)
{
multiplikator += CheckLine(
board[row, 0],
board[row, 1],
board[row, 2],
board[row, 3],
board[row, 4],
board[row, 5]);
}
// Senkrecht
for (int col = 0; col < 6; col++)
{
multiplikator += CheckLine(
board[0, col],
board[1, col],
board[2, col]);
}
// X Muster
bool x1 =
board[0, 0] == board[1, 1] &&
board[1, 1] == board[2, 2];
bool x2 =
board[0, 5] == board[1, 4] &&
board[1, 4] == board[2, 3];
if (x1 && x2 &&
board[0, 0] == board[0, 5])
{
multiplikator += X_MUSTER;
highestWin++;
}
// Rand
int randId = board[0, 0];
bool randGleich =
board[0, 0] == randId &&
board[0, 1] == randId &&
board[0, 2] == randId &&
board[0, 3] == randId &&
board[0, 4] == randId &&
board[0, 5] == randId &&
board[2, 0] == randId &&
board[2, 1] == randId &&
board[2, 2] == randId &&
board[2, 3] == randId &&
board[2, 4] == randId &&
board[2, 5] == randId &&
board[1, 0] == randId &&
board[1, 5] == randId;
if (randGleich)
{
multiplikator += RAND;
highestWin++;
}
// X + Rand Bonus
if (
randGleich &&
x1 && x2 &&
board[0, 0] == board[0, 5]
)
{
multiplikator += X_UND_RAND;
highestWin++;
}
// Fullboard
int first = board[0, 0];
bool fullBoard = true;
for (int r = 0; r < 3; r++)
{
for (int c = 0; c < 6; c++)
{
if (board[r, c] != first)
{
fullBoard = false;
break;
}
}
}
if (fullBoard)
{
multiplikator += FULLBOARD;
highestWin++;
}
if (multiplikator != 0) PlayWinSound();
return einsatz * multiplikator;
}
private decimal CheckLine(params int[] werte)
{
decimal gewinn = 0;
int laenge = werte.Length;
for (int start = 0; start < laenge; start++)
{
int gleiche = 1;
for (int next = start + 1; next < laenge; next++)
{
if (werte[next] == werte[start])
gleiche++;
else
break;
}
if (gleiche >= 3)
gewinn += DREI;
if (gleiche >= 4)
{
gewinn += VIER;
highestWin++;
}
if (gleiche >= 5)
{
gewinn += FUENF;
highestWin++;
}
if (gleiche >= 6)
{
gewinn += SECHS;
highestWin++;
}
}
return gewinn;
}
//Numberfield
private static readonly Regex _regex = new Regex("^[0-9]"); // Regex to match non-numeric input
//nur nummern
private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
e.Handled = !_regex.IsMatch(e.Text);
}
// Keine Spaces
private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Space)
{
e.Handled = true;
}
}
//das man nichts reinkopieren kann außer Zahlen
private void TextBox_Pasting(object sender, DataObjectPastingEventArgs e)
{
if (e.DataObject.GetDataPresent(typeof(String)))
{
var text = (String)e.DataObject.GetData((typeof(String)));
if (!_regex.IsMatch((string)text))
{
e.CancelCommand();
}
}
}
void PlayWinSound()
{
audio.PlaySound(db.GetSound(highestWin));
highestWin = 5;
}
}
}