Class design question

Hi,

I have a question about what would be the best way to implement this
functionality. I have a deck class which represents a deck of
something (for example cards), with the functionality you would
expect: cut, shuffle, draw, peek, etc. It basically wraps an array of
objects (for the deck class the objects themselves don't matter).
Now, I need to implement different decks of things for different games
or for different games within a game. Those decks don't usually have
other functionality themselves, although some might. They just provide
the objects that the deck will be composed of. For example:

class Deck

  def initialize

    @deck = []

  end

        # rest of methods: cut, draw, etc.
end

class AlhambraMoneyDeck < Deck
  
  def initialize

    super

    AlhambraMoney::types.each do |type|

      3.times do

        (1..9).each {|value| self << AlhambraMoney.new(type,value)
}

      end
    end
  end
end

Another example:

class DistrictsDeck < Deck
  def initialize
    super
    5.times {self << District.new("Taberna", 1, :green)}
    4.times {self << District.new("Mercado", 2, :green)}
    4.times {self << District.new("Tienda", 2, :green)}
    3.times {self << District.new("Almacen", 3, :green)}
    3.times {self << District.new("Puerto", 4, :green)}
    2.times {self << District.new("Ayuntamiento", 5, :green)}
                # ... many more (this is Citadels, in case you are wondering)
        end
end

I am not very happy with this way of doing things, since the
subclasses need to call super and then use the method << to add things
to the deck. As you can see it's not very pretty. What I would like to
ask is what good idiom/design I could use to ease the children
definition. I was thinking of using a class method on deck to pass an
array of objects like:

class TestDeck < Deck
    items %w{a b c d e}
end

But I'm worried the code could get messy if the creation of those
objects is a bit more complex, as in the cases I presented above.
Another idea is to call an items method in deck's initialize. The
method should return an array, and children classes would need to
override it:

class Deck
   def initialize
      @deck = items
   end
   def items
      []
   end
end

class TestDeck < Deck
   def items
      %w{a b c d e}
   end
end

Then, only thing is that subclasses should not override initizalize
without calling super, which I'm not sure how good/bad it is as a well
designed framework.

Well, that's the issue, any ideas are very welcome.

Regards,

Jesus.

Jesús Gabriel y Galán wrote:

Then, only thing is that subclasses should not override initizalize
without calling super, which I'm not sure how good/bad it is as a well
designed framework.

Well, that's the issue, any ideas are very welcome.

Regards,

Jesus.

Jesús

First, it's great to meet another EuroGamer.. (and just so you know,
Alhambra has already been done :slight_smile:

Anyways, none of your 3 decks differ in ANY way.. the only way they
differ is in their operation, not their structure which means it's not a
good candidate for sub classing.

i.e... the contents of the deck can be pulled out into a deck builder
(check out GOF builder/GOF factory) (probably some type of game class)
that would construct the deck for a particular game but the deck for
each game would have the same operations such as shuffle(), sort(),
draw(), etc..

hth

ilan

···

--
Posted via http://www.ruby-forum.com/\.

Take heart friends. We're out there. :wink:

I wiped the walls with the whole family in Dominion last night. I also played Agricola all last weekend, though I got as well as I gave in that.

James Edward Gray II

···

On Jan 21, 2009, at 3:26 PM, Ilan Berci wrote:

First, it's great to meet another EuroGamer..

Jesús Gabriel y Galán wrote:

First, it's great to meet another EuroGamer.. (and just so you know,
Alhambra has already been done :slight_smile:

Yep, it's good to know there are more like me. Although I use BGG a lot,
so I know a lot of people who are so addicted as me (or even more).
Probably most games I could do are already done, but when has that stopped
anyone to build their own version of something *cough* web server
*cough* web framework*
:smiley:

In any case, mi idea is build a little framework, DSL, something to
build these games
easily. And I want to build my own, because I like to practice my
Ruby, and: frameworks
are fun, applications are boring :-).

Anyways, none of your 3 decks differ in ANY way.. the only way they
differ is in their operation, not their structure which means it's not a
good candidate for sub classing.

