|
-
Aug 30th, 2024, 11:30 PM
#1
Thread Starter
PowerPoster
Simpler Select Case maintenance for long list of cases
Took me way too long to think of this.
Remember that game engine I've been writing? Well it's come a long way since last time I posted anything about it.
But one of the things is long lists of Select Case statements that I keep editing - mostly adding to.
So instead of going from lowest number to highest, do just the opposite.
Now everything I need is at the top of the procedure instead of the bottom.
Code:
' Declarations.
Private Const NON_CONSECUTIVE_RANDOM_MULTIPLIER As Double = 0.75
Public Property Get GotBuffed() As String
Const MAX_CHOICE As Long = 17
Static nRecentChoice(1 To MAX_CHOICE * NON_CONSECUTIVE_RANDOM_MULTIPLIER) As Long
Dim nChoice As Long
nChoice = SelectNonConsecutiveRandomChoice(MAX_CHOICE, nRecentChoice)
Select Case nChoice
Case 17
GotBuffed = "It's dangerous to go alone. Take this."
Case 16
GotBuffed = "You've been hitting the gym!"
Case 15
GotBuffed = "Great job on those TPS reports!"
Case 14
GotBuffed = "You found the perfect balance between exploiting labor and resources for profit while selling the public an image of 'Corporate Responsibility'."
Case 13
' Etc.
End Select
End Property
Public Function SelectNonConsecutiveRandomChoice(ByRef NumChoices As Long, ByRef RecentChoices() As Long) As Long
Dim nRnd As Long
Dim n As Long
Dim s() As String
Do
nRnd = RollDie(NumChoices)
Loop While ArrayItemExistsLong(nRnd, RecentChoices)
ReDim s(LBound(RecentChoices) To UBound(RecentChoices))
For n = UBound(RecentChoices) To LBound(RecentChoices) + 1 Step -1
RecentChoices(n) = RecentChoices(n - 1)
s(n) = CStr(RecentChoices(n))
Next n
RecentChoices(LBound(RecentChoices)) = nRnd
s(LBound(RecentChoices)) = CStr(nRnd)
AddPlayerMessage vbCrLf & "Recent Choices: " & Join(s, ","), 0, True
SelectNonConsecutiveRandomChoice = nRnd
End Function
-
Aug 31st, 2024, 12:27 AM
#2
Re: Simpler Select Case maintenance for long list of cases
Feel like something like an array would be better...
-tg
-
Aug 31st, 2024, 02:16 AM
#3
Thread Starter
PowerPoster
Re: Simpler Select Case maintenance for long list of cases
Well, both approaches are "wrong" because it's really just resource data in this case an doesn't belong in the exe at all.
The content wasn't the point though.
It's just that over the years I've added a lot of things to existing select case blocks and never thought to do it this way. Have always had to scroll to the bottom to add a new thing.
-
Aug 31st, 2024, 10:12 AM
#4
Re: Simpler Select Case maintenance for long list of cases
 Originally Posted by techgnome
Feel like something like an array would be better...
-tg
A Dictionary is the best abstraction here.
-
Aug 31st, 2024, 11:01 AM
#5
Re: Simpler Select Case maintenance for long list of cases
 Originally Posted by Niya
A Dictionary is the best abstraction here.
Yep - initially filled from a matching Resorces-DB-Table at App-Startup.
A disconnected Recordset would be another nice container-alternative to an Array -
(no datacopying-loop into a Dictionary necessary, ... a Delete-Method id available on Rs as well).
Olaf
-
Aug 31st, 2024, 11:50 AM
#6
Re: Simpler Select Case maintenance for long list of cases
I like that idea. A Recordset is even better in cases where you may want more fields than just a key/value pair.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|