Jump to content

Recursion Help Needed


Recommended Posts

My script gets all combinations of 1-10 in 5 different groups.

Rules:
1- Must ALWAYS have at least 1 bet registered ($totalBetĀ CANNOT become zero).
2- Maximum bet you can have on one lane/horse is $5.
3- Must NEVER have more than 10 bets registered.

Explanation:
Assume you have TENĀ $1 notes and go to a horse race which has 5 lanes/horses. You are to bet on every combination you can with your $10, but you must follow the rules above. You start with $1 auto-placed into lane 1. In total, there are 2373 combinations.

I believe this is what I'm expecting: Top row = lane, each row below = your $ placement each combination throughout the execution of the script.
Picture.thumb.png.1ee4ed8eeec005d30301abdb7e3b2adf.png

Question:
How do I recurs this properly? p.s. The script starts at the bottom (inside the ;Script=== comment)

HotKeySet("{DEL}", "endScript")

#include <Date.au3>
#include <MsgBoxConstants.au3>

Global $bet[6] = ["0", "1", "0", "0", "0", "0"] ;$bet[0] is not used.
Global $totalBet = 1 ;Default: 1.

Global $BETsDone = 0

Global $debugger1 = True ;Increment/Decrement Bet.
Global $debugger2 = False ;Total Bet.
Global $debugger3 = False ;PlayBET().
Global $debugger4 = False ;$lane.
Global $debugger5 = False ;CheckOtherBets().

FileDelete("MyBets234.txt") ;Delete previous.
Sleep(500)
Global $file = FileOpen("MyBets234.txt", 1)

Func IncreaseBet($lane)
   If($lane = 1) Then
      ;MouseClick.
   ElseIf($lane = 2) Then
      ;MouseClick.
   ElseIf($lane = 3) Then
      ;MouseClick.
   ElseIf($lane = 4) Then
      ;MouseClick.
   ElseIf($lane = 5) Then
      ;MouseClick.
   EndIf

   $bet[$lane] = $bet[$lane] + 1
   $totalBet = $totalBet + 1

   If($debugger1 = True) Then
   FileWrite($file, _Now() & ": Incremented lane " & $lane & " to bet " & $bet[$lane] & @CRLF)
   EndIf

   If($debugger2 = True) Then
   FileWrite($file, _Now() & ": Total Bet = " & $totalBet & @CRLF)
   EndIf
EndFunc

Func DecreaseBet($lane)
   If($totalBet > 1) Then
      If($lane = 1) Then
         ;MouseClick.
      ElseIf($lane = 2) Then
         ;MouseClick.
      ElseIf($lane = 3) Then
         ;MouseClick.
      ElseIf($lane = 4) Then
         ;MouseClick.
      ElseIf($lane = 5) Then
         ;MouseClick.
      EndIf

      $bet[$lane] = $bet[$lane] - 1
      $totalBet = $totalBet - 1
   ElseIf($totalBet = 1) Then
      IncreaseBet($lane + 1)

      If($lane = 1) Then
         ;MouseClick.
      ElseIf($lane = 2) Then
         ;MouseClick.
      ElseIf($lane = 3) Then
         ;MouseClick.
      ElseIf($lane = 4) Then
         ;MouseClick.
      ElseIf($lane = 5) Then
         ;MouseClick.
      EndIf

      $bet[$lane] = $bet[$lane] - 1
      $totalBet = $totalBet - 1
   EndIf

   If($debugger1 = True) Then
   FileWrite($file, _Now() & ": Decremented lane " & $lane & " to bet " & $bet[$lane] & @CRLF)
   EndIf

   If($debugger2 = True) Then
   FileWrite($file, _Now() & ": Total Bet = " & $totalBet & @CRLF)
   EndIf
EndFunc

Func PlayBET()
   ;MouseClick.
   $BETsDone = $BETsDone + 1

   If($debugger3 = True) Then
   FileWrite($file, _Now() & ": Played BET: " & $BETsDone & @CRLF)
   EndIf
EndFunc

Func checkOtherBets($lane)
   $tempReturn = 0

   For $tempLoop = $lane + 1 To 5
      $tempReturn = $tempReturn + $bet[$tempLoop]
   Next

   If($debugger5 = True) Then
      FileWrite($file, _Now() & ": checkOtherBets = " & $tempReturn & @CRLF)
   EndIf

   Return $tempReturn
EndFunc

