To keep track of state and behavior, you need to define methods that take in one parameter, the "self" keyword, and prepend variables with "self". This allows us to refer to a specific object's variable or method.
For example, one instance of a game board can look different from another game board by passing in different column and row sizes. Python will keep track of the state (different game board size) and behavior (keeping track of the randomly placed mines).
As you can see, the steps listed align pretty well with the code block on the right. Python’s syntax is easy to read and write, which made it a breeze for me to get started quickly and easily. Keep reading to find out how I set up the game logic.
Define game board size
Start game
While game is not ended,
Place x and y coordinates
Show points
If user hits a mine,
End the game and show points
OOP Concepts
For those who work in the OOP paradigm, the keywords "this" (like Java and C++) and "self" (used in Swift, Ruby, and Python) usually refer to an instance of a class. These keywords allow us to access instance variables and methods while keeping track of state and behavior. Notice how I purposefully left out JavaScript in these examples. The "this" keyword can be a tricky concept to grasp when working with JavaScript, mainly because its behavior changes depending on the context. You need to be extra careful when using "this", as it can point to different things depending on how and where you call a function, not just where it's written.
Now, back to Python. The "self" keyword works just as you'd expect: it accesses instance variables and methods on an object of a class. It is directly tied to an instance of the class. If you're learning JavaScript and struggling with "this", you'll find that the concept in Python is a bit easier to grasp.