M
M
Maxximu52014-07-31 18:17:59
Programming
Maxximu5, 2014-07-31 18:17:59

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

2 answer(s)
S
Sergey Mozhaykin, 2014-07-31
@smozhaykin

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.

V
Vitaly, 2014-08-01
@vt4a2h

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 question

Ask a Question

731 491 924 answers to any question