18
\$\begingroup\$

Write a program/function to output a peg solitaire board in its starting position:

 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ○ ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 

Pegs must be represented as your choice of: ,x,X,+,#

The central hole must be represented as your choice of ,o,O,0,_,.

Note there is one space between each two pegs/holes on the same row.

Extra space above, below, or to the right of the board is allowed.

Code golf.

\$\endgroup\$

18 Answers 18

13
\$\begingroup\$

JavaScript (ES6), 62 bytes

f=(n=98)=>n--?` +o `[n%14?n-49?~153>>n/3.5%20&n&1:2:3]+f(n):"" 

Try it online!

Method

We use a counter \$n\$ going from \$97\$ at the top-left corner to \$0\$ at the bottom-right corner. Even values are converted to either a space or a linefeed and the central hole is located at \$n=49\$. Remaining odd values are shown in figure 1.

We apply a horizontal partition by computing \$\lfloor 2n/7\rfloor\$ (figure 2).

We reduce modulo \$20\$ to make the first two rows look like the last two ones (figure 3).

There are pegs everywhere except when the result is \$0\$, \$3\$, \$4\$ or \$7\$.

97 95 93 91 89 87 85 | 27 27 26 26 25 24 24 | 7 7 6 6 5 4 4 83 81 79 77 75 73 71 | 23 23 22 22 21 20 20 | 3 3 2 2 1 0 0 69 67 65 63 61 59 57 | 19 19 18 18 17 16 16 | 19 19 18 18 17 16 16 55 53 51 -- 47 45 43 | 15 15 14 -- 13 12 12 | 15 15 14 -- 13 12 12 41 39 37 35 33 31 29 | 11 11 10 10 9 8 8 | 11 11 10 10 9 8 8 27 25 23 21 19 17 15 | 7 7 6 6 5 4 4 | 7 7 6 6 5 4 4 13 11 9 7 5 3 1 | 3 3 2 2 1 0 0 | 3 3 2 2 1 0 0 Fig. 1 Fig. 2 Fig. 3 

Commented

