Rand question convert

Started by Frank Brübach, March 09, 2024, 07:17:16 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Frank Brübach

Hi and good morning

I will translate an old freebasic example of myself ( a Kind of minesweeper now with win.inc of Peter projB / winp) step by step cause I want understand all functions and subs :-)
I have detected some Problem zones so I am Sure to solve it soon

My First question (more to come later)

How I can translate correct this Rand(a,b) function below in my example (Works fine but it's perhaps Not correct Translation)

'-- I try to convert an old freebasic code step by step
'-- this little minesweeper example
'
print "ok"

'include "win.inc"

sys seed=0x12345678

Function Rand(sys z1,z2) as sys
sys  rnd
mov  eax,z2
sub  eax,z1
inc  eax
imul edx,seed,0x8088405
inc  edx
mov  seed,edx
mul  edx
add  edx,z1
mov  rnd,edx
Return rnd
End Function

' how I can translate these two lines correct with rand(a,b) function ?
'row = rand * rows
'col = rand * cols

'---------------------//
sub initializeBoard()
    dim i As Integer
    dim j As Integer
    dim rows, cols as integer
    rows=10 : cols = 10
    dim mineCount as integer = 15 
    dim board(int rows, int cols) as string
    dim revealed(int rows, int cols) as integer

print "ok1"

    ' Place mines
    dim row As Integer
    Dim col as integer
    dim placedMines as integer = 0
    do
        row = rand(row * rows,2) ' original: row=rand*rows '???
        col = rand(col * cols,4) ' original: col=rand*cols '???
        print "row: "+row 'only a test
        print "col: "+col 'only a test

print "ok2"

    loop until mineCount  'placedMines = mineCount ''15
    'return mineCount
    print mineCount 'only a test
    'waitkey
end sub

print "ok3"

initializeboard()

  •  

Charles Pegge

#1
Hi Frank,

It looks like the original rand() would return a float value between 0 and 1.0. So you can use:

arnd()*rows or irnd(0,rows) based on the o2 chaos.inc randomizers below:

  'RANDOMIZERS
  ============

  int seed=0x12345678
  '
  function Rnd() as float
  =======================
  '
  'returns float between -1.0 and 1.0
  '
  Static As float f, d=1/0x7fffffff
  mov eax,seed
  inc eax
  rol eax,13
  xor eax,0xdab5ca3a
  mov seed,eax
  push eax
  fild dword [esp]
  pop eax
  fmul dword d
  fstp dword f
  return f
  end function


  'ABS RANDOMISER
  '
  function arnd() as float
  ========================
  '
  'returns float between 0.0 and 1.0
  '
  return Abs(Rnd)
  end function


  function irnd(int z1, z2) as int
  ================================
  '
  'returns integer between z1 and z2
  '
  mov    eax,z2
  sub    eax,z1
  inc    eax
  imul   edx,Seed,0x8088405
  inc    edx
  mov    Seed,edx 'store new seed
  mul    edx 'multiply eax by edx
  return edx+z1
  end Function

Frank Brübach

#2
Thank you Charles this Arnd looks good or I am using irnd(0,rows) :)

I have managed to Convert all three other parts... Although I am Not working in console Modus I wanted to Finish this example

The Last Part however I dont know to adept... The original freebasic Code I have sent too in my example and make a Demo for Input and results...

If you have another Idea or example to study for me I am Glad

Console example


'-- part four kind of minesweeper
'-- console modus, 9-3-24, frank bruebach
'-- all parts nearly 90 per cent ready
'
uses console

SetConsoleTitle "a kind of minesweeper"

print "ok"
def locate SetConsoleCursorPosition ConsOut,(%1)*0x10000+0 %3

print "ok1"

'''''''''''''''''''''''''''''''''''''''''''''
'-------------- freebasic original ------- //
' sub gamemain()
'    dim board(rows, cols) as string
'    Dim revealed(rows, cols) as integer

'    initializeBoard()
'    printBoard()

'    dim row As integer, col as integer
'    do
'        print "Enter row (1-10): "
'        input row
'        print "Enter col (1-10): "
'        input col
'        revealCell(row, col)
'        printBoard()
'    loop
' end sub
'-------------- freebasic original ends ------- //
'
''''''''''''''''''''''''''''''''''''''''''

sub game0main()
    'initializeBoard() 'go
    'printBoard() 'go
    dim st,cr as string
    int a,i,le,doprint
    dim row, col as long
    redim int col_in_row(0)
    redim int indexes(0) 
    int solution
    int size_of_board
    string ans
    sys *index
    solution = 0
   
    cls
    setpos 0,1

    do
        cls
         '
         ' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
         ' how to make an input for rows and cols that take access to revealcell and printboards?
         '
         '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
         '
         ' this demo below structure gives input and result for row and col not more
         '
         print "Enter row (1-10): " : size_of_board = val(input())
         if size_of_board < 1 then size_of_board = 1
         if size_of_board > 10 then size_of_board = 10
         print "Your input row = " size_of_board 'printl

         print  " Show solutions - default is first and last? (Y/N): " : ans=ltrim rtrim(input) : print
         if lcase(ans) = "y" then doprint = 1 else doprint = 0

         cls

         print "Enter col (1-10): " : size_of_board = val(input())
         if size_of_board < 1 then size_of_board = 1
         if size_of_board > 10 then size_of_board = 10
         print "Your input col = " size_of_board 'printl

         print  " Show solutions -> Quit = Yes / No => go on (Y/N): " : ans=ltrim rtrim(input) : print
         if lcase(ans) = "y" then doprint = 1 else doprint = 0
         if not doprint = 0 then exit do
         'if lcase(ans) = "n" then doprint = exit do
         
         redim int col_in_row(size_of_board) 
         redim int indexes(size_of_board)
 cls
        'revealCell(row, col) 'go
        'printBoard() 'go
loop

end sub

game0main()

print "ok2"

Thanks nice Weekend, frank

  •