Func CycleBET($lane)
   If($bet[$lane] < 5 And $totalBet < 10) Then
      IncreaseBet($lane)

      CycleBET($lane)
   ElseIf($lane < 5 And $totalBet < 10) Then
      CycleBET($lane + 1)
   ElseIf($lane = 5 And $totalBet < 10) Then
      IncreaseBet($lane)
   EndIf

   If($debugger4 = True) Then
      FileWrite($file, _Now() & ": $lane = " & $lane & @CRLF)
   EndIf

   If($lane < 5 And $totalBet < 10) Then
      CycleBET($lane + 1)
   EndIf

   PlayBET()

   If($lane < 5 And checkOtherBets($lane) = 0) Then
      DecreaseBet($lane)
   EndIf
EndFunc

;Script=========================================
FileWrite($file, _Now() & ": Script started." & @CRLF & @CRLF)

CycleBET(1)

FileWrite($file, _Now() & ": Script finished." & @CRLF & @CRLF)
;===============================================

Func endScript()
   FileWrite($file, _Now() & ": Script terminated by the Delete hotkey." & @CRLF & @CRLF)

   FileClose($file)

   Exit 0
EndFunc

Ā 

Thank you in advance.

Edited by IAMK
Link to comment
Share on other sites

How do you get 2373 combinations?Ā  i.e. what is the minimum bet per line, I assumed you wanted minimum/maximum bet of $10 per lineĀ based upon your image.Ā  SoĀ would the lowest number be

0 | 0 | 0 |5 | 5

To the largest number

5 | 5 | 0 | 0 |0

Based upon that I could only get 651 variations, although my math might not be right :)Ā 

Link to comment
Share on other sites

I count a slightly lower number of possible bets: only 2231

The actual count is indeed 3272 (I missed a few amount representations)

; list of all bet amount representations having total T such as 0 < T < 11
; amounts in non increasing order numbers in each row
; combinations will be computed later

Local $aBetRepresentations = [ _
    "1,0,0,0,0", _  ; 1 with total amount = 1
    "2,0,0,0,0", _  ; 2 with total amount = 2
    "1,1,0,0,0", _
    "3,0,0,0,0", _  ; 3 with total amount = 3
    "2,1,0,0,0", _
    "1,1,1,0,0", _
    "4,0,0,0,0", _  ; 5 with total amount = 4
    "3,1,0,0,0", _
    "2,2,0,0,0", _
    "2,1,1,0,0", _
    "1,1,1,1,0", _
    "5,0,0,0,0", _  ; 7 with total amount = 5
    "4,1,0,0,0", _
    "3,2,0,0,0", _
    "3,1,1,0,0", _
    "2,2,1,0,0", _
    "2,1,1,1,0", _
    "1,1,1,1,1", _
    "5,1,0,0,0", _  ; 9 with total amount = 6
    "4,2,0,0,0", _
    "4,1,1,0,0", _
    "3,3,0,0,0", _
    "3,2,1,0,0", _
    "3,1,1,1,0", _
    "2,2,2,0,0", _
    "2,2,1,1,0", _
    "2,1,1,1,1", _
    "5,2,0,0,0", _  ; 11 with total amount = 7
    "5,1,1,0,0", _
    "4,3,0,0,0", _
    "4,2,1,0,0", _
    "4,1,1,1,0", _
    "3,3,1,0,0", _
    "3,2,2,0,0", _
    "3,2,1,1,0", _
    "3,1,1,1,1", _
    "2,2,2,1,0", _
    "2,2,1,1,1", _
    "5,3,0,0,0", _  ; 14 with total amount = 8
    "5,2,1,0,0", _
    "5,1,1,1,0", _
    "4,4,0,0,0", _
    "4,3,1,0,0", _
    "4,2,2,0,0", _
    "4,2,1,1,0", _
    "4,1,1,1,1", _
    "3,3,2,0,0", _
    "3,3,1,1,0", _
    "3,2,2,1,0", _
    "3,2,1,1,1", _
    "2,2,2,2,0", _
    "2,2,2,1,1", _
    "5,4,0,0,0", _  ; 16 with total amount = 9
    "5,3,1,0,0", _
    "5,2,2,0,0", _
    "5,2,1,1,0", _
    "5,1,1,1,1", _
    "4,4,1,0,0", _
    "4,3,2,0,0", _
    "4,3,1,1,0", _
    "4,2,2,1,0", _
    "4,2,1,1,1", _
    "3,3,3,0,0", _
    "3,3,2,1,0", _
    "3,3,1,1,1", _
    "3,2,2,2,0", _
    "3,2,2,1,1", _
    "2,2,2,2,1", _
    "5,5,0,0,0", _  ; 18 with total amount = 10
    "5,4,1,0,0", _
    "5,3,2,0,0", _
    "5,3,1,1,0", _
    "5,2,2,1,0", _
    "5,2,1,1,1", _
    "4,4,2,0,0", _
    "4,4,1,1,0", _
    "4,3,3,0,0", _
    "4,3,2,1,0", _
    "4,3,1,1,1", _
    "4,2,2,2,0", _
    "4,2,2,1,1", _
    "3,3,3,1,0", _
    "3,3,2,2,0", _
    "3,3,2,1,1", _
    "3,2,2,2,1", _
    "2,2,2,2,2" _
]

