Hallo,

ich frage mich, wie ich für ein dynamisches Vier gewinnt eine Gewinnabfrage erstellen kann.

Mein aktueller Code (ohne Klassen für Grafik) sieht so aus:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Animation;
using System.Windows.Threading;

namespace N_Gewinnt_Mölleken
{
  public partial class MainWindow : Window
  {
    Linie linie;
    Chip chip;
    private bool isHorizontalMoveEnabled = true;
    int anzahlspalten = 40;
    int anzahlzeilen = 9;
    int BallColor = 1;
    int GewinnMenge = 4;
    DispatcherTimer timer;
    Double ticks_old;
    int Spieler = 1;
    Double BRadius;
    Double BallX;
    Double AktuellX = 0;
    int NichtAus;
    private bool isLeftKeyPressed = false;
    private bool isRightKeyPressed = false;
    int count = 0;
    int[,] BallPosition;

    public MainWindow()
    {
      InitializeComponent();
      BallPosition = new int[anzahlspalten, anzahlzeilen];
    }

    private void wnd_Loaded(object sender, RoutedEventArgs e)
    {
      this.WindowState = WindowState.Maximized;
      InitializeGame();
    }

    private void InitializeGame()
    {
      timer = new DispatcherTimer();
      timer.Tick += timer_Tick;
      timer.Interval = new TimeSpan(0, 0, 0, 0, 15);
      timer.Start();
      double screenwidth = SystemParameters.PrimaryScreenWidth;
      double screenheight = SystemParameters.PrimaryScreenHeight;
      double x1 = screenwidth - (screenwidth * 0.3), y1 = screenheight - (screenheight * 0.10), x2 = x1, y2 = screenheight - (screenheight * 0.9);

      for (int spalten = 0; spalten <= anzahlspalten; spalten++)
      {
        linie = new Linie(x1, y1, x2, y2);
        linie.Draw(cvs);
        x1 -= ((screenwidth - (screenwidth * 0.3)) / anzahlspalten);
        x2 = x1;
      }

      linie = new Linie(screenwidth * 0.7, screenheight * 0.9, x1 + ((screenwidth - (screenwidth * 0.3)) / anzahlspalten), screenheight * 0.9);
      linie.Draw(cvs);
      BRadius = ((screenwidth - 4 - (screenwidth * 0.3)) / anzahlspalten) / 2;
      BallX = ((screenwidth - (screenwidth * 0.3)) / anzahlspalten) / 2;
      chip = new Chip(BallX, 130, BRadius, 0, 0, Spieler);
      chip.Draw(cvs);
    }

    private void timer_Tick(object sender, EventArgs e)
    {
      Double ticks = Environment.TickCount;

      if (ticks_old == 0)
      {
        ticks_old = ticks;
        return;
      }

      double elapsed = (ticks - ticks_old);
      ticks_old = ticks;
    }

    private void wnd_KeyDown(object sender, KeyEventArgs e)
    {
      if (e.Key == Key.Left)
      {
        MoveBallLeft();
      }
      else if (e.Key == Key.Right)
      {
        MoveBallRight();
      }

      if (e.Key == Key.Down)
      {
        MoveBallDown();
      }
    }

    private void MoveBallRight()
    {
      if (isHorizontalMoveEnabled && count < anzahlspalten - 1)
      {
        AktuellX = AktuellX + BallX;
        Canvas.SetLeft(chip.Elli, AktuellX * 2);
        count += 1;
      }
    }

    private void MoveBallLeft()
    {
      if (isHorizontalMoveEnabled && count > 0)
      {
        AktuellX = AktuellX - BallX;
        Canvas.SetLeft(chip.Elli, AktuellX * 2);
        count -= 1;
      }
    }

    // ...

    private void MoveBallDown()
    {
      NichtAus = 0;

      for (int i = 0; i < anzahlzeilen; i++)
      {
        if (BallPosition[count, i] == 0)
        {
          BallPosition[count, i] = Spieler;
          // Logik für die Darstellung des Chips
          MessageBox.Show("ChipPlatziert");
          NichtAus = 1;
          MessageBox.Show($"Chip an Position ({count}, {i}) platziert. Wert im Array: {BallPosition[count, i]}");
          
          break;
        }
        MessageBox.Show("EineZeileBlockiert");
      }

      if (NichtAus == 0)
      {
        MessageBox.Show("Sie können in dieser Zeile nicht platzieren");
      }

      if (Spieler == 1)
      {
        Spieler = -1;
      }
      else if (Spieler == -1)
      {
        Spieler = 1;
      }
    }    
  }
}