7
\$\begingroup\$

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).

enter image description here

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) -> -5+1 or -S5+1
S [(7,6), (5,6), (9,6), (5,8), (7,8)] (7,7) -> -7+1 or -S7+1
S [(9,6), (5,6), (7,6), (5,8), (7,8)] (9,7) -> S9+1
S [(5,8), (5,6), (7,6), (9,6), (7,8)] (5,9) -> +5+1 or +S5+1
S [(7,8), (5,6), (7,6), (9,6), (5,8)] (7,9) -> +7+1 or +S7+1
H [(3,4), (3,8)]                      (4,6) -> -H+4
H [(3,8), (3,4)]                      (4,6) -> +H-4
H [(3,8)]                             (4,6) -> H3-4
S [(5,4), (5,8), (7,8), (5,6), (7,6)] (5,5) -> 35+1 or -5+1 or 3S5+1 or -S5+1
S [(5,6), (5,8), (7,8), (5,4), (7,6)] (5,7) -> 25+1 or 2S5+1
S [(5,8), (5,6), (7,8), (5,4), (7,6)] (5,9) -> +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 & 3<x<7 & y<4
A (4,1)(6,1) r^2=2 & 3<x<7 & y<4
E (3,1)(7,1) r^2=8 & y<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 & y>=y0 & (y>y0 | y>5)
\$\endgroup\$
  • 6
    \$\begingroup\$ You should consider adding a minimum formatting, such as 'Task summary / Input / Output / Detailed rules'. This looks like an interesting challenge but is really hard to read as-is. \$\endgroup\$ – Arnauld May 25 '18 at 14:34

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.