Local $aAllBets[0]

For $a In $aBetRepresentations
    $a = _ArrayPermute(StringSplit($a, ",", 3))
    _ArrayDelete($a, 0)
    _ArrayConcatenate($aAllBets, _ArrayUnique($a), 1)
Next

_ArrayDisplay($aAllBets, UBound($aAllBets) & " possible bets")

Notes:

No recursion needed actually.

Listing of all possible amount representations is best done by hand in such simple cases. That's because additive Number Theory (NT) doesn't enjoy as many powerful features as multiplicative NT. There is no simple way to enumerate all numbers whose sum is given, except if you use ad hoc functions of a computer algebra system covering additive NT.

If _ArrayDelete had a counterpart not using ByRef we could make the content of the whole loop a one-liner.

Array functions returning the count in [0] are a royal pain.

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

@Subz @jchd

combinations with $1 =

"1,0,0,0,0", "0,1,0,0,0", "0,0,1,0,0", "0,0,0,1,0", "0,0,0,0,1"

Combinations with $2 =

"2,0,0,0,0", "1,1,0,0,0", "1,0,1,0,0", "1,0,0,1,0", "1,0,0,0,1"
"0,2,0,0,0", "0,1,1,0,0", "0,1,0,1,0", "0,1,0,0,1"
"0,0,2,0,0", "0,0,1,1,0", "0,0,1,0,1"
"0,0,0,2,0", "0,0,0,1,1"
"0,0,0,0,2"

etc.

You can have bet = $1, $2, ... $9, $10. It's not a minimum of $10. The table I drew was just the start. I can't write the entire thing by hand.

Edited by IAMK
Link to comment
Share on other sites

I have fixed the issue. It was missing a few if scenarios. Specifically, if statements for $lane = 5 and $totalBet = 10.
Since those scenarios weren't handled properly, it would not fit the "<" if statements and exit the recursion early.

p.s. My script has 2848Ā combinations... but it looks correct...

Edited by IAMK
Link to comment
Share on other sites

I coded according to your rules. Have you tried my script?

Can you post your working script yielding 2848 results? I'd like to look why we count differently.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

@jchd Unfortunately, I don't have the script at home. I can show you next week. I just ran your script now, at home. Three versions of this script were made, and all 3 yielded different results; 2231, 2373 and 3848... The strange thing is, your numbers almost appear in the same order mine do, and it looks like they are showing the same information.

