-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrackTheLock.xaml.cs
118 lines (111 loc) · 3.48 KB
/
CrackTheLock.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace Clue
{
/// <summary>
/// Interaction logic for CrackTheLock.xaml
/// </summary>
public partial class CrackTheLock : UserControl
{
private readonly int[] _lockCombination = { 3, 0, 1 };
private int _hintIndex = 2; // Track which digit to hint next
public CrackTheLock()
{
InitializeComponent();
}
private void Submit_Click(object sender, RoutedEventArgs e)
{
// Parse player input
if (!int.TryParse(Num1.Text, out int guess1) ||
!int.TryParse(Num2.Text, out int guess2) ||
!int.TryParse(Num3.Text, out int guess3))
{
FeedbackLabel.Text = "Please enter valid numbers!";
return;
}
// Check if the guess is correct
int[] playerGuess = { guess1, guess2, guess3 };
if (IsCorrect(playerGuess))
{
FeedbackLabel.Text = "🎉 Correct! You unlocked the lock!";
MainWindow mainWindow = Window.GetWindow(this) as MainWindow;
if (mainWindow != null)
{
mainWindow.GameWin();
}
}
else
{
FeedbackLabel.Text = "❌ Incorrect! Try again!";
}
}
private void Button_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
e.Handled = true; // Prevent the button from responding to Enter
}
}
private void Hint_Click(object sender, RoutedEventArgs e)
{
// Provide hint logic
if (_hintIndex < _lockCombination.Length)
{
FeedbackLabel.Text = $"Hint: It's the date that marks the beginning of Lent in \"Ash Wednesday Feast\"";
_hintIndex++;
}
else
{
FeedbackLabel.Text = "No more hints available!";
}
}
private bool IsCorrect(int[] guess)
{
// Compare guess with the lock combination
for (int i = 0; i < _lockCombination.Length; i++)
{
if (guess[i] != _lockCombination[i])
return false;
}
return true;
}
private void Num1_KeyDown(object sender, KeyEventArgs e)
{
if (int.TryParse(((TextBox)sender).Text, out int _))
{
switch (((TextBox)sender).Name)
{
case "Num1":
Num2.Focus();
break;
case "Num2":
Num3.Focus();
break;
}
}
else if (e.Key == Key.Back)
{
switch (((TextBox)sender).Name)
{
case "Num2":
Num1.Focus();
break;
case "Num3":
Num2.Focus();
break;
}
}
}
}
}