Golf Challenge

Given the below ASCII "Green".

|          |
|  |>      |
|  |       |
|  O       |
|          |
|          |
|          |
|          |
|          |
|          |

Let | Denote a wall
Let | Denote half the flag pole
Let > Denote the flag on the pole
Let O Denote the hole
Let o Denote the ball

The dimensions of the "Green" is 10x10. There are ten spaces between the two walls |.
There are also ten spaces, empty or not between the top and the bottom of the green.


Input an x and y value or generate two random numbers to "shoot" a golf ball onto the green.
If the x, y generated does not touch the hole or the flag pole/flag output "Try Again!"
If the x, y generated hits the hole output "Hole in One!"
if the x, y generated hits the pole output "Lucky Shot!"
if the x, y generated hits the flag output "Close One!"

After the shot, output the location of the ball on the green with a o, replacing any character it hit. Also output the respective saying above.


//Hole in one example, the O was replaced with a o
Randomed x = 3
Randomed y = 4

"Hole in One!"

|          |
|  |>      |
|  |       |
|  o       |
|          |
|          |
|          |
|          |
|          |
|          |

//Clone example, the top half of the pole was replaced with a o
Randomed x = 3
Randomed y = 2

"Lucky Shot!"

|          |
|  o>      |
|  |       |
|  O       |
|          |
|          |
|          |
|          |
|          |
|          |

//Lucky Shot example, the > was replaced with a o
Randomed x = 4
Randomed y = 2

"Close One!"

|          |
|  |o      |
|  |       |
|  O       |
|          |
|          |
|          |
|          |
|          |
|          |

//Try Again example, the <space> was replaced with a o
Randomed x = 5
Randomed y = 1

"Try Again!"

|     o    |
|  |>      |
|  |       |
|  O       |
|          |
|          |
|          |
|          |
|          |
|          |

Have fun and good luck and as this is the shortest code wins!

  • \$\begingroup\$ Is the flag / pole always at the same position? \$\endgroup\$
    – corvus_192
    Oct 25, 2016 at 19:08
  • \$\begingroup\$ You can leave it where it is, or have some fun with it and move it around. I figured it would be too much of a pain to move it around but I think it adds a fun challenge. If you move it I would make sure 2<h<=10 where h is the hole's height index. That way the flag isn't off the screen. \$\endgroup\$ Oct 25, 2016 at 19:10
  • 2
    \$\begingroup\$ Either take two paramters i and k where 0<i<=10 and 0<k<=10 or set i and k using random number generation \$\endgroup\$ Oct 25, 2016 at 19:20
  • 1
    \$\begingroup\$ @corvus_192 absolutely \$\endgroup\$ Oct 25, 2016 at 19:20
  • 1
    \$\begingroup\$ Those output strings are painful for code golf. Since there are no answers yet, consider allowing to take them as an input \$\endgroup\$
    – Luis Mendo
    Oct 25, 2016 at 19:58

13 Answers 13


JavaScript (ES6) 210 208 193 184 bytes