I did suspect that mine was logging extras (which I couldn't find when I scanned through manually), which is not too much of an issue for me, assuming it covers every possible scenario, but it is still strange.

Link to comment
Share on other sites

@jchd

HotKeySet("^{DEL}", "endScript") ;Ctrl+Delete
HotKeySet("{`}", "togglePause")

#include <Date.au3>
#include <MsgBoxConstants.au3>

Global $bet[7] = ["0", "1", "0", "0", "0", "0", "0"] ;$bet[0] and $bet[6] are not used. $bet[0] is to keep lanes 1-5 = $bet[1-5], and $bet[6] is to allow $bet[5] to check $bet[$lane+1].
Global $totalBet = 1 ;Default: 1.

Global $prevCombo = "00000"

Global $betsDone = 0

Global $paused = True

Global $debugger = True ;$bet[] array (FileWrite).

FileDelete("45616.txt") ;Delete previous.
Sleep(500)
Global $file = FileOpen("45616.txt", 1)

Func IncreaseBet($lane)
   $bet[$lane] = $bet[$lane] + 1
   $totalBet = $totalBet + 1
EndFunc

Func DecreaseBet($lane)
   If($totalBet = 1) Then
      IncreaseBet($lane + 1)
   EndIf

   $bet[$lane] = $bet[$lane] - 1
   $totalBet = $totalBet - 1
EndFunc

Func betbet()
   $betsDone = $betsDone + 1
EndFunc

Func checkOtherBets($lane)
   $tempReturn = 0

   For $tempLoop = $lane + 1 To 5
      $tempReturn = $tempReturn + $bet[$tempLoop]
   Next

   Return $tempReturn
EndFunc

Func getCombo()
   Return String($bet[1]) & String($bet[2]) & String($bet[3]) & String($bet[4]) & String($bet[5])
EndFunc

Func CycleBET($lane)
   If($lane < 5 And $bet[$lane] < 5 And $totalBet < 10) Then
      IncreaseBet($lane)

      CycleBET($lane)
   ElseIf($lane < 5 And $totalBet < 10) Then
      CycleBET($lane + 1)
   ElseIf($lane = 5 And $bet[$lane] < 5 And $totalBet < 10) Then
      IncreaseBet($lane)
      CycleBET($lane)
   EndIf

   If($lane < 5 And $totalBet < 10) Then
      CycleBET($lane + 1)
   EndIf

   If($lane = 5 Or $totalBet = 10) Then
      If($debugger = True) Then
         FileWrite($file, _Now() & ": [" & $bet[1] & "] [" & $bet[2] & "] [" & $bet[3] & "] [" & $bet[4] & "] [" & $bet[5] & "]" & @CRLF)
      EndIf

      If($prevCombo <> getCombo()) Then
         $prevCombo = getCombo()

         betbet()
      EndIf
   EndIf

   If($bet[$lane] > 0) Then
      If($lane < 5 And checkOtherBets($lane) = 0) Then
         DecreaseBet($lane)
      ElseIf($lane = 5) Then
         DecreaseBet($lane)
      ElseIf($lane = 1 And checkOtherBets($lane) = 0) Then
         CycleBET($lane + 1)
      EndIf
   EndIf
EndFunc

;Script=========================================
While($paused = True)
   Sleep(500)
WEnd

For $startlane = 1 To 5
   CycleBET($startlane)
Next

FileWrite($file, @CRLF & _Now() & ": bets done: " & $betsDone & @CRLF)
;===============================================

Func togglePause()
   $paused = Not($paused)

   If($paused = True) Then
      ToolTip("Script Paused!", 0, 0)
      FileWrite($file, "Script paused." & @CRLF)
   Else
      FileWrite($file, "Script continued." & @CRLF)
   EndIf

   While($paused = True)
      Sleep(500)
   WEnd

   ToolTip("")
EndFunc

Func endScript()
   FileWrite($file, _Now() & ": Script terminated by the Delete hotkey." & @CRLF & @CRLF)

   FileClose($file)

   Exit 0
EndFunc

p.s. The other person still stands by their number of 2373. I Have verified that all unique combinations I have, they have. Whether or not they have duplicates or not I did not verify.

Edited by IAMK
Link to comment
Share on other sites

Sorry, I missed some representations. There are indeed 3272 possible bets (your count is off by 1).
I've fixed my post above.

Ā 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

@jchd

Ah, yes, sorry. I forgot to -1 for the heading in excel.

Also, what is "cleaner" code? Hardcoding like you said, and having a function to check the previous bet, and calculate the next bet? Or recursion (the way I did it)? Because yours is hardcoded, but mine is much longer.

Edited by IAMK
Link to comment
Share on other sites

It isn't actually hard to built the representation table by code. Here's one way to build it (without manual enumeration, proven prone to error!):

; individual allowed bet amounts with max duplicity (total T must verify 0 < T < 11)
Local $aAmounts = [0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 5, 5]
Local $aCombi5 = _ArrayCombinations($aAmounts, 5, "+")      ; big hammer!
$aCombi5 = _ArrayUnique($aCombi5, 0, 1)
Local $sRepresentations
For $i = 1 To $aCombi5[0]
    If Execute($aCombi5[$i]) <= 10 Then $sRepresentations &= $aCombi5[$i] & ";"
Next
$aBetRepresentations = StringSplit(StringReplace(StringTrimRight($sRepresentations, 1), "+", ","), ";", 2)

Again one can see how inconveniently inconsistent array functions are

In my code the bulk part is done by _ArrayPermute plus some clean up following (removing dups).

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

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

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...