Add comments
This commit is contained in:
@ -19,6 +19,7 @@ namespace Memory.Gui
|
||||
private Game? game;
|
||||
private int _deckSize = 5;
|
||||
|
||||
// Property with propertychanged listener to update it in the ui
|
||||
public int DeckSize { get => _deckSize; set
|
||||
{
|
||||
_deckSize = value;
|
||||
@ -36,6 +37,7 @@ namespace Memory.Gui
|
||||
|
||||
private void StartGame(object sender, RoutedEventArgs args)
|
||||
{
|
||||
// Check if the name is valid, otherwise display error
|
||||
string name = Name.Text;
|
||||
if (name.Length > 32 || name.Length < 2)
|
||||
{
|
||||
@ -43,6 +45,7 @@ namespace Memory.Gui
|
||||
}
|
||||
else
|
||||
{
|
||||
// Hide startscreen and show gamescreen and create a grid and draw the cards
|
||||
game = new(new ScoreHandler(), name, DeckSize);
|
||||
ErrorLabel.Visibility = Visibility.Hidden;
|
||||
StartScreen.Visibility = Visibility.Hidden;
|
||||
@ -53,10 +56,12 @@ namespace Memory.Gui
|
||||
}
|
||||
}
|
||||
|
||||
// Callback functions for changing the size of the deck
|
||||
protected void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||||
private void Decrease(object sender, RoutedEventArgs args) => DeckSize = Math.Max(DeckSize - 1, 2);
|
||||
private void Increase(object sender, RoutedEventArgs args) => DeckSize = Math.Min(DeckSize + 1, 10);
|
||||
|
||||
// Draw the highscores
|
||||
private void DrawScores()
|
||||
{
|
||||
Highscores.Children.Clear();
|
||||
@ -77,6 +82,7 @@ namespace Memory.Gui
|
||||
}
|
||||
}
|
||||
|
||||
// Enable the finish screen with scores
|
||||
private void FinishGame()
|
||||
{
|
||||
DrawScores();
|
||||
@ -84,6 +90,7 @@ namespace Memory.Gui
|
||||
FinishScreen.Visibility = Visibility.Visible;
|
||||
}
|
||||
|
||||
// Draw grid
|
||||
private void CreateGrid()
|
||||
{
|
||||
Cards.ColumnDefinitions.Clear();
|
||||
@ -121,11 +128,13 @@ namespace Memory.Gui
|
||||
{
|
||||
Cards.Children.Clear();
|
||||
Deck grid = Game.GetGridSize(DeckSize);
|
||||
// Loop over cards to draw them
|
||||
for (int i = 0; i < game!.Cards.Count; i++)
|
||||
{
|
||||
Card card = game.Cards[i];
|
||||
if (!card.Completed)
|
||||
{
|
||||
// Draw card with image if card isn't completed
|
||||
Image image = new()
|
||||
{
|
||||
Source = new BitmapImage(new($"pack://application:,,,/images/{card.ID}.png")),
|
||||
@ -138,11 +147,12 @@ namespace Memory.Gui
|
||||
Background = new SolidColorBrush(card.Selected() ? Color.FromRgb(0, 255, 0) : Color.FromRgb(255, 0, 0)),
|
||||
Padding = new Thickness(0)
|
||||
};
|
||||
|
||||
// Set the card in the correct grid position
|
||||
Grid.SetColumn(button, i % grid.Columns);
|
||||
Grid.SetRow(button, i / grid.Columns);
|
||||
button.Click += (object sender, RoutedEventArgs args) =>
|
||||
{
|
||||
// Onclick check if game is finished
|
||||
game.ClickCard(card);
|
||||
if (!game.IsFinished())
|
||||
{
|
||||
|
Reference in New Issue
Block a user