-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
175 lines (161 loc) · 6.26 KB
/
MainWindow.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
using System;
using System.Collections.Generic;
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;
namespace Bacterium
{
/// <summary>
/// Логика взаимодействия для MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private Game _game;
private MyPoint _currentPoint;
public MainWindow()
{
InitializeComponent();
StartGame();
}
private void StartGame()
{
//String path = Directory.GetCurrentDirectory() + @"\..\..\";
String path = Directory.GetCurrentDirectory() + @"\";
loadButton.Source = new BitmapImage(new Uri(path + @"Images\SaveAndLoad\Load1.png"));
saveButton.Source = new BitmapImage(new Uri(path + @"Images\SaveAndLoad\Save1.png"));
resetButton.Source = new BitmapImage(new Uri(path + @"Images\RestartArrows\RestartArrow1.png"));
imageFlag1.Source = new BitmapImage(new Uri(path + @"Images\Flags\RedFlag.png"));
imageFlag2.Source = new BitmapImage(new Uri(path + @"Images\Flags\GreenFlag.png"));
BitmapImage tmp = new BitmapImage(new Uri(path + @"Images\Cells.png"));
ImageBrush brush = new ImageBrush(tmp);
RootGrid.Background = brush;
_game = new Game(PointGrid);
_game.Start();
ShowState();
this.Icon = new BitmapImage(new Uri(path + @"Images\icon.ico"));
MessageBox.Show("The game is began!");
}
private void loadButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
try
{
_game.Load();
ShowState();
MessageBox.Show("Loaded! :)", "Message");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
StartGame();
}
}
private void ShowState()
{
label1.Content = Convert.ToString(_game.LightCount);
label2.Content = Convert.ToString(_game.DarkCount);
if (_game.IsLightTurn)
{
imageFlag1.Visibility = Visibility.Visible;
imageFlag2.Visibility = Visibility.Hidden;
}
else
{
imageFlag2.Visibility = Visibility.Visible;
imageFlag1.Visibility = Visibility.Hidden;
}
}
private void PointGrid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (_currentPoint != null)
{
Cell tmp = _currentPoint.CurrentCell;
_currentPoint.SetNewLocation(tmp.X, tmp.Y);
_currentPoint = null;
}
int side;
if (_game.IsLightTurn)
side = MyPoint.LIGHT;
else
side = MyPoint.DARK;
int x = (int)e.GetPosition(PointGrid).X;
int y = (int)e.GetPosition(PointGrid).Y;
_currentPoint = _game.Find(side, x, y);
if (_currentPoint != null)
{
_currentPoint.BringToFront();
}
}
private void PointGrid_MouseMove(object sender, MouseEventArgs e)
{
if (!e.LeftButton.Equals(MouseButtonState.Pressed))
return;
if (_currentPoint == null)
return;
int x = (int)e.GetPosition(PointGrid).X - MyPoint.WIDTH / 2;
int y = (int)e.GetPosition(PointGrid).Y - MyPoint.HEIGHT / 2;
if (x < _currentPoint.X || x > _currentPoint.X + MyPoint.WIDTH ||
y < _currentPoint.Y || y > _currentPoint.Y + MyPoint.HEIGHT)
{
Cell tmp = _currentPoint.CurrentCell;
_currentPoint.SetNewLocation(tmp.X, tmp.Y);
}
_currentPoint.SetNewLocation(x, y);
}
private void PointGrid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (_currentPoint == null)
return;
int x = (int)e.GetPosition(PointGrid).X;
int y = (int)e.GetPosition(PointGrid).Y;
if (x < _currentPoint.X || x > _currentPoint.X + MyPoint.WIDTH ||
y < _currentPoint.Y || y > _currentPoint.Y + MyPoint.HEIGHT ||
!_game.JumpToCell(x, y, _currentPoint))
{
Cell tmp = _currentPoint.CurrentCell;
_currentPoint.SetNewLocation(tmp.X, tmp.Y);
}
ShowState();
if (_game.FreeCellsAmount == 0)
MessageBox.Show(String.Format("Game over! The winner is {0}", _game.DarkCount < _game.LightCount ? "Red team" : "Green team"));
else
if (_game.IsLocked)
{
int lightCount = _game.LightCount + (_game.IsLightTurn ? 0 : _game.FreeCellsAmount);
int darkCount = _game.DarkCount + (_game.IsLightTurn ? _game.FreeCellsAmount : 0);
MessageBox.Show(String.Format("Game over! The winner is: {0}", (lightCount > darkCount) ? "Red team" : "Green team"));
}
_currentPoint.SetToBack();
_currentPoint = null;
}
private void saveButton_MouseDown(object sender, MouseButtonEventArgs e)
{
_game.Save();
MessageBox.Show("Saved! :)", "Message");
}
private void resetButton_MouseDown(object sender, MouseButtonEventArgs e)
{
_game.Restart();
ShowState();
}
private void RootGrid_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton != MouseButtonState.Pressed)
return;
if (_currentPoint == null)
return;
int x = (int)e.GetPosition(PointGrid).X - MyPoint.WIDTH / 2;
int y = (int)e.GetPosition(PointGrid).Y - MyPoint.HEIGHT / 2;
_currentPoint.SetNewLocation(x, y);
}
}
}