Archived

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

Very Simple Question (need help - 2D arrays)

This topic is 6162 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 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 this post


Link to post
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 ruitine
void 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 collision
bool RectRect(const RECT &a, const RECT &b) {
if(a.left>b.right) return false;
if(a.right<b.left) return false;
if(a.top>b.bottom) return false;
if(a.bottom<b.top) return false;
return true;
}

// Handles movement of a character, tests for collision
void MoveCharacterDir(long x, long y) { // Amount to move char.
RECT play_rect;
RECT tile_rect;
SetRect(&play_rect,PlayerOne.x+x,PlayerOne.y+y,PlayerOne.x+TILE_PIX_WIDTH+x,PlayerOne.y+TILE_PIX_HEIGHT+y);
for(int a=0; a<10; a++) {
for(int b=0; b<10; b++) {
SetRect(&tile_rect,a*TILE_PIX_WIDTH,b*TILE_PIX_HEIGHT,(a+1)*TILE_PIX_HEIGHT,(b+1)*TILE_PIX_WIDTH)
if(RectRect(play_rect,tile_rect) return;
}
}
PlayerOne.x += x;
PlayerOne.y += y;
}

// A fake Windows message handler (very bad impression, I know)
SUMTIN HandleKeyboard(SOMEMSG Msg, SOMETHING Key) {
switch(Msg) {
case SOME_MSG_KEY_DOWN:
KeyBoard[Key] = true;
break;
case SOME_MSG_KEY_UP:
KeyBoard[Key] = false;
break;
default:
return DoDefaultDeally();
break;
}

// A really pitiful game-loop, the manager of your game
void GameLoop(void) {
if(KeyBoard[SOME_LEFT_KEY]) MoveCharacterDir(-5, 0);
else if(KeyBoard[SOME_RIGHT_KEY]) MoveCharacterDir(5, long y);
else if(KeyBoard[SOME_UP_KEY]) MoveCharacterDir(0, -5);
else if(KeyBoard[SOME_DOWN_KEY]) MoveCharacterDir(0, 5);
RenderMap();
/* Do something with HandleKeyboard here */

}

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 this post


Link to post
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 this post


Link to post
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 <iostream.h>
#include <conio.h>

//Prototypes

void clrscrn();
int moveChar(char);

//Global Variables

int px; //player X and Y coordinates

int py;
char provOwner; //stores owner of the province

int 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 only

char 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 ruler

int sCheck; //checks if input was valid

int i; //loop counter


void 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 loop

int 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

Share this post


Link to post
Share on other sites