CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: OOP Design

  1. #1
    Join Date
    Aug 2006
    Posts
    2

    OOP Design

    I have many years coding behind me, but I’m new to C# and OOP. I have read a book on C# and understand how to write classes etc.

    I am trying to write a card game (Not poker) that involves biding, and the playing of cards.

    I have the following classes and methods:-

    Class :
    Cards

    Methods:
    Shuffle
    DealCards

    Class :
    Player (Have an array to hold the cards it has been dealt)

    Methods:
    Bid
    Play Card

    Class: Game

    Method:
    NewGame (to control the game).


    The Game object creates a Cards object and 4 players objects which I put into an array so I can loop though for the bidding and card playing.

    My questions:-

    a)Logically the DealCards method should be in the Cards class, but the DealCards needs to know about the Players (number of players etc).
    1) Do I pass my array of Players to the DealCards method or
    2) should each Player have a GetDealtCards method or
    3) should the Cards object create a DealtCards Object and if so how will it know which player to pass it too?
    b) Is the general design correct or should I be using other objects and methods?
    c) Is there any good books or web sites that cover how a Object Orientated program should be designed?

    Thanks in Advance

  2. #2
    Join Date
    Apr 2005
    Posts
    576

    Re: OOP Design

    a, b) This is a difficult question, if you have a class horse and a class saddle, do you tell the horse to saddle itself, the saddle to put itself on a horse, or should you have a third class that saddles the horse? The answer is - it depends. Often maintainabiliity is an issue - you want to make the code easy to fix and make it easy to add new things to it. In other cases performance is an issue.

    I would probably use classes like
    - Card (a single Card),
    - CardCollection (many cards),
    - Deck (all cards in the Deck),
    - Player (has a CardCollection, knows the rules of the game, could be abstract or an interface to allow different player implementations for different games or strategies),
    - Game (sets everything up)

    Deck would need a Shuffle method. Maybe Deck should be responsible for dealing, but that could also be the responsibility of Game. If Deck does that, Game creates Players, and a Deck, passes players to the DealCards method of Deck that shuffles and then Adds cards to each player's CardCollection. If Game deals, then it would tell the Deck to shuffle itself, then draw cards and add them to each player's card collection.

    Which is best? If deck is to be used in solitaire games also, Deal will not be needed. But if many games are implemented, they would each need to implement DealCards (but that could be implemented in a base class for games). I think that you need to decide for yourself what is best.

    c) I have not read any such books for a long time, I would definitely look into CRC which is a simple technique for doing object oriented designs, Grady Booch's book Object oriented analysis and design for applications is a good one I have read, 3rd edition came a few years ago.

  3. #3
    Join Date
    Aug 2003
    Location
    London
    Posts
    515

    Re: OOP Design

    Have a check on CodeProject, there are some nice articles on exactly this thing...

    This one might be useful to you.
    If it helped, then please rate the post by clicking "Rate this post"!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured