0

我必须实现一个棋盘游戏,它有 17 个位置,它像链表一样“链接”。

所以,我打算为此使用 ArrayList 的 LinkedList。(LinkedList<ArrayList<String>>或者……)

但是,它有两个起始位置,在几个节点之后合并,玩家可以选择他们想要从哪个位置开始。(这意味着两个节点的“下一个”字段指向一个节点,一个节点可能(但不是必需的,我猜?)需要两个“prev”字段。)

另外,我想让最后一个位置旁边的附加节点的“下一个”字段指向自身。

简而言之,我想要一个类似于 LinkedList 的集合类,但有一些突变。

我怎么能做到这一点?

我的编程水平是初级~中级,我研究过类似于Java Collections Framework的LinkedList定义(但不记得太多了......),以及使用教科书的c++ std::list。

所以我的问题是这些。

我是否有必要实现一个简单的列表(和节点)类,只创建 18 个节点并以我想要设置的方式设置这些节点的私有字段,并将 ArrayLists 添加到其“数据”字段中?

另外,我打算使用 Stack 的 LinkedList(因为我只需要最新推送的元素),但我觉得如果我只是创建私有 ArrayList 类并根据我的喜好定制它会更有用,以类似于实现 Stack 的方式使用 ArrayList 类。听起来怎么样?

我担心的是我不是一个很好的程序员,如果我自己定义它们,它们可能不可靠,而且工作也可能非常耗时。(它有截止日期)

有没有更好的方法来实现这一点?(比如,也许扩展标准类??不确定..)

任何建议将不胜感激。

谢谢你。

4

1 回答 1

1

恐怕您对您正在尝试做的事情(即您的要求)的描述太难以理解。所以我将自己限制在一个一般性的答案上。

如果您真的需要能够对类似列表的数据结构进行“手术”,那么您应该从头开始自己实现它/它们。

尝试通过扩展现有的列表/集合类来做到这一点可能会因几个原因而失败(至少)。

  • Some of the stuff that your code needs to tinker with is declared as private, and that makes it very difficult (and highly inadvisable) to tinker with it. (It is not impossible, but you'd need to do some nasty reflection tricks and that would make your code complicated, fragile and inefficient. Its is not the sort of thing you should do, unless there is absolutely no other alternative.)

  • Even if you succeeded, you'd have a data structure that nominally implements one or more existing collection interfaces, but (most likely) breaks the contract of how that interface is supposed to behave.


What I am worried about is that I am not a very good programmer, and if I define them by myself, they can be unreliable, and the job may be very time-consuming too. (it has deadline)

  1. The "point of the exercise" is to learn to be a better programmer ...
  2. IMO, you are likely to spend more time doing the work to avoid the extra work, and the end result would be less reliable
  3. Deadlines are a worry, but the best way to deal with them is to start early enough (assuming you can), and try to solve the problem the right way rather than taking risky short-cuts.

Anyway, my advice would be to try to do it the right way.

  • If you fail to meet the deadline, you still have code that looks like a good attempt, and (one would hope) should be marked accordingly.

  • On the other hand, if you try to do this the wrong way, you may get a working program but still be marked down on the basis that you've done it the wrong way. And you may fail, and be marked worse that if you had failed while trying to write the code properly.

于 2013-03-30T04:02:11.550 回答