Math Problem & ListView

Recommended Posts

I can't seem to figure out what is wrong with the random winner button.

I could just choose a random number and make that person the winner, but then it wouldn't be fair to the people who bought more than 1 ticket... so I made an array and put the name in different spots in the array for however many tickets they had (or at least thats what I tried to do), but for some reason \$Runners[Random] = "", and I can't figure out why. If you can shed some light on the reason for this, that would be great!

Script:

CODE
```#include <GuiConstants.au3>
#include<GuiListView.au3>

GuiCreate("Lottery Calculator", 316, 266,-1, -1 , BitOR(\$WS_OVERLAPPEDWINDOW, \$WS_CLIPSIBLINGS))

\$List = GuiCtrlCreateListView("Player                   | Tickets", 20, 20, 170, 110)
\$AddButton = GuiCtrlCreateButton("Add", 110, 160, 60, 30)
\$RemoveButton = GuiCtrlCreateButton("Remove...", 210, 110, 60, 30)
\$NameInput = GuiCtrlCreateInput("Name", 20, 150, 70, 20)
\$TickInput = GuiCtrlCreateInput("# of tickets", 20, 180, 70, 20)
\$Calc = GuiCtrlCreateButton("Calculate Winnings", 210, 40, 100, 30)
\$ExitButton = GuiCtrlCreateButton("Exit", 250, 230, 60, 30)
\$SaveButton = GuiCtrlCreateButton("Save to INI", 10, 230, 70, 30)
\$LoadButton = GuiCtrlCreateButton("Load from INI", 90, 230, 80, 30)
\$WinnerButton = GUICtrlCreateButton("Pick Random Winner!", 190, 180, 120, 30)

GuiSetState()

While 1
\$msg = GuiGetMsg()
Select
Case \$msg = \$GUI_EVENT_CLOSE or \$msg = \$ExitButton
ExitLoop
Case \$msg = \$AddButton

GUICtrlCreateListViewItem(\$AddName & "|" & \$TickNumb, \$List)

Case \$msg = \$RemoveButton
\$Name = _GUICtrlListViewGetItemText(\$List,_GUICtrlListViewGetSelectedIndices(\$List), 0)
\$Ticks = _GUICtrlListViewGetItemText(\$List,_GUICtrlListViewGetSelectedIndices(\$List), 1)

If \$Removee = 0 Then
MsgBox(0, "Error", "You need to select someone from the list before pressing this button")
Else
\$Answer = MsgBox(4, "Removing...", "Are you sure you want to remove " & \$Name & " who has " & \$Ticks & " Tickets?")
If \$Answer = 6 Then
_GUICtrlListViewDeleteItem(\$List, \$Removee)
EndIf
EndIf

Case \$msg = \$Calc
Dim \$TotalTicks = 0;\$TotalTicks is the total number of tickets

For \$x = 0 to _GUICtrlListViewGetItemCount(\$List); - 1
\$TotalTicks += _GUICtrlListViewGetItemText(\$List, 0 + \$x, 1)
Next

\$WinningsGUI = GuiCreate("", 160, 100,-1, -1 , BitOR(\$WS_OVERLAPPEDWINDOW, \$WS_CLIPSIBLINGS))
\$CostLabel = GuiCtrlCreateLabel("Cost of Each Ticket?", 20, 10, 110, 20)
\$GoldInput = GuiCtrlCreateInput("Gold", 10, 40, 30, 20)
\$SilverInput = GuiCtrlCreateInput("Silver", 50, 40, 40, 20)
\$CopperInput = GuiCtrlCreateInput("Copper", 100, 40, 40, 20)
\$OKButton = GUICtrlCreateButton("OK", 20, 70, 25, 20)
\$CancelButton = GUICtrlCreateButton("Cancel", 80, 70, 45, 20)
GuiSetState()
While 1
\$msg = GuiGetMsg()
Select
Case \$msg = \$GUI_EVENT_CLOSE or \$msg = \$CancelButton
ExitLoop
Case \$msg = \$OKButton
\$Gold = GUICtrlRead(\$GoldInput) * \$TotalTicks
\$Silver = GUICtrlRead(\$SilverInput) * \$TotalTicks
\$Copper = GUICtrlRead(\$CopperInput) * \$TotalTicks

If \$Copper >= 100 Then
Do
\$Copper -= 100
\$Silver += 1
Until \$Copper < 100
EndIf

If \$Silver >= 100 Then
Do
\$Silver -= 100
\$Gold += 1
Until \$Silver < 100
EndIf

MsgBox(0,"winner money", "total money for the winner: " & \$Gold & "g " & \$Silver & "s " & \$Copper & "c")
GUIDelete(\$WinningsGUI)
ExitLoop

Case Else
;;;
EndSelect
WEnd
Case \$msg = \$SaveButton

Case \$msg = \$LoadButton

Case \$msg = \$WinnerButton
Dim \$Runners[\$TotalTicks], \$Tick

Dim \$TotalTicks = 0;\$TotalTicks is the total number of tickets

For \$x = 0 to _GUICtrlListViewGetItemCount(\$List) - 1
\$TotalTicks += _GUICtrlListViewGetItemText(\$List, 0 + \$x, 1)

Next;get numb of tickets

For \$x = 0 To _GUICtrlListViewGetItemCount(\$List) - 1
\$Tick += _GUICtrlListViewGetItemText(\$List, \$x, 1)
;Msgbox(0, "\$Tick", \$Tick)
For \$y = \$Tick - _GUICtrlListViewGetItemText(\$List, \$x, 1)  To \$Tick
\$Runners[\$y] = _GUICtrlListViewGetItemText(\$List, \$x, 0)
Next
Next

\$Random = Random(0, \$TotalTicks - 1, 1)
MsgBox(0, "Winner!", "THE WINNER IS " & \$Runners[\$Random] & " \$Random = " & \$Random)
Case Else
;;;
EndSelect
WEnd
Exit```
Edited by Kickassjoe

