Immutable.js: 表示2D游戏领域的数据结构

Immutable.js: Data structure to represent 2D game field

我想知道我应该使用什么数据结构来表示方形游戏板(考虑每个单元格可以有一些颜色)。最自然的想法是二维List,但是很难查询和更改。

所以,现在使用一个映射,其中键是 ${x}.${y}(JS:() 中没有元组,值是代表颜色的字符串 像这样:

Map([['0.0', 'red'], ['0.1', 'red'], ['1.0', 'blue'], ['1.1', 'red']])

这样的数据结构可以吗?在Immutable.js方面有没有更好的解决方案?

为什么不能像这样使用二维数组是有原因的:

let square = [
    ['red', 'green', 'blue'],
    ['orange', 'red', 'blue'],
    ['red', 'blue', 'blue']
];

然后你可以将上面的数据结构添加到你的地图中。

因此,要访问中间的图块,您只需使用数组的 [1][1] 索引即可。

我也在做自己的2D游戏板,遇到了同样的问题。我做的解决方案是 Record.

它只是看起来像一个对象,也表现得像一个对象。但是对于 vanilla 对象,您不能执行以下映射字典操作。

const dict = {};

const key1 = { row: 0, col: 1 };
const value1 = { some: 'value' };

dict[key1] = value; // will not work

虽然这就是我想要的,但我试图使映射尽可能简单。使用 Immutable.js 中的 RecordMap,您可以执行以下操作。

import { Map, Record } from 'immutable';

const dict = Map();
const Pos = Record({ row: 0, col: 0 }); // some initial value.
const Val = Record({ some: 'value' }); // same here.

const key1 = new Pos({ row: 0, col: 1 });
const value1 = new Val({ some: 'value' });

dict = dict.set(key1, value1); // works like you wish

您可以阅读官方文档了解更多信息。也许您有更好的解决方案,请告诉我:)。

我很好奇为什么您认为列表的列表难以查询和更改。您可以使用长度为 2 的数组作为 [x, y] 对,并将其传递给 getInsetInupdateIn 方法。

let grid = Immutable.toJS([
    ['red', 'green'],
    ['blue', 'yellow']
]);

grid.getIn([0, 1]); // => 'green';
grid = grid.setIn([0, 1], 'purple');
grid.getIn([0, 1]); // => 'purple';
grid = grid.updateIn([0, 0], cell => cell.toUpperCase());
grid.getIn([0, 0]); // => 'RED';

使用 map(...):

可以很容易地将一些函数应用于网格中的每个单元格
grid.map((row, x) => row.map(cell, y) => x + cell + y);
grid.get([1, 1]); // => '1yellow1'

可能比使用 Map 更棘手的一件事是尝试查找值的坐标。

const x = grid.findIndex(row => row.contains('blue')); // => 1
const y = grid.get(x).indexOf('blue'); // => 0
grid.get([x, y]); // => blue