0

我得到了这种类型的数组设置的代码:

create grid 16 cells allot
: init-grid grid 16 cells 0 fill ;

如何在应用程序运行时检查它。当应用程序运行时,可以添加单元格(如果使用键盘输入相邻)或不在 X/Y 上(我目前不使用对角线)

但是一个单元格必须可以自由地添加一个新值,每个循环可能会在当前有或没有释放一个单元格的情况下计算数组,所以在某些情况下,我会在输入错误时得到无限循环。

所以我必须按这个顺序检查:

are all cell used ? no => check next loop of operation & input
are all cell used ? yes => force operation if possible & next loop else end input save square etc...

如何检查下一个操作是否正常并释放一个单元格?

4

1 回答 1

0

最后我设法用更好的方法来做到这一点;而不是将其作为向量(垂直和距离)读取,直到我找到第一个空闲单元格(至少一个)

我使用并将所有单元格索引为 X/Y 数组,所以现在我可以使用:builder:

\ in Forth, you do many things on your own. This word is used to define 2D arrays
: 2D-ARRAY ( height width )
        CREATE DUP ,
                * CELLS ALLOT
        DOES> ( y x baseaddress )
                ROT    ( x baseaddress y )
                OVER @ ( x baseaddress y width )
                *      ( x baseaddress y*width )
                ROT    ( baseaddress y*width x )
                + 1+ CELLS +
;

接着

: findfreecell ( index -- addr )
        0 0 tab SWAP ( curr-addr index )
        0 0 tab @ 0<> IF 
                1+ 
        THEN
        0 ?DO ( find the next free space index times )
                BEGIN
                        CELL+ DUP @ 0=
                UNTIL
        LOOP
;

这样我就可以确定有空闲单元用于下一次操作并且我可以调用操作程序。

于 2020-09-14T15:25:40.917 回答