Ok, I don't understand the algorithms you are being implementing folk, but you could plug it in the vizualizer now , via an external sub that is defined at the end of the code:
Code: Select all
'--------------------------------------------------------
'run over code combinations of the form a0a1..an, ai=0..9
'--------------------------------------------------------
#include once "fbgfx.bi"
randomize TIMER
dim as integer scrW => any
dim as integer scrH => any
scope
var dskW => -1
var dskH => -1
screenControl fb.GET_DESKTOP_SIZE, _
dskW, _
dskH
'
scrW = dskW - 2*dskW\32
scrH = dskH - 2*dskH\8
screenRes scrW, scrH, _ 'sets application screen dimension
32, _ 'sets application screen color depth
2, _ 'sets application screen page number
fb.GFX_SHAPED_WINDOW + _ 'enables application standard transparency
fb.GFX_ALPHA_PRIMITIVES + _ 'enables application standard alpha
fb.GFX_NO_FRAME 'sets application borders to none
end scope
type GENERATORPROCTYPE as sub(() as integer, () as integer)
type COMBINATION
declare constructor()
declare constructor(byval CodeLength as integer)
declare operator cast() as string
declare property Value() as integer
declare sub PlugGenerator(byval GeneratorPtr as GENERATORPROCTYPE)
declare sub Generate(byval Seed as integer=0)
declare sub PrintCombinationStringInfo()
declare sub RenderAsBarPlot()
as integer _a(any)
as integer _rankOfGeneration
static as GENERATORPROCTYPE _generator
static as integer frequency(any)
static as integer codeLength
static as integer generationCounter
static as fb.IMAGE ptr renderedImagePtr
static as COMBINATION ptr arrayOfGeneratedCombinationPtr(any)
end type
dim as GENERATORPROCTYPE COMBINATION._generator => 0
dim as integer COMBINATION.frequency(any)
dim as integer COMBINATION.codeLength => 0
dim as integer COMBINATION.generationCounter => 0
dim as fb.IMAGE ptr COMBINATION.renderedImagePtr => 0
dim as COMBINATION ptr COMBINATION.arrayOfGeneratedCombinationPtr(any)
constructor COMBINATION()
'note:
'when a combination is constructed it is not yet generated, generation_rank==-1
'when a combination is generated,
'it keeps its first attributed generation_rank - unless it is reinitialized by the constructor
'
THIS._rankOfGeneration => -1
'
if COMBINATION.codeLength<1 then
COMBINATION.codeLength => 1
end if
redim THIS._a(COMBINATION.codeLength - 1)
redim preserve COMBINATION.frequency(10^COMBINATION.codeLength - 1)
end constructor
constructor COMBINATION(byval CodeLengthArg as integer)
THIS._rankOfGeneration => -1
if CodeLengthArg<1 then
CodeLengthArg = 1
end if
COMBINATION.codeLength = CodeLengthArg
redim THIS._a(COMBINATION.codeLength - 1)
redim preserve COMBINATION.frequency(10^COMBINATION.codeLength - 1)
end constructor
operator COMBINATION.cast() as string
dim as string castValue => ""
for arrayIndex as integer = lBound(THIS._a) to _
uBound(THIS._a)
castValue &= str(THIS._a(arrayIndex))
next arrayIndex
'
return castValue
end operator
property COMBINATION.Value() as integer
dim as integer returnValue => -1
dim as integer sum => 0
for arrayIndex as integer = 0 to COMBINATION.codeLength - 1
sum += THIS._a(COMBINATION.codeLength - 1 - arrayIndex)*10^arrayIndex
if arrayIndex=(COMBINATION.codeLength - 1) then
returnValue = sum
end if
next arrayIndex
'
return returnValue
end property
sub COMBINATION.PlugGenerator(byval GeneratorPtr as GENERATORPROCTYPE)
COMBINATION._generator = GeneratorPtr
end sub
sub COMBINATION.Generate(byval Seed as integer=0)
if THIS._rankOfGeneration=-1 then
COMBINATION.generationCounter += 1
THIS._rankOfGeneration = COMBINATION.generationCounter
redim preserve _
COMBINATION.arrayOfGeneratedCombinationPtr(uBound(COMBINATION.arrayOfGeneratedCombinationPtr) + 1)
COMBINATION.arrayOfGeneratedCombinationPtr(uBound(COMBINATION.arrayOfGeneratedCombinationPtr)) = @THIS
end if
'
if THIS._generator=0 then
if uBound(COMBINATION.arrayOfGeneratedCombinationPtr)>1 then
for arrayIndex as integer = 0 to uBound(THIS._a) - 1
THIS._a(arrayIndex) = _
COMBINATION.arrayOfGeneratedCombinationPtr(uBound(COMBINATION.arrayOfGeneratedCombinationPtr) - 1)-> _
_a(arrayIndex + 1)
next arrayIndex
THIS._a(uBound(THIS._a)) = int(rnd()*10)
else
for arrayIndex as integer = 0 to uBound(THIS._a)
THIS._a(arrayIndex) = valInt( _
mid( str(Seed), _
uBound(THIS._a) - arrayIndex + 1, _
1 _
) _
)
next arrayIndex
end if
else
if uBound(COMBINATION.arrayOfGeneratedCombinationPtr)>1 then
(COMBINATION._generator)( COMBINATION.arrayOfGeneratedCombinationPtr(THIS._rankOfGeneration - 2)->_a(), _
THIS._a() _
)
end if
end if
'
COMBINATION.frequency(THIS.Value) += 1
end sub
sub COMBINATION.PrintCombinationStringInfo()
? THIS
end sub
sub COMBINATION.RenderAsBarPlot()
dim as integer scrW, scrH
screenControl fb.GET_SCREEN_SIZE, _
scrW, _
scrH
'
view (10, 10)-(scrW -1 - 10, scrH - 1 - 10), _
rgb(220,250,200), _
rgb(100,100,200)
line (0,scrH - 135)-step(scrW -1 - 20,20), rgb(100,200,100), bf
if COMBINATION.renderedImagePtr<>0 then
put (0,0), COMBINATION.renderedImagePtr, TRANS
end if
imageDestroy COMBINATION.renderedImagePtr
COMBINATION.renderedImagePtr = imageCreate(scrW - 20, scrH - 20, rgb(255,0,255), 32)
window (0,0)-(10^COMBINATION.codeLength - 1, 40)
line (THIS.Value, 0)-step(1, COMBINATION.frequency(THIS.Value)), _
rgba(180,100,120, 100), _
bf
window screen
view screen
get (10, 10)-(scrW -1 - 10, scrH - 1 - 10), COMBINATION.renderedImagePtr
view (10, 10)-(scrW -1 - 10, scrH - 1 - 10), _
rgb(220,220,180), _
rgb(100,100,200)
put (0,0), COMBINATION.renderedImagePtr, TRANS
window (0,0)-(10^COMBINATION.codeLength - 1, 40)
draw string (THIS.Value, 10), str(THIS.Value)
window screen
view screen
end sub
declare sub UserGenerator(() as integer, () as integer)
'-----------------------------------------------------------------------------------------MAIN
redim as COMBINATION arrayOfComb(0)
arrayOfComb(0) => COMBINATION(3)
arrayOfComb(0).PlugGenerator(@UserGenerator)
arrayOfComb(0).Generate()
var hasFoundSomeUnknownCombination => FALSE
var lastMinZeroPosition => 0
do
screenSet 1, 0
redim preserve arrayOfComb(uBound(arrayOfComb) + 1)
arrayOfComb(uBound(arrayOfComb)).Generate(1)
'comment/uncomment to toogle text/graphics:
'arrayOfComb(uBound(arrayOfComb)).PrintCombinationStringInfo()
arrayOfComb(uBound(arrayOfComb)).RenderAsBarPlot()
screenCopy 1, 0
'
'test if the whole combinations have been found
hasFoundSomeUnknownCombination = FALSE
for frequencyArrayIndex as integer = lastMinZeroPosition to uBound(COMBINATION.frequency)
if COMBINATION.frequency(frequencyArrayIndex)=0 then
lastMinZeroPosition = frequencyArrayIndex
hasFoundSomeUnknownCombination = TRUE
exit for
end if
next frequencyArrayIndex
'
sleep 10
loop until ( inkey()=chr(27) orElse (not hasFoundSomeUnknownCombination) )
color ,rgb(200,0,0)
? COMBINATION.generationCounter
screenCopy 1, 0
'---------------------------------------------------------------------------------------------
getKey()
'------------------------------------------------------------------------------------------SUB
sub UserGenerator(Pre() as integer, Nex() as integer)
'reuse of the last digits of the previously generated code:
for codeArrayIndex as integer = lBound(Pre) to uBound(Pre) - 1
Nex(codeArrayIndex) = Pre(codeArrayIndex + 1)
next codeArrayIndex
'
'set now a new digit at the last position of the new code:
static as integer i 'put your algorithm here
i += 1 '
Nex(uBound(Pre)) = (1000*rnd()*(i mod (10 - (1 + rnd()*8))) mod 10) '
end sub
'(eof)
I have seen here and there some MOD operator so I've implemented a dummy example to show where the algorithm goes. I hope it is understandable.