6
\$\begingroup\$

The Najdorf Variation of the Sicilian Defence is an opening often played at the highest level of chess. It is named after GM Miguel Najdorf and is known for being extremely sharp and theory heavy.

Your task is to write a program that, given a Boolean-like input, prints a chessboard representation of the opening from either White or Black perspective.

I/O format

Your program can take any input that has two possible values. Boolean, Integer, etc.

Your program must output a square chessboard using your language's standard output (usually the console). Each piece must have a corresponding character (your choice). Black and white pieces must be different. Black and white squares are allowed to be the same, but you are allowed to have different characters for them if you wish.

Using standard notation and uppercase for white and lowercase for black, here are the two chessboards, though you are free to represent the pieces differently:

True               False

R   B K Q B   R    r n b q k b   r
P P P     P P P      p     p p p p
          N        p     p   n    
      P N                         
                         N P      
    n   p     p        N          
p p p p     p      P P P     P P P
r   b k q b n r    R   B Q K B   R

This is , so shortest program in bytes wins!

\$\endgroup\$
5
  • \$\begingroup\$ Challenges should be as self-contained as possible. I've included the chessboard configurations themselves into the challenge so that the challenge contains the details, but also keeps the link to the Wikipedia page for further details. \$\endgroup\$
    – hyper-neutrino
    Apr 30 at 4:47
  • 6
    \$\begingroup\$ Is the example correct? Shouldn't the opposite side see the board 180 degrees rotated instead of rows reversed? \$\endgroup\$
    – Bubbler
    Apr 30 at 4:55
  • 2
    \$\begingroup\$ Also, it seems like the Black King's Knight is not at the right position in the True version. \$\endgroup\$
    – Arnauld
    Apr 30 at 5:21
  • \$\begingroup\$ @Bubbler Fixed. \$\endgroup\$ Apr 30 at 10:49
  • \$\begingroup\$ @Bubbler You're right; that's my bad. Sorry. \$\endgroup\$
    – hyper-neutrino
    May 1 at 21:43
2
\$\begingroup\$

Jelly, 45 bytes

“¤d2ʂẠ⁽ÞWƓż:§ßẸ:ȯlEØƘƑṀ°@œḥƥßıẠ’ṃ“Œ⁼L»s8UṚ$¡G

0 outputs from White's perspective, 1 from Black's.

As allowed in the question this uses other characters, the mapping is:

     Black  White
R      u      t
N      y      i
B      n      a
Q      p      b
K      r      l
P      d      c
empty  e      e

Try it online!
Or see this version which uses the letters from the question's example output and . for empty.

How?

“...’ṃ“Œ⁼L»s8UṚ$¡G - Main Link: integer (from [0,1]), is_Black
“...’              - a large integer (stored in base 250)
      “Œ⁼L»        - dictionary word = "unpredictably"
     ṃ             - base-decompress (i.e. use "unpredictably" as the base-13 digits)
           s8      - split into slices eight long
                ¡  - repeat (is_Black) times:
               $   -   last two links as a monad:
             U     -     reverse each
              Ṛ    -     reverse
                       (equivalent to rotating a 1/2 turn)
                 G - format as a grid
\$\endgroup\$
1
  • \$\begingroup\$ Oh, using another character set like that is very clever :) \$\endgroup\$
    – hyper-neutrino
    May 1 at 21:42
2
\$\begingroup\$

JavaScript (Node.js), 114 89 124 bytes

x=>`RNBQKB R
P  PPPP
P  P N
  
   np
  n
ppp  ppp
r bqkb r`.split`
`.sort(_=>x-1).map(n=>[...n].sort(_=>x-1).join``).join`
`

Try it online!

Take the grid and flip if needed.

\$\endgroup\$
3
  • \$\begingroup\$ You can save a byte by using f.sort(_=>x||-1) instead of (x?f:f.reverse()). But the 2nd row is only 7 characters long. I guess they should all be padded to 8 characters or not padded at all. \$\endgroup\$
    – Arnauld
    Apr 30 at 5:32
  • \$\begingroup\$ (Since the sort is stable, f.sort(_=>x-1) should be fine as well.) \$\endgroup\$
    – Arnauld
    Apr 30 at 6:05
  • \$\begingroup\$ @Arnauld Thanks! \$\endgroup\$
    – emanresu A
    Apr 30 at 7:15
