/
frmSearch.vb
210 lines (194 loc) · 8.61 KB
/
frmSearch.vb
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
Option Explicit On
Public Class frmSearch
Dim starmap As starmap = Form1.starmap
Dim agentList As agentList = Form1.agentList
Dim goodsList As List(Of String)
Dim planetList As List(Of planet)
Dim comboboxlist As New List(Of ComboBox)
Public selectedPlanet As planet
Private Sub frmSearch_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
initialiseFrmSearch()
End Sub
Private Sub initialiseFrmSearch()
'populate comboboxes
If goodsList Is Nothing Then goodsList = New List(Of String)
repopGoodsList()
For Each good In goodsList
cmbSupply.Items.Add(good)
cmbDemand.Items.Add(good)
Next
cmbStar.Items.Add("")
For Each star In starmap.stars
If star.type <> "Blackhole" Then cmbStar.Items.Add(star.name)
Next
cmbCities.Items.Add("")
For i As Integer = 1 To 5
cmbCities.Items.Add(i)
Next
cmbSuffix.Items.Add("")
For i As Integer = 1 To planetSuffixDictionary.Count Step 1
cmbSuffix.Items.Add(planetSuffixDictionary(i))
Next
'cmbGovernment and cmbPrefix are all added manually
' add all the comboboxes into comboboxlist
For Each control As Control In Me.Controls
If (TypeOf control Is ComboBox) Then comboboxlist.Add(control)
Next
End Sub
Private Sub repopGoodsList()
goodsList.Add("")
For Each item In defaultSystemSupply
goodsList.Add(item)
Next
goodsList.Add("None")
End Sub
Private Sub runSearch()
' if no control is selected then exit sub
If allIsEmpty(comboboxlist) = True Then Exit Sub
If planetList Is Nothing Then
planetList = New List(Of planet)
Else
planetList.Clear()
End If
For Each star As star In starmap.stars
If star.type <> "Blackhole" Then
For Each planet As planet In star.planets
'every planet that meets the search criteria goes into planetList
'if nothing is selected for a particular criteria the function automatically returns True
If isStar(planet.starName) = True AndAlso _
isSupply(planet.supply) = True AndAlso _
isDemand(planet.demand) = True AndAlso _
isCities(planet.size) = True AndAlso _
isGovernment(planet.government) = True AndAlso _
isPrefix(planet.prefix) = True AndAlso _
isSuffix(planet.suffix) = True AndAlso _
isAgent(planet.stationedAgents) = True Then
planetList.Add(planet)
End If
Next
End If
Next
For Each planet In planetList
Dim n As Integer = DataGridView1.Rows.Add()
Dim str As String = ""
DataGridView1.Rows.Item(n).Cells(0).Value = planet.starName & " " & romanNumeralDictionary(planet.number)
DataGridView1.Rows.Item(n).Cells(1).Value = planet.size
DataGridView1.Rows.Item(n).Cells(2).Value = planetTypeShortformDictionary(planet.prefix)
DataGridView1.Rows.Item(n).Cells(3).Value = planetTypeShortformDictionary(planet.suffix)
For Each supply As String In planet.supply
str = str & supply & " "
Next
DataGridView1.Rows.Item(n).Cells(4).Value = str
str = ""
For Each demand As String In planet.demand
str = str & demand & " "
Next
DataGridView1.Rows.Item(n).Cells(5).Value = str
DataGridView1.Rows.Item(n).Cells(6).Value = planet.stationedAgents.Count
Next
End Sub
Private Function allIsEmpty(comboboxlist As List(Of ComboBox)) As Boolean
allIsEmpty = True
For Each cmb As ComboBox In comboboxlist
If cmb.SelectedItem <> Nothing Then allIsEmpty = False
Next
End Function
Private Function isStar(ByVal starName As String) As Boolean
If cmbStar.SelectedItem = Nothing Then
Return True
Else
If cmbStar.SelectedItem.ToString = starName Then Return True Else Return False
End If
End Function
Private Function isCities(ByVal cities As Integer) As Boolean
If cmbCities.SelectedItem = Nothing Then
Return True
Else
If Convert.ToInt32(cmbCities.SelectedItem.ToString) = cities Then Return True Else Return False
End If
End Function
Private Function isSupply(ByVal supply As List(Of String)) As Boolean
If cmbSupply.SelectedItem = Nothing Then
Return True
Else
Dim str As String = cmbSupply.SelectedItem.ToString
For Each good As String In supply
If good = str Then Return True
Next
Return False
End If
End Function
Private Function isDemand(ByVal demand As List(Of String)) As Boolean
If cmbDemand.SelectedItem = Nothing Then
Return True
Else
Dim str As String = cmbDemand.SelectedItem.ToString
For Each good As String In demand
If good = str Then Return True
Next
Return False
End If
End Function
Private Function isGovernment(ByVal government As String) As Boolean
If cmbGovernment.SelectedItem = Nothing Then
Return True
Else
If cmbGovernment.SelectedItem.ToString = government Then Return True Else Return False
End If
End Function
Private Function isPrefix(ByVal prefix As String) As Boolean
If cmbPrefix.SelectedItem = Nothing Then
Return True
Else
If cmbPrefix.SelectedItem.ToString = prefix Then Return True Else Return False
End If
End Function
Private Function isSuffix(ByVal suffix As String) As Boolean
If cmbSuffix.SelectedItem = Nothing Then
Return True
Else
If cmbSuffix.SelectedItem.ToString = suffix Then Return True Else Return False
End If
End Function
Private Function isAgent(ByVal stationedAgents As List(Of agent)) As Boolean
If cmbAgents.SelectedItem = Nothing Then Return True
Select Case cmbAgents.SelectedIndex
Case 1
' no agents
If stationedAgents.Count = 0 Then Return True Else Return False
Case 2
'at least 1
If stationedAgents.Count > 0 Then Return True Else Return False
Case 3
'more than 1
If stationedAgents.Count > 1 Then Return True Else Return False
Case Else
'bugcatch
Return False
End Select
Return False
End Function
Private Function realNumberofAgents(ByVal stationedAgents As List(Of String)) As Integer
If stationedAgents.Item(0) = "000" Then
Return 0
Else
Return stationedAgents.Count
End If
End Function
Private Sub ComboBox_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbSupply.SelectedIndexChanged, _
cmbDemand.SelectedIndexChanged, _
cmbCities.SelectedIndexChanged, _
cmbStar.SelectedIndexChanged, _
cmbPrefix.SelectedIndexChanged, _
cmbSuffix.SelectedIndexChanged, _
cmbGovernment.SelectedIndexChanged, _
cmbAgents.SelectedIndexChanged
DataGridView1.Rows.Clear()
runSearch()
End Sub
Private Sub DataGridView1_CellContentDoubleClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
If e.RowIndex = -1 Then Return
selectedPlanet = planetList(e.RowIndex)
Me.DialogResult = Windows.Forms.DialogResult.OK
End Sub
End Class