Jump to content
  • Advertisement
Sign in to follow this  
BravoOne

Arrays

This topic is 5412 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm just learning CC+ at the moment, I used to program computers a looong time ago (we're talking ZX Spectrums here), some things change, some stay the same. For my first project I am making a simple strategy game that I believe should give me a good foundation upon which to build once it's completed. For the game I have designed a total of 10 tiles to be used as part of a map, these are 48x48 pixels and saved in a .bmp format. I have them saved as 5 on top row and 5 on bottom. I will include more tiles later, this is just to get me going. Forgive me for referring to old ways of coding, but it's what I know. I'll refer to the BASIC programming language as that seems to me to be closer to C++ than the Z80 instruction code that I previously used: I would have set an array up for the large scale map, eg MAP(100,100) Then I would have had another array for the actual display map eg DISP(10,10) which would load it's data from the map array dependent upon where you were at the time. This would have been in a for, next loop eg x=10 y=10 //we are starting 10 across and 10 down for a=1 to 10 for b=1 to 10 DISP(a,b)=MAP(a+x,b+y) next b next a The value of each location would reflect a tile number ie 1 to 10 (well more, because I will either assign more numbers to indicate what is located within the tile, or make it a 3 dimensional array). Can C++ do this in as simple a way as it could be done in BASIC?

Share this post


Link to post
Share on other sites
Advertisement
Quote:

x=10
y=10 //we are starting 10 across and 10 down

for a=1 to 10
for b=1 to 10
DISP(a,b)=MAP(a+x,b+y)

next b
next a


it would look like this:

int x=10;
int y=10;

for (int a=1;a<=10;a=a+1)
for (int b=1;b<=10;b=b+1)
{
DISP[a]=MAP[a+x][b+x]
}



Edit: As Elementary pointed out, there is a blaring error in the code above. Not paying enough attention.

[Edited by - Kelly G on September 26, 2004 9:50:44 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Kelly G
Quote:

x=10
y=10 //we are starting 10 across and 10 down

for a=1 to 10
for b=1 to 10
DISP(a,b)=MAP(a+x,b+y)

next b
next a


it would look like this:
*** Source Snippet Removed ***



C++ arrays are zero based.


int x = 10, y = 10;

for(unsigned int row = 0; row < 10; ++row )
for(unsigned int col = 0; col < 10; ++col )
disp[row][col] = map[row + x][col + x];






You should seriously consider buying a book or ten, you can't learn C++ well without doing so.

Share this post


Link to post
Share on other sites
Ah, I see. Thanks for the quick reply, Kelly.

So, for a for loop you don't have to predefine the variable you are using for the loop itself?
And there is no 'next' to indicate the end of the loop, the {}'s do this job?

It's even easier than BASIC was, I'm gonna enjoy learning it I think :D

Share this post


Link to post
Share on other sites
Quote:
Original post by BravoOne
Ah, I see. Thanks for the quick reply, Kelly.

So, for a for loop you don't have to predefine the variable you are using for the loop itself?
And there is no 'next' to indicate the end of the loop, the {}'s do this job?

It's even easier than BASIC was, I'm gonna enjoy learning it I think :D


for loops take the form


for ( forinitstatement condition ; expression ) statement


So for(unsigned int row = 0; row < 10; ++row) is declaring an unsigned integer variable named row, and initializing it to 0. The row < 10 is the condition of the for loop, the loop checks this optional condition every time it iterates, if it is true, the loop continues to iterate, if it is false, it stops. ++row is an optional expression which increments the row variable by 1, it is executed each iteration of the loop after the condition is checked and found to be true.

You really need a good book on this.

Quote:

It's even easier than BASIC was...


That opinion wont last long :)

Share this post


Link to post
Share on other sites
To predefine an array, the format would be as follows:

int MAP [100][100];

Is this correct, I only ask these questions as it is an aspect of C++ that I can seem to find few resources in online C++ tutorials.

I have other questions, but will reserve them to another thread.

As for buying a book...

I am a Yorkshireman, and buying a book would imply spending money. And as a Yorkshireman, spending money is totally out of the question :p

Share this post


Link to post
Share on other sites
Well, as a Yorkshireman, learning C++ is also out of the question then.

yes int MAP[100][100]; is valid.

the elements of MAP go from 0 to 99, not 1 to 100.

MAP[0][0] = first element
MAP[99][99] = last element
MAP[99][100] = overwriting the array bounds
MAP[100][99] = same as above


Note: There is a free C++ online book available here, Thinking in C++. It will teach you the basics, but you still need other books to cover other aspects of the language.

Share this post


Link to post
Share on other sites
Quote:
Original post by graveyard filla
i disagree that you need a book to learn C++. i learned C++ completely from tutorials on the internet.


And im sure you know it very well...

Can you list the 2 possible compiler errors in this code?


#include<iostream>

int main(){

std::cout << "Hello World" << std::endl;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by elementary
Note: There is a free C++ online book available here, Thinking in C++. It will teach you the basics, but you still need other books to cover other aspects of the language.


Thanks for the link, elementary, looks good to me :)

One thing better than any book is asking a human to put something you cant quite grasp in another way. That will beat any book on the planet hands down. Though of course, reading is important in order to ascertain what questions to ask in the first place ;)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!