Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Card Game Player Class OOP Design

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.

like image 974
Zack Avatar asked Feb 10 '12 09:02

Zack


2 Answers

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.

like image 108
driangle Avatar answered Sep 18 '22 23:09

driangle


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

like image 31
Denis Biondic Avatar answered Sep 21 '22 23:09

Denis Biondic