f = ( // f is a recursive function taking: n = 98 // n = cell counter, starting at 98 ) => // n-- ? // if n is not 0 (decrement afterwards): ` +o\n`[ // lookup string n % 14 ? // if this is not an end of row: n - 49 ? // if this is not the central cell: ~153 // 32-bit mask: 11111111111111111111111101100110 // i.e. only bits #0, #3, #4 and #7 are zero >> // right-shift n / 3.5 // divide n by 7/2 // (the result is implicitly floored below) % 20 // reduce modulo 20 & n // bitwise AND with n for the parity & 1 // isolate the least significant bit // -> this gives ' ' or '+' : // else: 2 // use 'o' : // else: 3 // use '\n' ] // + f(n) // append the result of a recursive call : // else: "" // stop 
\$\endgroup\$
12
\$\begingroup\$

Python 3, 51 bytes

for i in b"":print(*"_++++o "[i:i+4]+"+"*3) 

Try it online!

The bytestring contains unprintable ASCII with codes [6,6,1,2,1,6,6]. Compare to the printable version that follows, except here the indices have been shifted up by 1 to avoid null bytes which must be escaped.

53 bytes

for i in b"5501055":print(*"++++o "[i%8:][:4]+"+"*3) 

Try it online!

The idea is to encode each line as a substring of ++++o , specifically, one of ++++, +++o, or . Then, append +++, and print the result space separated. The indices give the start position of the substring, which is truncated to length 4.

\$\endgroup\$
8
\$\begingroup\$

R, 58 56 bytes

d=matrix("X",7,7) d[z<--3:-5,z]=" " d[25]=0 write(d,1,7) 

Attempt This Online!

Construct the array iteratively and write. It's 2 bytes shorter to initialize the array with X and use negative indexing to assign spaces.

R, 64 60 bytes

write(c(" ","X",0)[1+outer(x<-abs(-3:3)<2,x,"|")+!24:0],1,7) 

Attempt This Online!

Construct indices (1,2,3) using row() and t() corresponding to " ","X", and 0, respectively. Abuses recycling to set the center as 0.

\$\endgroup\$
5
\$\begingroup\$

Charcoal, 16 12 bytes

E³XX‖O↘¬OUE¹ 

Try it online! Link is to verbose version of code. Uses X and O for the output characters. Explanation:

E³XX 

Output a 2×3 rectangle of Xs.

‖O↘¬ 

Reflect diagonally downright, then separately down and left, to complete the remaining Xs.

O 

Output the central O. (It saves a byte to do this here rather than elsewhere in the code.)

UE¹ 

Space out the pegboard horizontally.

\$\endgroup\$
5
\$\begingroup\$

Canvas, 13 bytes

“Q╴gG"kj╶‟┼ * 

Uses # for pegs and o as center hole.

Try it online.

The compressed string " ##¶ ##¶####¶###o" can likely be golfed by simply generating this string, but this will do for now.

Explanation:

“Q╴gG"kj╶‟ "# Compressed " ##¶ ##¶####¶###o" ┼ # Quad-palindromize with overlap * # Add a space between each pair of characters # (after which the result is output implicitly) 
\$\endgroup\$
5
\$\begingroup\$

Python 3, 53 bytes

a=" +o+ " for x in a:print(*map({x:x}.get,a,7*"+")) 

Try it online!

Another "clean" (no non printables) 53er.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Wow, that's impressively dense \$\endgroup\$ Commented Sep 17 at 4:59
5
\$\begingroup\$

AWK, 71 68 bytes

BEGIN{a="***";for(;++i<8;)print(i<3||i>5?" "a" ":i~4?a"0"a:a"*"a)} 

Try it online!

Removed some curly braces and used for(;++i<8;) instead of for(;i<7;i++). Could also use END instead of a BEGIN (-2 bytes; prints the output when the process is killed).

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

Vyxal 3, 15 bytes

"ᑂ∨⎘“BᏜx ip√7⧢' 

Vyxal It Online!

binary encoding of the first half of the string, prepend to empty stack (0) and palindromized before splitting

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

x86-64 machine code, 33 bytes

B2 71 B8 20 20 20 20 AB 6A 03 59 B0 58 66 F3 AB B0 0A AA B1 07 D0 EA 73 F2 75 E7 88 17 00 4F D2 C3 

Try it online!

Following the standard calling convention for Unix-like systems (from the System V AMD64 ABI), this takes in RDI a memory address at which to place the result, as a null-terminated byte string. The chosen characters are X and _.

In assembly:

f: mov dl, 0b01110001 # Set DL to this value. r0: mov eax, 0x20202020 # Set EAX to this value (4 spaces in ASCII). stosd # Write it to the output string, advancing the pointer. push 3; pop rcx # Set RCX to 3. r1: mov al, 0x58 # Set the low byte of EAX to 'X' in ASCII. rep stosw # Write the low two bytes of EAX to the output string, # advancing the pointer, and repeating RCX times, counting RCX down to 0. mov al, 0x0A # Set the low byte of EAX to a line feed in ASCII. stosb # Write it to the output string, advancing the pointer. mov cl, 7 # Set the low byte of RCX to 7, making RCX 7. shr dl, 1 # Shift DL right by 1 bit. The low bit goes into CF. jnc r1 # If that bit is 0, jump to make a middle row. jnz r0 # If the shift result is nonzero, jump to make an outer row. mov [rdi], dl # End the string with a 0 byte from DL. add [rdi - 46], cl # Add CL (7) to the middle character: 'X' becomes '_'. ret # Return. 
\$\endgroup\$
1
  • \$\begingroup\$ Wow, cool choice of language! \$\endgroup\$ Commented Sep 17 at 12:03
3
\$\begingroup\$

05AB1E, 19 18 bytes

„ #D0JŽ∞,ÅΓ4ä€û».∊ 

Uses # as pegs and 0 as center hole.

Try it online.

Explanation:

„ # # Push string " #" D # Duplicate it 0 # Push a 0 J # Join the stack together to a string: " # #0" Ž∞, # Push compressed integer 22291 ÅΓ # Run-length decode, using this string and digits: # [" "," ","#","#"," "," ","#","#","#","#","#","#","#","#","#","0"] 4ä # Split the list of characters into 4 equal-sized parts €û # Palindromize each list » # Join each inner list by spaces, # and then each string by newlines .∊ # Mirror it vertically with overlapping center row # (after which the result is output implicitly) 

See this 05AB1E tip of mine (section How to compress large integers?) to understand why Ž∞, is 22291.

\$\endgroup\$
3
\$\begingroup\$

Vyxal 3, 18 bytes

'x"6⊍æ“f×6«¨„”ٽ0≜ 

Vyxal It Online!

'x"6⊍æ“f×6«¨„”ٽ0≜­⁡​‎‎⁡⁠⁡‏⁠‎⁡⁠⁢‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁣⁡‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁣‏⁠‎⁡⁠⁤‏⁠‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏⁠‎⁡⁠⁢⁣‏⁠‎⁡⁠⁢⁤‏‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁣⁢‏⁠‎⁡⁠⁣⁣‏‏​⁡⁠⁡‌⁢⁡​‎‎⁡⁠⁣⁤‏⁠‎⁡⁠⁤⁡‏‏​⁡⁠⁡‌⁢⁢​‎‎⁡⁠⁤⁢‏‏​⁡⁠⁡‌⁢⁣​‎⁠‎⁡⁠⁢⁡⁣‏‏​⁡⁠⁡‌⁢⁤​‎‎⁡⁠⁤⁣‏⁠‎⁡⁠⁤⁤‏‏​⁡⁠⁡‌⁣⁡​‎‎⁡⁠⁢⁡⁡‏⁠‎⁡⁠⁢⁡⁢‏‏​⁡⁠⁡‌­ 'x # ‎⁡single character 'x' × # ‎⁢repeated "6⊍æ“f # ‎⁣3,3,9,9,9,3,3 times 6« # ‎⁤prepend spaces to each until length is >=6 ¨„ # ‎⁢⁡intersperse spaces in each ” # ‎⁢⁢join on newlines ≜ # ‎⁢⁣replace ٽ # ‎⁢⁤the middle character 0 # ‎⁣⁡with 0 💎 

Created with the help of Luminespire.

<script type="vyxal3"> 'x"6⊍æ“f×6«¨„”ٽ0≜ </script> <script> args=[] </script> <script src="https://themoonisacheese.github.io/snippeterpreter/snippet.js" type="module"/>

\$\endgroup\$
5
  • 1
    \$\begingroup\$ 0 (a zero) is allowed as center, so you could replace the 'o with 0 for -1 byte. :) \$\endgroup\$ Commented Sep 16 at 13:56
  • \$\begingroup\$ @KevinCruijssen true, very nice! \$\endgroup\$ Commented Sep 16 at 14:02
  • \$\begingroup\$ 16 bytes using binary encoding \$\endgroup\$ Commented Sep 16 at 17:27
  • \$\begingroup\$ @pacman256 yeah that sounds about right. feel free to submit your own answer as this is a totally other technique \$\endgroup\$ Commented Sep 16 at 21:38
  • \$\begingroup\$ @pacman256 also -1 byte by replacing 0+ with p because empty stack \$\endgroup\$ Commented Sep 16 at 21:42
3
\$\begingroup\$

Jelly, 16 bytes

⁽doṃ⁾ x;0s4ŒBŒḄG 

A full program printing to stdout.

Try it online!

How?

⁽doṃ⁾ x;0s4ŒBŒḄG - Main Link: no arguments ⁽do - 26112 ṃ⁾ x - cast to base 2 with 1 as " " and 0 as "x" ;0 - concatenate a zero s4 - split into fours -> top-left ŒB - bounce each row -> top half ŒḄ - bounce -> whole board G - format as a grid (add adjoining spaces and newline characters) - implicit, smashing print 

I do quite like the more mathsy, \$17\$ bytes - Try it online!:

2ŻŻŒB+þ`ƽị“xo ”G - Main Link 2 - two Ż - zero-range -> [0,1,2] Ż - prefix with a zero -> [0,0,1,2] ŒB - bounce -> [0,0,1,2,1,0,0] +þ` - table of addition -> [[0, 0, 1, 2, 1, 0, 0], [0, 0, 1, 2, 1, 0, 0], [1, 1, 2, 3, 2, 1, 1], [2, 2, 3, 4, 3, 2, 2], [1, 1, 2, 3, 2, 1, 1], [0, 0, 1, 2, 1, 0, 0], [0, 0, 1, 2, 1, 0, 0]] ƽ - integer square-root (0->0; 1,2,3->1; 4->2) ị“xo ” - 1-index, cyclically into "xo " G - format as a grid (add adjoining spaces and newline characters) - implicit, smashing print 

...which is the same length as not using the integer square-root, with:

2ŻŻŒB+þ`ị“xxxo ”G 
\$\endgroup\$
3
\$\begingroup\$

Maple,  90   89  88 bytes

proc()M:=Matrix(7,fill=" ");M(15..35):="x";M[3..5,..]:="x";M(25):="o";printf("%s",M)end; 
f:=proc() M:=Matrix(7,fill=" "); M(15..35):="x"; M[3..5,..]:="x"; M(25):="o"; printf("%s",M) end; 

The "%s" format automatically puts spaces between characters and inserts blank lines.

\$\endgroup\$
2
  • \$\begingroup\$ -1 replacing M[4,4] by M(25) following @Guiseppe \$\endgroup\$ Commented Sep 16 at 23:11
  • \$\begingroup\$ another -1 byte replacing M[..,3..5] by M(15..35); same philosophy as above comment \$\endgroup\$ Commented Sep 16 at 23:21
3
\$\begingroup\$

Ruby, 60 bytes

a=(0..6).map{|i|" "*(j=4&5-i%5)+"# "*j^=7}*$/ a[43]=?. $><<a 

Try it online!

\$\endgroup\$
3
\$\begingroup\$

brainfuck, 163 bytes

Count excludes 8 newlines added for clarity.

+++++++++++[->+>++++>+++>++++>+++>++++<<<<<<] >[->] <<....>[.<] >>>>>....>[.<] >>[.>]<<.>.<.>[.<] >>[.>]<<.>+++.---<.>[.<] >>[.>]<<.>.<.>[.<] >>>>>....>[.<] >>>>>....>[.<] 

Try it online!

We create 11,44,33,44,33,44 on the tape, then subtract 1 from each to give + + + with a newline at the lefthand end. For rows with 3 symbols we print some spaces then run through the tape right to left ending in newline. For rows with 7 symbols we run left to right, fiddle about to create the centre symbol, then run right to left ending in newline.

The code is quite repetitive so there is some opportunity for further golfing.

\$\endgroup\$
3
\$\begingroup\$

Japt -R, 21 20 19 bytes

[3,3FE]Ëìxio)¸êÃû ê 

Test it

[3,3FE]Ëìxio)¸êÃû ê [ :Create an array containing 3,3 : Two copies of 3 F : 15 E : 14 ] :End array Ë :Map ì : Convert to digit array in base xio : "x" prepended with "o" ) : End base conversion ¸ : Join with spaces ê : Palindromise à :End map û :Centre pad with spaces to length of longest ê :Palidromise :Implicit output joined with newlines 
\$\endgroup\$
2
\$\begingroup\$

APL+WIN 52 bytes

m←v⍪(3 13⍴13⍴'# ')⍪v←2 13⍴13↑¯9↑'# # #'⋄m[4;7]←'o'⋄m 

Try it online! Thanks to Dyalog Classic

\$\endgroup\$
2
\$\begingroup\$

Perl 5, 63 bytes

say"@$_"for@;=([$"x5,(X)x3])x2,$c=[(X)x9],[(X)x4,O,(X)x4],$c,@; 

Try it online!

\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.