Imports System.IO
'PseudoCode
' Solve 3 way quandary, List of Words
'Get a Selection of Unused elements -(CountToBeUsed)
'Selection of Element are not all Used, Return Only Used (Class WordGroup)
'Use all Elements, Reset All Used - Manual & Auto (GetWordAutoReset() As String) 'Add a form Msg "All Words Reset"
'Ex1 - Ex2 - Return Some Ellapsed Time
Public Class Form1
Private m_wordGroups2 As List(Of WordGroup) = New List(Of WordGroup)()
Dim allNumbers As New List(Of Integer)
Dim selectedNumbers As New List(Of Integer)
Dim CountToBeUsed As Integer
Dim rand As New Random
Public UseStreamreader As Boolean = False
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If UseStreamreader = True Then
Dim path2 As String = Application.StartupPath & "\Def2.txt"
m_wordGroups2.Add(New WordGroup(path2))
Else
Dim path1 As String = "one two three four five six seven eight nine"
m_wordGroups2.Add(New WordGroup(path1))
End If
End Sub
Private Sub rndword()
CountToBeUsed = 5
'Generic list for holding the lines
allNumbers.Clear()
selectedNumbers.Clear()
For i As Integer = 0 To m_wordGroups2(0).Count - 1
If Not m_wordGroups2(0).m_wordList(i).Used Then
allNumbers.Add(i)
End If
Next
If allNumbers.Count > CountToBeUsed Then
For i As Integer = 0 To CountToBeUsed
Dim index As Integer = rand.Next(0, allNumbers.Count)
Dim selectedNumber As Integer = allNumbers(index)
selectedNumbers.Add(selectedNumber)
allNumbers.RemoveAt(index)
Next
For i As Integer = 0 To selectedNumbers.Count - 1
ListBox1.Items.Add(selectedNumbers(i).ToString)
Next
ListBox1.Items.Add(" end")
Else
'Get All
CountToBeUsed = allNumbers.Count - 1
For i As Integer = 0 To allNumbers.Count - 1
selectedNumbers.Add(allNumbers(i))
Next
For i As Integer = 0 To selectedNumbers.Count - 1
ListBox1.Items.Add(selectedNumbers(i).ToString)
Next
ListBox1.Items.Add(" end fin")
End If
End Sub
' Load file
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles BtnAutoHandle.Click
rndword()
' Manual Handle
If Not m_wordGroups2(0).CountUsedWords() = m_wordGroups2(0).Count Then
Dim scramble As String = m_wordGroups2(0).GetRandomWord()
ListBox2.Items.Add(scramble)
Else
m_wordGroups2(0).ResetAllWords()
MsgBox("All Words Used")
Dim scramble As String = m_wordGroups2(0).GetRandomWord()
ListBox2.Items.Add(scramble)
End If
End Sub
Private Sub BtnTest_Click(sender As Object, e As EventArgs) Handles BtnTest.Click
allNumbers.Clear()
For i As Integer = 0 To m_wordGroups2(0).Count - 1
If Not m_wordGroups2(0).m_wordList(i).Used Then
allNumbers.Add(i)
End If
Next
Dim Index As Integer = rand.Next(0, allNumbers.Count)
Dim GetIndexValue As Integer = allNumbers(Index)
m_wordGroups2(0).m_wordList(GetIndexValue).Used = True
TextBox2.AppendText(GetIndexValue.ToString & " ")
End Sub
End Class
Public Class WordGroup
Private m_letterCount As Integer = 0
Public m_wordList As New List(Of WordItem)(1)
Private m_trackUsedWords As Boolean = True
Private m_highEnd As Integer = -1
Private m_currentWord As String = ""
#Region "Read-only properties"
Public ReadOnly Property Count() As Integer
Get
Return m_wordList.Count
End Get
End Property
Public ReadOnly Property CountUsed() As Integer
Get
Return CountUsedWords()
End Get
End Property
Public ReadOnly Property CurrentWord() As String
Get
Return m_currentWord
End Get
End Property
#End Region
#Region "Non-read-only properties"
Public Property TrackRepeats() As Boolean
Get
Return m_trackUsedWords
End Get
Set(ByVal value As Boolean)
m_trackUsedWords = value
End Set
End Property
#End Region
#Region "Constructors"
Public Sub New(ByVal path As String)
m_wordList.Clear()
If Form1.UseStreamreader = True Then
LoadFile(path)
Else
LoadFile2(path)
End If
End Sub
#End Region
Public Function CountUsedWords() As Integer
Dim used As Integer = 0
For i As Integer = 0 To m_wordList.Count - 1
If m_wordList(i).Used Then
used += 1
End If
Next i
Return used
End Function
Public Function LoadFile(ByVal fileName As String) As Boolean
Dim success As Boolean = False
Try
Dim stream As New FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)
Dim reader As New StreamReader(stream)
Dim words As String
Do While Not reader.EndOfStream
words = reader.ReadLine()
If words.Length > 0 Then
Dim wordsplit() As String = words.Split(vbNewLine)
For i As Integer = 0 To wordsplit.Length - 1
Dim item As New WordItem(wordsplit(i).ToLower())
m_wordList.Add(item)
Next i
End If
Loop
success = True
Catch e As Exception
If e IsNot Nothing Then
End If
End Try
Return success
End Function
Public Function LoadFile2(ByVal words As String) As Boolean
Dim success As Boolean = False
Try
If words.Length > 0 Then
Dim wordsplit() As String = words.Split(" ")
For i As Integer = 0 To wordsplit.Length - 1
Dim item As New WordItem(wordsplit(i).ToLower())
m_wordList.Add(item)
Next i
End If
success = True
Catch e As Exception
If e IsNot Nothing Then
End If
End Try
Return success
End Function
Public Sub ResetAllWords()
For x As Integer = 0 To m_wordList.Count - 1
If m_wordList(x).Used Then
m_wordList(x).Used = False
End If
Next
End Sub
Public Function GetWordAutoReset() As String
Dim randomWord As String = ""
If Not CountUsed = m_wordList.Count Then
randomWord = GetRandomWord()
Return randomWord
Else
ResetAllWords()
'
randomWord = GetRandomWord()
Return randomWord
End If
End Function
Public Function GetRandomWord() As String
If m_wordList.Count = 0 Then
Return ""
End If
Dim rndm As New Random
' get the word we think we want to use
Dim randomWord As String = ""
Do While randomWord = ""
Dim index As Integer = rndm.Next(0, m_wordList.Count)
If (Not m_wordList(index).Used) Then
randomWord = m_wordList(index).Text
m_wordList(index).Used = True
End If
Loop
Return randomWord
End Function
End Class
Public Class WordItem
Private m_text As String = ""
Private m_used As Boolean = False
Public ReadOnly Property Text() As String
Get
Return m_text
End Get
End Property
Public Property Used() As Boolean
Get
Return m_used
End Get
Set(ByVal value As Boolean)
m_used = value
End Set
End Property
'--------------------------------------------------------------------------------
Public Sub New(ByVal text As String)
m_text = text
m_used = False
End Sub
End Class
Comments