OOP Design Game Class

I am going to create a class Player and AI Players (AIBasicPlayer, AINormalPlayer and AIHardPlayer) for my card game (gin rummy). What would be the best OOP approach or design pattern for creating these classes? I tested some of the open source card games and compared their approaches, the following approaches I have compiled:

***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 could not decide which one to apply or implement. I am new to OOP or design pattern, and your advice and sample code will be of great help.

+6
source share
2 answers

I would start with approach 3, it provides the least amount of grip between the two classes. If you find that there are many common functions, use approach 2 or extract this functionality to other classes that your IPlayer implementations IPlayer . I usually try to use composition over inheritance because it makes your code easier to change during the reorganization and more dynamic at runtime.

+7
source

I would choose the second option, because the player will have a certain functionality (and data), specific for both ordinary and AI players.

It should also be noted that I would also define the IPlayer interface that Player would execute

+4
source

Source: https://habr.com/ru/post/908130/


All Articles