Advertisement
Roland-2

Untitled

May 17th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 8.39 KB | None | 0 0
  1. Imports System.IO
  2.  
  3. 'PseudoCode
  4. ' Solve 3 way quandary, List of Words
  5.  
  6. 'Get a Selection of Unused elements -(CountToBeUsed)
  7. 'Selection of Element are not all Used, Return Only Used (Class WordGroup)
  8. 'Use all Elements, Reset All Used - Manual & Auto (GetWordAutoReset() As String)   'Add a form Msg "All Words Reset"
  9.  
  10. 'Ex1 - Ex2 - Return Some Ellapsed Time
  11.  
  12.  
  13.  
  14. Public Class Form1
  15.  
  16.  
  17.  
  18.     Private m_wordGroups2 As List(Of WordGroup) = New List(Of WordGroup)()
  19.     Dim allNumbers As New List(Of Integer)
  20.     Dim selectedNumbers As New List(Of Integer)
  21.  
  22.     Dim CountToBeUsed As Integer
  23.  
  24.     Dim rand As New Random
  25.  
  26.     Public UseStreamreader As Boolean = False
  27.  
  28.  
  29.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  30.  
  31.         If UseStreamreader = True Then
  32.             Dim path2 As String = Application.StartupPath & "\Def2.txt"
  33.             m_wordGroups2.Add(New WordGroup(path2))
  34.  
  35.         Else
  36.             Dim path1 As String = "one two three four five six seven eight nine"
  37.             m_wordGroups2.Add(New WordGroup(path1))
  38.         End If
  39.  
  40.     End Sub
  41.  
  42.  
  43.     Private Sub rndword()
  44.  
  45.         CountToBeUsed = 5
  46.  
  47.         'Generic list for holding the lines  
  48.  
  49.         allNumbers.Clear()
  50.         selectedNumbers.Clear()
  51.  
  52.         For i As Integer = 0 To m_wordGroups2(0).Count - 1
  53.             If Not m_wordGroups2(0).m_wordList(i).Used Then
  54.                 allNumbers.Add(i)
  55.             End If
  56.  
  57.         Next
  58.  
  59.         If allNumbers.Count > CountToBeUsed Then
  60.  
  61.             For i As Integer = 0 To CountToBeUsed
  62.  
  63.                 Dim index As Integer = rand.Next(0, allNumbers.Count)
  64.                 Dim selectedNumber As Integer = allNumbers(index)
  65.  
  66.                 selectedNumbers.Add(selectedNumber)
  67.  
  68.                 allNumbers.RemoveAt(index)
  69.  
  70.             Next
  71.  
  72.             For i As Integer = 0 To selectedNumbers.Count - 1
  73.  
  74.                 ListBox1.Items.Add(selectedNumbers(i).ToString)
  75.  
  76.             Next
  77.  
  78.             ListBox1.Items.Add(" end")
  79.         Else
  80.             'Get All
  81.  
  82.             CountToBeUsed = allNumbers.Count - 1
  83.  
  84.             For i As Integer = 0 To allNumbers.Count - 1
  85.                 selectedNumbers.Add(allNumbers(i))
  86.  
  87.             Next
  88.  
  89.             For i As Integer = 0 To selectedNumbers.Count - 1
  90.  
  91.                 ListBox1.Items.Add(selectedNumbers(i).ToString)
  92.  
  93.             Next
  94.             ListBox1.Items.Add(" end fin")
  95.         End If
  96.  
  97.  
  98.     End Sub
  99.  
  100.     ' Load file
  101.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles BtnAutoHandle.Click
  102.  
  103.         rndword()
  104.         ' Manual Handle
  105.         If Not m_wordGroups2(0).CountUsedWords() = m_wordGroups2(0).Count Then
  106.             Dim scramble As String = m_wordGroups2(0).GetRandomWord()
  107.             ListBox2.Items.Add(scramble)
  108.         Else
  109.             m_wordGroups2(0).ResetAllWords()
  110.             MsgBox("All Words Used")
  111.             Dim scramble As String = m_wordGroups2(0).GetRandomWord()
  112.             ListBox2.Items.Add(scramble)
  113.         End If
  114.  
  115.     End Sub
  116.  
  117.     Private Sub BtnTest_Click(sender As Object, e As EventArgs) Handles BtnTest.Click
  118.  
  119.         allNumbers.Clear()
  120.  
  121.         For i As Integer = 0 To m_wordGroups2(0).Count - 1
  122.             If Not m_wordGroups2(0).m_wordList(i).Used Then
  123.                 allNumbers.Add(i)
  124.             End If
  125.  
  126.         Next
  127.  
  128.         Dim Index As Integer = rand.Next(0, allNumbers.Count)
  129.         Dim GetIndexValue As Integer = allNumbers(Index)
  130.         m_wordGroups2(0).m_wordList(GetIndexValue).Used = True
  131.  
  132.         TextBox2.AppendText(GetIndexValue.ToString & " ")
  133.  
  134.     End Sub
  135. End Class
  136.  
  137.  
  138. Public Class WordGroup
  139.     Private m_letterCount As Integer = 0
  140.  
  141.     Public m_wordList As New List(Of WordItem)(1)
  142.     Private m_trackUsedWords As Boolean = True
  143.  
  144.     Private m_highEnd As Integer = -1
  145.  
  146.     Private m_currentWord As String = ""
  147.  
  148. #Region "Read-only properties"
  149.     Public ReadOnly Property Count() As Integer
  150.         Get
  151.             Return m_wordList.Count
  152.         End Get
  153.     End Property
  154.     Public ReadOnly Property CountUsed() As Integer
  155.         Get
  156.             Return CountUsedWords()
  157.         End Get
  158.     End Property
  159.  
  160.     Public ReadOnly Property CurrentWord() As String
  161.         Get
  162.             Return m_currentWord
  163.         End Get
  164.     End Property
  165. #End Region
  166.  
  167. #Region "Non-read-only properties"
  168.     Public Property TrackRepeats() As Boolean
  169.         Get
  170.             Return m_trackUsedWords
  171.         End Get
  172.         Set(ByVal value As Boolean)
  173.             m_trackUsedWords = value
  174.         End Set
  175.     End Property
  176. #End Region
  177.  
  178. #Region "Constructors"
  179.  
  180.     Public Sub New(ByVal path As String)
  181.         m_wordList.Clear()
  182.         If Form1.UseStreamreader = True Then
  183.  
  184.             LoadFile(path)
  185.         Else
  186.             LoadFile2(path)
  187.  
  188.         End If
  189.  
  190.  
  191.     End Sub
  192. #End Region
  193.  
  194.     Public Function CountUsedWords() As Integer
  195.         Dim used As Integer = 0
  196.  
  197.         For i As Integer = 0 To m_wordList.Count - 1
  198.             If m_wordList(i).Used Then
  199.                 used += 1
  200.             End If
  201.         Next i
  202.  
  203.         Return used
  204.     End Function
  205.  
  206.     Public Function LoadFile(ByVal fileName As String) As Boolean
  207.         Dim success As Boolean = False
  208.         Try
  209.             Dim stream As New FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)
  210.             Dim reader As New StreamReader(stream)
  211.             Dim words As String
  212.             Do While Not reader.EndOfStream
  213.                 words = reader.ReadLine()
  214.                 If words.Length > 0 Then
  215.                     Dim wordsplit() As String = words.Split(vbNewLine)
  216.                     For i As Integer = 0 To wordsplit.Length - 1
  217.                         Dim item As New WordItem(wordsplit(i).ToLower())
  218.                         m_wordList.Add(item)
  219.                     Next i
  220.                 End If
  221.             Loop
  222.             success = True
  223.         Catch e As Exception
  224.             If e IsNot Nothing Then
  225.             End If
  226.         End Try
  227.  
  228.         Return success
  229.     End Function
  230.  
  231.     Public Function LoadFile2(ByVal words As String) As Boolean
  232.         Dim success As Boolean = False
  233.         Try
  234.  
  235.  
  236.             If words.Length > 0 Then
  237.                 Dim wordsplit() As String = words.Split(" ")
  238.             For i As Integer = 0 To wordsplit.Length - 1
  239.                 Dim item As New WordItem(wordsplit(i).ToLower())
  240.                 m_wordList.Add(item)
  241.             Next i
  242.             End If
  243.  
  244.             success = True
  245.         Catch e As Exception
  246.             If e IsNot Nothing Then
  247.             End If
  248.         End Try
  249.  
  250.         Return success
  251.     End Function
  252.  
  253.     Public Sub ResetAllWords()
  254.  
  255.         For x As Integer = 0 To m_wordList.Count - 1
  256.  
  257.             If m_wordList(x).Used Then
  258.  
  259.                 m_wordList(x).Used = False
  260.             End If
  261.         Next
  262.  
  263.     End Sub
  264.  
  265.     Public Function GetWordAutoReset() As String
  266.  
  267.         Dim randomWord As String = ""
  268.  
  269.         If Not CountUsed = m_wordList.Count Then
  270.             randomWord = GetRandomWord()
  271.             Return randomWord
  272.  
  273.         Else
  274.             ResetAllWords()
  275.             '
  276.             randomWord = GetRandomWord()
  277.             Return randomWord
  278.  
  279.         End If
  280.  
  281.     End Function
  282.  
  283.     Public Function GetRandomWord() As String
  284.         If m_wordList.Count = 0 Then
  285.             Return ""
  286.         End If
  287.  
  288.         Dim rndm As New Random
  289.         ' get the word we think we want to use
  290.         Dim randomWord As String = ""
  291.         Do While randomWord = ""
  292.             Dim index As Integer = rndm.Next(0, m_wordList.Count)
  293.             If (Not m_wordList(index).Used) Then
  294.                 randomWord = m_wordList(index).Text
  295.  
  296.                 m_wordList(index).Used = True
  297.  
  298.             End If
  299.         Loop
  300.  
  301.         Return randomWord
  302.     End Function
  303.  
  304.  
  305.  
  306. End Class
  307.  
  308.  
  309. Public Class WordItem
  310.     Private m_text As String = ""
  311.     Private m_used As Boolean = False
  312.     Public ReadOnly Property Text() As String
  313.         Get
  314.             Return m_text
  315.         End Get
  316.     End Property
  317.     Public Property Used() As Boolean
  318.         Get
  319.             Return m_used
  320.         End Get
  321.         Set(ByVal value As Boolean)
  322.             m_used = value
  323.         End Set
  324.     End Property
  325.  
  326.     '--------------------------------------------------------------------------------
  327.     Public Sub New(ByVal text As String)
  328.         m_text = text
  329.         m_used = False
  330.     End Sub
  331.  
  332. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement