Skip to content

C# Card Games: Architecture, System Design, and UML

Overview

In the last step, Intermediate C# Programming: Card Games Introduction, you were challenged to list the patterns you see across several card games and the properties of a single card. The lists you created will help you identify classes, attributes, and operations to include in UML diagrams. The UML diagrams you create will give you insight into how your system will be structured (before you begin coding). You can also get an idea of the type of components you'll want to build out in your system.

C# Card Games Patterns

What patterns did you find across multiple card games? In the expandable section below are card and game patterns that others have identified. Did you find patterns others haven't? Are there any patterns that you think are important that you didn't have on your list?

Game Patterns Examples

Card

  • Card value (and whether certain cards such as Aces have alternative values)
  • Card number
  • Card suit

Game

  • # of cards in deck
  • Card value (and whether certain cards such as Aces have alternative values)
  • Card number
  • Card suit
  • Comparing cards (value/number/suit)
  • Deal
  • Dealer
  • Deck(s)
  • Discarding cards
  • Game names
  • Game rules
  • Goals/Objectives/Win conditions (for example, discarding all cards in your hand before everyone else)
  • Minimum/maximum number of players
  • Origin (of game and/or deck)
  • Player hand
  • Player(s)
  • Playing cards
  • Points
  • Random
  • Ranks/Face cards
  • Sequences/combinations/melds
  • Showing/revealing cards
  • Shuffle
  • Strategies
  • System
  • Total points (or adding up points)
  • Turn(s)

Declarative Statements

Writing declarative statements can help you design your system architecture. There are four main types of sentences categorized by function: declarative, interrogative, imperative, and exclamatory. A declarative sentence is informational or states a fact or opinion. It's often formatted as [subject] [verb] [object]. Example: The water is blue.

If you are new to object-oriented design it may be daunting to create a system. What parts of the system should be classes? What are the relationships between the classes? To determine what classes to make, and what the attributes and operations the classes should have, write out several declarative sentences.

This is an important step because you are starting to express your mental model of the system you want to create. There is no "one right way" to create a system like this.

Example

Here are a few declarative sentences:

  1. The card game system has several card games.
  2. The card game system deals cards to the player.
  3. Each card game has a name, instructions, game rules, a win condition, a deck of cards, and at least one player.

Your sentences might be different, and that's okay. This is just one way of thinking about a card game system.

Evaluate Nouns

Based on these example sentences we can start to identify nouns. Nouns are a good place to begin thinking about classes.

Nouns:

  • card game system
  • card game(s)
  • player
  • card(s)
  • name
  • instruction(s)
  • rule(s)
  • win condition
  • deck

Of these nouns, which are ones that model things that have identity, behavior, or a lifecycle? Which are ones that are just information?

Guideline:

  • Class: model
  • Attribute (Property): information

Based on this guide, the nouns that are potentially just information are name, instructions, game rules, and win condition. These can be attributes of a class. The rest of the nouns can be classes.

Evaluate Verbs

Next, look for verbs that could be operations. For example, in the second example declarative sentence the card game system deals cards to the player. This is an excellent candidate for an operation (class method).

To Do: Write Declarative Sentences

Assignment

  1. Write out as many declarative sentences as you can.

Draft Structure

The example sentences color-coded by class, property, and method:

  1. The Card Game System has several Card Games.
  2. The Card Game System Deals Cards to the Player.
  3. Each Card Game has a Name, Instructions, Game Rules, a Win Condition, a Deck of Cards, and at least one Player.

Based on these example sentences, here is a draft outline of system classes.

  • System
    • Attributes:
      • collection of game instances (card games)
      • player (or collection of players if multi-player)
    • Operations:
      • deal
  • Game
    • Attributes:
      • name
      • instructions
      • rules
      • win condition (this could also be an operation)
    • Operations:
  • Card
    • Attributes:
    • Operations:
  • Player
    • Attributes:
    • Operations:
  • Deck
    • Attributes:
      • collection of card instances
    • Operations:

To Do: Create a Draft Structure

Create an outline structure. Don't worry about making it perfect. As we build out the system you can improve it.

If you haven't created many systems or object-oriented applications you might not yet know everything that you will want to include and that's okay. Many programmers say they don't fully understand object-oriented programming until they've worked with it for a few years. It's okay to make something that isn't perfect while you are learning.

Assignment

  1. Use your declarative sentences to create a draft structure for the system.

C# Card Games UML

Build out the draft outline into classes. For example, if you want to have a class that will be a template for any card in any card game, you can group together the patterns you've identified that relate to the concept of "a card".

Card Class Attributes

  • Value
  • Suit
  • Name (i.e., one-eyed Jack)

There are other data structures and ways of organizing patterns that can also work aside from just classes. Right now we are staying with classes to keep this simple, but once you've finished building your first version and you are ready to refactor and improve your code, you can look at other structures such as structs and interfaces.

Card Game Class Examples


Below are some patterns that could be grouped together into a template for a card game.

Examples of Class Attributes

  • Game name
  • Number of players
  • Number of cards in the deck (and/or number of decks)
  • Rules of the game
  • Cards in the deck (set of instances of a Card class, each with suit/value/number/name)
  • Discard pile (set of instances of a Card class)
  • Player(s)
  • Number of cards a player is dealt (a player's hand)
  • Win condition

Examples of Class Operations

  • Deal
  • Shuffle
  • Draw
  • Discard
  • Play/Place card
  • Compare cards
  • Win
  • Lose
  • Fold
  • Begin game (and restart game)
  • End game

UML Diagrams

There are many ways to create UML diagrams. If you are using Visual Studio and have the Class Designer installed, you can create diagrams in a Visual Studio project. Visual Studio can then create code from your diagrams. This approach allows you to visualize the system before coding.

You can also write code first, then have Visual Studio create UML diagrams from your code, or update the diagrams you've created previously.

To Do: Create UML Diagrams

Create UML diagrams of classes based on the draft structure you outlined.

Assignments

  1. If you haven't used UML before, read UML Introduction.
  2. Use your declarative sentences, the draft structure, and the patterns you've identified to build UML diagrams of your system structure.

Next Step

Use the UML you've created to build out the first draft of your application code.