Forum: Ruby Class design question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E088bb5c80fd3c4fd02c2020cdacbaf0?d=identicon&s=25 Jesús Gabriel y Galán (Guest)
on 2009-01-21 19:55
(Received via mailing list)
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.
391f9b787cdc12aa2c179713f5103e3a?d=identicon&s=25 Ilan Berci (iberci)
on 2009-01-21 22:27
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  :)

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
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2009-01-21 22:39
(Received via mailing list)
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.  ;)

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
A61ecce13ed142622f24a5ca3a123922?d=identicon&s=25 Matthew Moss (Guest)
on 2009-01-21 22:49
(Received via mailing list)
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.  ;)

Yes we are!  :D


> 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.
E088bb5c80fd3c4fd02c2020cdacbaf0?d=identicon&s=25 Jesús Gabriel y Galán (Guest)
on 2009-01-22 10:35
(Received via mailing list)
On Wed, Jan 21, 2009 at 10:26 PM, Ilan Berci <coder68@yahoo.com> wrote:
> 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  :)

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*
:D

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.
E088bb5c80fd3c4fd02c2020cdacbaf0?d=identicon&s=25 Jesús Gabriel y Galán (Guest)
on 2009-01-22 10:41
(Received via mailing list)
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:
>
>> First, it's great to meet another EuroGamer..
>
> Take heart friends.  We're out there.  ;)
>
> 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:

http://www.boardgamegeek.com/collection/user/jgabr...

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.
E088bb5c80fd3c4fd02c2020cdacbaf0?d=identicon&s=25 Jesús Gabriel y Galán (Guest)
on 2009-01-22 10:42
(Received via mailing list)
On Wed, Jan 21, 2009 at 10:46 PM, Matthew Moss <matt@moss.name> wrote:
>
>
>> 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.

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. :-)

Jesus.
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2009-01-22 14:40
(Received via mailing list)
On Jan 22, 2009, at 3:38 AM, Jesús Gabriel y Galán wrote:

> 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.  :)

James Edward Gray II
This topic is locked and can not be replied to.