请问HN:你们是如何建模暂时无效的数据结构的?
假设你正在制作一个国际象棋游戏。一个有效的棋盘上每个格子只能有一个棋子,或者没有棋子。但是,你有一个算法可以通过移动棋子来生成棋盘,在这个算法中,你可能会将一个棋子移动到另一个棋子上,然后再决定移动其中一个棋子以恢复到有效状态。
你会尝试使用现有的棋盘类型,并在你的算法中避免那些无效状态(例如,通过使用栈或某种数据结构来避免逐个移动棋子)吗?
你会有一个单独的无效棋盘类型,允许每个格子上有多个棋子吗?
我认为这取决于上下文,但我很好奇你见过的不同处理方式。
查看原文
Say you’re making a chess game. A valid board only has one piece per square, or zero. But, you have an algo that generates boards by moving pieces around and during that algo you may move a piece on top of another and then later decide to move one of them to get back to valid.<p>Do you try to use some existing Board type and just avoid in your algo those invalid states (like by using a stack or some data structure to avoid iteratively moving pieces one at a time).<p>Do you have a separate InvalidBoard type that allows multiple pieces per square?<p>I think it’s context dependent but I’m curious how you’ve seen this handled in different ways.