我正在使用 OpenGL(加上 GLUT 和 GLUI)实现一个简单的棋盘游戏(突破)。
我正在考虑实现一个 Board
类,它将有一个 vector 。 >
作为其属性之一。 Cell
代表游戏板中的一个空间。它可以包含一个GameObject
。 GameObject
将是一个纯抽象类。例如,它要求其派生类实现 render() 。可能的派生类将是:
-
Blank
,代表一个空的空间
Pawn
,代表一个棋子(Breakthrough 中唯一可能的棋子)
棋盘将通过首先渲染棋盘来渲染,然后迭代每个Cell
,获取其内容并为每个单元调用render()
。
我能想到的实现此目的的唯一可能方法是将 Cell
中的 GameObject
设为指针 (board[y][x].getContents()-> ;render()
,其中 getContents()
返回 GameObject*
)
这是执行此操作的最佳方法吗?这是指针的正确用法吗?
I am implementing a simple board game (Breakthrough) using OpenGL (plus GLUT and GLUI).
I'm thinking of implementing a Board
class, which will have a vector<vector<Cell> >
as one of its attributes. Cell
represents a space in the game board. It can contain a GameObject
. GameObject
will be a pure abstract class. It mandates that its derivative classes implement render()
, for example. Possible derivative classes will be:
Blank
, representing an empty space
Pawn
, representing a pawn (the only possible pieces in Breakthrough)
The board will be rendered by first rendering the board, then iterating through each Cell
, getting its contents and calling render()
for each of them.
The only possible way I can think of to achieving this is making the GameObject
in Cell
a pointer (board[y][x].getContents()->render()
, where getContents()
returns the GameObject*
)
Is this the best way to do this? Is this an appropriate usage of pointers?
发布评论
评论(2)
让我将我的评论转化为答案。这并不意味着它在任何意义上都是完整的,只是这允许我阐明一些代码示例。我原来的评论:
我可能会这样做:
Let me promote my comment into an answer. This doesn't mean that it's in any sense complete, only that this allows me to spell out some code examples. My original comment:
Here's how I might go about this:
是的。
另外,也许你应该为你的细胞使用另一个容器(某种矩阵等)
Yes.
Also, maybe you should use another container for your cells (some kind of matrices or so)