Lists inside Classes

not quite sure about implementation...

Page 1 of 1

7 Replies - 952 Views - Last Post: 05 March 2009 - 08:33 AM Rate Topic: -----

#1 Choscura   User is offline

  • D.I.C Lover


Reputation: 470
  • View blog
  • Posts: 2,235
  • Joined: 18-October 08

Lists inside Classes

Posted 04 March 2009 - 03:46 PM

hey everyone, just a quick very basic problem. I've got to keep track of a list of attributes (a la roleplaying games): each attribute, in turn, has a list of skills that add a number to that attribute if the skill is used, and a *third* layer of specialties that add a number to both the skill and the attribute.

I'm having trouble getting the lists to implement, however, and after 4 hours of banging my head into this, I hereby submit two thoughts to you to see which is the best way to go about this:

	public class Attribute
	{
		public List<Skill> childskills = new List<Skill>();
		public int score;
		public string name;
		public Attribute(int score, string name)
		{
			this.score = score;
			this.name = name;
		}
	}

	public class Skill
	{
		public List<specialty> childspecs = new List<specialty>();
		public int score;
		public string name;
		public Skill(int score, string name)
		{
			this.score = score;
			this.name = name;
		}
	}

	public class specialty
	{
		public int score;
		public string name;
		public specialty(int score, string name)
		{
			this.score = score;
			this.name = name;
		}
	
	}



this is the way I think it is most likely to work: however, whatever syntax I try I can't figure out how to access the lists in each object. my code is stuck at
 public List<Attribute> Monkey=new List<Attribute>();


and, inside public Form1(){

 Monkey.Add(10, "strength", );


with metaphorical question marks after that comma. I can't for the life of me figure out what's supposed to go there.


the second implementation I had thought of (which I've almost rejected, but not quite) goes something like this:

List<string, int, List<string, int, List<string, int>/*specialty*/>/*skill*/>/*attribute*/>character_name = new List<string, int, List<string, int, List<string, int>>>();


if anybody can shed some light on how to make this work or if there is a better way, that would be great!

Is This A Good Question/Topic? 0
  • +

Replies To: Lists inside Classes

#2 baavgai   User is online

  • Dreaming Coder
  • member icon


Reputation: 7181
  • View blog
  • Posts: 14,969
  • Joined: 16-October 07

Re: Lists inside Classes

Posted 04 March 2009 - 04:15 PM

View PostChoscura, on 4 Mar, 2009 - 04:46 PM, said:

 Monkey.Add(10, "strength", );


Monkey is to hold an instance of Attribute. First you must make that instance...

 
Monkey.Add(new Attribute(10, "strength"));



You may extend the class that Monkey is, if you want it to do certain things for you:
public class MonkeyType : List<Attribute> {
   public void Add(int score, string name) {
      this.Add(new Attribute( score,  name));
   }
}

public MonkeyType Monkey=new MonkeyType();


Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: Lists inside Classes

Posted 04 March 2009 - 04:19 PM

You should maybe think your design through a bit more, as there's a lot of duplication there. How about this (long way):
List<Attribute> monkey = new List<Attribute>();
specialty bananapeeling = new specialty(100, "banana");
Skill hands = new Skill(50, "hands");
hands.childspecs.Add(bananapeeling);

Attribute dex = new Attribute(18, "dexterity"));
dex.childskill.Add(hands);

monkey.Add(dex);


Was This Post Helpful? 0
  • +
  • -

#4 coden4fun   User is offline

  • if(coden4fun.NotTired){ ContinueToCode(); }
  • member icon

Reputation: 28
  • View blog
  • Posts: 696
  • Joined: 27-February 09

Re: Lists inside Classes

Posted 04 March 2009 - 10:45 PM

View PostChoscura, on 4 Mar, 2009 - 02:46 PM, said:

hey everyone, just a quick very basic problem. I've got to keep track of a list of attributes (a la roleplaying games): each attribute, in turn, has a list of skills that add a number to that attribute if the skill is used, and a *third* layer of specialties that add a number to both the skill and the attribute.

I'm having trouble getting the lists to implement, however, and after 4 hours of banging my head into this, I hereby submit two thoughts to you to see which is the best way to go about this:


Let's not even talk about code, and I'm with JackOfAllTrades let's design this in detail, and then let's code.

Could you explain the above in detail. Just because it acts like a list doesn't mean you need to use a C# list,

have you thought about a Dictionary? Or do you know how many attributes you will have for certain? Maybe you need to use a linkedlist ?

I say we design this in detail before we start coding.

This post has been edited by coden4fun: 04 March 2009 - 10:46 PM

Was This Post Helpful? 0
  • +
  • -

#5 Choscura   User is offline

  • D.I.C Lover


Reputation: 470
  • View blog
  • Posts: 2,235
  • Joined: 18-October 08

Re: Lists inside Classes

Posted 05 March 2009 - 12:32 AM

I've considered a few options for this. I need to be able to store not only this, but eventually a few other arrangements of data. lists seemed to be the simplest implementation of this. I haven't looked at dictionaries yet, but I've considered linked lists (and think it's a good idea also) and dynamic arrays.

