2 Replies - 223 Views - Last Post: 02 October 2012 - 05:14 PM Rate Topic: -----

#1 bean_grinder  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 8
  • Joined: 02-October 12

Do I "for" loop the arrays in my struct...or the structs in my

Posted 02 October 2012 - 10:30 AM

I am trying to build a data structure. 10 little ones each inside 100 bigger ones each inside one huge one. I think I've done this, but can't figure out how to check. Once built I need to locate any of the smaller ones and cout it's relative location to the bigger ones and the huge one (if possible)... I'm trying to create a text game and track the location of a player. I've tried multiple combinations of structs and arrays, but to no avail. Here's a sample of what I'm trying:

struct sBig                /* This is my big 1000 x 1000 array.
{
 int medium1 [10][10];     /* There are 100 of these middle arrays inside
 int medium2 [10][10];     /* the one big one.
};

struct sMedium
{
 int small1 [10][10];       /* There are 10 of these small arrays inside 
 int small2 [10][10];       /* each of the middle ones
}

int sBig [10][10];           /* trying to connect everything with values
 for (x=0; x<10; x++)
  for (y=0; y<10; y++)
  {
   do something              /* don't know how to connect
  };

int sMedium [100][100]      /* trying to connect everything with values
 for (x=0; x<100; x++)
  for (y=0; y<100; y++)
  {
   do something              /* don't know how to connect
  };


It compiles.I want the small 10 x 10 arrays inside middle 100 x 100 arrays inside one big 10 x 10 array with a cart coord system, so if a player was in , for example, small2 > medium3 he would be located at ? < x < ?,
? < y < ?. Does this make any sense?

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Do I "for" loop the arrays in my struct...or the structs in my

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3172
  • View blog
  • Posts: 9,610
  • Joined: 05-May 12

Re: Do I "for" loop the arrays in my struct...or the structs in my

Posted 02 October 2012 - 11:39 AM

I think you want something like:
struct Room
{
    Object grid[10][10];
};

struct Dungeon
{
    Room rooms[10][10];
};

struct Map
{
    Dungeon dungeons[10][10];
};



But this can get very expensive in terms of memory very quickly depending on how much memory Object takes up. Additionally, if you have sparse grid, rooms, or dungeons array, you be wasting a lot of space storing nothing. A quick short term solution is to use pointers to Object, Room, and Dungeons respectively, but you may want to consider reading up on implementing sparse arrays using linked lists.

baavgai has a natural instinct for picking very good data structures. Hopefully, he'll chime in.
Was This Post Helpful? 0
  • +
  • -

#3 bean_grinder  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 8
  • Joined: 02-October 12

Re: Do I "for" loop the arrays in my struct...or the structs in my

Posted 02 October 2012 - 05:14 PM

SKYDIVER,

I honestly do not understand pointers...yet. I've been trying to point the structs to the structs. The only examples in the three C++ books I have are arrays pointing to structs. So I tried that and in the process I did the following.

#include "stdafx.h"
#include <iostream>
#include "windows.h"
using namespace std;

struct Room
{
  int xyCoord;               //Things each either contains or doesn't contain
  int object;
  int box;
  int note;
};

Room rooms [] =             //I'm looking for a better way to do the coords
{
  {10,-1, -1, -1},          //If there are 10 small rooms inside each medium rooms (100)...
  {20,1, -1, 1},            //that's a lot of typing...must find another way
  {30,1, 1, -1},
  {40,-1, 1, 1}
};

int main()    //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx   MAIN   XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
{
  int i;
  for (i=0; i<4; i++)
    {
     cout << rooms[i].xyCoord << ' '
	  << rooms[i].object << ' '
	  << rooms[i].box << ' '
	  << rooms[i].note << endl;

      if (rooms[i].xyCoord == 10)
	cout << "You are in the kitchen.\n";
       else if (rooms[i].xyCoord == 20)
	cout << "You are in the basement.\n";
       else if (rooms[i].xyCoord == 30)
	cout << "You are in the living room.\n";
       else if(rooms[i].xyCoord == 40)
	cout << "You are in the bathroom.\n";
       else if(rooms[i].xyCoord == 50)
	cout << "You are in the garage.\n";
       else
	cout  << "Neither you or I know where you are.\n";
		
		
      if (rooms[i].object == 1)
	{
	cout << "There is an object in the corner.\n";
	}
	if (rooms[i].box == 1)
	{
	cout << "There is a box in the corner.\n";
	}
	if (rooms[i].note == 1)
	{
	cout << "There is a note in the corner.\n";
	}
    }
	system("pause");
	return 0;
}


So there is a draft of how to do the little rooms. I still need to point the rooms to the bigger rooms. This code compiles and runs.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1