Build a checkerboard

20

3

For this challenge you will print the coordinates and color of each piece at the start of a game of checkers.

Enter the x and y (comma seperated) for every square (indexed 0-7) on a checkerboard, followed by a "r" or "b" (for red or black) where the top of the board (closer to y=0) is red and the bottom is black. newlines between pieces are required and no spaces are necessary.

This is so answers are scored in bytes with fewer bytes being better.

Trailing newlines are allowed but not necessary and the order must be exactly of that below.

Desired output:

0,0r
0,2r
0,4r
0,6r
1,1r
1,3r
1,5r
1,7r
2,0r
2,2r
2,4r
2,6r
5,1b
5,3b
5,5b
5,7b
6,0b
6,2b
6,4b
6,6b
7,1b
7,3b
7,5b
7,7b

Justin

Posted 2017-09-27T22:13:07.720

Reputation: 443

Ok is the above edit sufficient? Sorry this is my first post to this stack exchange. If it's not sufficient could you edit it? Thank you – Justin – 2017-09-27T22:23:14.737

Is a trailing newline allowed/necessary? – HyperNeutrino – 2017-09-27T22:27:02.377

Allowed: yes. Necessary: no – Justin – 2017-09-27T22:27:51.460

3Is there a particular reason you skip x=3 and x=4? – HyperNeutrino – 2017-09-27T22:35:03.677

1@HyperNeutrino because a checkerboard has only the top and bottom 3 rows filled (at the start of the game) – Justin – 2017-09-27T22:36:03.127

Oh you're talking about pieces ._. nvm my answer isn't adjustable rip – HyperNeutrino – 2017-09-27T22:36:46.280

3

So that's basically the starting position of Checkers, aka English Draughts. Is that correct?

– Arnauld – 2017-09-27T22:47:03.710

Does the order of the output matter? – Digital Trauma – 2017-09-27T23:02:46.583

2

Welcome to PPCG.SE! A feature of this site that some of us like to use is the sandbox. You don't have to use it, but it is a useful channel through which to get feedback about any new challenge ideas you have, without having to go through the whole downvote/closevote misery.

– Digital Trauma – 2017-09-27T23:06:17.670

@DigitalTrauma Yes order must be as displayed in the question – Justin – 2017-09-28T01:51:58.050

1Can the coordinates be separated by a delimiter other than a new-line? I.e. 0,0r;0,2r;0,4r;0,6r;1,1r;1,3r;1,5r;1,7r;2,0r;2,2r;2,4r;2,6r;5,1b;5,3b;5,5b;5,7b;6,0b;6,2b;6,4b;6,6b;7,1b;7,3b;7,5b;7,7b? – Kevin Cruijssen – 2017-09-28T11:52:29.667

Can the delimiter for the coordinates themselves be different, like a space. I.e 0 0r\n0 2r\n...? – Stan Strum – 2017-09-28T17:19:22.257

Answers

8

05AB1E, 26 bytes

2ÝD5+«v4Fy',N·yÉ+„bry3‹èJ,

Try it online!

Explanation

2Ý                           # push [0,1,2]
  D5+                        # duplicate and add 5: [5,6,7]
     «                       # concatenate
      v                      # for each y in [0,1,2,5,6,7] do:
       4F                    # for each N in [0 ... 3] do:
         y                   # push y
          ',                 # push ","
            N·yÉ+            # push N*2+isOdd(y)
                 „br         # push "br"
                    y3‹è     # index into the string with y<3
                        J,   # join everything to a string and print

Emigna

Posted 2017-09-27T22:13:07.720

Reputation: 50 798

I feel like [0,1,2,5,6,7] could be pushed in less bytes... but all I could think of was 7Ý34SK so I guess not. – Magic Octopus Urn – 2017-09-29T16:50:05.453

7

C (gcc), 83 81 79 78 bytes

  • Saved two bytes thanks to Tahg; golfing x/4+2*(x/12) to x/4+x/12*2.
  • Saved two bytes thanks to Kevin Cruijssen; golfing x%8*2%8 to x*2%8.
  • Saved a byte thanks to ceilingcat.
