Skip to content

Commit 4df773d

Browse files
committed
First version of the code
1 parent 462e456 commit 4df773d

12 files changed

+1199
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 11.00
3+
# Visual Basic Express 2010
4+
Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "StackMathGenerator", "StackMathGenerator\StackMathGenerator.vbproj", "{58B7B556-6FD1-44C2-82BC-079A153E81E8}"
5+
EndProject
6+
Global
7+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
8+
Debug|x86 = Debug|x86
9+
Release|x86 = Release|x86
10+
EndGlobalSection
11+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
12+
{58B7B556-6FD1-44C2-82BC-079A153E81E8}.Debug|x86.ActiveCfg = Debug|x86
13+
{58B7B556-6FD1-44C2-82BC-079A153E81E8}.Debug|x86.Build.0 = Debug|x86
14+
{58B7B556-6FD1-44C2-82BC-079A153E81E8}.Release|x86.ActiveCfg = Release|x86
15+
{58B7B556-6FD1-44C2-82BC-079A153E81E8}.Release|x86.Build.0 = Release|x86
16+
EndGlobalSection
17+
GlobalSection(SolutionProperties) = preSolution
18+
HideSolutionNode = FALSE
19+
EndGlobalSection
20+
EndGlobal

StackMathGenerator/StackMathGenerator/Form1.Designer.vb

