首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >弗莱斯纳格栅javascript

弗莱斯纳格栅javascript
EN

Stack Overflow用户
提问于 2022-02-23 23:28:06
回答 1查看 43关注 0票数 0

我可以使用一个函数来获取隐藏的消息吗?如果是的话,你能告诉我,我该怎么做呢?

使用fleissner格栅6x6解密隐藏的信息:“lróaon.sg sdersoildsu.:.cc kiomamii”。为此,它必须顺时针旋转90°。打开的网格位于初始位置(0度)的[1,1],[4,1],[2,2],[6,2],[5,3],[1,4],[4,4],[ 3,5],[6,5]位置。

  • 请注意,每当我们顺时针旋转网格90时,第一行(x轴)变成第六列(y轴),第二行变成第五列…。第六行成为第一列。
  • 一旦您拥有所有位置(0、90、180和270)的所有坐标对,对消息进行解密就是将消息的每个字母的位置与坐标对关联起来:消息解密消息的第一个字母将位于位置1,1,解密消息的第二个字母将位于4,1,解密消息的第三个字母将位于第2,2,2号位置。到目前为止,我知道如何获取隐藏消息吗?
代码语言:javascript
运行
复制
const sideSize    = 6; // Grid 6
const gridPosBase = [[1,1],[4,1],[2,2],[6,2],[5,3],[1,4],[4,4],[3,5],[6,5]];
const encriptedMessage = 'lróaon. sg sdersoildsu.:.cc kiomamii';
const x = 0;
const y = 1;
             
function rotateArray90(gridToTurn) {
  const gridRotated = [];
  gridToTurn.forEach((par, index) => {
    gridRotated[index]    = [];
    gridRotated[index][x] = (sideSize + 1) - par[y];
    gridRotated[index][y] = par[x];
  });
  return gridRotatedOrdered;  // ¡Warning! rotated but no ordered
}

// Rotate the array 90º 3 times and order the coordinates, to obtain all the open cells

function decrypt(text, grid) {
  text = '';
  grid = [];
}
    
const decryptedMessage = decrypt(encriptedMessage, gridPosBase);
console.log(decryptedMessage);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-24 03:20:24

我不知道什么是弗莱斯纳格莱尔,但这个instructables.com网站解释得很好。

这在一些地方有点棘手,我希望我的解释能有所帮助。

getChar根据网格位置计算读出"encMsg“中的哪个字符。这是(第1行)*6+(列- 1)。需要从行和列中减去1,因为数组和字符串从0开始,而不是从1开始。

rotateGrid,这稍微骗了你一下。如果你画一幅画就更容易了。如何从旧的列和行中获得新的列和行变得很清楚,但是接下来您必须对数组进行排序。在网格旋转之后,您必须从左上角开始读取,所以我们需要对网格进行排序。

readGrid这个循环遍历网格位置,每次从encMsg中读取一个字符,然后返回该消息块。

把它放在一起,我们读第一个块,然后“旋转和阅读”三次得到最后的信息。

代码语言:javascript
运行
复制
const encMsg = "lróaon. sg sdersoildsu.:.cc kiomamii";
let grid = [
  [1, 1],
  [4, 1],
  [2, 2],
  [6, 2],
  [5, 3],
  [1, 4],
  [4, 4],
  [3, 5],
  [6, 5]
];

const getChar = (x, y, str) => str[x - 1 + (y - 1) * 6];
const rotateGrid = grid =>
  grid
  .map(([x, y]) => [7 - y, x])
  .sort((a, b) => {
    if (a[1] < b[1]) return -1;
    if (a[1] === b[1] && a[0] < b[0]) return -1;
    return 1;
  });
const readGrid = (grid, encMsg) =>
  grid.map(([x, y]) => getChar(x, y, encMsg)).join("");

let unEncMsg = readGrid(grid, encMsg);

for (let ix = 0; ix < 3; ix++) {
  grid = rotateGrid(grid);
  unEncMsg += readGrid(grid, encMsg);
}

console.log(unEncMsg);

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71245238

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档