OOP Programming: Classes & Objects

OOP-Class-ObjectIn this blog post we will investigate how to create some basic classes and objects that could be used in a range of programming projects.

Classes and Objects are the building blocks of Object-Oriented programs. You can find out about more about the key concepts of Object-Oriented Programming (OOP).

A class is a template or blueprint from which objects will be instantiated. It’s like the mould from which objects can be created/instantiated from. A class represents a real life concept such as a person, an animal, a car, a house or a football.

A class consists of a collection of states (a.k.a. attributes or properties) and behaviours (a.k.a. methods).

An object is an instance of a class. The attributes of an object as stored using variables for storing data and the behaviour/methods of an object are implemented as functions or procedures and can be used to perform operations on the data.

Most classes have one method called a constructor. It is a function that is automatically called when an object is created/instantiated from a class. It is mainly used to initialise the default values for each attribute/variable of the object. In Python the constructor of a class is the __init__() function.

Dice ClassPadlock ClassDeck of cardsCoin Class
Our first class will be used to create basic game of dice. The Dice class will have two properties: numberOfSides and value and one method throw() that will update the dice value by generating a random number between 1 and numberOfSides (e.g. between 1 and 6 for a standard 6-side dice).
Dice-Class

Our second class is used to create a padlock with a four digit key. The Padlock class has an unlock() method used to test a combination to try to unlock the padlock.

The key of the padlock is stored in a private property: __key (In Python the “__” prefix is used to indicate a private property or method.) The key can still be accessed from outside the class using the two public methods getKey() and setKey(). This is an example of encapsulation.
Padlock-Class

To create a deck of cards we will create two classes as follows:
Card-and-Deck-Class

In order to create a game of Head or Tail we will create a Coin Class using two properties: faceUp (which will be either Head or Tail) and value of the coin (in pence) and one method to flip the coin.
coin-class
We will then create a game with ten rounds and two players. For each round, a coin will be created using a random value (1p, 2p, 5p, 10p, 20p, 50p, £1 or 2£). After flipping the coin, if the visible face is “Head”, player 1 wins the value of the coin otherwise it’s given to player 2.

Programming Challenge

Use the two classes Card and Deck (see above) to implement the card game “War” between two players using the following rules:

  • The deck of card is evenly divided among two players.
  • Battle: In unison, both players reveals the top card of their deck and the player with the higher card takes both of the cards played and add them to the bottom of their deck.
  • Rank: Aces are high, and suits are ignored.
  • War: A war occurs when the two cards played are of equal value. Both players place the next card of their pile face down and then another card face-up. The owner of the higher face-up card wins the war and wins all the cards on the table. If the face-up cards are once again of equal value then the battle repeats with another set of face-down/up cards. This repeats until one player’s face-up card is higher than their opponent’s.
  • Game Over: The objective of the game is to win all of the cards. The first player to run out of cards in their deck looses the war!
Share Button
Tagged with: ,