I'm about to create the Player and AI Players (AIBasicPlayer,AINormalPlayer and AIHardPlayer) class for my card game (gin rummy). What would be the best OOP or Design Pattern approach for creating the said classes? I checked some of the open source card game and compare their approaches, following are the approaches I have gathered:
***Classes**
1. player class only
public class player{
}
public class AIPlayer{
}
2. base class player
public abstract class player{
}
public class HumanPlayer extends player{
}
public class APlayer extends player{
}
3. interface player
public interface IPlayer{
}
public class Player implements IPlayer{}
public class AIPlayer implements IPlayer{}
*** Methods**
takeTurn()
doDiscard()
doDraw() //pick from discard pile or deck
doKnock()
I understand the use of the above codes, but I couldn't decide which one to apply or implement.I'm new to OOP or Design Pattern and your advise and code sample would be a very big help.
I would start out with approach 3, it provides the least amount of cohesion between the two classes. If you find that there is a lot of common functionality then use approach 2, or extract that functionality into other classes which your IPlayer
implementations are composed of. I usually try to favor composition over inheritance because it makes your code more easy to modify when refactoring and more dynamic at runtime.
I would pick the second option, because player will have some functionality (and data) defined for both regular and AI players.
Also to note, I would also define an IPlayer interface which Player would implement
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With