|          |`).repeat(10)])[17]=s[30]='|',s[18]='>',s[43]=0,s[a+=1+b*13]='o',(a-17&&a-30?a-18?a-43?'Try Again!':'Hole in One!':'Close One!':'Lucky Shot!')+s.join``)
  • -9 bytes thanx to Hedi



Jelly, 78 bytes


Play a Skill-Game or a Crap-Shoot at TryItOnline!

(Crap-shoot cost more bytes).


ṭḌ‘Çs⁵j@€⁾||Y - Main link: x, y (0-based)
ṭ             - tack            -> [y, x]
 Ḍ            - cast to decimal -> 10y+x
  ‘           - increment       -> 10y+x+1
   Ç          - call last link (1) as a monad
    s⁵        - split into chunks of size 10 (rows of green display)
         ⁾||  - literal ['|','|']
      j@€     - join €ach  with reversed @rguments (make the border)
            Y - join with line feeds
              - implicit print

⁶ẋ“€¡®µC‘ż“|>|O”©F”o⁸¦Ç - Link 1, Make green & place the ball: decimal 1-based location
  “€¡®µC‘               - code page indexes -> [12,0,8,9,67]
⁶                       - literal ' '
 ẋ                      - repeat (vectorises)
         ż              - zip with
          “|>|O”        - literal ['|','>','|','O']
                ©       -     and place the flag parts into the register
                 F      - flatten list
                     ¦  - apply to index at
                    ⁸   - input value
                  ”o    - literal 'o'
                      Ç - call the last link (2) as a monad

ċЀ®Ḍị“ȷþḄ7Ẋ“þẹƊ⁴ḳL&Ṛ“qĠṂ®““ÞzḊṁġ“»;”!Ṅṛ - Link 2, Print message: green with ball
   ®                                     - read register (the flag parts)     | > | O
ċЀ                                      - count occurrences e.g. HoleInOne: [2,1,2,0]
    Ḍ                                    - cast to decimal                  ->2120
     ị                                   - index into (1-based & modular) 2120 % 6 = 2
      “ȷþḄ7Ẋ“þẹƊ⁴ḳL&Ṛ“qĠṂ®““ÞzḊṁġ“»      - compressed list of (6) strings:
              ...["Lucky Shot","Hole in One","Try Again","","Close One",""]
                                   ;     - concatenate with
                                    ”!   - literal '!'
                                      Ṅ  - print with linefeed
                                       ṛ - yield right argument (the green)

Python 2, 290 264 262 252 248 245 bytes

It's not pretty and it's not short but I'm tired and its the first only Python answer. Enter shot in x,y format.


Golfed off 26 by redefining the way the list is built. Still no luck with the long if statement though.

-2 by replacing the long if with a dictionary and a shorter if.

-10 with thanks to @Noodle9 - I'd missed that one :)

-4 - thanks again :)

Another 3 off. Thanks.

x,y=input();a=[' ']*120;a[15]=a[27]='|';a[16],a[39],b='>','0',x+y*12
a[b],k='o',"Lucky Shot!";l={16:"Close One!",15:k,27:k,39:"Hole in One!"}
print l[b]if b in l else"Try Again!"
for z in range(10):c=z*12;a[c]=a[c+11]='|';print''.join(a[c:c+12])

For anyone who is interested in the logic, ungolfed with comments (1316 bytes but still easily fits on a 3.5" disk if anyone remembers them):

x,y=input()                                     #Get the input as a tuple
a=[' ']*120                                     #Create a great big list of spaces for the whole green
a[15]=a[27]='|'                                 #Put the flag pole in place
a[16]='>'                                       #Add the flag
a[39]='0'                                       #Add the hole
b=x+y*12                                        #Get the absolute position in the list of the input tuple 
a[b]='o'                                        #Place the ball on the green
k="Lucky Shot!"                                 #Set a variable for k because it is long and we're going to use it twice
l={16:"Close One!",15:k,27:k,39:"Hole in One!"} #Create a dictionary of the comments (using k)
print l[b]if b in l else"Try Again!"            #If the absolute index is in the dict then print it otherwise print the default
for z in range(10):                             #Loop through the length of the green
    c=z*12                                      #Set a variable for the start point of each line
    a[c]=a[c+11]='|'                            #Add the left and right walls
    print''.join(a[c:c+12])                     #Print each line in turn. Because this is in a for loop then Python will deal with newlines

Definitely the first time for me that a dictionary has been the best data format in a golf challenge.

  • \$\begingroup\$ you can use anything that's hashable as a dictionary key \$\endgroup\$
    – Noodle9
    Oct 27, 2016 at 12:52

C, 236 bytes

n,m;char*a[]={"Try Again!","Hole in One!","Lucky Shot!","Close One!"};f(x,y){n=130;m=142-y*13-x;puts(a[(m==87)+2*(m==113|m==100)+3*(m==112)]);while(n--)putchar(m==n?111:n%13?n%13==1|n%13==12|n==113|n==100?124:n==112?62:n==87?79:32:10);}


char*a[]={"Try Again!","Hole in One!","Lucky Shot!","Close One!"};
 puts(a[(m==87) + 2*(m==113|m==100) + 3*(m==112)]); 

Scala, 238 bytes

(x:Int,y:Int)=>{val r="<          |\n"
('"'+(if(x==2&y==3)"Hole in One!"else
if(x==2&(y==1|y==2))"Lucky Shot!"else
if(x==3&y==1)"Close One!"else
"Try again!")+"'",(r+"|  |>      |\n|  |       |\n|  O       |\n"+r*6)updated(1+x+13*y,'o'))}

Using zero-indexing.

This is way too readable :(


(x:Int,y:Int)=>{                                      //define an anonymous function
  val r="|          |\n"                                //a shortcut for an empty row
  (                                                     //return a tuple of
    '"'+                                                  //a double quote
    (if(x==2&y==3)"Hole in One!"                          //plus the correct string
    else if(x==2&(y==1|y==2))"Lucky Shot!"
    else if(x==3&y==1)"Close One!"
    else "Try again!"
    )+"'"                                                 //and another quote
  ,                                                     //and
    (r+"|  |>      |\n|  |       |\n|  O       |\n"+r*6) //the field
    updated(1+x+13*y,'o')                                //with the (1+x+13*y)th char replaced with a ball

I've used the formula 1+x+13*y to calculate the correct index, since each row is 13 chars long (2 borders, a newline and 10 spaces) plus an offset of one because (0,0) should be the second char.


Perl, 225 209 bytes

$_="|".$"x10 ."|
";$_.=sprintf("|  %-8s|
"x3,"|>","|",O).$_ x6;$d="Try Again!";($x,$y)=@ARGV;say$x==3?$y~~[2,3]?"Lucky Shot!":$y==4?"Hole in One!":$d:$x==4&&$y==2?"Close One!":$d;substr($_,$y*13-13+$x,1)=o;say

The two literal newlines each save one byte. Pretty standard. Prints the declaration, then the game board.


Charcoal, 99 bytes

NαNβ× ⁵↑¹⁰‖C←J⁴¦²←>↓²OM⁴↖P⁺⎇∧⁼α³⁼β⁴Hole in One⎇∧⁼α³⁼¹÷β²Lucky Shot⎇∧⁼α⁴⁼β²Close One¦Try Again¦!Jαβo

Takes 1-based input, space-separated, on stdin. Most of the code is for printing (one of) the four messages. Try it online!

Note: Charcoal is still a work in progress. This code works as of the current commit. If it stops working in the future (in particular, if the TIO link doesn't work as expected), ping me and I'll try to add a non-competing updated version that works.


NαNβ       Read two inputs as numbers into variables α and β

               Construct the green and flag:
× ⁵          Print to canvas 5 spaces
↑¹⁰          Print 10 | characters going up
‖C←         Reflect and copy leftward
             At this point, borders of green are complete; cursor is above left wall
J⁴¦²        Jump 4 units right and 2 down
←>           Print the flag, going leftward
↓²           Print the pin (2 | characters), going downward
O            Print the hole
             The last print was rightward by default, which means we're now at (4,4)
M⁴↖         Move 4 units up and left; cursor is above left wall again

               Add the proper message:
⎇∧⁼α³⁼β⁴    If α is 3 and β is 4 (in the hole):
Hole in One  
⎇∧⁼α³⁼¹÷β²  Else if α is 3 and β is 2 or 3 (hit the pin):
Lucky Shot
⎇∧⁼α⁴⁼β²    Else if α is 4 and β is 2 (hit the flag):
Close One
¦Try Again
⁺...¦!       Concatenate a ! to the string
P           Print it without changing the cursor position

               Overwrite the appropriate spot with o:
Jαβ         Jump α units right and β units down
o            Print o

Brain-Flak, 1466 1938 bytes


Try it online!

Did I win?

  • \$\begingroup\$ You seem to have a null byte being printed at the end of the first line of output. \$\endgroup\$
    – 0 '
    Oct 27, 2016 at 23:36
  • \$\begingroup\$ @1000000000 yes. I have fixed that with my latest update. Thank you for pointing that out. \$\endgroup\$
    – MegaTom
    Oct 28, 2016 at 13:33

TI-Basic, 183 bytes

Input X
Input Y
Output(1,Ans,"TRY AGAIN!
If X=4 and Y=4
Output(1,Ans,"HOLE IN ONE!
If X=5 and Y=2
Output(1,Ans,"CLOSE ONE!
If Y=2 or Y=3 and X=4
Output(1,Ans,"LUCKY SHOT!

Thank goodness TI-Basic uses tokens.

The | cannot normally be typed, but it is in the character set.

Please let me know if the result of the shot absolutely has to be lowercase.

I will add a screenshot of an example program result later.


Groovy - 235 bytes

My first attempt - A groovy closure accepting 2 integers from 0 to 9 as the X and Y coordinates for the shot.

{j,k->j++;c='';b='|';f='>';h='O';s=' ';v=[2:b,3:b,4:h];(0..9).each{y->l=(b+s*10+'|\n').chars;l[3]=v[y]?:s;l[4]=y==2?f:s;if(k==y){m=[(s):'Try Again!',(b):'Lucky Shot!',(f):'Close One!',(h):'Hole In One!'][""+l[j]];l[j]='o'};c+=l};c+=m}

Dyalog APL, 147 (or 127) bytes

Takes (y,x) as argument.

{G←10 10⍴''
G[⍳4;3]←' ||O'
G[⊃⍵;⊃⌽⍵]←'o'               ⍝ G[y;x]←
⎕←'|',G,'|'                 ⍝ Print G with sides
4 3≡⍵:'Hole in One!'        ⍝ If (y,x) ≡ (4,3)
(⊂⍵)∊2 3∘.,3:'Lucky Shot!'  ⍝ If (y,x) ∊ {(2,3), (2,3)}
2 4≡⍵:'Close One!'
'Try Again!'}               ⍝ Else

From version 16.0, we can almost halve the byte-count with the new @ operator;

@ puts the left operand into the right-operand positions in the right argument: NewChars @ Positions ⊢ Data

{⎕←'|','|',⍨' ||O>o'@((2 4)⍵,⍨3,⍨¨⍳4)⊢10 10⍴''
4 3≡⍵:'Hole in One!'
(⊂⍵)∊2 3∘.,3:'Lucky Shot!'
2 4≡⍵:'Close One!'
'Try Again!'}

Slightly modified code to make permissible in TryAPL:

Hole in One, Lucky Shot 1, Lucky Shot 2, Close One, Random


Turtled, 164 bytes

Once again, showcasing Turtlèd's balance between golfiness and verbosity for the simplest things (like incrementing a number), Turtlèd beats all but the golfing langs.

6;11[*'|:'|>;<u]'|rrr'O8:'|u'|>;'|ddd'|l'|uuu<"|>":l'|u'|>11;'|?<:?;( #Try Again!#)(>#Close One!#)(|#Lucky Shot!#)(O#Hole in One!#)'o[|r][ u]dl[|l][ u]u@"-,r["+.r_]

Try it online

Note that it is half zero indexed and half one indexed; x is one indexed, y is zero indexed; 3,3 is a hole in one


R, 230 226 bytes

M=matrix("|",10,10);M[2:9,]=" ";M[34]="0";M[4,2:3]="f";M[15]=">";function(x,y){m=switch(M[y,x],">"="Close One","f"="Lucky Shot","0"="Hole In One","Try again");M[y,x]="o";cat(m,"!\n",sep="");cat(gsub("f","|",M),sep="",fill=10)}

Thanks to @billywob for -2 bytes, noticing M[a,b] is equivalent to M[c] in a couple of cases.

Annoyingly, the two cat calls (!) can't be concatenated into one, since the fill argument messes up the message. Argh!

  • 1
    \$\begingroup\$ Move the creation of the matrix inside the function and make in an unnamed one: function(x,y){M=matrix("|",10,10);M[2:9,]=" ";M[34]="0";M[4,2:3]="f";M[15]=">";m=switch(M[y,x],">"="Close One","f"="Lucky Shot","0"="Hole In One","Try again");M[y,x]="o";cat(m,"!\n",sep="");cat(gsub("f","|",M),sep="",fill=10)} \$\endgroup\$
    – Billywob
    Oct 26, 2016 at 14:03
  • \$\begingroup\$ Oh, fair enough. Mind you, I don't think I needed f= in my solution anyway. Removed. \$\endgroup\$
    – JDL
    Oct 26, 2016 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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