我有以下代码用于动态Polybius正方形:

/*
  1 2 3 4 5
1 A B C D E
2 F G H I K
3 L M N O P
4 Q R S T U
5 V W X Y Z
*/
alphabet = 'ABCDEFGHIKLMNOPQRSTUVWXYZ';
tiles = {
A : {letter: alphabet.charAt(0), x: 1, y: 1},
B : {letter: alphabet.charAt(1), x: 2, y: 1},
C : {letter: alphabet.charAt(2), x: 3, y: 1},
D : {letter: alphabet.charAt(3), x: 4, y: 1},
E : {letter: alphabet.charAt(4), x: 5, y: 1},
F : {letter: alphabet.charAt(5), x: 1, y: 2},
G : {letter: alphabet.charAt(6), x: 2, y: 2},
H : {letter: alphabet.charAt(7), x: 3, y: 2},
I : {letter: alphabet.charAt(8), x: 4, y: 2},
K : {letter: alphabet.charAt(9), x: 5, y: 2},
L : {letter: alphabet.charAt(10), x: 1, y: 3},
M : {letter: alphabet.charAt(11), x: 2, y: 3},
N : {letter: alphabet.charAt(12), x: 3, y: 3},
O : {letter: alphabet.charAt(13), x: 4, y: 3},
P : {letter: alphabet.charAt(14), x: 5, y: 3},
Q : {letter: alphabet.charAt(15), x: 1, y: 4},
R : {letter: alphabet.charAt(16), x: 2, y: 4},
S : {letter: alphabet.charAt(17), x: 3, y: 4},
T : {letter: alphabet.charAt(18), x: 4, y: 4},
U : {letter: alphabet.charAt(19), x: 5, y: 4},
V : {letter: alphabet.charAt(20), x: 1, y: 5},
W : {letter: alphabet.charAt(21), x: 2, y: 5},
X : {letter: alphabet.charAt(22), x: 3, y: 5},
Y : {letter: alphabet.charAt(23), x: 4, y: 5},
Z : {letter: alphabet.charAt(24), x: 5, y: 5},
};


在某个时候,我将要交换行和列。例如,如果要交换第1行和第2行,则将x == 1的每个图块的x值更改为2,反之亦然。

我该怎么办?

编辑:

使用第一个答案的代码,我得到“无法读取未定义的属性x”错误:

    function getLetterCoords(letter)
    {
    coords = [];
    Object.values(tiles).map(tile =>
        {
            if(tile.letter === letter)
                {
                coords['x'] = tile.x;
                coords['y'] = tile.y;
                }
        return coords;
        })
    }

function getLetterFromCoords(coords)
    {
    Object.values(tiles).map(tile =>
        {
            if ((tile.x === coords['x']) && (tile.y === coords['y']))
                {
                return tile.letter;
                }
        })
    }

function encrypt()
    {
    plaintext = document.getElementById('plaintext').value;
    key = document.getElementById('key').value;
    ciphertext = '';

    for (var i = 0; i < plaintext.length; i++)
        {
        plaintextCoords = getLetterCoords(plaintext.charAt(i));
        keyCoords = getLetterCoords(key.charAt(i));
        ciphertext += getLetterFromCoords(keyCoords);
        }

    document.getElementById('ciphertext').value = ciphertext;
    }

最佳答案

转换为array并使用map并返回更改



var alphabet = 'ABCDEFGHIKLMNOPQRSTUVWXYZ';
var tiles = {
A : {letter: alphabet.charAt(0), x: 1, y: 1},
B : {letter: alphabet.charAt(1), x: 2, y: 1},
C : {letter: alphabet.charAt(2), x: 3, y: 1},
D : {letter: alphabet.charAt(3), x: 4, y: 1},
E : {letter: alphabet.charAt(4), x: 5, y: 1},
F : {letter: alphabet.charAt(5), x: 1, y: 2},
G : {letter: alphabet.charAt(6), x: 2, y: 2},
H : {letter: alphabet.charAt(7), x: 3, y: 2},
I : {letter: alphabet.charAt(8), x: 4, y: 2},
K : {letter: alphabet.charAt(9), x: 5, y: 2},
L : {letter: alphabet.charAt(10), x: 1, y: 3},
M : {letter: alphabet.charAt(11), x: 2, y: 3},
N : {letter: alphabet.charAt(12), x: 3, y: 3},
O : {letter: alphabet.charAt(13), x: 4, y: 3},
P : {letter: alphabet.charAt(14), x: 5, y: 3},
Q : {letter: alphabet.charAt(15), x: 1, y: 4},
R : {letter: alphabet.charAt(16), x: 2, y: 4},
S : {letter: alphabet.charAt(17), x: 3, y: 4},
T : {letter: alphabet.charAt(18), x: 4, y: 4},
U : {letter: alphabet.charAt(19), x: 5, y: 4},
V : {letter: alphabet.charAt(20), x: 1, y: 5},
W : {letter: alphabet.charAt(21), x: 2, y: 5},
X : {letter: alphabet.charAt(22), x: 3, y: 5},
Y : {letter: alphabet.charAt(23), x: 4, y: 5},
Z : {letter: alphabet.charAt(24), x: 5, y: 5},
};

Object.values(tiles).map(tile => {
  if(tile.letter === 'A') {
    tile.x -= 1;
    tile.y += 1;
  }
  return tile;
})

console.log(tiles);

07-28 09:54
查看更多