-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.vb
216 lines (163 loc) · 5.35 KB
/
Program.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
211
212
213
214
215
216
Imports System
Module Program
Dim Cellen As New Lijst
Sub Main()
Dim x As Short = 1
Dim y As Short = 1
Dim z As Short = 1
Console.WriteLine("Start")
While x <= 9
y = 1
While y <= 9
Dim C As New Cell(x, y)
Cellen.Add(C)
y += 1
End While
x += 1
End While
Console.WriteLine("Cellen gemaakt")
For Each C As Cell In Cellen
If C.X = 1 AndAlso C.Y = 7 Then C.Waarde = 4
If C.X = 2 AndAlso C.Y = 1 Then C.Waarde = 8
If C.X = 2 AndAlso C.Y = 3 Then C.Waarde = 3
If C.X = 2 AndAlso C.Y = 6 Then C.Waarde = 4
If C.X = 3 AndAlso C.Y = 8 Then C.Waarde = 1
If C.X = 3 AndAlso C.Y = 9 Then C.Waarde = 9
If C.X = 3 AndAlso C.Y = 6 Then C.Waarde = 7
If C.X = 4 AndAlso C.Y = 2 Then C.Waarde = 5
If C.X = 4 AndAlso C.Y = 3 Then C.Waarde = 4
If C.X = 4 AndAlso C.Y = 4 Then C.Waarde = 1
If C.X = 4 AndAlso C.Y = 6 Then C.Waarde = 8
If C.X = 5 AndAlso C.Y = 8 Then C.Waarde = 6
If C.X = 6 AndAlso C.Y = 9 Then C.Waarde = 7
If C.X = 6 AndAlso C.Y = 1 Then C.Waarde = 2
If C.X = 6 AndAlso C.Y = 4 Then C.Waarde = 9
If C.X = 7 AndAlso C.Y = 1 Then C.Waarde = 3
If C.X = 7 AndAlso C.Y = 2 Then C.Waarde = 8
If C.X = 8 AndAlso C.Y = 7 Then C.Waarde = 3
If C.X = 8 AndAlso C.Y = 8 Then C.Waarde = 5
If C.X = 8 AndAlso C.Y = 1 Then C.Waarde = 4
If C.X = 8 AndAlso C.Y = 6 Then C.Waarde = 2
If C.X = 9 AndAlso C.Y = 4 Then C.Waarde = 6
If C.X = 9 AndAlso C.Y = 9 Then C.Waarde = 1
If C.X = 9 AndAlso C.Y = 2 Then C.Waarde = 7
Select Case C.X
Case 1 To 3
Select Case C.Y
Case 1 To 3
C.Z = 1
Case 4 To 6
C.Z = 4
Case 7 To 9
C.Z = 7
End Select
Case 4 To 6
Select Case C.Y
Case 1 To 3
C.Z = 8
Case 4 To 6
C.Z = 5
Case 7 To 9
C.Z = 2
End Select
Case 7 To 9
Select Case C.Y
Case 1 To 3
C.Z = 9
Case 4 To 6
C.Z = 3
Case 7 To 9
C.Z = 6
End Select
End Select
Next
Console.WriteLine("Start solving")
If Solve() Then
Console.WriteLine("YEEEEEEEEEY")
For Each C As Cell In Cellen
Console.WriteLine($"x = {C.X}, Y = {C.Y}, Z = {C.Z}, Val = {C.Waarde}")
Next
Else
Console.WriteLine("Geen uitkomst gevonden")
For Each C As Cell In Cellen
Console.WriteLine($"x = {C.X}, Y = {C.Y}, Val = {C.Waarde}")
Next
End If
End Sub
Public Function Solve()
Dim C As Cell
Dim num As Short
C = Cellen.GetFirstEmpty()
If C Is Nothing Then
Return True
End If
For num = 1 To 9
If CheckConstraint(C, num) Then
C.Waarde = num
Console.WriteLine($"x = {C.X}, Y = {C.Y}, Val = {C.Waarde}")
If Solve() Then
Return True
End If
End If
Console.Write("b")
C.Waarde = 0
Next
Return False
End Function
Public Function CheckConstraint(cel, num)
Return Not inX(cel, num) AndAlso Not inY(cel, num) AndAlso Not inZ(cel, num)
End Function
Public Function inX(cel, num)
For Each C As Cell In Cellen
If C.X = cel.x Then
If C.Waarde = num Then
Return True
End If
End If
Next
Return False
End Function
Public Function inY(cel, num)
For Each C As Cell In Cellen
If C.Y = cel.Y Then
If C.Waarde = num Then
Return True
End If
End If
Next
Return False
End Function
Public Function inZ(cel, num)
For Each C As Cell In Cellen
If C.Z = cel.Z Then
If C.Waarde = num Then
Return True
End If
End If
Next
Return False
End Function
End Module
Public Class Cell
Public Property X As Short
Public Property Y As Short
Public Property Z As Short
Public Property Waarde As Short = 0
Public Sub New(x, y)
Me.X = x
Me.Y = y
End Sub
End Class
Public Class Lijst
Inherits List(Of Cell)
Public Function GetFirstEmpty()
Dim I As Short = 0
While I < Me.Count()
If Me.Item(I).Waarde = 0 Then
Return Me.Item(I)
End If
I += 1
End While
Return Nothing
End Function
End Class