x;main(){for(;x<24;)printf("%d,%d%c\n",x/4+x++/12*2,x*2%8+x/4%2,114-x/12*16);}

Try it online!

Jonathan Frech

Posted 2017-09-27T22:13:07.720

Reputation: 6 681

2You can do x/4+x/12*2 and get rid of the () – Tahg – 2017-09-28T11:29:04.487

x%8*2%8 can be x*2%8 – Kevin Cruijssen – 2017-09-28T11:37:09.210

@ceilingcat Thank you. – Jonathan Frech – 2019-09-14T14:15:36.303

7

JavaScript (ES6), 66 bytes

Includes a trailing newline.

f=(n=24)=>n--?f(n)+[2*(x=n>11)+(y=n>>2),n%4*2+y%2+'rb'[+x]]+`
`:''

Demo

f=(n=24)=>n--?f(n)+[2*(x=n>11)+(y=n>>2),n%4*2+y%2+'rb'[+x]]+`
`:''

console.log(f())

Arnauld

Posted 2017-09-27T22:13:07.720

Reputation: 111 334

7

Perl 5, 53 bytes

//&map{say"$',",$_+$'%2,$'<4?r:b}0,2,4,6for 0..2,5..7

Try it online!

Xcali

Posted 2017-09-27T22:13:07.720

Reputation: 7 671

6

Python 2, 74 73 67 bytes

for i in 0,1,2,5,6,7:j=i%2;exec"print`i`+','+`j`+'rb'[i>4];j+=2;"*4

Try it online!

TFeld

Posted 2017-09-27T22:13:07.720

Reputation: 19 246

in(...): -> in ...:. – Jonathan Frech – 2017-09-28T12:09:09.473

4

Java 8, 102 96 95 93 91 bytes

v->{for(int i=0;i<24;System.out.printf("%d,%d%c%n",i/4+i/12*2,i*2%8+i/4%2,i++<12?114:98));}

Port from @JonathanFrech's C answer, after which I golfed 5 bytes myself.

Try it here.

Explanation:

v->{                    // Method without empty unused parameter and no return-type
  for(int i=0;i<24;     //  Loop from 0 to 24 (exclusive)
    System.out.printf("%d,%d%c%n",
                        //   Print with format:
                        //   (%d=digit; %c=character; %n=new-line)
      i/4+i/12*2,       //    Print first coordinate
      i*2%8+i/4%2,      //    Print second coordinate
      i++<12?114:98)    //    Print either 'r' or 'b'
  );                    //  End of loop
}                       // End of method

Kevin Cruijssen

Posted 2017-09-27T22:13:07.720

Reputation: 67 575

4

Bash, 59 bytes

Not the shortest, but a fun one:

printf %s\\n {{0,2,6}\,{0,2,4,6}r,{1,5,7}\,{1,3,5,7}b}|sort

Try it online!

ბიმო

Posted 2017-09-27T22:13:07.720

Reputation: 15 345

4

APL (Dyalog Classic), 38 bytes

(⍕⍪4/3 4~⍨⍳8),',',(⍕⍪24⍴⍋8⍴⍳2),12/'rb'

Try it online!

ngn

Posted 2017-09-27T22:13:07.720

Reputation: 11 449

3

Jelly, 35 bytes

8Ḷḟ3ḟ4µḂr7m2ṭ€µ€Ẏµḣ1<3Ḣị⁾rbṭj”,$µ€Y

Try it online!

Full program taking no arguments

Explanation

