Class design question

  • Thread starter Jesús Gabriel y Galán
  • Start date
J

Jesús Gabriel y Galán

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.
 
I

Ilan Berci

Jesús Gabriel y Galán said:
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
 
J

James Gray

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
 
M

Matthew Moss

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.
 
J

Jesús Gabriel y Galán

Jes=FAs Gabriel y Gal=E1n 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 exa=
mple,
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.
 
J

Jesús Gabriel y Galán

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/jgabriel?own=1&ff=1

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.
 
J

Jesús Gabriel y Galán

Yes we are! :D



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.
 
J

James Gray

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 =20
it. I'll admit that she's a heavy gamer herself though.
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 =20
understand what I should be doing too well until I played it for the =20
fifth time or so.

It's worth the climb though. Probably the best resource management =20
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 =20
to me. Thanks for the pointers.

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

James Edward Gray II
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top