Blog: R7D15: #100DaysOfCode (More Posts)
Photography
- Added “Animals” +4 making a total of 41
- Added “Birds” +5 making a total of 84 (showing 85)
- Added “Birds in flight” +4 making a total of 32
- Added “Boo” +4 making a total of 36
- Added “Coastal:” +4 making a total of 21
- Added “Flowers and Plants” +3 making a total of (44)
- Added “Weather” +2 making a total of 19
- Added “wildlife on doorstep” +3 making a total of 34 – Showing 32
- Added “Woodland” +9 making a total of 41
Game Class in Python Coding
Following on from where my work with the simple Python Memory game using OOP Python, the work continues with the initial development of the Game() class. We’ve already done the card class which thrashes out some tests about how the cards work in a command-line setting. We were satisifed that the tests indicated that everything so far was working as it should be.
The second class; the Game() class controls the program flow of the game itself. We start with a class called Game, and set the options and specifications for the game grid at the point that an instance of the class is initialised.
class Game:
def __init__(self):
self.size = 4
self.card_options = ['Add','Boo','Cat','Dev','Egg','Far','Gum','Hut']
self.columns = []
self.cards = []
self.locations = []
for column in self.columns:
for num in range(1, self.size + 1):
# print(f'{column}{num}')
But there was one other thing that stuck out to me was that when an instance is initialised it is surrounded by the following code.
if __name__ == '__main__':
Game()
The __name__ == “__main__” line is a condition statement runs blocks of code only when the Python script has been set to run by the user such as say a python script takes in arguments from the command line interface or even just running the filename as a command option in the CLI.
This is where we’ll run the game and perform any tests in code that we need to do check the game is working.
You’ll also notice in the Game class I commented out the print() method inside the loop that prints the range of cards and locations. This is the syntax for a version of Python that annoyingly supersedes the version I’m using on Git Bash. What I wanted to do was replicate the string output for as many versions of Python as possible.
And one way I’ve done this is using the format method with print.
print('{}{}').format(column, num)
I then use that string formatting to append the values passed in to the locations list instead of merely printing it.
self.locations.append('{}{}').format(column, num)
A Crucial Part of OOP is knowing to bring in code from another class so they can be kept in separate files. Python reference to these as modules. Here we’re telling Python to bring the things we use in the Card class in card.py over into game.py so we can use them there.
from the cards file import the class Card
from cards import Card
import random
There’s a lot to unpack in the next function. We set the cards by making sure we’re not using a card that has already been used in the next turn. We do this by making a new iterable to store iterable locations, converting a list to a set and then back again subtracting the data from one and leaving the rest of the data.
def set_cards(self):
used_locations = []
for word in self.card_options:
for i in range(2):
available_locations = set(self.locations) - set(used_locations)
random_location = random.choice(list(available_locations))
used_locations.append(random_location)
card = Card(word, random_location)
self.cards.append(card)
I ran into a NameError at this point with Python telling me that the Game class was not defined.
Traceback (most recent call last):
File "game.py", line 5, in <module>
class Game:
File "game.py", line 32, in Game
game = Game()
NameError: name 'Game' is not defined
This would seem like a good point to pause and take a break and look at this with fresh eyes tomorrow. I’m not entirely sure why Python would have trouble identifying this as a simple instance of a class and what the error can be traced to. Line 5 is simply the class declaration and line 32 is the instance.


