Recording xiangqi moves The game of xiangqi, also known as Chinese chess, is a chess-like game popular in China, Vietnam, Taiwan, and other East Asian countries. The colours of the two sides in xiangqi are red and black. There are 16 black pieces in xiangqi: the 1 general (G), 2 advisor (A), 2 elephant (E), 2 horse (H), 2 chariot (R), 2 cannon (C), and 5 soldier (S). The vertical lines are known as files (columns), and the horizontal lines are known as ranks (rows).



A notation system partially described in A Manual of Chinese Chess and used by several computer software implementations describes moves in relative terms as follows:

[single-letter piece abbreviation][former file][operator indicating direction of movement][new file, or in the case of purely vertical movement, number of ranks traversed]


The file numbers are counted from each player's right to each player's left. For black, the file numbers are shown in the picture, white background.

In case there are two identical pieces in one file, symbols + (front, higher rank) and - (rear, lower rank) are used instead of former file number. If there are more than two, +(highest rank), 2(2nd highest rank), 3(3rd highest rank), ... is used. It's fine either you use the number or - to mean the lowest rank if more than two exist. If more than one file contain multiple identical pieces, both former file and order can't be omitted, but the piece abbreviation can be omitted.

[former order in the file][single-letter piece abbreviation][operator indicating direction of movement][new file, or in the case of purely vertical movement, number of ranks traversed]
[former order in the file][former file][operator indicating direction of movement][new file, or in the case of purely vertical movement, number of ranks traversed]


A plus sign(+) is used to indicate forward movement. A minus sign(-) is used to indicate backwards movement. A dot or period(.) or equal sign(=) is used to indicate horizontal or lateral movement. For a piece that moves diagonally, the plus or minus sign is used rather than the period.

Thus, if the five soldiers are at (5,6), (7,6), (9,6), (5,8), (7,8), then each of them moving one rank forward are expressed as -5+1, -7+1, S9+1, +5+1, +7+1; two horses at (3,4) and (3,8) each moving to (4,6) are expressed as -H+4 and +H-4; five soldiers are at (5,6), (7,6), (5,4), (5,8), (7,8), then each of them moving one rank forward are expressed as 25+1, -7+1, 35+1, +5+1, +7+1; two horses at (3,4) and (5,8) each moving to (4,6) are expressed as 3H+4 and 5H-4.

Given the current position and destination of the piece to move, and a list/set containing all positions of the same kind of pieces, output its expression. Input format is quie flexible. Shortest code win.

Samples: (use format that first item of the array is moved)

S [(5,6), (7,6), (9,6), (5,8), (7,8)] (5,7) -&gt; -5+1 or -S5+1
S [(7,6), (5,6), (9,6), (5,8), (7,8)] (7,7) -&gt; -7+1 or -S7+1
S [(9,6), (5,6), (7,6), (5,8), (7,8)] (9,7) -&gt; S9+1
S [(5,8), (5,6), (7,6), (9,6), (7,8)] (5,9) -&gt; +5+1 or +S5+1
S [(7,8), (5,6), (7,6), (9,6), (5,8)] (7,9) -&gt; +7+1 or +S7+1
H [(3,4), (3,8)]                      (4,6) -&gt; -H+4
H [(3,8), (3,4)]                      (4,6) -&gt; +H-4
H [(3,8)]                             (4,6) -&gt; H3-4
S [(5,4), (5,8), (7,8), (5,6), (7,6)] (5,5) -&gt; 35+1 or -5+1 or 3S5+1 or -S5+1
S [(5,6), (5,8), (7,8), (5,4), (7,6)] (5,7) -&gt; 25+1 or 2S5+1
S [(5,8), (5,6), (7,8), (5,4), (7,6)] (5,9) -&gt; +5+1 or +S5+1


Pieces starting places and moving rules about from place(x0,y0), to place(x,y) and their distance r, if that helps:

G (5,1)      r=1 &amp; 3&lt;x&lt;7 &amp; y&lt;4
A (4,1)(6,1) r^2=2 &amp; 3&lt;x&lt;7 &amp; y&lt;4
E (3,1)(7,1) r^2=8 &amp; y&lt;6
H (2,1)(8,1) r^2=5
R (1,1)(9,1) (x-x0)(y-y0)=0
C (2,3)(8,3) (x-x0)(y-y0)=0
S (1,4)(3,4)(5,4)(7,4)(9,4)
             r=1 &amp; y&gt;=y0 &amp; (y&gt;y0 | y&gt;5)
Stax, 63 bytes
÷Qa9═wë•Θ╣äUuâ£▼+∙8WU▓kw≈▲!.┤α@∟┐₧╨═ºî⌂z▼0╬⌂6╕ú▄O&amp;⌂¼^Mgâ╗╕.î¡☼:

