-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
131 lines (129 loc) · 5.22 KB
/
MainWindow.xaml.cs
File metadata and controls
131 lines (129 loc) · 5.22 KB
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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Common;
using System.IO;
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.Navigation;
using System.Windows.Shapes;
using System.Xml.Serialization;
//класс для работы с окном
namespace SaperWPF
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
const int MaxHighscoreListEntryCount = 5;
private Game gameLogic;
public TimeSpan currentScore;
public MainWindow()
{
InitializeComponent();
PreviewKeyDown += MainWindow_PreviewKeyDown;
gameLogic = new Game(this); // Передача ссылки на экземпляр MainWindow в конструктор Game
DataContext = gameLogic;
LoadHighscoreList();
}
private void MouseRightBottonUpHandler(object sender, MouseEventArgs e)
{
var viewModel = DataContext as Game;
if(viewModel != null)
{
var button=e.Source as Button;
if(button != null)
{
var cell=button.Tag as Cell;
viewModel.CellRightClickCommand.Execute(cell);
}
}
}
public ObservableCollection<Record> HighscoreList
{
get; set;
} = new ObservableCollection<Record>();
private void SaveHighscoreList()
{
XmlSerializer serializer = new XmlSerializer(typeof(ObservableCollection<Record>));
using (Stream writer = new FileStream("saper_highscorelist.xml", FileMode.Create))
{
serializer.Serialize(writer, this.HighscoreList);
}
}
private void BtnShowHighscoreList_Click(object sender, RoutedEventArgs e)
{
bdrWelcomeMessage.Visibility = Visibility.Collapsed;
bdrHighscoreList.Visibility = Visibility.Visible;
}
private void BtnAddToHighscoreList_Click(object sender, RoutedEventArgs e)
{
if (!string.IsNullOrWhiteSpace(txtPlayerName.Text)) // Проверка на пустое значение
{
int newIndex = 0;
// Where should the new entry be inserted?
if ((this.HighscoreList.Count > 0) && (currentScore < this.HighscoreList.Max(x => x.TimeElapsed)))
{
Record justAbove = this.HighscoreList.OrderBy(x => x.TimeElapsed).First(x => x.TimeElapsed >= currentScore);
if (justAbove != null)
newIndex = this.HighscoreList.IndexOf(justAbove) + 1;
}
// Create & insert the new entry
this.HighscoreList.Insert(newIndex, new Record()
{
PlayerName = txtPlayerName.Text,
TimeElapsed = currentScore,
});
// Make sure that the amount of entries does not exceed the maximum
while (this.HighscoreList.Count > MaxHighscoreListEntryCount)
this.HighscoreList.RemoveAt(MaxHighscoreListEntryCount);
SaveHighscoreList();
txtPlayerName.Text = string.Empty;
bdrNewHighscore.Visibility = Visibility.Collapsed;
bdrHighscoreList.Visibility = Visibility.Visible;
}
else
{
MessageBox.Show("Пожалуйста, введите имя игрока."); // Предупреждение об отсутствии имени игрока
}
}
private void MainWindow_PreviewKeyDown(object sender, KeyEventArgs e)
{
// Проверяем, что bdrNewHighscore не видимый перед выполнением RestartGame
if (gameLogic != null &&
bdrNewHighscore.Visibility != Visibility.Visible &&
bdrWelcomeMessage.Visibility != Visibility.Collapsed
) // Добавляем проверку на Visibility.Collapsed
{
// если нажата клавиша пробела
if (e.Key == Key.Space)
{
gameLogic.RestartGame(null); // вызываем метод перезапуска игры
}
}
}
private void LoadHighscoreList()
{
if (File.Exists("saper_highscorelist.xml"))
{
XmlSerializer serializer = new XmlSerializer(typeof(List<Record>));
using (Stream reader = new FileStream("saper_highscorelist.xml", FileMode.Open))
{
List<Record> tempList = (List<Record>)serializer.Deserialize(reader);
this.HighscoreList.Clear();
foreach (var item in tempList.OrderBy(x => x.TimeElapsed))
this.HighscoreList.Add(item);
}
}
}
}
}