8Ḷḟ3ḟ4µḂr7m2ṭ€µ€Ẏµḣ1<3Ḣị⁾rbṭj”,$µ€Y  Main link; no arguments
8                                    8
 Ḷ                                   Push 0 .. 8 - 1
  ḟ                                  Remove all instances of
   3                                 3
    ḟ                                Remove all instances of
     4                               4
                €                    For each in [0, 1, 2, 5, 6, 7]
       µḂr7m2ṭ€µ                     Generate all of the piece coordinates across that row
        Ḃ                            row number % 2
         r                           inclusive range up to
          7                          7
           m                         modular; take every   elements
            2                                            2
              €                      For each column coordinate
             ṭ                       Tack the row coordinate (reverse append)
                 Ẏ                   Tighten; flatten once
                                 €   For each piece coordinate (in the right order now)
                 µḣ1<3Ḣị⁾rbṭj”,$µ    Convert to its final output
                  ḣ                  Head; take the first   element(s)
                   1                                      1
                    <                Less Than; compare each element to
                     3               3
                      Ḣ              Head; take the comparison out of the list
                       ị             Index (1-indexed) into
                        ⁾rb          "rb"
                           ṭ         Tack the color character behind
                            j”,$     The coordinates joined by a comma
                            j        Join with separator
                             ”,      ","
                                  Y  Separate by newlines

HyperNeutrino

Posted 2017-09-27T22:13:07.720

Reputation: 26 575

3

Java 8, 97 bytes

o->{int i=0,g;for(;i<8;i+=i==2?3:1)for(g=i%2;g<8;g+=2)System.out.println(i+","+g+(i<5?"r":"b"));}

Roberto Graham

Posted 2017-09-27T22:13:07.720

Reputation: 1 305

3

APL (Dyalog), 45 44 bytes*

-1 thanks to ngn.

Niladic function which assumes 0-based indexing (⎕IO←0) which is default on many systems. Prints to STDOUT.

↑' '⎕R','⊃,/(8 ¯8↑¨⊂⍕¨⍸∘.=⍨2|⍳8),¨¨'rb'

Try it online!

(),¨¨'rb' append "r" to each of the first group of items and "b" to each of the second:

⍳8 zero through eight

2| division remainder when halved

∘.+⍨ plus table with itself on along both axes

 indices of true values

⍕¨ format each (converts to strings with the pattern d d where each d is a digit)

 enclose (so we can reuse it for each…)

8 ¯8↑¨ take the first nine and the last nine

now we have two lists of d d strings

,/ catenation reduction (combine the two lists)

 disclose (because the reduction reduced the rank from 1 to 0)

' '⎕R',' PCRE Replace spaces with commas

 mix (the all the lists into a single matrix)


* In Dyalog Classic, counting as ⎕U2378.

Adám

Posted 2017-09-27T22:13:07.720

Reputation: 37 779

~2|∘.+⍨⍳8 could be ∘.=⍨2|⍳8 – ngn – 2017-09-30T09:58:48.920

you shouldn't output rows x=3 and x=4

– ngn – 2017-09-30T10:03:15.900

@ngn 2⍴⊂'Thanks' – Adám – 2017-10-02T11:55:06.197

3

JavaScript (ES6), 64 bytes

This seems sufficiently different from @Arnauld's to warrant posting:

f=(n=0,y=n>>3,c=y>2)=>y<6?[y+c*2,n%8+y%2+'rb'[+c]]+`
`+f(n+2):''

Explanation:

f=(n = 0,            //the numbered square on checkerboard
   y = n >> 3,       //current row (n / 8 rounded)
   c = y > 2         //false if red pieces, true if black
  ) => 
  y < 6 ?            //if y less than 6 
    [                //  using an array automatically adds the comma
     y + c * 2,      //  0 - 2 if red pieces, 5 - 7 if black
     n%8 + y%2 +     //  n%8 returns 0, 2, 4, or 6.
                     //  y%2 returns 0 or 1.
                     //  added, they return the appropriate position (0 - 7)
     'rb'[+c]        //  'r' if red, 'b' if black.  Plus sign coerces boolean to number.
    ]+`              //  new line
    `+
    f(n+2) :         //  recurse on n+2
    ''               //else return an empty string

Snippet:

f=(n=0,y=n>>3,c=y>2)=>y<6?[y+c*2,n%8+y%2+'rb'[+c]]+`
`+f(n+2):''

