×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# Randomizing PUT: efficiency problem

## Randomizing PUT: efficiency problem

(OP)

I have 3 BSAVEd graphics that are BLOADed into a file. I'm using PUT statements to display them. The problem is the fact that each graphic is stored in a different array, which means 3 different arrays, and I'm using a random number to call a set of IF...THEN...ELSE statements which call a certain PUT statement based on the number. I was wondering if there was a way to call a single PUT statement, with a certain array as the target array based on the random number. I'm only using 3 graphics (which will eventually grow larger over time), so I'm basically just asking for code optimization, if that's possible.
Here is the code (note the redundancies when you look at the PUT statements):

#### CODE

SCREEN 13
CLS

DIM blbelt(901) AS INTEGER, blmage(937) AS INTEGER, fighter(937) AS INTEGER

' "Fast Palette Swapping" technique
'\$INCLUDE: 'colors.bi'

DEF SEG = VARSEG(blbelt(0))
DEF SEG

DEF SEG = VARSEG(blmage(0))
DEF SEG

DEF SEG = VARSEG(fighter(0))
DEF SEG

FOR drawcharsY% = 0 TO 140 STEP 70
FOR drawcharsX% = 0 TO 280 STEP 40
RANDOMIZE TIMER
char% = INT(RND * 3) + 1
IF char% = 1 THEN
PUT (drawcharsX%, drawcharsY%), blbelt
ELSEIF char% = 2 THEN
PUT (drawcharsX%, drawcharsY%), blmage
ELSEIF char% = 3 THEN
PUT (drawcharsX%, drawcharsY%), fighter
END IF
NEXT drawcharsX%
NEXT drawcharsY%

Okay, the idea I'm having is to do something like this:
PUT (drawcharsX%, drawcharsY%), arrayName(char%)
where arrayName is an array that can be referenced to fetch one of the arrays that hold graphics, based on the value of "char%".

For example:

#### CODE

char% = 1
PUT (drawcharsX%, drawcharsY%), arrayName(char%) ' fetches the "blbelt" array
char% = 2
PUT (drawcharsX%, drawcharsY%), arrayName(char%) ' fetches the "blmage" array
char% = 3
PUT (drawcharsX%, drawcharsY%), arrayName(char%) ' fetches the "fighter" array

I hope somebody can help. I also hope I explained it well enough...

### RE: Randomizing PUT: efficiency problem

I like your idea of using an array to recall graphics.  Back when I was programming Caelius (God rest it's soul) I had over 150 tiles I wanted to load.  Naming them is a pain in the ass so I decided to use an array.

First you want to declare a two-dimensional array containing the tiles size and number of tiles:

size% = ((height * width) / 2) + 1
numOfGfx% = 3
DIM GfxArray(size%, numOfGfx% - 1)

then you can use the array to call-up the right graphic variable based on a random number:

PUT (x, y), GfxArray (size%, char%)

.........................................

In terms of optimizing...
First, put "RANDOMIZE TIMER" at the top of the code, this way you don't call it over and over again as it only needs to be called once.

Second, with this array, you don't need IF..THEN statements..(and if you were to use them, I'd recommend replacing them with SELECT..CASE.  SELECT is MUCH faster because the condition is evaluated once, unlike ELSEIF)

OPTIMIZED CODE
----------------
'Define all variables as integers unless specified otherwise
DEFINT A-Z

'Randomize seed
RANDOMIZE TIMER

'Define Graphics array with 20x20 tiles (201 bytes)
DIM GfxArray(201, 2)

FOR drawcharsY = 0 TO 140 STEP 70
FOR drawcharsX = 0 TO 280 STEP 40
PUT (drawcharsX, drawcharsY), GfxArray(201, INT(RND * 3) + 1)
NEXT drawcharsX
NEXT drawcharsY

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!