9
1
Introduction
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 seven pieces in
xiangqi: the general (G
), advisor (A
), elephant (E
), horse (H
), chariot
(R
), cannon (C
), and soldier (S
). For the purposes of this challenge,
uppercase pieces are considered red and lowercase pieces black. Most of these
pieces have a rough equivalent in Western chess, but there is one completely unique piece: the cannon.
The cannon moves like a rook in chess or a chariot in xiangqi (moving any number of spaces on either the X or Y axes), but cannot attack this way. Instead, it attacks by jumping along the X or Y axes (the same way it moves) over one piece of any colour (friend or foe) and landing on the opposite-coloured piece, which it then captures. Note that like all chess and xiangqi pieces, cannons cannot capture pieces of their own colour.
For example, in the following diagram, the spaces to which the cannon (C
)
can move are marked with *
, and those to which it can jump and capture
are marked with X
, assuming that there is a black/lowercase piece there.
....X....
.........
.........
....h....
....*....
****C**aX
....E....
....X....
....g....
....R....
Challenge
Write a program or function that, given a xiangqi board and the coordinates of a cannon on that board as input, outputs a list of coordinates to which the cannon can move or jump.
The format for all I/O is flexible.
Acceptable formats for the xiangqi board include a newline-separated string, a
list of strings, or a string with any other separator not in aceghrsACEGHRS.
.
You may assume the board will always be 9x10, the size of a xiangqi board.
The contents of the board itself will consist of a number of periods (.
),
representing empty points on the board, and characters representing pieces. The
piece-to-character mapping is as follows:
A -> advisor
C -> cannon
E -> elephant
G -> general
H -> horse
R -> chariot
S -> soldier
Uppercase letters represent red pieces, and lowercase letters represent black pieces. Characters not listed here (i.e. not in aceghrsACEGHRS.
) will not appear in the board.
The format of the input coordinate is flexible, and is not required to match
the format of the output coordinates. It can be a list of two integer elements,
a 2-tuple, two numbers with any separator, or two characters, for example. It
may also be either 0-indexed or 1-indexed. You may assume that the coordinate
on the board will always resolve to a cannon (C
or c
).
The coordinates to which the cannon can jump and move must appear in the same list in output; distinction between the two is not necessary. Acceptable formats for any individual output coordinates are the same as those for the input coord. The coordinates may be newline-separated, output as a list, or any other representation. No particular order is necessary; the order does not even have to be deterministic.
Note that jumps onto a piece of the same colour (case) of the cannon are not legal, and thus cannot appear in the output.
Test cases
Note that not all of the test cases are possible xiangqi positions.
Input board
Input coordinate (0-indexed)
List of output coordinates
.........
.........
.........
.........
.........
....C....
.........
.........
.........
.........
(4, 5)
[(0, 5), (1, 5), (2, 5), (3, 5), (5, 5), (6, 5), (7, 5), (8, 5), (4, 0), (4, 1), (4, 2), (4, 3), (4, 4), (4, 6), (4, 7), (4, 8), (4, 9)]
.........
.....G...
.........
.........
.....e...
.........
.........
h..R.c..S
.....a...
.........
(5, 7)
[(4, 7), (6, 7), (7, 7), (5, 6), (5, 5), (5, 1)]
..s......
..A...e..
.........
EACCcsh.H
..r......
.....S...
......s..
....C....
..g......
(2, 3)
[(2, 0), (2, 2), (4, 3), (2, 9)]
rheagaehr
.........
.c.....c.
s.s.s.s.s
.........
.........
S.S.S.S.S
.C.....C.
.........
RHEAGAEHR
(7, 7)
[(2, 7), (3, 7), (4, 7), (5, 7), (6, 7), (8, 7), (7, 0), (7, 3), (7, 4), (7, 5), (7, 6), (7, 8)]
Scoring
This is code-golf, so the shortest answer (in bytes) wins. Happy golfing!
3
+1 for Xiangqi. It is an awesome game, that I had the opportunity to learn from my colleagues when I worked for a Chinese company. The general feel is just like playing chess (keep a sharp eye for defense but play aggressively) but the tactics are different (it's good to bring the chariots out early, whereas it's bad to bring the rooks out early in chess.) https://en.wikipedia.org/wiki/Xiangqi
– Level River St – 2016-09-10T20:27:35.903@LevelRiverSt it's much less technical than chess and feels like a videogame with a different strategic feel. I like it! – noɥʇʎԀʎzɐɹƆ – 2016-09-10T23:36:13.517
Seems there may be no king, but can there be more than possible piece? – l4m2 – 2018-05-04T09:04:59.930