What goes around comes around... Payback's a bitch.

Share on other sites

I've solved this problem, by creating an extra array which holds a name each ticket, and by changing the array as the listview is updated you always have two copies of the same data. Each of them, suitable for a different purpose (calculations || display)..

CODE
```#include <GuiConstants.au3>
#include <GuiListView.au3>
#include <Array.au3>

Dim \$Contestants[1]

GuiCreate("Lottery Calculator", 316, 266,-1, -1 , BitOR(\$WS_OVERLAPPEDWINDOW, \$WS_CLIPSIBLINGS))

\$List = GuiCtrlCreateListView("Player               | Tickets", 20, 20, 170, 110)
\$AddButton = GuiCtrlCreateButton("Add", 110, 160, 60, 30)
\$RemoveButton = GuiCtrlCreateButton("Remove...", 210, 110, 60, 30)
\$NameInput = GuiCtrlCreateInput("Name", 20, 150, 70, 20)
\$TickInput = GuiCtrlCreateInput("# of tickets", 20, 180, 70, 20, \$ES_NUMBER)
\$Calc = GuiCtrlCreateButton("Calculate Winnings", 210, 40, 100, 30)
\$ExitButton = GuiCtrlCreateButton("Exit", 250, 230, 60, 30)
\$SaveButton = GuiCtrlCreateButton("Save to INI", 10, 230, 70, 30)
\$LoadButton = GuiCtrlCreateButton("Load from INI", 90, 230, 80, 30)
\$WinnerButton = GUICtrlCreateButton("Pick Random Winner!", 190, 180, 120, 30)

GuiSetState()

While 1
\$msg = GuiGetMsg()
Select
Case \$msg = \$GUI_EVENT_CLOSE or \$msg = \$ExitButton
ExitLoop
Case \$msg = \$AddButton

GUICtrlCreateListViewItem(\$AddName & "|" & \$TickNumb, \$List)
#region NEW
For \$x = 1 To \$TickNumb
Next
_ArrayDisplay(\$Contestants,"\$Contestants for testing purposes!!") ; REMOVE THIS LINE
#endregion

Case \$msg = \$RemoveButton
\$Name = _GUICtrlListViewGetItemText(\$List,_GUICtrlListViewGetSelectedIndices(\$List), 0)
\$Ticks = _GUICtrlListViewGetItemText(\$List,_GUICtrlListViewGetSelectedIndices(\$List), 1)

If \$Removee = 0 Then
MsgBox(0, "Error", "You need to select someone from the list before pressing this button")
Else
\$Answer = MsgBox(4, "Removing...", "Are you sure you want to remove " & \$Name & " who has " & \$Ticks & " Tickets?")
If \$Answer = 6 Then
_GUICtrlListViewDeleteItem(\$List, \$Removee)
#region NEW
Dim \$i = 0
For \$x = 1 to UBound(\$Contestants)-1
If \$Contestants[\$x] = \$Name Then
_ArrayDelete(\$Contestants,\$x)
\$x -= 1
\$i += 1
If \$i = \$Ticks Then
ExitLoop
EndIf
EndIf
Next
_ArrayDisplay(\$Contestants,"\$Contestants for testing purposes!!") ; REMOVE THIS LINE
#endregion
EndIf
EndIf

Case \$msg = \$Calc
Dim \$TotalTicks = 0;\$TotalTicks is the total number of tickets

For \$x = 0 to _GUICtrlListViewGetItemCount(\$List); - 1
\$TotalTicks += _GUICtrlListViewGetItemText(\$List, 0 + \$x, 1)
Next

\$WinningsGUI = GuiCreate("", 160, 100,-1, -1 , BitOR(\$WS_OVERLAPPEDWINDOW, \$WS_CLIPSIBLINGS))
\$CostLabel = GuiCtrlCreateLabel("Cost of Each Ticket?", 20, 10, 110, 20)
\$GoldInput = GuiCtrlCreateInput("Gold", 10, 40, 30, 20)
\$SilverInput = GuiCtrlCreateInput("Silver", 50, 40, 40, 20)
\$CopperInput = GuiCtrlCreateInput("Copper", 100, 40, 40, 20)
\$OKButton = GUICtrlCreateButton("OK", 20, 70, 25, 20)
\$CancelButton = GUICtrlCreateButton("Cancel", 80, 70, 45, 20)
GuiSetState()
While 1
\$msg = GuiGetMsg()
Select
Case \$msg = \$GUI_EVENT_CLOSE or \$msg = \$CancelButton
ExitLoop
Case \$msg = \$OKButton
\$Gold = GUICtrlRead(\$GoldInput) * \$TotalTicks
\$Silver = GUICtrlRead(\$SilverInput) * \$TotalTicks
\$Copper = GUICtrlRead(\$CopperInput) * \$TotalTicks

If \$Copper >= 100 Then
Do
\$Copper -= 100
\$Silver += 1
Until \$Copper < 100
EndIf

If \$Silver >= 100 Then
Do
\$Silver -= 100
\$Gold += 1
Until \$Silver < 100
EndIf

MsgBox(0,"winner money", "total money for the winner: " & \$Gold & "g " & \$Silver & "s " & \$Copper & "c")
GUIDelete(\$WinningsGUI)
ExitLoop

Case Else
;;;
EndSelect
WEnd
Case \$msg = \$SaveButton

Case \$msg = \$LoadButton

Case \$msg = \$WinnerButton
#region NEW
MsgBox(0, "Winner!", "THE WINNER IS " & \$Contestants[Random(1, UBound(\$Contestants),1)])
#endregion
Case Else
;;;
EndSelect
WEnd
Exit```