Well, I have left out some methods I added to some decks, maybe they don't
belong there, as you say. But then I need a class to place them in. For example,
the AlhambraMoneyDeck has a method to insert the two scoring cards.

i.e... the contents of the deck can be pulled out into a deck builder
(check out GOF builder/GOF factory) (probably some type of game class)
that would construct the deck for a particular game but the deck for
each game would have the same operations such as shuffle(), sort(),
draw(), etc..

That's a great idea, thanks.

Jesus.

···

On Wed, Jan 21, 2009 at 10:26 PM, Ilan Berci <coder68@yahoo.com> wrote:

First, it's great to meet another EuroGamer..

Take heart friends. We're out there. :wink:

Yes we are! :smiley:

I wiped the walls with the whole family in Dominion last night. I also played Agricola all last weekend, though I got as well as I gave in that.

Ooo, agricola.rb anyone? My animeeples need to be taken out to pasture.

···

On Jan 21, 2009, at 3:37 PM, James Gray wrote:

On Jan 21, 2009, at 3:26 PM, Ilan Berci wrote:

First, it's great to meet another EuroGamer..

Take heart friends. We're out there. :wink:

I wiped the walls with the whole family in Dominion last night.

Dominion is on my radar, but I doubt my wife will like it (I play
mostly with her lately, waiting for having some more free time to meet
friend gamers, and for my two daugthers to grow up a little bit :-).

I also
played Agricola all last weekend, though I got as well as I gave in that.

I got Agricola this Christmas, still pending to play my first game. It
seems I have many of those in my shelf:

My latest games have been mostly Roma and Mr. Jack (both great games
for 2 players). In fact I'm building Roma, and I'm using my first
tries (never finished any of them) of building Alhambra, Citadels,
Ticket to Ride and Roma to abstract everything I need in a little
framework: components (deck, dice, etc), player management, turn
management (state machine), etc. My problem is, as usual, that I don't
have too much free time.

Cheers,

Jesus.

···

On Wed, Jan 21, 2009 at 10:37 PM, James Gray <james@grayproductions.net> wrote:

On Jan 21, 2009, at 3:26 PM, Ilan Berci wrote:

If you can wait, I don't know, maybe... forerever ! I'm planning on
building a game framework
and then implementing every game under the sun to play via web. :slight_smile:

Jesus.

···

On Wed, Jan 21, 2009 at 10:46 PM, Matthew Moss <matt@moss.name> wrote:

On Jan 21, 2009, at 3:37 PM, James Gray wrote:

On Jan 21, 2009, at 3:26 PM, Ilan Berci wrote:

First, it's great to meet another EuroGamer..

Take heart friends. We're out there. :wink:

Yes we are! :smiley:

I wiped the walls with the whole family in Dominion last night. I also
played Agricola all last weekend, though I got as well as I gave in that.

Ooo, agricola.rb anyone? My animeeples need to be taken out to pasture.

First, it's great to meet another EuroGamer..

Take heart friends. We're out there. :wink:

I wiped the walls with the whole family in Dominion last night.

Dominion is on my radar, but I doubt my wife will like it (I play
mostly with her lately, waiting for having some more free time to meet
friend gamers, and for my two daugthers to grow up a little bit :-).

For what it's worth, my wife loves it and is always wanting to play it. I'll admit that she's a heavy gamer herself though.

I also
played Agricola all last weekend, though I got as well as I gave in that.

I got Agricola this Christmas, still pending to play my first game.

It's excellent.

It definitely has a huge learning curve though. I didn't even understand what I should be doing too well until I played it for the fifth time or so.

It's worth the climb though. Probably the best resource management game I've played.

My latest games have been mostly Roma and Mr. Jack (both great games
for 2 players).

I wasn't familiar with those two. Mr. Jack looks pretty interesting to me. Thanks for the pointers.

I promise to shut up and let this off topic line die now. Sorry for the noise. :slight_smile:

James Edward Gray II

···

On Jan 22, 2009, at 3:38 AM, Jesús Gabriel y Galán wrote:

On Wed, Jan 21, 2009 at 10:37 PM, James Gray <james@grayproductions.net > > wrote:

On Jan 21, 2009, at 3:26 PM, Ilan Berci wrote: