using Memory.Data; using Memory.Logic; using System.Windows; using System.Windows.Controls; using System.Windows.Media; namespace Memory.Gui { public partial class MainWindow : Window { public const int SCOREPADDING = 160; public const int SCOREMARGIN = 30; private Game? game; public MainWindow() { InitializeComponent(); CreateGrid(); } private void StartGame(object sender, RoutedEventArgs args) { string name = Name.Text; if (name.Length > 32 || name.Length < 2) { ErrorLabel.Visibility = Visibility.Visible; } else { game = new(new ScoreHandler(), name); ErrorLabel.Visibility = Visibility.Hidden; StartScreen.Visibility = Visibility.Hidden; FinishScreen.Visibility = Visibility.Hidden; GameScreen.Visibility = Visibility.Visible; Redraw(); } } private void DrawScores() { Highscores.Children.Clear(); OwnScore.Content = $"Your score: {game!.Scoring.Points}"; List highscores = game.ScoreHandler.GetTopScores(); for (int i = 0; i < highscores.Count; i++) { Score score = highscores[i]; Label label = new() { Content = $"{i+1}. {score.Name}: {score.Points}", HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Top, FontSize = 30, Margin = new Thickness(0, SCOREPADDING + SCOREMARGIN * i, 0, 0) }; Highscores.Children.Add(label); } } private void FinishGame() { DrawScores(); GameScreen.Visibility = Visibility.Hidden; FinishScreen.Visibility = Visibility.Visible; } private void CreateGrid() { int columns = Game.GRIDSIZE; int rows = Game.DECKSIZE * 2 / Game.GRIDSIZE; for (int i = 0; i < columns; i++) { ColumnDefinition colDef = new() { Width = new(1, GridUnitType.Star) }; Cards.ColumnDefinitions.Add(colDef); } for (int i = 0; i < rows; i++) { RowDefinition rowDef = new() { Height = new(1, GridUnitType.Star) }; Cards.RowDefinitions.Add(rowDef); } } private void Redraw() { Cards.Children.Clear(); Score.Content = $"Score: {game!.Scoring.Points}"; for (int i = 0; i < game!.Cards.Count; i++) { Card card = game.Cards[i]; if (!card.Completed) { Button button = new() { Content = card.Selected() ? card.ID : null, FontSize = 30, Background = new SolidColorBrush(card.Selected() ? Color.FromRgb(0, 255, 0) : Color.FromRgb(255, 0, 0)) }; Grid.SetColumn(button, i % Game.GRIDSIZE); Grid.SetRow(button, i / Game.GRIDSIZE); button.Click += (object sender, RoutedEventArgs args) => { game.ClickCard(card); if (!game.IsFinished()) { Redraw(); } else { FinishGame(); } }; Cards.Children.Add(button); } } } } }