Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Question about object oriented design with Ruby

Tags:

oop

ruby

I'm thinking of writing a CLI Monopoly game in Ruby. This would be the first large project I've done in Ruby. Most of my experience in programming has been with functional programming languages like Clojure and Haskell and such. I understand Object Orientation pretty well, but I have no experience with designing object oriented programs.

Now, here's the deal.

In Monopoly, there are lots of spaces spread around the board. Most spaces are properties, and others do other things.

Would it be smart to have a class for each of the spaces? I was thinking of having a Space class that all other spaces inherit from, and having a Property class that inherits from Space, and then having a class for every property that inherits from Property. This would mean a lot of classes, which leads me to believe that this is an awful way to do what I'm trying to do.

What I also intended to do, was use the 'inherited' hook method to keep track of all the properties, so that I could search them and remove them from the unbought list when needed.

This sort of problem seems to arise in a lot of programs, so my question is: Is there a better way to do this, and am I missing something very crucial to Object Oriented design?

I'm sorry if this is a stupid question, I'm just clueless when it comes to OOPD.

Thanks.

like image 752
Rayne Avatar asked Oct 15 '09 14:10

Rayne


1 Answers

You're on the right track, but you've gone too far, which is a common beginner's mistake with OOP. Every property should not be a separate class; they should all be instances of the Property class. I'd do classes with attributes along these lines:

Space

  • Name
  • Which pieces are on it
  • Which space is next (and maybe previous?)
  • Any special actions which take place when landing on it

Property (extends Space)

  • Who owns it
  • How many houses/hotels are on it
  • Property value
  • Property monopoly group
  • Rent rates
  • Whether it is mortgaged

So, for example, Boardwalk would be an object of type Property, with specific values that apply to it, such as belonging to the dark blue monopoly group.

like image 156
Pesto Avatar answered Oct 20 '22 01:10

Pesto