-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemoryMatch.xaml.cs
196 lines (171 loc) · 6.57 KB
/
MemoryMatch.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
namespace Clue // Ensure namespace matches XAML's x:Class
{
public partial class MemoryMatch : UserControl
{
private List<string> imagePaths;
private Button firstClicked = null;
private Button secondClicked = null;
private DispatcherTimer countdownTimer;
private int timeLeft = 100; // Countdown time in seconds
private bool canClick = true; // To prevent fast clicking
public MemoryMatch()
{
InitializeComponent();
InitializeGame();
}
private void InitializeGame()
{
// Create a list of image paths (8 pairs - example paths)
imagePaths = new List<string>
{
"pack://application:,,,/Images/Countries/CanadaPhoto.png", "pack://application:,,,/Images/Countries/FREEPALESTINE.png",
"pack://application:,,,/Images/Countries/NorwayPhoto.png", "pack://application:,,,/Images/Countries/PakistaniPhoto.png",
"pack://application:,,,/Images/Countries/PhilippinesPhoto.png", "pack://application:,,,/Images/Countries/PortugalPhoto.png",
"pack://application:,,,/Images/Countries/UKPhoto.png", "pack://application:,,,/Images/Countries/USAPhoto.png",
"pack://application:,,,/Images/Countries/CanadaPhoto.png", "pack://application:,,,/Images/Countries/FREEPALESTINE.png",
"pack://application:,,,/Images/Countries/NorwayPhoto.png", "pack://application:,,,/Images/Countries/PakistaniPhoto.png",
"pack://application:,,,/Images/Countries/PhilippinesPhoto.png", "pack://application:,,,/Images/Countries/PortugalPhoto.png",
"pack://application:,,,/Images/Countries/UKPhoto.png", "pack://application:,,,/Images/Countries/USAPhoto.png"
};
// Shuffle the image paths
var random = new Random();
imagePaths = imagePaths.OrderBy(x => random.Next()).ToList();
// Add buttons to the grid
CardGrid.Children.Clear();
for (int i = 0; i < 16; i++)
{
var button = new Button
{
Background = Brushes.LightGray,
Tag = imagePaths[i] // Store the image path in the button's Tag property
};
button.Focusable = false;
button.Click += Button_Click;
CardGrid.Children.Add(button);
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (!canClick) // Prevent clicking while processing
return;
var clickedButton = sender as Button;
if (clickedButton == null || clickedButton.Content != null)
return;
// Reveal the image
string imagePath = clickedButton.Tag.ToString();
try
{
var image = new Image
{
Source = new BitmapImage(new Uri(imagePath)),
Stretch = Stretch.Uniform
};
clickedButton.Content = image;
}
catch (Exception ex)
{
MessageBox.Show($"Error loading image: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
// Handle first and second clicks
if (firstClicked == null)
{
firstClicked = clickedButton;
return;
}
secondClicked = clickedButton;
// Disable clicking during evaluation
canClick = false;
// Check for a match
if (firstClicked.Tag.ToString() == secondClicked.Tag.ToString())
{
firstClicked = null;
secondClicked = null;
canClick = true; // Re-enable clicking
CheckForWinner();
}
else
{
// Use a timer to delay hiding the cards again
var timer = new DispatcherTimer
{
Interval = TimeSpan.FromSeconds(0.75)
};
timer.Tick += (s, args) =>
{
timer.Stop();
firstClicked.Content = null;
secondClicked.Content = null;
firstClicked = null;
secondClicked = null;
canClick = true; // Re-enable clicking
};
timer.Start();
}
}
private void CheckForWinner()
{
foreach (Button button in CardGrid.Children)
{
if (button.Content == null)
return;
}
countdownTimer.Stop(); // Stop the countdown if the player wins
MainWindow mainWindow = Window.GetWindow(this) as MainWindow;
if (mainWindow != null)
{
mainWindow.GameWin();
}
}
private void ResetGame()
{
CardGrid.Children.Clear();
InitializeGame();
timeLeft = 100; // Reset the timer
TimerText.Text = "Time Left: 100";
StartCountdown();
}
private void StartCountdown()
{
// Initialize the countdown timer
countdownTimer = new DispatcherTimer
{
Interval = TimeSpan.FromSeconds(1)
};
countdownTimer.Tick += (s, e) =>
{
timeLeft--;
// Update the timer display
TimerText.Text = $"Time Left: {timeLeft}";
// Check if time has run out
if (timeLeft <= 0)
{
countdownTimer.Stop();
MainWindow mainWindow = Window.GetWindow(this) as MainWindow;
if (mainWindow != null)
{
mainWindow.GameLose();
}
//ResetGame();
}
};
countdownTimer.Start();
}
private void UserControl_GotFocus(object sender, RoutedEventArgs e)
{
ResetGame();
}
private void UserControl_LostFocus(object sender, RoutedEventArgs e)
{
countdownTimer.Stop();
}
}
}