• 8 heures
  • Facile

Ce cours est visible gratuitement en ligne.



J'ai tout compris !

Mis à jour le 28/03/2024

Implement the Model for Your Application

Now for the fun part: we’re going to create a simple card game application together, following the MVC design pattern. After building a 52 standard playing card deck, we will have a selectable number of players and deal a card to each player with the highest card winning. 🃑 🂱 🃁

First, we begin coding and make changes as we go along. Later, we will develop some parts of the application further. If we follow sound coding principles and MVC, the impact of our changes will be minimal.

Here are the requirements for the application:

Gameplay Specifics

  • Create a standard 52 card deck.

  • Enter player names. Limit the number of players to five.

  • Shuffle the deck of cards.

  • Deal one card to each player (face down).

  • Flip over all players' cards, showing what card they have.

  • Check which player has the highest ranked card: Ace > King > Queen > Jack > 10 > . . . 2.

  • If tied, the winner is based on suit:
    Clubs > Spades > Hearts > Diamonds

  • Show the winning player name and card.

  • Put all cards back into the deck.

  • Jump back to the shuffle step.

How Do You Plan Out the Model?

Let's find the primary items players will view and interact with. The most straightforward way to find model objects is to read through the requirements, looking for the nouns. So looking at the description, you find a card deck (a collection of playing cards), players, card rank, and card suit.

But how do we connect players and their cards together? A player is going to possess a card during the game, right?

While not explicitly mentioned, the single card for each player goes in a Hand class. It follows the principle of single responsibility - one of the SOLID principles that you’ll encounter later.

A player consists of their name and hand. In this way, if the rules change, it's probably in the number of cards a hand holds or how the cards are manipulated. You shouldn't have to change the basic model of a player if you play a different game.

How Do You Implement the Model?

Now you will need to create Python objects for each of the model elements. Let's do it together! Open up a blank Python file in your editor and follow along as you watch the videos.

Step 1: Generate the Playing Cards

We’ve created:

  • A constant dictionary SUITS listing the suits.

  • A constant dictionary RANKS listing the ranks.

  • A PlayingCard class that has a rank and suit.

Step 2: Generate the Deck of Playing Cards

We’ve created:

  • A Deck class that contains all the playing cards and some methods to manipulate them.

Step 3: Generate the Players

We’ve created:

  • A Hand class that contains 0 or more PlayingCard objects.

  • A Player class that contains a name and a hand.

We’ll further modify the model later when you learn about SOLID principles! But first, we have to create the view and the controller.

Let’s Recap!

  • The model consists of the elements with which you interact. These contain the state information of the system.

  • To find your model objects, review the list of requirements.

  • In our application, we've defined that:

    • The model consists of a player, hand, playing card, deck, rank, and suit.

    • A player has a hand. A hand holds playing cards. A deck holds playing cards.

In the next chapter, you will learn how to control the sequence of the game events.

Et si vous obteniez un diplôme OpenClassrooms ?
  • Formations jusqu’à 100 % financées
  • Date de début flexible
  • Projets professionnalisants
  • Mentorat individuel
Trouvez la formation et le financement faits pour vous
Exemple de certificat de réussite
Exemple de certificat de réussite