Looking for Opinions on my first Class

Class for Windows Form Cop RPG

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 860 Views - Last Post: 21 January 2010 - 04:23 PM Rate Topic: -----

#1 Carson2006  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 18-January 10

Looking for Opinions on my first Class

Post icon  Posted 18 January 2010 - 06:01 PM

Hello.

This would be my very first piece of code that I've come up with on my own. As a learning tool, I have decided to start making a RPG in a form application. I realize that this can, and most likely will be, an incredibly difficult task for someone as new to programming as myself. I do plan on doing side projects to test ideas before I implement them into my game, but this will be the background project that gives me direction.

With that said, I think I may have created my first class. I would really appreciate it if anyone could look this over and tell me if I'm not even close.

(BTW the game is going to be a Cop Simulation, The end result I have in mind is crimes/situations that are generated somewhat randomly. The longer your career, the better officer you become, ect...)

Here it goes!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CopGame
{
	public class Character
	{
		private string _firstName;
		private string _lastName;
		private int _health;
		private int _intelligence;
		private int _dexterity;
		private int _speed;
		private int _aim;
		private int _luck;
		private int _strength;

		public string FirstName
		{
			get { return _firstName; }
			set { _firstName = value; }
		}
		public string LastName
		{
			get { return _lastName; }
			set { _lastName = value; }
		}
		public int Health
		{
			get { return _health; }
			set { _health = value; }
		}
		public int Intelligence
		{
			get { return _intelligence; }
			set { _intelligence = value; }
		}
		public int Dexterity
		{
			get { return _dexterity; }
			set { _dexterity = value; }
		}
		public int Speed
		{
			get { return _speed; }
			set { _speed = value; }
		}
		public int Aim
		{
			get { return _aim; }
			set { _aim = value; }
		}
		public int Luck
		{
			get { return _luck; }
			set { _luck = value; }
		}
		public int Strength
		{
			get { return _strength; }
			set { _strength = value; }
		}
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Looking for Opinions on my first Class

#2 finaiized  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 29-November 09

Re: Looking for Opinions on my first Class

Posted 18 January 2010 - 06:04 PM

What do you mean by close? The code doesn't do anything, but yes, you set up the class as it should be. You might even want to derive Cop and Bad Guy from the character class.
Was This Post Helpful? 1
  • +
  • -

#3 Carson2006  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 18-January 10

Re: Looking for Opinions on my first Class

Posted 18 January 2010 - 06:12 PM

View Postfinaiized, on 18 Jan, 2010 - 05:04 PM, said:

What do you mean by close? The code doesn't do anything, but yes, you set up the class as it should be. You might even want to derive Cop and Bad Guy from the character class.


Sorry,

I do realize that this post is about as Newb as it gets. But I'm very unsure about what I'm doing so as this is the first thing that I've wrote, without copying word for word off of a video or text, I figured it wouldn't hurt to have a pair of experienced eyes look it over real quick. Thanks for your time!
Was This Post Helpful? 0
  • +
  • -

#4 finaiized  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 29-November 09

Re: Looking for Opinions on my first Class

Posted 18 January 2010 - 06:16 PM

View PostCarson2006, on 18 Jan, 2010 - 05:12 PM, said:

Sorry,

I do realize that this post is about as Newb as it gets. But I'm very unsure about what I'm doing so as this is the first thing that I've wrote, without copying word for word off of a video or text, I figured it wouldn't hurt to have a pair of experienced eyes look it over real quick. Thanks for your time!

Experienced, no. :P But it looks great so far. I would agree making a C# game in Windows Forms is pretty big task especially your first... I'd sort of want to point you out to the XNA Framework made by Microsoft- it has some useful math functions and all sorts of stuff for games. That's your choice though.
Was This Post Helpful? 0
  • +
  • -

#5 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: Looking for Opinions on my first Class

Posted 18 January 2010 - 07:15 PM

Hi Carson.

One recommendation I'd like to give you is that you can use shorthand code for declaring get/set methods for your class attributes.

public string CharacterName { get; set; }

That line of code works exactly the way your properties work, only makes neater code. There are of course some limitations but for a novice, you can safely ignore them for now. :)
Was This Post Helpful? 0
  • +
  • -

#6 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Looking for Opinions on my first Class

Posted 18 January 2010 - 07:35 PM

Yes, short hand is nice but you will want to keep with what you have, to be honest. Exposing your fields through properties like you have done allows for verification down the line. Also, it is in line with the idea of encapsulation. Validating strings can be tricky but with numbers it is easier. You wouldn't want to have a negative health or an aim of 100,000 if your maximum is 25. I would also agree with the idea of deriving cops and bad guys from this class. One field that you might want to add in is a current health field. In RPGs characters usually have a maximum health and a current health. The current health would decrease if they are injured and increase as they heal. Those are just a few notes, but you are going in the right direction.

Write it right the first time and you won't have to go back and write it again.
Was This Post Helpful? 0
  • +
  • -

#7 finaiized  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 29-November 09

Re: Looking for Opinions on my first Class

Posted 18 January 2010 - 07:37 PM

Like sixOfEleven said, planning is probably going to be the biggest process for you. Pen and paper and a quick graph is a useful thing to do. I did it once and it totally paid off :P
Was This Post Helpful? 0
  • +
  • -

#8 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: Looking for Opinions on my first Class

Posted 18 January 2010 - 08:18 PM

View PostSixOfEleven, on 18 Jan, 2010 - 06:35 PM, said:

Yes, short hand is nice but you will want to keep with what you have, to be honest. Exposing your fields through properties like you have done allows for verification down the line.



Can you explain this a bit more. Does using inline properties not allow verification? I see no difference tbqh.
Was This Post Helpful? 0
  • +
  • -

#9 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Looking for Opinions on my first Class

Posted 18 January 2010 - 09:09 PM

Properties are C#'s answer to get and set accessor method from Java and C++. When you create a property you can perform validation in the set part, unless it is inline. This follows one of the principles of object-oriented programming encapsulation. The person using your classed doesn't, or shouldn't, need to know how the class works. Look at the simple class belowfor a test. You want to restrict the score of the test so it is in a valid range. There is no way to do that with an inline property. If you try and do validation the compiler will throw an error. You could use inline properties and validate the values passed to the class but it should be the other way around. The class should be responsible for validating.

	class Test
	{
		int testScore;

		public int TestProperty
		{
			get { return testScore; }
			set 
			{
				if (value < 0 || value > 100)
					throw new IndexOutOfRangeException();

				testScore = value; 
			}
		}

		public int TestInlineProperty
		{
			get;
			set;
		}


Was This Post Helpful? 0
  • +
  • -

#10 Charles:)  Icon User is offline

  • D.I.C Regular

Reputation: 142
  • View blog
  • Posts: 346
  • Joined: 26-November 09

Re: Looking for Opinions on my first Class

Posted 19 January 2010 - 06:39 AM

For a game like this it would be good to raise events when certain properties change. For example, create an event called HealthChanged with a custom EventArgs class which contains the previous health value and the current health value, and raise this event everytime the health changes.

In your game you can then add a handler to this event and the event args will make it easy to find out how much the health has changed by.
Was This Post Helpful? 0
  • +
  • -

#11 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: Looking for Opinions on my first Class

Posted 19 January 2010 - 08:29 AM

View PostSixOfEleven, on 18 Jan, 2010 - 08:09 PM, said:

Properties are C#'s answer to get and set accessor method from Java and C++. When you create a property you can perform validation in the set part, unless it is inline. This follows one of the principles of object-oriented programming encapsulation. The person using your classed doesn't, or shouldn't, need to know how the class works. Look at the simple class belowfor a test. You want to restrict the score of the test so it is in a valid range. There is no way to do that with an inline property. If you try and do validation the compiler will throw an error. You could use inline properties and validate the values passed to the class but it should be the other way around. The class should be responsible for validating.

	class Test
	{
		int testScore;

		public int TestProperty
		{
			get { return testScore; }
			set 
			{
				if (value < 0 || value > 100)
					throw new IndexOutOfRangeException();

				testScore = value; 
			}
		}

		public int TestInlineProperty
		{
			get;
			set;
		}




Oh! That's what I meant by 'it has limitation', I just got confused when you called it validation. :P Yeah, using get/set in shorthand only is useful for getting and setting, nothing more.
Was This Post Helpful? 0
  • +
  • -

#12 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 526
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: Looking for Opinions on my first Class

Posted 19 January 2010 - 09:54 AM

I personally would encapsulate most of those fields into another class. Essentially, all the things that are related to stats should probably be in a stats class.

public class PlayerStatistics
{
	public int Intelligence
	{ get; private set; }

	public int Dexterity
	{ get; private set; }

	public int Speed
	{ get; private set; }

	public int Aim
	{ get; private set; }

	public int Luck
	{ get; private set; }

	public int Strength
	{ get; private set; }
}



Then update the player class:

public class Character
{	
	public string FirstName
	{ get; private set; }
	
	public string LastName
	{ get; private set; }
	
	public int Health
	{ get; private set; }
	
	public PlayerStatistics Statistics
	{ get; private set; }
	
	public Character() : this(String.Empty, String.Empty, new PlayerStatistics())
	{
	
	}
	
	public Character(string firstName, string lastName, PlayerStatistics playerStats)
	{
		FirstName = firstName;
		LastName = lastName;
		Statistics = playerStats;
	}
}



I know there was some banter around your properties. I kinda subscribe to the idea that if they currently don't need validation or a backing field at all, to just go with get; private set;. When I know otherwise, it's a pretty easy change.

So, now you can do something like a level up class too.

public static class LevelUpEngine
{
	public static void IncreaseStats(PlayerStatistics stats)
	{
		// increase each stat by some amount.
	}
}



Make sense?

PS: All these RPG posts is getting me kinda antsy to make my own - but I know what an endeavor that is... decisions decisions...
Was This Post Helpful? 0
  • +
  • -

#13 Carson2006  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 18-January 10

Re: Looking for Opinions on my first Class

Posted 19 January 2010 - 11:20 AM

View PostMentalFloss, on 19 Jan, 2010 - 08:54 AM, said:

I personally would encapsulate most of those fields into another class. Essentially, all the things that are related to stats should probably be in a stats class.


I had to look that post over a few times to digest what you did there. lol.
Not sure I understand everything you did, but for the most part I see what you mean.

I hadn't decided exactly how I was gonna attack leveling, making a leveling engine in a class seems like a good option.

Would this make it more complicated to use the same class to create NPC's as well as the Main Character?

I seem to still have a misunderstanding of how classes work. Currently I have an TitleWindow that hides when you click a button "New Recruit". This also opens a new form called Character Creation. I seem to be having trouble getting radio buttons to effect the base stats, and showing in text boxes (btw I added an = 10 next to all the private int's). When the form is opened it creates a new Character: "Player". The text boxes display the base 10's, but the radio buttons do not seem to see the reference. I'm trying to let the player select from a choice of 3 character backgrounds.

Not looking for a straight out answer, maybe a suggested link. I had a vague Idea of what I was getting myself into, and I don't wanna get in the habit of having you guys walk me through it. :-P

This post has been edited by Carson2006: 19 January 2010 - 11:31 AM

Was This Post Helpful? 0
  • +
  • -

#14 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,599
  • Joined: 25-December 08

Re: Looking for Opinions on my first Class

Posted 19 January 2010 - 11:43 AM

Generally you would use inheritance to handle the similarities between your your main character (the cop) and the NPC's (robbers for example). Now all Cops and robbers, while having differently still are both human and therefore share simiilar attributes. You would want to create 3 classes to hand this, Human.cs, Cop.cs, Robber.cs. the Cop and Robber class would inherit the Human class. The human class would store all of the str,dex,hp, etc.. attributes. that way every time you create a cop or a robber they will have a set of attributes.

now for attacking, this is where OOP comes in very handy. All you have to do is, once again in your human class you would have some method liek this (making it virtual if you want later).

public void attackTarget( Human h ){
	h.Health -= this.Strength;
}



then later in your program where you actually tell the cop to attack you can have something like this:

Cop c = new Cop();
Robber r = new Robber();
Console.WriteLine( r.Health ); //ex health is 100
c.attackTarget( r );
Console.WriteLine( r.Health); //ex health will be 90


This post has been edited by SwiftStriker00: 19 January 2010 - 11:44 AM

Was This Post Helpful? 0
  • +
  • -

#15 Carson2006  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 18-January 10

Re: Looking for Opinions on my first Class

Posted 19 January 2010 - 12:17 PM

Well in that case I'll most likely keep this as the Human class, and create a new Cop, Civi, and Criminal classes.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2