I'm currently attempting to put together a basic game engine for games involving playing cards. I've being doing some research and have realised that the approach I'm taking seems to be different to that taken by others posting online with respect to which classes the methods that manipulate decks (or any collection) of cards are contained within. I am seeking some advice on why one way seems to be preferred over another (mine...).
My current setup uses a generic CardList class that contains a variety of methods specifically designed to handle the 'deck' of cards, although it could also be used for player hands, etc. It is a general 'pile' of cards, that may or may not be ordered, and may or may not be visible to each player.
As an example, suppose I want a method to allow me to shuffle a deck of cards. My approach has been to include within the CardList class a public method shuffle() that shuffles the cards within that list. So it would be called as deck1.shuffle() from within the game loop.
What I am seeing is that others have set up a shuffling method within the main game loop and providing the deck they want to shuffle as an input. For instance they may instead use shuffle(deck1).
I am currently attempting (real code more complicated, of course):
public static void main(String[] args) {
// main game loop goes here
CardList deck1 = new CardList();
deck1.shuffle();
// more code
}
public class CardList {
// attributes and other methods
public void shuffle() {
// shuffle code
}
}
Whereas the more common approach seems to be along the lines of:
public static void main(String[] args) {
// main game loop goes here
Game game1 = new Game();
CardList deck1 = new CardList();
// more code
game1.shuffle(deck1); // or alternatively, within a method in the Game class
// more code
}
public class Game {
public void shuffle(CardList list) {
// shuffle code
}
// attributes and other methods
}
public class CardList {
// attributes and methods
}
So the question seems to be, should it be an inherent method within the object's own class, or should it be an external method that takes the object to be shuffled as an input?
I am keen to hear advantages/disadvantages to both approaches - I can't see why 2 would be preferred to 1 myself!