I'm writing an Ultimate Tic-Tac-Toe engine as a programming exercise (for myself, not homework) and I have two similar classes. One is a class MiniBoard
that contains a single normal Tic-Tac-Toe board, and another Board
that is itself a large Tic-Tac-Toe board conisiting of 9 MiniBoards
, one for each large cell in the game.
I also have a function int getState( ... )
that returns a certain number depending on whether a board is won, lost, drawn or in-progress. Since there is obvious symmetry between MiniBoard
and Board
it appears to me that I should be able to make my getState
function accept both MiniBoard
and Board
as an argument, afterall they both must be either won, drawn, lost or in-progress.
At first I thought to make Board
inherit from MiniBoard
, but this approach turned out unnecessarily complicated because the functionality of MiniBoard
is fundamentally different from Board
, even though they do represent very similar objects. It also doesn't really follow the "IS A" relationship of OOP inheritance.
The idea that I currently have, but don't know if is any good, is to make an abstract base class BaseBoard
that defines the common properties and methods as virtual and then have Board
and MiniBoard
derive from BaseBoard
. Then I would define my function for getting the state as:
int getState(const BaseBoard &board)
The main issue I have with this approach is that I don't know upfront what kinds of properties and methods will I need for my BaseBoard down the line. And if I ever get the the point of having a large chain of inheritance that starts with BaseBoard and wanting to change something, that would mean I would have to change every class definition down the chain of inheritance. What is the solution to this issue? Just derive more classes?
All in all, is this a good way to solve the problem? And what are the pros and cons? I'm not very familiar with abstract base classes in C++. Any help is appreciated.
Aucun commentaire:
Enregistrer un commentaire