console.log(f());

Rick Hitchcock

Posted 2017-09-27T22:13:07.720

Reputation: 2 461

3

Python 2, 63 bytes

x=0;exec"print`x/8+x/24*2`+','+`x%8+x/8%2`+'rb'[x/24];x+=2;"*24

Try it online!

Lynn

Posted 2017-09-27T22:13:07.720

Reputation: 55 648

2

Bubblegum, 59 bytes

00000000: 15ca b101 0030 0400 c1de 2c0a 2462 1f23  .....0....,.$b.#
00000010: d8bf 886f ae3a 531b 310d b8f0 465c 1d0e  ...o.:S.1...F\..
00000020: 24d4 48ec 5b02 2eec 4bf5 5e0e 2454 cb53  $.H.[...K.^.$T.S
00000030: 8380 0baf a5d4 e140 42f5 07              .......@B..

Try it online!

ovs

Posted 2017-09-27T22:13:07.720

Reputation: 21 408

This is the hexdump, right? – Stan Strum – 2017-09-28T16:37:53.930

@StanStrum Yes this is the hexdump of the actual source. By using the hexdump no unprintables get lost. The actual source doesn't add anything to the answer IMO. – ovs – 2017-09-28T16:44:57.433

Okay, I was just wondering. Thanks – Stan Strum – 2017-09-28T16:59:26.123

2

Pyth, 37 36 35 bytes

isaacg wouldn't be proud

V+U3}5 7FG4p++N\,+yG?!%N2Z1?<N5\r\b

Explanation:

V+U3}5 7            For in the array [0, 1, 2, 5, 6, 7] as N
 FG4                 For in the array [0, 1, 2, 3] as G
  p                   Print without newline:
   ++N\,+yG?!%N2Z1     N, (2 * G) + 1 if N is even, else 0
  ?<N5\r\b            Output with newline "r" if N < 5 else "b"

This uses a simple pattern I cut down a bit. As follows:

If the X coord is even, use the even numbers 0, 2, 4, 6. Else, 1, 3, 5, 7 for Y.

If the X coord is less than 5, the color (r or b) is r. Else, it is b.

Try it online!

edit: overnight got +40 rep on 3 different answers w00t

Stan Strum

Posted 2017-09-27T22:13:07.720

Reputation: 436

1

Javascript (89 bytes):

for(x=y=0,r="r";x<7|y<8;console.log(x+","+y+r),y+=2){if(y>7)y=++x%2;if(x==3){x+=2;r="b"}}

Readable:

for(var x = y = 0, red = true; x < 7 || y < 8; y += 2) {
    if(y > 7) { //new row
        x++;
        y = x % 2;
    }
    if(x == 3) { //new color
        x += 2;
        red = false;
    }
    console.log(x + "," + y + (red ? "r" : "b") );
}

Try it online!

Justin

Posted 2017-09-27T22:13:07.720

Reputation: 443

4Please note that it is discouraged to answer your own challenge just after posting it. – Arnauld – 2017-09-27T22:22:03.760

Ok. Should I take it down and possibly post later? – Justin – 2017-09-27T22:24:13.317

Ok will do. Thanks for the heads up – Justin – 2017-09-27T22:26:22.050

This might sound stupid but how do I delete a post on mobile? – Justin – 2017-09-27T22:27:12.723

@Justin You can't delete posts on mobile. It is an annoyance to many. – Post Rock Garf Hunter – 2017-09-27T22:28:15.997

Huh that's weird. I wonder why. – Justin – 2017-09-27T22:29:01.367

You could put Non-competing for the time being – Stan Strum – 2017-09-28T17:25:39.970

1

SOGL V0.12, 31 bytes

