• Popular Now

• 15
• 15
• 11
• 9
• 10

Archived

This topic is now archived and is closed to further replies.

Very Simple Question (need help - 2D arrays)

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

Recommended Posts

I am currently starting a new programming project. It''s going to be a text-based meld of RTS/town simulator. Now, here''s my question. I know how to use 2D arrays a bit, but I haven''t been able to get mine working. My reference books don''t seem to help me (mostly since my problem is with function calls). Can I see what you all would write (very basically) for a 10x10 2D array and allow the person to move between the tiles? I''m not sure where my problem lies. I''d like to see an example of something similar to what I have so I can find out where I went wrong. Thanks in advance. --Samdil

Share on other sites
Here''s some very simple tile based [pseudo-]code. It doesn''t cover loading of maps, objects on the ground, scrolling of the screen, initialization (general, or with any API), or anything seriously complex (I''m writing this with about 5 minutes of time, heh). It also doesn''t show you the best coding habits, but I''m just meaning it to get the point across:

  #define TILE_WIDTH 10#define TILE_HEIGHT 10#define TILE_PIX_WIDTH 50#define TILE_PIX_HEIGHT 50// We''ll assume that the character is the same size as a tile =P/* Character info */struct Player { long x, y; /* Insert Image Stuff Here */};/* Will hold information about whether or not a tile is able to be walked through. If collide equals true then you cannot walk through it */struct TileInfo { bool collide; /* Insert Image Stuff Here */};/* Holds a "palette" of tiles, and 10x10 references to them. You should of course make it dynamic for better usability and efficiency */struct Map { TileInfo TI[256]; unsigned char tiles[TILE_WIDTH][TILE_HEIGHT];}Map WorldMap;Player PlayerOne;bool KeyBoard[256]; // Init all to false in your code// Outlines a really cheesy map rendering ruitinevoid RenderMap(void) { /* Clear Screen */ for(int a=0; a<10; a++) { for(int b=0; b<10; b++) { /* Draw tile image WorldMap.TI[WorldMap.tiles[a][b]] at x=a*TILE_PIX_WIDTH by y=b*TILE_PIX_WIDTH */ } } /* Render Character at x=PlayerOne.x by y=PlayerOne.y */}// Tests for rectangular collisionbool RectRect(const RECT &a, const RECT &b) { if(a.left>b.right) return false; if(a.rightb.bottom) return false; if(a.bottom

Ask as many questions as you''d like, I know that I left out a ton of stuff, but I was trying to provide a simple framework. I stress that this is horrible, non-functional pseudo-code, but I hope it gives you an idea. Good luck. Pray that someone posts some better code, and that mine doesn''t confuse you... heh

http://www.gdarchive.net/druidgames/

Share on other sites
Well, that''s not quite was I was looking for, since I''m just doing a VERY simple *text* game. But, I think I have it working now. I''ll post an update after I''ve finished with the adjustments.

Thanks for the help, however.

--Samdil

Share on other sites
Well, it works, but not the way I''d like.

If someone could post a sample to compare, that''d be great. Thanks in advance.

--Samdil

Share on other sites
OK, I decided to post my source so far and explain the problem.

I can get this to compile and link and execute fine, but when I move, the coords update only the Y value, and only in the positive direction. Example: start 0,0; input N - 0,1; input E - 0,2; input S - 0,3; input W - 0,4 etc.

Again this is just a simple program so far; I want to get this main part working before I expand on it. Source:

  #include #include //Prototypesvoid clrscrn();int moveChar(char);//Global Variablesint px; //player X and Y coordinatesint py;char provOwner; //stores owner of the provinceint provinces[10][10] ={2, 1, 1, 1, 2, 4, 3, 3, 1, 4, 2, 2, 1, 1, 2, 2, 3, 3, 3, 4, 2, 2, 2, 3, 3, 2, 2, 4, 3, 3}; //numbers for test purposes onlychar input, fInput; //player input and proxy variable for function moveChar()struct kings { char name;} p[4]; //this is here because eventually this will expand to hold all variables for each rulerint sCheck; //checks if input was validint i; //loop countervoid main() { //Get player name, store in their structure cout << "What is your name?\n"; cin >> p[0].name; clrscrn(); do { //Assign province owner names if(provinces[px][py] == 1) { provOwner = p[0].name; } if(provinces[px][py] == 2) { provOwner = p[1].name; } if(provinces[px][py] == 3) { provOwner == p[2].name; } if(provinces[px][py] == 4) { provOwner == p[3].name; } //Tells where player is cout << "You are in " << provOwner << "'s kingdom. -- " << px << "," << py << "\n\n"; //Input on direction to go (no restrictions yet on going "out of bounds") cout << "Go where?\n\n"; cout << "(N)orth, (S)outh, (E)ast, (W)est\n"; do { //Get input until valid entry is inputted cin >> input; sCheck = moveChar(input); } while(sCheck == 0); clrscrn(); } while(input != 'exit'); //Loop while user doesn't input 'exit'/* DOESN'T WORK - still here in case I need it later; it keeps asking for input when I use this switch(input) { case 'N': case 'n': py++; sCheck = 1; break; case 'E': case 'e': px++; sCheck = 1; break; case 'S': case 's': py--; sCheck = 1; break; case 'W': case 'w': px--; sCheck = 1; break; default: break; }}*///Function seems to work, but not as intended; actually breaks input loopint moveChar(char fInput) { if(input == 'N' | 'n') { py++; return 1; } else { if(input == 'S' | 's') { py--; return 1; } else { if(input == 'E' | 'e') { px++; return 1; } else { if(input == 'W' | 'w') { px--; return 1; } else { return 0; } } } }}//Clear scren :) void clrscrn() { for(i=0; i<25; i++) { cout << "\n"; } i=0;} `

Edited by - Samdil on February 1, 2001 2:32:23 PM