(note- please excuse inaccuracies or generalizations here, I haven't actually played these games in years)
the biggest problem is that this has to be dynamic. the program I'm working on is a framework for doing skill tests for various role-playing games on the computer, and since different games have different systems, this needs to change size sometimes. for example, the system in WoD games (eg, exalted, werewolf, vampire) divides attributes into seperate categories (physical, mental, social), each of which has three main attributes (eg, for physical, they have strength, dexterity, and constitution). skill checks are performed by adding the number on these base classes to the skill number and using a prescribed number of dice to tr to each a target number. some dice add towards reaching this number and some detract from it.

this system has to be somewhat more universal, though. one other game that I'm thinking of making this adapt to is called paranoia (excellent game, btw. think 1984 as written George Carlin). the system in this game is similar, but has a list of attributes, and each individual attribute has its own list of skills, and some (not all) of the skills have their own specialties. the way the game is played, however, some of the specialties can have their own specialties as well (eg- the attribute intelligence includes the engineering skill, the engineering skill has the nuclear physics specialty, and the nuclear physics specialty could theoretically have fission and fusion reactions as specialties).

the one data structure I keep coming up with to organize this completely is a tree, and I know- on a very basic theoretical level, with no practical knowledge- how this is done. it seems like a good way to do this- have the root node (the character), the attribute child nodes, the skill child nodes on top of that, and the specialties on top of that- where applicable- and then a blank node that can be filled in and made into a specialty coming out from each node on every level.

(to be continued...)
Was This Post Helpful? 0
  • +
  • -

#6 Choscura   User is offline

  • D.I.C Lover


Reputation: 470
  • View blog
  • Posts: 2,235
  • Joined: 18-October 08

Re: Lists inside Classes

Posted 05 March 2009 - 01:55 AM

(continued)

the long story short is I need to build this system first, though. right now, lists seem like the most practical short-term way for a FNG like me to do this. I'm looking into making dictionaries and trees now, though, and the list-derivative class that jackofalltrades suggested looks very interesting!
Was This Post Helpful? 0
  • +
  • -

#7 baavgai   User is online

  • Dreaming Coder
  • member icon


Reputation: 7181
  • View blog
  • Posts: 14,969
  • Joined: 16-October 07

Re: Lists inside Classes

Posted 05 March 2009 - 05:39 AM

You have Visual Studio? Play around with a DataSet. If you're going to have lots of objects that reference other instances and then perhaps want to ultimately save and retrieve that data, this would be the place to start.

It honestly looks more like a relational data design than an OO one. Even if you're going, "Yeah, but I just want to work with classes", that fine. Looking at the program from a data design perspective might still help.
Was This Post Helpful? 0
  • +
  • -

#8 Choscura   User is offline

  • D.I.C Lover


Reputation: 470
  • View blog
  • Posts: 2,235
  • Joined: 18-October 08

Re: Lists inside Classes

Posted 05 March 2009 - 08:33 AM

thanks for the tip, I'll play around with this a bit.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1