-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
120 lines (96 loc) · 3.77 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
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.Navigation;
using System.Windows.Shapes;
namespace Prisoners
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
double playgroundPrisoners = 0;
double playgroundRepeat = 0;
List<Playground> pg = new List<Playground>();
private void buttonNew_Click(object sender, RoutedEventArgs e)
{
string message = string.Empty;
if (SetPlaygroundParameters(out message))
Launch();
else
MessageBox.Show(message, "Parameters error", MessageBoxButton.OK, MessageBoxImage.Error);
}
private bool SetPlaygroundParameters(out string message)
{
bool prisonersIsValid = false;
bool repeatIsValid = false;
message = string.Empty;
prisonersIsValid = Double.TryParse(textBoxNumberOfPrisoners.Text, out playgroundPrisoners);
repeatIsValid = Double.TryParse(textBoxNumberOfRepeat.Text, out playgroundRepeat);
if (!prisonersIsValid || playgroundPrisoners % 2 != 0 || playgroundPrisoners <= 0)
{
prisonersIsValid = false;
message = "Count of prisoners must be positive even number!";
}
if (!repeatIsValid || playgroundRepeat <= 0)
{
repeatIsValid = false;
message = "Count of repeat must be greater than 0!";
}
return prisonersIsValid && repeatIsValid;
}
private void Launch()
{
double passed = 0;
dataGridPlaygrondList.ItemsSource = null;
dataGridPrisonersList.ItemsSource = null;
pg.Clear();
for (double i = 1; i <= playgroundRepeat; i++)
{
Playground p = new Playground(Convert.ToInt32(playgroundPrisoners), (int)i - 1 );
p.CreateBoxes();
p.CreatePrisoners();
foreach (Prisoner prisoner in p.Prisoners)
p.DoJourney(prisoner);
if (p.Prisoners.FindAll(a => a.Result == Result.Passed).Count() == playgroundPrisoners)
{
passed++;
p.Result = Result.Passed;
}
else
p.Result = Result.Failed;
int countPassed = p.Prisoners.FindAll(a => a.Result == Result.Passed).Count();
int countFailed = p.Prisoners.FindAll(a => a.Result == Result.Failed).Count();
p.CountFailed = countFailed;
p.CountPassed = countPassed;
pg.Add(p);
}
labelSuccessRatio.Content = Math.Round(passed / playgroundRepeat, 2).ToString("#.##%");
dataGridPlaygrondList.ItemsSource = pg;
}
private void dataGridPlaygroundList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (((DataGrid)sender).SelectedItems.Count > 0)
{
Playground selectedPlayground = (Playground)((DataGrid)sender).CurrentItem;
dataGridPrisonersList.ItemsSource = selectedPlayground.Prisoners;
labelPassed.Content = selectedPlayground.CountPassed;
labelFailed.Content = selectedPlayground.CountFailed;
}
}
}
}