|
| 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