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 |
Imports System.Security.Permissions Public Class Form1 Dim Deffinitons As ArrayList Dim myRandom As New Random() Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load LoadExnayWordList() ComboBox2.Items.Add("1 - Few Blanks") ComboBox2.Items.Add("2 - Here and there Blanks") ComboBox2.Items.Add("3 - Alot of Blanks") ComboBox2.SelectedIndex = 0 DownloadDefinitions("torts.txt") DownloadDefinitions("criminal.txt") DownloadDefinitions("contracts.txt") ComboBox1.Items.AddRange(IO.Directory.GetFiles(".", "*.txt")) If ComboBox1.Items.Count > 0 Then ComboBox1.SelectedIndex = 1 End If Return For Each MyFile In IO.Directory.GetFiles(".", "*.txt") Debug.WriteLine(MyFile) ComboBox1.Items.Add(MyFile) Next End Sub Private Function DownloadDefinitions(ByVal FileDef As String) If Not IO.File.Exists(".\" & FileDef) And System.Security.SecurityManager.IsGranted(New System.Security.Permissions.FileIOPermission(FileIOPermissionAccess.Write, System.IO.Directory.GetCurrentDirectory() & "\" & FileDef)) Then My.Computer.Network.DownloadFile("http://controllingtheinter.net/dcaupload/" & FileDef, ".\ " & FileDef) End If End Function Private Function LoadDefinitons(ByVal FilePath As String) As ArrayList Deffinitons = New ArrayList Dim Data As String = IO.File.ReadAllText(FilePath) Dim SplitString As String = vbCrLf & vbCrLf Dim Deffs() As String = Data.Split({SplitString}, StringSplitOptions.RemoveEmptyEntries) For Each MyString In Deffs Debug.WriteLine("[" & MyString & " ]") Deffinitons.Add(MyString) Next End Function Private Function GrabRandomDef() As String Return Deffinitons.Item(myRandom.Next(0, Deffinitons.Count - 1)) End Function Dim GlobalAnswer As String = Nothing Private Function FillInTheBlank() As Boolean Dim WasABlankFilledByChange As Boolean = False GlobalAnswer = GrabRandomDef() Dim DefSplit() As String = GlobalAnswer.Split(" ") While Not WasABlankFilledByChange 'Debug.WriteLine("Cycle") For i = 2 To DefSplit.Length - 1 If RandomChance(15) And Not DefSplit(i).ToString.Contains(vbCrLf) And CheckForAlphaNumericCharacters(DefSplit(i).ToString) And Not ExnayWordList.Contains(DefSplit(i).ToString) Then 'Debug.WriteLine("True") DefSplit(i) = StrDup(DefSplit(i).Length, New String("_")) WasABlankFilledByChange = True Else 'Debug.WriteLine("false") End If Next End While Dim Final As String = String.Join(" ", DefSplit) RichTextBox1.Text = Final Debug.WriteLine(Final) Application.DoEvents() MakeFirstWordRed() Return WasABlankFilledByChange End Function Dim ExnayWordList As New ArrayList Private Sub LoadExnayWordList() ExnayWordList.AddRange({"a", "an", "as", "at", "and", "is", "to", "in", "if", "by", "the", "or", "on", "when", "were"}) End Sub Function CheckForAlphaNumericCharacters(ByVal StringToCheck As String) For i = 0 To StringToCheck.Length - 1 If Not Char.IsLetter(StringToCheck.Chars(i)) And Not IsNumeric(StringToCheck.Chars(i)) Then Return False End If Next Return True 'Return true if all elements are characters End Function Private Function RandomChance(PercentChangeToBeTrue As Integer) As Boolean 'Passing the value 10 gives a 10% change to be true Dim RandomINT As Integer = myRandom.Next(0, 100) If ComboBox2.Text.StartsWith("1") Then PercentChangeToBeTrue = 15 End If If ComboBox2.Text.StartsWith("2") Then PercentChangeToBeTrue = 30 End If If ComboBox2.Text.StartsWith("3") Then PercentChangeToBeTrue = 50 End If 'Debug.WriteLine(RandomINT) If RandomINT < PercentChangeToBeTrue Then Return True Else Return False End If RandomINT = 0 End Function Private Sub MakeFirstWordRed() Dim DefSplit() As String = RichTextBox1.Text.Split(" ") Dim StartingIndex As Integer = 0 For Each MyString In DefSplit If MyString.StartsWith("_") Then Dim returnFirstPos = RichTextBox1.Find(MyString) 'Debug.WriteLine(returnFirstPos) RichTextBox1.Select(returnFirstPos, MyString.Length) RichTextBox1.SelectionColor = Color.Red RichTextBox1.SelectionStart = returnFirstPos 'RichTextBox1.ReadOnly = True RichTextBox1.Focus() Return End If Next End Sub Private Sub WriteFirstLetter() Dim returnFirstPos = RichTextBox1.Find("_") If returnFirstPos = -1 Then RichTextBox1.ReadOnly = True Return Else RichTextBox1.ReadOnly = False End If 'Debug.WriteLine(returnFirstPos) RichTextBox1.Select(returnFirstPos, 1) RichTextBox1.SelectionColor = Color.Red RichTextBox1.SelectionStart = returnFirstPos End Sub Private Sub RichTextBox1_SelectionChanged(sender As Object, e As EventArgs) Handles RichTextBox1.SelectionChanged End Sub Private Sub RichTextBox1_Enter(sender As Object, e As EventArgs) Handles RichTextBox1.Enter WriteFirstLetter() End Sub Private Sub RichTextBox1_LocationChanged(sender As Object, e As EventArgs) Handles RichTextBox1.LocationChanged End Sub Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged WriteFirstLetter() If RichTextBox1.Find("_") = -1 Then If StrComp(RichTextBox1.Text, GlobalAnswer, CompareMethod.Text) = 0 Then Debug.WriteLine("Got it!") ListBox1.Items.Add("Correct: " & GlobalAnswer.Replace(vbCrLf, "[crlf]")) Else ListBox1.Items.Add("Q: " & RichTextBox1.Text.Replace(vbCrLf, "[crlf]")) ListBox1.Items.Add("A: " & GlobalAnswer.Replace(vbCrLf, "[crlf]")) Debug.WriteLine("failed!") End If FillInTheBlank() RichTextBox1.ReadOnly = False End If End Sub Private Sub RichTextBox1_CursorChanged(sender As Object, e As EventArgs) Handles RichTextBox1.CursorChanged WriteFirstLetter() End Sub Private Sub RichTextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles RichTextBox1.KeyPress 'Debug.WriteLine("Back2" & e.KeyChar) 'WriteFirstLetter() End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click FillInTheBlank() End Sub Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged LoadDefinitons(ComboBox1.SelectedItem) FillInTheBlank() End Sub Private Sub RichTextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles RichTextBox1.KeyDown If e.KeyData = Keys.Back Then e.Handled = True BackSpace() Else WriteFirstLetter() End If End Sub Private Sub BackSpace() Dim returnFirstPos = RichTextBox1.Find("_") If returnFirstPos = -1 Then RichTextBox1.ReadOnly = True Return Else RichTextBox1.ReadOnly = False End If For i = (returnFirstPos - 1) To 1 Step -1 RichTextBox1.Select(i, 1) 'Debug.WriteLine(i & ": " & RichTextBox1.SelectionColor.ToString) If RichTextBox1.SelectionColor = Color.Red Then RichTextBox1.Select(i, 1) RichTextBox1.SelectionStart = i RichTextBox1.SelectedText = "_" Exit For End If Next WriteFirstLetter() End Sub End Class |