158 lines
5.1 KiB
C#
158 lines
5.1 KiB
C#
using Memory.Data;
|
|
using Memory.Logic;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Media;
|
|
using System.Windows.Data;
|
|
using System.Windows.Media.Imaging;
|
|
|
|
namespace Memory.Gui
|
|
{
|
|
public partial class MainWindow : Window
|
|
{
|
|
public const int SCOREPADDING = 160;
|
|
public const int SCOREMARGIN = 30;
|
|
public const int GRIDWIDTH = 700;
|
|
public const int GRIDHEIGHT = 500;
|
|
|
|
private Game? game;
|
|
private int deckSize = 5;
|
|
|
|
public MainWindow()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
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, deckSize);
|
|
ErrorLabel.Visibility = Visibility.Hidden;
|
|
StartScreen.Visibility = Visibility.Hidden;
|
|
FinishScreen.Visibility = Visibility.Hidden;
|
|
GameScreen.Visibility = Visibility.Visible;
|
|
CreateGrid();
|
|
Redraw();
|
|
}
|
|
}
|
|
|
|
private void Decrease(object sender, RoutedEventArgs args)
|
|
{
|
|
deckSize = Math.Max(deckSize - 1, 2);
|
|
DeckSize.Content = deckSize;
|
|
}
|
|
|
|
private void Increase(object sender, RoutedEventArgs args)
|
|
{
|
|
deckSize = Math.Min(deckSize + 1, 10);
|
|
DeckSize.Content = deckSize;
|
|
}
|
|
|
|
private void DrawScores()
|
|
{
|
|
Highscores.Children.Clear();
|
|
OwnScore.Content = $"Your score: {game!.Scoring.Points}";
|
|
List<Score> 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()
|
|
{
|
|
Cards.ColumnDefinitions.Clear();
|
|
Cards.RowDefinitions.Clear();
|
|
Deck grid = Game.GetGridSize(deckSize);
|
|
int columns = grid.Columns;
|
|
int rows = grid.Rows;
|
|
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);
|
|
}
|
|
if (rows > columns)
|
|
{
|
|
Cards.Width = GRIDWIDTH * (double)columns / rows;
|
|
}
|
|
else if (columns > rows)
|
|
{
|
|
Cards.Height = GRIDHEIGHT * (double)rows / columns;
|
|
}
|
|
}
|
|
|
|
private void Redraw()
|
|
{
|
|
Cards.Children.Clear();
|
|
Deck grid = Game.GetGridSize(deckSize);
|
|
for (int i = 0; i < game!.Cards.Count; i++)
|
|
{
|
|
Card card = game.Cards[i];
|
|
if (!card.Completed)
|
|
{
|
|
Image image = new()
|
|
{
|
|
Source = new BitmapImage(new($"pack://application:,,,/images/{card.ID}.png")),
|
|
Stretch = Stretch.Fill,
|
|
};
|
|
Button button = new()
|
|
{
|
|
Content = card.Selected() ? image : null,
|
|
FontSize = 30,
|
|
Background = new SolidColorBrush(card.Selected() ? Color.FromRgb(0, 255, 0) : Color.FromRgb(255, 0, 0)),
|
|
Padding = new Thickness(0)
|
|
};
|
|
|
|
Grid.SetColumn(button, i % grid.Columns);
|
|
Grid.SetRow(button, i / grid.Columns);
|
|
button.Click += (object sender, RoutedEventArgs args) =>
|
|
{
|
|
game.ClickCard(card);
|
|
if (!game.IsFinished())
|
|
{
|
|
Redraw();
|
|
}
|
|
else
|
|
{
|
|
FinishGame();
|
|
}
|
|
};
|
|
Cards.Children.Add(button);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |