Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Seeding Multi Dimension Array and printing the output.

Author
Message
erebusman
13
Years of Service
User Offline
Joined: 23rd Jul 2011
Location: Sacramento, CA
Posted: 15th Aug 2011 04:32
Hello,

First a brief intro: I decided to learn to program about 1 month ago and did some research and decided I liked DBPro.

I have since then been doing tutorials voraciously and been through many including most of the ones on TDK's tutorial thread http://forum.thegamecreators.com/?m=forum_view&t=99497&b=10

Now I am moving on to start a "learning phase" where I take ideas from the tutorials and try to implement them in ways that will eventually permit me to make a game that I have in mind.

The first task is to learn to work with multi dimensional arrays and I need to perform the following tasks:

1) Create an 8,8 array
2) Populate the array with random integer numbers ranging from 1 to 7 (no zeros)
3) Print the array out as if you were looking at a game board

NOTE: I'm doing all this in text output ; later graphics will come - I feel learning the text end lets me focus on code not on glitz.

After the first 3 goals are done I have a second set of goals:

4: Develop a method to elimate numbers (replace them with 0's);
5: Then do a bubble sort on each column to "sink" any numerical value down and replace 0s' (aka empty squares),
6: Create a process to then top fill the blanks left at the top.

I say that to understand my end goal but its not the question I'm focusing on first (confirm my current process is good then move on right? )

So all that said I do not come empty handed!

My first code seemed sucessful in that I was able to create the array, and print out 8 random numbers between 1-7.





This outputs a list on the screen like so:

3
7
2
1
3
1
1
4

So it suited my needs.. success for step 1!

What I then attempted to was a nested loop to try and fix in the y element of the array like so:



This outputs such a long string of numbers on my screen (top to bottom) and there is no scroll bar -- so I can not tell if its doing 64 total items (7,7) array should be 8x8 elements right?

So I tried changing my print command to Print GameBoard(x,y) and there is no difference.


So I have some questions given what I've done.


1) Is my nested loop doing what I want it to do?
2) Any advice (tutorial based or otherwise) on how to print my output that resembles a grid instead of linear output
3) Are these values actually getting stored in the array?

I think so because I made a print loop at the end as such:



And it outputs like so:
14345671
23548787
65498878
46456545

And so on (appropriate size)

But I don't want to assume this is really 'saved in the array' because I'm new and dont know if there was a command I needed to 'save' it into the array?

Thanks in advance for any feedback and advice!


PS: I sure wish there was a preview post button so I could edit this a few times -- with the new member 'your not allowed to post' without review feature I may have to edit this a couple of times
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 15th Aug 2011 05:10 Edited at: 15th Aug 2011 05:12
You're actually picking a number twice. The second "GameBoard(x,y)=rnd(6)+1" is the last one ran so it's the only one that keeps it's value. To prevent this remove the first set of random number picking and the print. You can also print it in the way you want the first time if you change the number to a string with the STR$() command to use the ; as it's intended use. The FOR/NEXT loop for x is where you need a PRINT statement without a ; to skip to the next line.



Edit: We call it a Newbie Slap but the post approval phase doesn't last long.

Bulsatar
13
Years of Service
User Offline
Joined: 19th Apr 2011
Location:
Posted: 15th Aug 2011 05:17
Arrays are global in dbpro so your values in your array are stored for as long as you are running your program and you don't change them. If you want to save the values of your array to use them for later you will have to export the values to a file and then import that file at the beginning if you are "loading" the array values.

Take a look in the help documentation for file commands and specifically Make File, Open to Read, Open to Write, File Exist, Read String and/or Read File, Load Array and Save Array.

There are LOTS of different ways to do things, it just depends on how you choose to set up your information
erebusman
13
Years of Service
User Offline
Joined: 23rd Jul 2011
Location: Sacramento, CA
Posted: 15th Aug 2011 05:30
Grog & Bulsatar;

Thanks both great input & answers to my questions much appreciated.

I'll have to review your suggested code change .. I think I see how it works but I want to understand how I was doing it twice..

Yours clearly does it in one pass which is what I wanted to start with but I had a hard time getting it to go correctly myself
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 15th Aug 2011 06:53
Quote: "I want to understand how I was doing it twice.."


It was reading the code like this:


On closer inspection I can see why you didn't have a problem because on the second time it looped y should equal 8 (because it was after counting to 7) but because you left off the y in the array it didn't cause an error.

This (the first random number pick):


Should of been this:


This is your code with and 8x8 grid (to prevent error and you'll see why when you run it) with the array info printing with the number. It technically only does it twice the first time then it goes beyond the original array size you wanted.



erebusman
13
Years of Service
User Offline
Joined: 23rd Jul 2011
Location: Sacramento, CA
Posted: 17th Aug 2011 04:12
Grog,

That last bit of code did the trick for me, I was trying to figure out how to get it to print what it was doing as it went along; but I lacked the expertise to do that.

Your output puts it in a way that I can see whats happened, thank you that was very educational!

Login to post a reply

Server time is: 2024-11-22 19:14:22
Your offset time is: 2024-11-22 19:14:22