Link to home
Start Free TrialLog in
Avatar of icysmarty
icysmarty

asked on

Display random characters

Hi

I want to display random characters at a designated rectangular.
I am able to generate teh random characters but how can I make them display only within the rectangular starting at say row 5 column 5. Assume that the rectangular runs from row 5 through 15 and column 5 through 10. I have tried using RandomRange to set the number and call Gotoxy.. but it doesn't see to work.
Avatar of dimitry
dimitry

Can you show your source, please ?
Avatar of icysmarty

ASKER

Here it is..
.data
wheight      byte      80
wwidth      byte      25
numchar      dword   2000      
delayt      dword      3000      ;delay 3 seconds
ldelay      dword      300      ;loop delay

nrow      byte      5
ncol      byte      8

CR = 0dh            ;carriage return
LF = 0Ah            ;line feed
;***************************************************************************
.code
main PROC

;Set text color to balck text on yellow background:

      mov eax, black + (yellow *16)
      call SetTextColor
      call Clrscr      ;clear the screen
      mov ecx, numchar      ;loop counter
      
;Generate 2000 characters random integers between 0 and 99
;Include a 3000 milisecond delay
      

      mov dh, nrow
      mov dl, ncol
L1:      
      call Gotoxy
      
      mov eax,16
      call randomRange
      call settextcolor      

      mov eax,50+1
        call randomRange
      add eax,' '

      
        call WriteChar
      
      mov bh, 5
      call RandomRange
      mov dh, bh

      mov bl, 17
      call RandomRange
      mov dl, bl

      ;call RandomRange
      ;add dl, al

      ;mov eax, nrow
      ;call RandomRange
      ;add dh, ah
      
      loop L1



      mov eax, delayt
      call Delay
      ret


main ENDP
END main


I have made some modification since I posted the last message. Now it is getting worse.
Where are these functions: Gotoxy, RandomRange, WriteChar ?
It is defined in the library.
What is your compiler ? Nasm, Tasm or Masm ?
Masm615
Can you please tell me the logic to display characters only in a rectangular area at a entire screen?
The problem I have is , i cannot make the random characters show in that rectangular only.
So you are trying to create Console application for Win32 ?
What is your compilation usage ?
what do you mean?
It is not very complicated from "logical" point of view.
Your screen is 80x25 characters. So your X coordinate can be any number from 1 to 80 and Y - from 1 to 25.
Gotoxy(X,Y) moves your "current" cursor position to (X,Y) and you are printing character in this position.
However, implementation with specific library functions demands accurate use of them.
Can you please give me definitions of these functions: Gotoxy, RandomRange and WriteChar ?
I suspect you may pass not correct values to them.
How do you compile your code ?
this is the randomRange
;--------------------------------------------------------------
RandomRange PROC
;
; Returns an unsigned pseudo-random 32-bit integer
; in EAX, between 0 and n-1. Input parameter:
; EAX = n.
; Last update: 09/06/2002
;--------------------------------------------------------------
       push  ebx
       push  edx

       mov   ebx,eax  ; maximum value
       call  Random32 ; eax = random number
       mov   edx,0
       div   ebx      ; divide by max value
       mov   eax,edx  ; return the remainder

       pop   edx
       pop   ebx

       ret
RandomRange ENDP

;--------------------------------------------------
Gotoxy PROC
;
; Locate the cursor
; Receives: DH = screen row, DL = screen column
; Last update: 7/11/01
;--------------------------------------------------------
.data
_cursorPosition COORD <>
.code
      pushad

      CheckInit      ; was console initialized?
      movzx ax,dl
      mov _cursorPosition.X, ax
      movzx ax,dh
      mov _cursorPosition.Y, ax
      INVOKE SetConsoleCursorPosition, consoleOutHandle, _cursorPosition

      popad
      ret
Gotoxy ENDP

;------------------------------------------------------
WriteChar PROC
;
; Write a character to standard output
; Recevies: AL = character
; Last update: 10/30/02
; Note: WriteConole will not work unless direction flag is clear.
;------------------------------------------------------
      pushad
      pushfd      ; save flags
      CheckInit

      mov  buffer,al

      cld      ; clear direction flag
      INVOKE WriteConsole,
        consoleOutHandle,      ; console output handle
        OFFSET buffer,      ; points to string
        1,      ; string length
        OFFSET bytesWritten,        ; returns number of bytes written
        0

      popfd      ; restore flags
      popad
      ret
WriteChar ENDP
/***********************************************************

I compile the code using Masm as well

Here is what I understand.
First i paint the background with yellow color, then I mov the cursor to (5,8) (row 5 column 8)... from there I print the random character. After that , i generate a random coordinate within the

(5,8) -------------(5,25)
|                          |
|                          |
|                          |
|                          |
|                          |
(10,8)------------(10,25)

This is the part I did not get
Ok,

So in order to get row and colomn you need to do something like this:
L1:    
     call Gotoxy
     
     mov eax,16
     call randomRange
     call settextcolor    

     mov eax,50+1
     call randomRange
     add eax,' '
     call WriteChar
     
     mov eax, 5
     call RandomRange
     mov dl, al
     add  dl, 6

     mov eax, 10
     call RandomRange
     mov dh, al
     add  dh, 6

     loop L1
I posted code for (5,5) - (15,10)
You need to generate random number between 0 and (15-5) and add to initial coordinate 6. (6 if you want to be inside your rectangle)
why though?
Now it only runs from row 5,5 - 5,10
Are you sure ? Can you check real values of dl and dh before Gotoxy call ?
For (5,8) - (10,25) code should be:
L1:    
     call Gotoxy
     
     mov eax,16
     call randomRange
     call settextcolor    

     mov eax,50+1
     call randomRange
     add eax,' '
     call WriteChar
     
     mov eax, 18
     call RandomRange
     mov dl, al
     add  dl, 8

     mov eax, 6
     call RandomRange
     mov dh, al
     add  dh, 5

     loop L1

It works now..
I put
mov eax,6
call RandomRange
mov dh, 'ah'
add dh, 5

can you please explain to me why
mov eax, 18
     call RandomRange
     mov dl, al
     add  dl, 8

     mov eax, 6
     call RandomRange
     mov dh, al
     add  dh, 5

Isn't it adding add the column to the previous value like
8 then 16 then 24 to the column?
ASKER CERTIFIED SOLUTION
Avatar of dimitry
dimitry

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you ... i just figured that out.
You are welcome