1
\$\begingroup\$

Jelly, 64 bytes

“¢ƙ]Ŀ°Mė8Ḃ“¢Ɠt:bẎ€^E“¡ṣḂƘ3^Q““¡⁽Ñ<Ṅ“Ṛ"ß“¡żN5Aʠṭm“¡ƬọG¢2|ie»UṚ$¡G

Try it online!

-6 bytes thanks to Lyxal by compressing the string using Jelly's built-in dictionary compressed string type.

Explanation

“...»       The grid itself
     UṚ$    Reverse the order of rows and rows themselves (180 degree rotation)
        ¡   N times
         G  Formatted as a grid
\$\endgroup\$
4
  • \$\begingroup\$ You mean string compression is useless here? \$\endgroup\$
    – lyxal
    Apr 30 at 10:36
  • \$\begingroup\$ Your program must output a square chessboard using your language's standard output - I think the footer, which formats it into a grid, has to count towards the size of the program \$\endgroup\$ Apr 30 at 10:49
  • \$\begingroup\$ @EnderShadow8 Fair enough. Outputting 2D text in native data types is usually accepted but since this is very similar to a kolmogorov challenge it's fair enough that I need to output the actual text and not just an internal representation. \$\endgroup\$
    – hyper-neutrino
    Apr 30 at 17:09
  • \$\begingroup\$ @Lyxal I'm surprised, but despite there being next to no pattern to this, dictionary compression does actually shorten it. It might be due to all the spaces. \$\endgroup\$
    – hyper-neutrino
    Apr 30 at 17:13
1
\$\begingroup\$

Vyxal j, 49 45 42 bytes

»¬℅→⁺w½]
oꜝ}l÷₄ǒẇ‡⋎ḋ∩P,*ø:⁺Ȯ∞J₀»k63ȯτ?ßṘ8/

Try it Online!

»...»           # Push compressed integer
     k6         # Push constant `0123456789abcdef`
       3ȯ       # Slice from 3rd character (`3456789abcdef`)
         τ      # Custom base conversion (to base13 with above)
          ?ßṘ   # If input is truthy, reverse
             8/ # Divide into 8 pieces
                # Implicit join by newline
\$\endgroup\$
2
  • \$\begingroup\$ The shorter one is allowed by the rules stated. \$\endgroup\$ May 21 at 12:09
  • \$\begingroup\$ @EnderShadow8 Ok. \$\endgroup\$
    – emanresu A
    May 21 at 12:10
0
\$\begingroup\$

Charcoal, 46 bytes

”⟲.>Xc'eΣZ\`KOXW⟲w‴PVCτΣ\⁻μ⊖[⊖U×K↙mψQ{π!J”UE¹⟲N

Try it online! Link is to verbose version of code. Uses 0 for White's view and 4 for Black's view. Assumes that Black wants to see the board rotated rather than flipped. Explanation:

”⟲.>Xc'eΣZ\`KOXW⟲w‴PVCτΣ\⁻μ⊖[⊖U×K↙mψQ{π!J”

Print the compressed board.

UE¹

Space the characters out horizontally.

⟲N

Rotate the board as necessary.

42 bytes without the horizontal spacing:

✳N”⟲.>Xc'eΣZ\`KOXW⟲w‴PVCτΣ\⁻μ⊖[⊖U×K↙mψQ{π!J

Try it online! Link is to verbose version of code. Explanation: Prints the same compressed string, but in the input direction, which means that the string terminator is no longer required.

30 bytes by using a custom output format:

✳N“↶⊟∨dO9Rξ1d"⁼¦Q≕τm×↖WSLQg℅R₂

Try it online! Link is to verbose version of code. Uses the following key:

.   Black pawn
!   White pawn
"   Black rook
#   Black knight
$   Black bishop
%   White rook
&   White knight
'   White bishop
(   Black king
)   Black queen
*   White king
+   White queen
\$\endgroup\$
1
  • 1
    \$\begingroup\$ I think spacing out the characters isn't needed, althought the examples show it that way. \$\endgroup\$
    – Razetime
    Apr 30 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.