B
B
BonBon Slick2019-09-24 17:18:31
Algorithms
BonBon Slick, 2019-09-24 17:18:31

Optimizing the polyomino inference algorithm?

I have a two dimensional array

{0, 0, 0, 0, 0},
    {0, 0, 1, 0, 0}, 
    {0, 0, 2, 0, 0}, // 2 is rotation center, center of 5x5 array
    {0, 0, 1, 0, 0},
    {0, 0, 1, 0, 0}
// он же
00000
00010
00210
00100
00000

If you notice, 2 is the center of the polyomino rotation, 1 piece, 0 is an empty space.
I tried to parse similar or such inference algorithms without success.
Therefore, I made statics, we simply store each element in an array, there were problems when rendering. Namely, this kind.
if (2 == piece) {
        setChar(pX, pY, pieceSymbol);
      }
      // piece parts, drawing calculated from rotation point which is always at array[2][2](3-d from all sides) position in array[5][5]
      if (1 == piece) {
        // [0][0]
        if (0 == x, 0 == y) {
          setChar(pX - 2, pY - 2, pieceSymbol);
        }
        else if (0 == x, 1 == y) {
          setChar(pX - 2, pY + 1, pieceSymbol);
        }
        else if (0 == x, 2 == y) {
          setChar(pX - 2, pY, pieceSymbol);
        }
        else if (0 == x, 3 == y) {
          setChar(pX - 2, pY + 1, pieceSymbol);
        }
        else if (0 == x, 4 == y) {
          setChar(pX - 2, pY + 2, pieceSymbol);
        }

        // [1][0]
        else if (1 == x, 0 == y) {
          setChar(pX - 1, pY - 2, pieceSymbol);
        }
        else if (1 == x, 1 == y) {
          setChar(pX - 1, pY - 1, pieceSymbol);
        }
        else if (1 == x, 2 == y) {
          setChar(pX - 1, pY, pieceSymbol);
        }
        else if (1 == x, 3 == y) {
          setChar(pX - 1, pY + 1, pieceSymbol);
        }
        else if (1 == x, 4 == y) {
          setChar(pX - 1, pY + 2, pieceSymbol);
        }

        // [2][0]
        else if (2 == x, 0 == y) {
          setChar(pX, pY - 2, pieceSymbol);
        }
        else if (2 == x, 1 == y) {
          setChar(pX, pY - 1, pieceSymbol);
        }
        else if (2 == x, 2 == y) {
          setChar(pX, pY, pieceSymbol);
        }
        else if (2 == x, 3 == y) {
          setChar(pX, pY + 1, pieceSymbol);
        }
        else if (2 == x, 4 == y) {
          setChar(pX, pY + 2, pieceSymbol);
        }

        // [3][0]
        else if (3 == x, 0 == y) {
          setChar(pX+ 1, pY - 2, pieceSymbol);
        }
        else if (3 == x, 1 == y) {
          setChar(pX+ 1, pY - 1, pieceSymbol);
        }
        else if (3 == x, 2 == y) {
          setChar(pX+ 1, pY, pieceSymbol);
        }
        else if (3 == x, 3 == y) {
          setChar(pX+ 1, pY + 1, pieceSymbol);
        }
        else if (3 == x, 4 == y) {
          setChar(pX+ 1, pY + 2, pieceSymbol);
        }
....

This is drawing, there are a lot of if s .
I am sure that even for statics there is some template, an output algorithm.
Who can tell you how to compress this in 10 times? :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Sokolov, 2019-09-24
@BonBonSlick

The meaning of all these conditions is only to translate from coordinates centered at (0, 0) to coordinates centered at (2, 2)?
It won't replace all if's?
setChar(pX - 2 + x, pY - 2 + y, pieceSymbol);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question