Edit: What is so mathematical about this anyway? That is what got my interest in this topic..

Share on other sites

I guess it wasn't very mathematical... Eh, it was 5am when I posted, and I hadn't had any sleep...

Then I took a 2 hour power nap, and fixed the problem myself in around 5 mins...

CODE
```#include <GuiConstants.au3>
#include<GuiListView.au3>

GuiCreate("Lottery Calculator", 316, 266,-1, -1 , BitOR(\$WS_OVERLAPPEDWINDOW, \$WS_CLIPSIBLINGS))

\$List = GuiCtrlCreateListView("Player                    | Tickets", 20, 20, 170, 110)
\$AddButton = GuiCtrlCreateButton("Add", 110, 160, 60, 30)
\$RemoveButton = GuiCtrlCreateButton("Remove...", 210, 110, 60, 30)
\$NameInput = GuiCtrlCreateInput("Name", 20, 150, 70, 20)
\$TickInput = GuiCtrlCreateInput("# of tickets", 20, 180, 70, 20)
\$Calc = GuiCtrlCreateButton("Calculate Winnings", 210, 40, 100, 30)
\$ExitButton = GuiCtrlCreateButton("Exit", 250, 230, 60, 30)
\$SaveButton = GuiCtrlCreateButton("Save to INI", 10, 230, 70, 30)
\$LoadButton = GuiCtrlCreateButton("Load from INI", 90, 230, 80, 30)
\$WinnerButton = GUICtrlCreateButton("Pick Random Winner!", 190, 180, 120, 30)

GuiSetState()

While 1
\$msg = GuiGetMsg()
Select
Case \$msg = \$GUI_EVENT_CLOSE or \$msg = \$ExitButton
ExitLoop
Case \$msg = \$AddButton

GUICtrlCreateListViewItem(\$AddName & "|" & \$TickNumb, \$List)

Case \$msg = \$RemoveButton
\$Name = _GUICtrlListViewGetItemText(\$List,_GUICtrlListViewGetSelectedIndices(\$List), 0)
\$Ticks = _GUICtrlListViewGetItemText(\$List,_GUICtrlListViewGetSelectedIndices(\$List), 1)

If \$Removee = 0 Then
MsgBox(0, "Error", "You need to select someone from the list before pressing this button")
Else
\$Answer = MsgBox(4, "Removing...", "Are you sure you want to remove " & \$Name & " who has " & \$Ticks & " Tickets?")
If \$Answer = 6 Then
_GUICtrlListViewDeleteItem(\$List, \$Removee)
EndIf
EndIf

Case \$msg = \$Calc
Dim \$TotalTicks = 0;\$TotalTicks is the total number of tickets

For \$x = 0 to _GUICtrlListViewGetItemCount(\$List); - 1
\$TotalTicks += _GUICtrlListViewGetItemText(\$List, 0 + \$x, 1)
Next

\$WinningsGUI = GuiCreate("", 160, 100,-1, -1 , BitOR(\$WS_OVERLAPPEDWINDOW, \$WS_CLIPSIBLINGS))
\$CostLabel = GuiCtrlCreateLabel("Cost of Each Ticket?", 20, 10, 110, 20)
\$GoldInput = GuiCtrlCreateInput("Gold", 10, 40, 30, 20)
\$SilverInput = GuiCtrlCreateInput("Silver", 50, 40, 40, 20)
\$CopperInput = GuiCtrlCreateInput("Copper", 100, 40, 40, 20)
\$OKButton = GUICtrlCreateButton("OK", 20, 70, 25, 20)
\$CancelButton = GUICtrlCreateButton("Cancel", 80, 70, 45, 20)
GuiSetState()
While 1
\$msg = GuiGetMsg()
Select
Case \$msg = \$GUI_EVENT_CLOSE or \$msg = \$CancelButton
ExitLoop
Case \$msg = \$OKButton
\$Gold = GUICtrlRead(\$GoldInput) * \$TotalTicks
\$Silver = GUICtrlRead(\$SilverInput) * \$TotalTicks
\$Copper = GUICtrlRead(\$CopperInput) * \$TotalTicks

If \$Copper >= 100 Then
Do
\$Copper -= 100
\$Silver += 1
Until \$Copper < 100
EndIf

If \$Silver >= 100 Then
Do
\$Silver -= 100
\$Gold += 1
Until \$Silver < 100
EndIf

MsgBox(0,"winner money", "total money for the winner: " & \$Gold & "g " & \$Silver & "s " & \$Copper & "c")
GUIDelete(\$WinningsGUI)
ExitLoop

Case Else
;;;
EndSelect
WEnd
Case \$msg = \$SaveButton

Case \$msg = \$LoadButton

Case \$msg = \$WinnerButton

Dim \$TotalTicks = 0;\$TotalTicks is the total number of tickets

For \$x = 0 to _GUICtrlListViewGetItemCount(\$List) - 1
\$TotalTicks += _GUICtrlListViewGetItemText(\$List, 0 + \$x, 1)

Dim \$Runners[\$TotalTicks], \$Tick = 0

Next;get numb of tickets

For \$x = 0 To _GUICtrlListViewGetItemCount(\$List) - 1
\$Tick += _GUICtrlListViewGetItemText(\$List, \$x, 1)
;Msgbox(0, "\$Tick", \$Tick)
For \$y = \$Tick - _GUICtrlListViewGetItemText(\$List, \$x, 1)  To \$Tick - 1
;Msgbox(0, "\$y", \$y)
\$Runners[\$y] = _GUICtrlListViewGetItemText(\$List, \$x, 0)
Next
Next

\$Random = Random(0, \$TotalTicks - 1, 1)
MsgBox(0, "Winner!", "THE WINNER IS " & \$Runners[\$Random])
Case Else
;;;
EndSelect
WEnd
Exit```
Edited by Kickassjoe

What goes around comes around... Payback's a bitch.

Share on other sites

I guess it wasn't very mathematical... Eh, it was 5am when I posted, and I hadn't had any sleep...

Then I took a 2 hour power nap, and fixed the problem myself in around 5 mins...

Sure, whatever works for you.. No problem.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account