简体   繁体   中英

How do I get the coordinates of a word in a Polybius cipher in Java?

I'm trying to build a Polybius cipher which I will need to encrypt and decrypt.

So for instance, how do I initially get the coordinates for the word "world" in this square?

public static char[][] cypher = {
    {'p', 'h', '0', 'q', 'g', '6'}, 
    {'4', 'm', 'e', 'a', '1', 'y'}, 
    {'l', '2', 'n', 'o', 'f', 'd'},
    {'x', 'k', 'r', '3', 'c', 'v'}, 
    {'s', '5', 'c', 'w', '7', 'b'}, 
    {'j', '9', 'u', 't', 'i', '8'},};

I know "World" would be 43 23 32 20 25

Actually Polybius checkerboard is a 2D-Array . to finding a Polybius checkerboard element index you can follow 2D-Array searching pseudocode

for i=0 : array.length
    for j=0 : array[i].length
        %check the desiare value with array[i][j] element%
    end;
end;

implementation of this pseudocode on your code:

public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
        char[][] cypher = {
            {'p', 'h', '0', 'q', 'g', '6'},
            {'4', 'm', 'e', 'a', '1', 'y'},
            {'l', '2', 'n', 'o', 'f', 'd'},
            {'x', 'k', 'r', '3', 'c', 'v'},
            {'s', '5', 'c', 'w', '7', 'b'},
            {'j', '9', 'u', 't', 'i', '8'}};

        char[] data = {'w', 'o', 'r', 'l', 'd'};//data convert to char array

        for (char c : data) {
            for (int i = 0; i < cypher.length; i++) {
                char[] cs = cypher[i];
                for (int j = 0; j < cs.length; j++) {
                    char d = cs[j];
                    if (d == c) {
                        sb.append(i);
                        sb.append(j);
                        sb.append(" "); 
                    }

                }

            }
        }
        System.out.println(sb.toString());
    }

output of this code is : 43 23 32 20 25

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM