25
\$\begingroup\$

Saw this in a PHP challenge. The objective is to make a chessboard with 64 squares (8*8) with the minimum amount of code. Simple enough, I made mine in PHP in 356 bytes (not impressive, I know) and I would like to see some other aproaches. This can be made in a language of your choice, as long as you keep it vanilla, so no imports. Smallest byte count wins.

The output should look like this:

screencap

And my code:

<table><?php
$c='black';function p($c,$n){echo'<td style="width:50px;height:50px;background:'.$c.'"></td>';if($n==1){echo"<tr>";}}for($i=1;$i<=64;$i++){if($i%8==0&&$c=="black"){$c="white";$n=1;}elseif($i%8==0&&$c=="white"){$c="black";$n=1;}elseif(isset($n)&&$n==1){$n=0;}elseif($c=="black"){$n=0;$c="white";}elseif($c=="white"){$n=0;$c="black";}p($c,$n);}

Or readable:

<table><tr>
<?php
$color = 'black';
function printcolor($color, $nl) {

    echo '<td style="width:50px; height:50px; background:' . $color . '"></td>';
    if ($nl == true) {
        echo "</tr><tr>";
    }
}
for ($i=1; $i<=64;$i++) {
    if ($i % 8 == 0 && $color == "black") {
        $color = "white";
        $nl = true;
    } elseif ($i % 8 == 0 && $color == "white") {
        $color = "black";
        $nl = true;
    } elseif (isset($nl) && $nl == true) {      
        $nl = false;
    } elseif ($color == "black") {
        $nl = false;
        $color = "white";           
        } 
    elseif ($color == "white")  {
        $nl = false;
        $color = "black";
    }       
    printcolor($color, $nl);
}

Edit:

Sorry I wasn't very specific at first:

  • Squares should have 50px * 50px except for vectorial images.
  • Output format or size is not relevant nor it needs to be an image.
  • For evaluation purposes the output must be visible such as in an image file or a screenshot
  • No libraries written after the challenge was posted
\$\endgroup\$
39
  • 3
    \$\begingroup\$ Welcome to PPCG, as it stands, this challenge doesn't really have anything to do with PHP, so I changed your tags. Also, I believe your reference implementation belongs as an answer, not in your question. As Stewie brought up, you should specify the required size of the image output, as well as things like colour specifics and whether a lossy image is allowed. \$\endgroup\$ – FryAmTheEggman Feb 4 '16 at 17:04
  • 2
    \$\begingroup\$ So some ASCII-magic is not allowed? :( \$\endgroup\$ – Denker Feb 4 '16 at 17:13
  • 4
    \$\begingroup\$ How basic is basic? What is the definition of an "import"? \$\endgroup\$ – Doorknob Feb 4 '16 at 17:59
  • 6
    \$\begingroup\$ It doesn't need to be an image but each square must be at least 50px? That seems self-contradictory to me. \$\endgroup\$ – Peter Taylor Feb 4 '16 at 19:48
  • 4
    \$\begingroup\$ Programming languages here are very diverse, including some that are made specifically for golfing and have many builtin functions. Therefore, I recommend that the restriction to non-library functions be removed, and that this question instead follow the default (all imports counted in byte count; no libraries written after the challenge was posted). \$\endgroup\$ – lirtosiast Feb 5 '16 at 3:23

46 Answers 46

1
2
1
\$\begingroup\$

J, 3331 bytes

I used a trick with binary.

   load'viewmat'
   viewmat 8 8$#:43605

... because 43605 in binary is 1010101001010101 (we use #: primitive to get it). Primitive $ shapes this array of binary digits into a matrix specified with the dimensions 8 8. And viewmat is just a system library.
enter image description here
The window is fully resizable.

Thanks to @FrownyFrog for shortening it to:

   load'viewmat'
   viewmat#:8$170 85
\$\endgroup\$
2
  • \$\begingroup\$ viewmat 8$#:170 85 is 1 byte shorter \$\endgroup\$ – FrownyFrog Oct 9 '17 at 10:04
  • 1
    \$\begingroup\$ viewmat#:8$170 85 \$\endgroup\$ – FrownyFrog Oct 9 '17 at 10:23
1
\$\begingroup\$

Excel VBA, 82 Bytes

Anonymous VBE immediate window function that takes no input and outputs a checkerboard to the range A1:H8 on the ActiveSheet object

Cells.RowHeight=48:[B1:B8,D1:D8,F1:F8,H1:H8].Interior.Color=0:[A2,A4,A6,A8].Delete

This function works by first creating 4 black columns in the range B1:B8,D1:D8,F1:F8,H1:H8 and then offsetting the coloring in rows 2,4,6 and 8 to the left by one cell.

Sample Output

CheckerBoard

\$\endgroup\$
1
\$\begingroup\$

05AB1E, 21 bytes

Outputs a PBM image.

„P1Ž1‘D‚T50×{Ƶ%׫4×»

Try it online!

Commented:

„P1                   # push the string "P1"
   Ž1‘                # push 400
      D‚              # duplicate and pair to [400, 400]
        T             # push 10
         50×          # repeat this 50 times as a string => "1010 ... 10"
            {         # sort the string                  => "00...011...1"
             Ƶ%×      # repeat it 200 times
                      # this is now a string of length 20000 which represents the top rank 
                Â     # bifurcate: push the string and a reversed copy
                 «    # concatenate both, this is now the top two ranks
                  4×  # repeat this string 4 times
                    » # join the stack on newlines 
                      # this joins all lists on spaces: [400, 400] -> "400 400"

converted to png
This image was converted to png, here is the original output.

\$\endgroup\$
1
\$\begingroup\$

Funky 2, 79 bytes

draw.newImageData(400,400).mapPixels((x,y)=>[c=1-(x//50+y//50)%2c,c]).export"o"

Or, if any size tiles is allowed...

Funky 2, 67 bytes

draw.newImageData(8,8).mapPixels((x,y)=>[c=1-(x+y)%2c,c]).export"o"

This produces a file called o which is a PNG file.

Result

Breakdown

draw.newImageData(400,400)  $ Create the 8x8 chessboard at 50x50 pixels per square
.mapPixels((x,y)=>          $ Map each pixel in the image according to a function
    [ c = 1-(x//50+y//50)%2 $ This is the x added to the y of the chess tile, mod 2.
                            $ We take the result from 1 as otherwise 0, 0 is black.
    c,c ]                   $ Take the result and use it in G and B
                            $ Funky2 doesn't require a , for arguments where it is unambiguous
).export"o"                 $ Write the image to a file called "o", defaultly a PNG file.
\$\endgroup\$
1
\$\begingroup\$

Java 8, 142 bytes

import java.awt.*;v->new Frame(){public void paint(Graphics g){for(int i=32;i-->0;)g.fillRect(i%4*100+58-i%8/4*50,i/4*50+38,50,50);}{show();}}

Screenshot of the output:

enter image description here

Explanation:

import java.awt.*;         // Required import for Frame and Graphics
v->                        // Method with empty unused parameter and Frame return-type
  new Frame(){             //  Create a new Frame
    public void paint(Graphics g){
                           //   Overwrite its paint method
      for(int i=32;i-->0;) //    Loop `i` in the range (32,0]:
        g.fillRect(        //     Draw a solid rectangle, with:
                           //     (the color is black by default)
                           //      The x-coordinate of the top-left corner is:
          i%4*100          //       `i` modulo-4, multiplied by 100
          +58              //       Plus 50 since the first square is white
                           //       Plus 8 for the Frame's border
          -i%8/4*50,       //       Minus `i` modulo-8, integer-divided by 4, multiplied by 50
                           //      The y-coordinate of the top-left corner is:
          i/4*50           //       `i` integer-divided by 4 multiplied by 50
          +38,             //       Plus 8 for the Frame's border
                           //       Plus 30 for the Frame's title bar
          50,50);}         //      With a size of 50x50 pixels
    {                      //   And in an inner code-block:
      show();}}            //    Show the Frame
\$\endgroup\$
1
\$\begingroup\$

JavaScript (V8), 129 bytes

q=()=>{for(s="",i=72;i--;)s+=i%9?`<canvas height="50"width="50"style="background:#${i%2?'eee':'000'}"></canvas>`:'<br>';return s}

console.log(q())

Try it online!

Function that returns an html chess board. Used a light grey instead of white for visibility sake.

Explanation:

q=()=>{for(s="",i=72;i--;)s+=i%9?`<canvas height="50"width="50"style="background:#${i%2?'eee':'000'}"></canvas>`:'<br>';return s}
q=()=>    // Function definition
     {for(s="",i=72;i--;)  // Loop 72 times, while we're at it, initialise an empty string.
         s+=i%9?  // Add the following to the string:
           // IF not the 9th tile:
             <canvas height="50"width="50"style="background:#${i%2?'eee':'000'}"></canvas>
           // IF is the 9th tile:
             <br>

return s
```
\$\endgroup\$
1
\$\begingroup\$

Scratch 3 (557 bytes)

Sb3 file - 4.15kb

The file can be found here - https://drive.google.com/file/d/1RJ8gaVUMKGW0xdC1tQSJqy0fvGsIp8n0/view?usp=sharing

You need to have scratch installed on the system.

Scratchblocks syntax - 557 bytes

when gf clicked
wait(0.4)seconds
go to x:(140)y:(140
stamp
repeat(3
change y by(-80
stamp
end
go to x:(140)y:(140
change x by(-40
change y by(-40
stamp
repeat(3
change y by(-80
stamp
end
go to x:(140)y:(140
change x by(-80
change y by(0
stamp
repeat(3
change y by(-80
stamp
end
go to x:(140)y:(140
change x by(-200
change y by(-40
stamp
repeat(3
change y by(-80
stamp
end
go to x:(140)y:(140
change x by(-240
change y by(0
stamp
repeat(3
change y by(-80
stamp
end
go to x:(140)y:(140
change x by(-280
change y by(-40
stamp
repeat(3
change y by(-80
stamp
end

:)

\$\endgroup\$
4
0
\$\begingroup\$

SELECT., 8844 8278 bytes

(9,9,50)EXP.SELECT.EXP.RIGHT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.LEFT.LEFT.LEFT.LEFT.LEFT.LEFT.LEFT.LEFT.LEFT.LEFT.LEFT.LEFT.LEFT.LEFT.LEFT.LEFT.LEFT.LEFT.LEFT.LEFT.LEFT.LEFT.LEFT.LEFT.LEFT.LEFT.LEFT.LEFT.LEFT.LEFT.LEFT.LEFT.LEFT.LEFT.LEFT.LEFT.LEFT.LEFT.LEFT.SELECT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.RIGHT.LOG.RIGHT.SELECT.EXP.LEFT.LEFT.SELECT.RIGHT.RIGHT.EXP.LEFT.SELECT.RIGHT.LOG.RIGHT.SELECT.LEFT.END.

Sure it's long, but the first two versions I generated for this task were twice as long.

Output:

screenshot

How it Works:

Here's the program used to generate it:

init(9,9,50)
makenum(8)
square()
dec()
loop("main",computei=True)
go(1)
makenum(8)
go(1)
copyfrom(-2)
intdiv(-1)
add(-5)           # n%8 blah blah blah blah n//8 k^(n//8) k^(n%8) (n//8+n%8)
go(1)
makeneg1()
exptarget(-1)
go(1)
ifnonpositive("drawtest")
go(1)
makenum(-4,-4)    # n%8 blah blah blah blah n//8 k^(n//8) k^(n%8) n//8+n%8 (-1)^(n//8+n%8) 4 1/2 I k^(-4I) -1 (-4-4I)
go(1)
multiply(-4,-11)   # n%8 blah blah blah blah n//8 k^(n//8) k^(n%8) n//8+n%8 (-1)^(n//8+n%8) 4 1/2 I k^(-4I) -1 -4-4I (nI//8)
add(-16)          # n%8 blah blah blah blah n//8 k^(n//8) k^(n%8) n//8+n%8 (-1)^(n//8+n%8) 4 1/2 I k^(-4I) -1 -4-4I nI//8 k^(nI//8) k^(n%8) (n%8+nI//8)
add(-4)           # n%8 blah blah blah blah n//8 (-1)^(n%8) 4 1/2 I k^(-4I) -1 -4-4I nI//8 k^(nI//8) k^(n%8) n%8+nI//8 k^(n%8+nI//8) k^(-4-4I) ((n%8-4)+I(n//8-4))
output()
endif("drawtest")
go(1)
endloop("main")
writetofile("chessboard4")

In other words, loop n from 0 to 63, drawing a square at (n%8-4) + (n//8-4)i if (-1)^(n//8 + n%8) is not positive.

\$\endgroup\$
2
  • \$\begingroup\$ Thats not really golf is it :p? \$\endgroup\$ – Bruno Feb 5 '16 at 15:04
  • \$\begingroup\$ I cannot be certain it's the shortest program that does the task, no. However, I am fairly certain that the difference between this and the best possible solution in this language is insignificant compared to the total length of the program. I have one more idea in mind that may or may not be shorter. I'll try it out sometime soon. \$\endgroup\$ – quintopia Feb 5 '16 at 17:38
0
\$\begingroup\$

JavaScript (ES6), 147

for(i=0;++i<72;)document.write(`<div style="${i%9?'':(++i,'clear:both;')}float:left;width:50px;height:50px;background:#${i&1?'fff':'000'}"></div>`)

\$\endgroup\$
0
\$\begingroup\$

Python, 57 bytes

c=50*"1"+50*"0"
print"P1 400 400 "+4*(200*c+200*c[::-1])

c is a line of 50 white and 50 black pixels, 200*c is a row with a white in the front, 200*c[::-1] the reverse.

Output as PPM, usage:

python golf_chess.py > chess.ppm
\$\endgroup\$
0
\$\begingroup\$

C, 157 Bytes

#include <stdio.h>
x,q,a;FILE*f;w(){f=fopen("a","w");fprintf(f,"P1400 400 ");for(;a++<8;)for(q=0;q++<400;)for(x=0;x++<50;)fprintf(f,"%d",(q+a)%2);fclose(f);}

This generate the file a, one PBM file.
You should add the .pmb extension

Full Program

#include <stdio.h>
x,q,a;FILE*f;w(){f=fopen("a","w");fprintf(f,"P1400 400 ");for(;a++<8;)for(q=0;q++<400;)for(x=0;x++<50;)fprintf(f,"%d",(q+a)%2);fclose(f);}
main(){w();}

C, Shorter Version (98 Bytes)

x,q,a;w(){printf("P1400 400 ");for(;a++<8;)for(q=0;q++<400;)for(x=0;x++<50;)printf("%d",(q+a)%2);}  

This version print in the STDOUT the file. So you have to redirect it in the file manually.

\$\endgroup\$
2
  • \$\begingroup\$ Should one not add a .pbm extension rather than .pmb? \$\endgroup\$ – Jonathan Frech Oct 8 '17 at 1:25
  • \$\begingroup\$ I think your 98 bytes long approach defines a function that is not reusable. \$\endgroup\$ – Jonathan Frech Oct 8 '17 at 1:26
0
\$\begingroup\$

Mathematica 43 bytes

ArrayPlot@Partition[Range[0,7]~Mod~2,8,1,1]
ArrayPlot@ArrayPad[{{0,1},{1,0}},3,"Periodic"]
\$\endgroup\$
1
  • \$\begingroup\$ It appears that this is two separate solutions - you should consider separating them and labeling the larger of the two as an alternate solution. Also, as this is a graphic challenge, including a screencap of the output really helps to make your solution stand out (and thus get more points :)) \$\endgroup\$ – Taylor Scott Oct 12 '17 at 2:19
0
\$\begingroup\$

LibreLogo, 121 bytes

LibreLogo produces a vector graphic that can be exported to SVG.

Code:

x=90 pu fc 0 repeat 64[ y=repcount if y%2=0[ square 1 ]if y%16=0[ rt x fd 1 rt x ][ if y%8=0[ lt x fd 1 lt x ][ fd 1 ] ]]

Result:

enter image description here

Result (Expanded):

enter image description here

Explanation:

x = 90                        ; x                  = 90°
pu                            ; Pen Up
fc 0                          ; Fill Color         = #000
repeat 64 [                   ; Repeat 64 Times
    y = repcount              ; y                  = Loop Index
    if y % 2 = 0 [
        square 1              ; Draw 1pt Square
    ]
    if y % 16 = 0 [
        rt x                  ; Turn Right 90°
        fd 1                  ; Move Forward 1pt
        rt x                  ; Turn Right 90°
    ] [
        if y % 8 = 0 [
            lt x              ; Turn Left 90°
            fd 1              ; Move Forward 1pt
            lt x              ; Turn Left 90°
        ] [
            fd 1              ; Move Forward 1pt
        ]
    ]
]
\$\endgroup\$
0
\$\begingroup\$

Tcl/Tk, 130 bytes

time {incr j
time {grid [text .t[incr i]_$j -bg [expr ([incr n]+$j)%2?"#000":"#FFF"] -wi 6 -he 3] -row $j -column $i} 8
set i 0} 8

Could not get exactly 50 pixels per square, so I went to a round-up! enter image description here

\$\endgroup\$
0
\$\begingroup\$

Haskell, 95 bytes

f s="Be7#"*>s<*[1..50]
main=mapM putStrLn$"P1\n400 400":f(concat.f<$>[["0 ","1 "],["1 ","0 "]])

Outputs to stdout a portable bitmap (PBM), shown below at half size.

enter image description here

Try it online!

\$\endgroup\$
0
\$\begingroup\$

C (gcc), 74 71 bytes

-3 bytes thanks to ceilingcat

Outputs a PBM file to STDOUT.

i;main(){for(;i<16e4;i++)puts(i?L"01"+(i/'N '^i/50)%2:"P1 400 400 0");}

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ @ceilingcat Neato! \$\endgroup\$ – gastropner Nov 26 '20 at 20:55
1
2

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.