⁰¹²⁵⁶⁷’{A4∫«Ha2\⌡HaOļ,pƧbra4>Wo

Try it Here!

dzaima

Posted 2017-09-27T22:13:07.720

Reputation: 19 048

1

Jelly, 27 bytes

8Ḷµḟ3,4pµSḂ$Ðḟj€”,ż⁾rbx12¤Y

A full program which prints the required output.

Try it online!

How

8Ḷµḟ3,4p⁸SḂ$Ðḟj€”,ż⁾rbx12¤Y - Link: no arguments
8                           - literal eight
 Ḷ                          - lowered range = [0,1,2,3,4,5,6,7]
  µ                         - new monadic chain
    3,4                     - literal list = [3,4]
   ḟ                        - filter discard = [0,1,2,5,6,7]
        ⁸                   - chain's left argument = [0,1,2,3,4,5,6,7]
       p                    - Cartesian product = [[0,0],[0,1],...,[2,7],[5,0],...,[7,6],[7,7]]
            Ðḟ              - filter discard if:
           $                -   last two links as a monad:
         S                  -     sum
          Ḃ                 -     modulo by 2
                ”,          - literal comma character
              j€            - join €ach pair with a comma = [0,',',0],[0,',',2],...,[2,',',6],[5,',',1],...,[7,',',5],[7,',',7]]
                         ¤  - nilad followed by links as a nilad:
                   ⁾rb      - literal list = ['r','b']
                       12   - literal twelve
                      x     - repeat = ['r','r','r','r','r','r','r','r','r','r','r','r','b','b','b','b','b','b','b','b','b','b','b','b']
                  ż         - zip together = [[[0,',',0],'r'],[[0,',',2],'r'],...,[[2,',',6],'r'],[[5,',',1],'b'],...,[[7,',',5],'b'],[[7,',',7],'b']]
                          Y - join with newlines = [[0,',',0],'r','\n',[0,',',2],'r','\n',...,'\n',[2,',',6],'r','\n',[5,',',1],'b','\n',...,'\n',[7,',',5],'b','\n',[7,',',7],'b']
                            - implicit print (smashes the list of lists and characters
                            -                 together and prints the digits)

Jonathan Allan

Posted 2017-09-27T22:13:07.720

Reputation: 67 804

1

Ruby, 63 60 bytes

[*0..11,*20..31].map{|x|puts [x/4,2*x%8+x[2]]*?,+"rb"[x[4]]}

Bitmask magic saves the byte.

Try it online!

G B

Posted 2017-09-27T22:13:07.720

Reputation: 11 099

1

PowerShell, 63 bytes

0..2+5..7|%{$i=$_;0,2,4,6|%{"$i,$($_+$i%2)"+('r','b')[$i-ge5]}}

Try it online!

Loops over 0,1,2,5,6,7 and each iteration sets $i to the current number. Then loops over 0,2,4,6. Each inner loop, we construct a string starting with $i, then concatenated with our inner loop's current number plus whether $i is even or odd (which gets us 0,2,4,6 one time and 1,3,5,7 the other time), then concatenated with either red or black based on whether $i is -greater-than-or-equal to 5. Those strings are all left on the pipeline and the implicit Write-Output at program completion gives us newlines for free.

AdmBorkBork

Posted 2017-09-27T22:13:07.720

Reputation: 41 581

1

J, 48 44 40 37 31 bytes

(}:"1":$.|:8$#:162 69),.12#'rb'

Try it online!

How it works

           8$#:162 69    first 2 columns in binary, take 8 of them
         |:              transpose
       $.                format as a sparse array, kind of looks like the goal
     ":                 "to text"
 }:"1                    delete the last column
       ,.12#'rb'         append 12 rs followed by 12 bs as the last column

FrownyFrog

Posted 2017-09-27T22:13:07.720

Reputation: 3 112

1

C# (.NET Core), 96 bytes

()=>{for(int i=0;i<24;)System.Console.Write($"{i/4+i/12*2},{i*2%8+i/4%2}{(i++>11?"b":"r")}\n");}

Try it online!

Essentially just a port to C# of @JonathanFrech's answer. I couldn't come up with any better way of doing the math.

Ayb4btu

Posted 2017-09-27T22:13:07.720

Reputation: 541