Lines changed: 338 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<root>
3+
<!--
4+
Microsoft ResX Schema
5+
6+
Version 2.0
7+
8+
The primary goals of this format is to allow a simple XML format
9+
that is mostly human readable. The generation and parsing of the
10+
various data types are done through the TypeConverter classes
11+
associated with the data types.
12+
13+
Example:
14+
15+
... ado.net/XML headers & schema ...
16+
<resheader name="resmimetype">text/microsoft-resx</resheader>
17+
<resheader name="version">2.0</resheader>
18+
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
19+
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
20+
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
21+
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
22+
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
23+
<value>[base64 mime encoded serialized .NET Framework object]</value>
24+
</data>
25+
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
26+
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
27+
<comment>This is a comment</comment>
28+
</data>
29+
30+
There are any number of "resheader" rows that contain simple
31+
name/value pairs.
32+
33+
Each data row contains a name, and value. The row also contains a
34+
type or mimetype. Type corresponds to a .NET class that support
35+
text/value conversion through the TypeConverter architecture.
36+
Classes that don't support this are serialized and stored with the
37+
mimetype set.
38+
39+
The mimetype is used for serialized objects, and tells the
40+
ResXResourceReader how to depersist the object. This is currently not
41+
extensible. For a given mimetype the value must be set accordingly:
42+
43+
Note - application/x-microsoft.net.object.binary.base64 is the format
44+
that the ResXResourceWriter will generate, however the reader can
45+
read any of the formats listed below.
46+
47+
mimetype: application/x-microsoft.net.object.binary.base64
48+
value : The object must be serialized with
49+
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
50+
: and then encoded with base64 encoding.
51+
52+
mimetype: application/x-microsoft.net.object.soap.base64
53+
value : The object must be serialized with
54+
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
55+
: and then encoded with base64 encoding.
56+
57+
mimetype: application/x-microsoft.net.object.bytearray.base64
58+
value : The object must be serialized into a byte array
59+
: using a System.ComponentModel.TypeConverter
60+
: and then encoded with base64 encoding.
61+
-->
62+
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
63+
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
64+
<xsd:element name="root" msdata:IsDataSet="true">
65+
<xsd:complexType>
66+
<xsd:choice maxOccurs="unbounded">
67+
<xsd:element name="metadata">
68+
<xsd:complexType>
69+
<xsd:sequence>
70+
<xsd:element name="value" type="xsd:string" minOccurs="0" />
71+
</xsd:sequence>
72+
<xsd:attribute name="name" use="required" type="xsd:string" />
73+
<xsd:attribute name="type" type="xsd:string" />
74+
<xsd:attribute name="mimetype" type="xsd:string" />
75+
<xsd:attribute ref="xml:space" />
76+
</xsd:complexType>
77+
</xsd:element>
78+
<xsd:element name="assembly">
79+
<xsd:complexType>
80+
<xsd:attribute name="alias" type="xsd:string" />
81+
<xsd:attribute name="name" type="xsd:string" />
82+
</xsd:complexType>
83+
</xsd:element>
84+
<xsd:element name="data">
85+
<xsd:complexType>
86+
<xsd:sequence>
87+
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
88+
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
89+
</xsd:sequence>
90+
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
91+
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
92+
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
93+
<xsd:attribute ref="xml:space" />
94+
</xsd:complexType>
95+
</xsd:element>
96+
<xsd:element name="resheader">
97+
<xsd:complexType>
98+
<xsd:sequence>
99+
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
100+
</xsd:sequence>
101+
<xsd:attribute name="name" type="xsd:string" use="required" />
102+
</xsd:complexType>
103+
</xsd:element>
104+
</xsd:choice>
105+
</xsd:complexType>
106+
</xsd:element>
107+
</xsd:schema>
108+
<resheader name="resmimetype">
109+
<value>text/microsoft-resx</value>
110+
</resheader>
111+
<resheader name="version">
112+
<value>2.0</value>
113+
</resheader>
114+
<resheader name="reader">
115+
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
116+
</resheader>
117+
<resheader name="writer">
118+
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
119+
</resheader>
120+
</root>
Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
Imports System.Collections.Specialized
2+
3+
Public Class MainForm
4+
5+
Dim Rand As New Random 'Random Number variable
6+
Dim StackNumbers() As Integer 'Numbers located on the stack
7+
Dim ValueResults As NameValueCollection = New NameValueCollection 'List of all possible values
8+
Dim OperationResults As NameValueCollection = New NameValueCollection 'What operations were done to get this output
9+
Dim Operations() As Integer 'List of current operations
10+
Dim PickList As List(Of Decimal) = New List(Of Decimal)
11+
12+
13+
'Switch the visibility of the answer text box
14+
Private Sub AnswerCheckBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AnswerCheckBox.CheckedChanged
15+
16+
AnswerTextBox.Visible = sender.checked
17+
18+
End Sub
19+
20+
'Avoid a range where the lower is greater than the higher (or vice versa)
21+
Private Sub LowRangeNumericUpDown_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LowRangeNumericUpDown.ValueChanged
22+
HighRangeNumericUpDown.Minimum = sender.Value + 1
23+
End Sub
24+
Private Sub HighRangeNumericUpDown_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HighRangeNumericUpDown.ValueChanged
25+
LowRangeNumericUpDown.Maximum = sender.Value - 1
26+
End Sub
27+
28+
Private Sub TargetMinNumericUpDown_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TargetMinNumericUpDown.ValueChanged
29+
TargetMaxNumericUpDown.Minimum = sender.value + 1
30+
End Sub
31+
Private Sub TargetMaxNumericUpDown_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TargetMaxNumericUpDown.ValueChanged
32+
TargetMinNumericUpDown.Maximum = sender.value - 1
33+
End Sub
34+
35+
'Generate puzzle
36+
Private Sub GenerateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GenerateButton.Click
37+
38+
Repick:
39+
'Reset the size of the array
40+
ReDim StackNumbers(StackSizeNumericUpDown.Value - 1)
41+
42+
'Pick the random numbers
43+
PickRandomNumbers()
44+
45+
'Cycle through all possible values
46+
GetAllOutputs()
47+
48+
'Find the output value to use
49+
PickList.Clear()
50+
For i = 0 To ValueResults.Count - 1
51+
'MsgBox(ValueResults.Item(i).ToString)
52+
Dim Count As Decimal = ValueResults.Item(i)
53+
Dim Tempitem As Decimal = ValueResults.GetKey(i)
54+
55+
'Only add the item if there is only 1 solution and the answer is an integer
56+
If (Count = 1 And (Math.Floor(Tempitem) = Math.Ceiling(Tempitem)) And (Tempitem <= TargetMaxNumericUpDown.Value) And (Tempitem >= TargetMinNumericUpDown.Value)) Then
57+
PickList.Add(Tempitem)
58+
End If
59+
60+
Next
61+
62+
'Make sure there is at least one value to pick from
63+
If PickList.Count = 0 Then GoTo Repick
64+
65+
'Choose a random number from 0 to count
66+
Dim Answer = Rand.Next(0, PickList.Count)
67+
68+
69+
'Export the target number
70+
TargetTextBox.Text = PickList.Item(Answer).ToString("n0")
71+
72+
'Get the operation and export to answer
73+
AnswerTextBox.Text = OperationResults.Item(PickList.Item(Answer).ToString).ToString
74+
75+
76+
'Export the list of numbers
77+
StackListBox.Items.Clear()
78+
For i = 0 To StackSizeNumericUpDown.Value - 1
79+
StackListBox.Items.Add(StackNumbers(i))
80+
Next
81+
82+
'Hide the answer
83+
AnswerCheckBox.Checked = False
84+
85+
End Sub
86+
87+
'Fills the stack numbers array with random numbers
88+
Sub PickRandomNumbers()
89+
90+
'Run the loop to pick a set of random numbers
91+
For i = 0 To StackSizeNumericUpDown.Value - 1 Step 1
92+
StackNumbers(i) = Rand.Next(LowRangeNumericUpDown.Value, HighRangeNumericUpDown.Value + 1)
93+
Next
94+
95+
End Sub
96+
97+
'Gets all possible target number outputs
98+
Sub GetAllOutputs()
99+
100+
'Resize the array to keep track of the operations
101+
'0 = +
102+
'1 = -
103+
'2 = *
104+
'3 = /
105+
ReDim Operations(StackSizeNumericUpDown.Value - 2)
106+
107+
'Clear the list of all possible values/operations
108+
ValueResults.Clear()
109+
OperationResults.Clear()
110+
111+
Dim EndArray As Boolean = False 'When to stop the loop
112+
113+
'Run the loop through all iterations
114+
Do Until EndArray = True
115+
116+
Dim Update = CalculateValue()
117+
118+
'Find if the value exists in the collection and increase the counter
119+
Try
120+
ValueResults.Item(Update.ToString) += 1
121+
Catch ex As Exception
122+
ValueResults.Add(Update.ToString, "1")
123+
End Try
124+
125+
'Keep track of the operation used to get this number
126+
Dim OperationString As String = "" 'Temp string variable
127+
For i = 0 To StackSizeNumericUpDown.Value - 2
128+
OperationString += GetOperationChar(Operations(i))
129+
Next
130+
131+
Try
132+
OperationResults.Item(Update.ToString) = OperationString.ToString
133+
Catch ex As Exception
134+
OperationResults.Add(Update.ToString, OperationString.ToString)
135+
End Try
136+
137+
'Update the operations array
138+
EndArray = UpdateOperations()
139+
140+
Loop
141+
142+
143+
144+
145+
End Sub
146+
147+
'Calculate the value from the stack array and operations list
148+
Function CalculateValue()
149+
150+
'Calculates the value associated with the current operations
151+
Dim Output As Decimal = StackNumbers(0) 'Output can be decimal
152+
153+
'Loop through operations
154+
For i = 0 To StackSizeNumericUpDown.Value - 2 Step 1
155+
Select Case Operations(i)
156+
Case 0
157+
Output += StackNumbers(i + 1)
158+
Case 1
159+
Output -= StackNumbers(i + 1)
160+
Case 2
161+
Output *= StackNumbers(i + 1)
162+
Case 3
163+
Output /= StackNumbers(i + 1)
164+
End Select
165+
166+
Next
167+
168+
Return Output
169+
170+
End Function
171+
172+
'Update the operations
173+
Function UpdateOperations()
174+
175+
Dim Break As Boolean = False
176+
Dim i As Integer = 0 'Index in the array
177+
Dim ReturnType As Boolean = False
178+
179+
Do Until Break = True
180+
181+
If Operations(i) < 3 Then
182+
Operations(i) += 1
183+
Break = True
184+
ReturnType = False
185+
Else
186+
Operations(i) = 0
187+
i += 1
188+
189+
'Run the loop until there are no more operations
190+
If i = StackSizeNumericUpDown.Value - 1 Then
191+
Break = True
192+
ReturnType = True
193+
End If
194+
195+
End If
196+
197+
Loop
198+
199+
Return ReturnType
200+
201+
End Function
202+
203+
'Returns the ascii character for the operation
204+
Function GetOperationChar(ByVal input As Integer)
205+
Select Case input
206+
Case 0
207+
Return "+"
208+
Case 1
209+
Return "-"
210+
Case 2
211+
Return "*"
212+
Case 3
213+
Return "/"
214+
Case Else
215+
Return " "
216+
End Select
217+
End Function
218+
219+
220+
221+
'Copy text to clipboard
222+
Private Sub CopyButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyButton.Click
223+
224+
'Make sure a puzzle is generated first
225+
If Not StackListBox.Items.Count < 1 Then
226+
227+
Dim str As String = ""
228+
229+
'Add the stack to the string
230+
str += "Stack Numbers:" & Environment.NewLine
231+
For i = 0 To StackListBox.Items.Count - 1
232+
str += StackListBox.Items.Item(i) & Environment.NewLine
233+
Next
234+
235+
'Add the target number
236+
str += Environment.NewLine & "Target Number:" & Environment.NewLine & TargetTextBox.Text.ToString
237+
238+
'If the answer is visible, add the answer
239+
If AnswerCheckBox.Checked = True Then
240+
str += Environment.NewLine & Environment.NewLine & "Answer:" & Environment.NewLine & AnswerTextBox.Text.ToString
241+
End If
242+
243+
Clipboard.SetText(str)
244+
Else
245+
MessageBox.Show("Please generate a puzzle first.", "Generate Puzzle First", MessageBoxButtons.OK, MessageBoxIcon.Information)
246+
End If
247+
248+
End Sub
249+
250+
251+
End Class

0 commit comments

Comments
 (0)