Answer the question
In order to leave comments, you need to log in
How to organize code through OOP for a simple task?
They gave me an assignment. Apply all OOP principles in Minesweeper game code.
I wrote the game, created two classes: "Field", where all the logic is processed, and the "Game" class, where the client calls the necessary methods and receives the result. Objects are present, encapsulation too, but I don’t know how to apply inheritance and polymorphism.
How would you do the task?
Answer the question
In order to leave comments, you need to log in
Well, at least you can add such a hierarchy: the base class Cell with the virtual method Open, the classes MineCell and EmptyCell are inherited from it. The MineCell.Open method ends the game, EmptyCell.Open opens the cell and shows the number of mines around.
In fact, by writing a program with a GUI, you, in most cases, have already used all the mechanisms of OOP, even if you yourself have not written a single class. But, I think that this answer will not satisfy your teacher, so here are a few options:
1. Inherit from basic graphic elements and create: a class of the playing field, cell, cell content ... And where fantasy leads. The drawing method will be polymorphic, at least. In my opinion, the option is bad.
2. Inherit from the button class and implement the appropriate behavior: put a checkbox there on the right click, etc. Well, when generating the field, the buttons will simply be placed in the right order. Here you may have to override the event handling method for the button, or do something similar...
3. You have a game class, so add the IGame ancestor class, with a virtual play () method. This will quickly and decisively allow the use of the missing principles of OOP. The shortest and saddest option) But the goal is to hand over the work, as I understand it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question