Run and debug it
Takes input as &lt;position before&gt; &lt;piece type&gt; [&lt;other pieces&gt;] &lt;position after&gt;.
Unpacked:
;x\EE-c:+&quot;=+-&quot;@s|aaE=s,h?$+Ydx]+MEsxh|I@|oc%vcaHX-xn^$'-?'+?z?,b'S&lt;*!,h$*++y+

Run and debug it
Explanation:
The first part calculates the last part: The direction of movement and the resulting file/change in rank:
;x\EE-c:+&quot;=+-&quot;@s|aaE=s,h?$+Yd
;x                            Push position after and before
  \                           Zip -&gt; [[file after, file before], [rank after, rank before]]
   E                          Push files and ranks separately
    E-                        Difference in ranks (after - before)
      c:+                     Copy and get the sign (-1, 0, +1)
         &quot;=+-&quot;@               Index into the string &quot;=+-&quot; (-1 means last one)
               s|a            Get the absolute value of the difference in rank
                  aE=         Check whether file before = file after (pure vertical movement)
                     s        Swap with abs of difference in rank
                      ,h      Get file after
                        ?     If file before = file after:
                                abs(rank after - rank before)
                              Else:
                                file after
                              (this is the number in the end)
                         $    Convert to string
                          +   Concat the sign (-=+) and number.
                           Yd Store in Y register and delete from stack.

The second part calculates the index in the file (+, - or a digit):
x]+MEsxh|I@|oc%vcaHX-xn^$'-?'+?z?
x]+                               Append the position before to the list of other pieces
   ME                             Push list of files, then list of ranks
     s                            Swap the two
      xh|I                        Find indices with file = file before
          @                       Get ranks of these
           |o                     Calculate destination index of each element if the ranks were sorted
             c%v                  Get length of the array - 1
                                    (no. of other pieces in file)
                ca                Get the array of dest. indices again
                  HX              Get the last index (= index of the moving pieces)
                                    and store in register X
                    -             Then subtract from the no. of other pieces
                     xn^          ... and increment the result
                                    -&gt; That's the index from big rank to small
                        $         ... and convert to string
                         '-?      If register X = 0 (lowest rank): &quot;-&quot;
                                  Else: The index from big to small
                            '+?   If index from big to small = 0 (high rank): &quot;+&quot;
                                  Else: Result from above
                               z? If length of array = 1: &quot;&quot;
                                  Else: Result from above

So this means:

If there are no other pieces in this rank, &quot;&quot;
Else if we have the highest rank, &quot;+&quot;
Else if we have the lowest rank, &quot;-&quot;
Else, the index of the rank as a string

These conditionals work in a somewhat special way: First, all the conditions are produced, most important to least important, then the true branch of the first condition is made (the index as a string). After this we push the falsy value for each condition, least important to most important, directly before performing the conditional ?.
The third part checks whether we need to add the file number:
,b'S&lt;*!,h$*
,           Get the piece type
 b          Copy both the result of part 2 and the piece type
  'S&lt;       Test whether the piece type is NOT &quot;S&quot;
              (actually less than &quot;S&quot;, because all other piece types come before S in ASCII)
     *      Repeat the result of part 2 by the result of this test:
            &quot;&quot; if the piece type is &quot;S&quot;
            the result of part 2 otherwise
      !     Negate this, &quot;&quot; is falsey.
            =&gt; This means NOT (part 2 is empty AND NOT piece type is &quot;S&quot;)
            =&gt; Part 2 is empty iff this is the only piece in the file
            =&gt; NOT (only piece in file AND NOT piece type is &quot;S&quot;)
            =&gt; NOT only piece in file OR piece type is &quot;S&quot;
       ,h   Push the file before
         $  and convert it to string
          * Repeat that string by the result of above check:
            &quot;&quot; unless other pieces in file or piece type is &quot;S&quot;

So, after parts 1-3, the stack looks like this:
&lt;position in the file (+, -, or number)&gt; &lt;piece type&gt; &lt;file if necessary&gt;

And the Y register contains the movement itself (from part 1).
The final part combines these into a single string:
++y+ &quot;&lt;position in the file&gt;&quot; &quot;&lt;piece type&gt;&quot; &quot;&lt;file if necessary&gt;&quot;
+    &quot;&lt;position in the file&gt;&quot; &quot;&lt;piece type&gt;&lt;file if necessary&gt;&quot;
 +   &quot;&lt;position in the file&gt;&lt;piece type&gt;&lt;file if necessary&gt;&quot;
  y  &quot;&lt;position in the file&gt;&lt;piece type&gt;&lt;file if necessary&gt;&quot; &quot;&lt;movement&gt;&quot;
   + &quot;&lt;position in the file&gt;&lt;piece type&gt;&lt;file if necessary&gt;&lt;movement&gt;&quot;

This final result is implicitly output.