# A golfed golfing game

You have to make a golf game with the following specification :-

1. The field is 10 X 10 characters without spaces.

2. Free ground is represented by .

3. A hole generated at random(not at 0,0) is shown by O (its alphabet and not zero).

4. The ball starts at 0,0 represented by X.

5. You can hit the ball with input d 4 (meaning direction to the bottom with magnitude of 4 characters after the ball more like vectors in physics).

Example:

X.........
..........
..........
..........
..........
..........
..........
..........
..........
.........O
score : 0
b 4

But, since you are not so good at golfing therefore the force you think is applied is often only close to the actual force that is applied !(if x the offset in force then -2 < X < +2)

6. you should have to tell the real force applied and display the ball moved to that real force like this :-

lets say the ball actually moved by 3.

..........
..........
..........
X.........
..........
..........
..........
..........
..........
.........O
score : 1
applied force : 4
real force    : 3

The four directions are U (up), D (down), l (left), r (right) and magnitude of force should be between 0 and 10 (excluding both 0 and 10).

7. The ball should not bounce off the edges, if the force reaches that much then the the ball stops at the last point at the boundary.

r 9

..........
..........
..........
.........X
..........
..........
..........
..........
..........
.........O
score : 2
applied force : 9
real force    : 11

8. In the end when the game is finished you have to display Par in <n> shots! (where n is score)

## Coding:

1. You have to design a working program showing the exact output.
2. post the code in golfed and ungolfed versions.
3. also post a successful test run (images if possible).
4. No use of functions or languages excessively specialized to this task (I don't know if they really exist!)

## Winner:

Winner will be chosen 1 week from now. Winner is the submission with least characters.

# And, most important thing

Best of luck :)

• I would like to see the reasons of up-votes(because I don't know why I got up-votes and how will I keep it up) or down-votes(Obviously to improve).Also, please don't answer my question if it has down-votes. Commented Mar 15, 2014 at 15:19
• @the downvoter Please explain the reason of your down-vote! Commented Mar 15, 2014 at 15:38
• "you are not so good at golfing" seems like an understatement if I can aim to hit the ball (e.g. D 1) and not move it at all (X=-1). Most of the spec is intelligible, but you haven't said what to do if a) the intended stroke would place the ball out of bounds; b) the intended stroke wouldn't, but with X taken into account it would. Commented Mar 15, 2014 at 16:31
• This is woefully underspecified. Physics challenges are tough. The obvious loophole here has nothing to do with physics, though: you haven't required that the force applied must be randomized, and you have explicitly requested that the force not be displayed. Consequently, instead of an elementary physics simulation you are likely to get the golfiest approach to displaying the output field with one character replaced by an O. Commented Mar 15, 2014 at 18:35
• @LegoStormtroopr "magnitude of force should be between 0 and 10 (excluding both 0 and 10)" bounds the force offset. Commented Mar 18, 2014 at 1:08

# K, 319305 308

{b:(10*!-_-(100)%10)_@[100#".";0,1?100;:;"XO"];c:0 0;e:s:-1;while["O"in,/b;e@b;e"Score : ",$s+:1;h:"SI"$" "\:0:0;h[1]:1|*(w:h 1)+1?-2+!5;b:.[;;:;]/[b;(c;d:9&((rdlu!({(0,x)+};{(x,0)+};{(0,-x)+};{(-x,0)+}))[h 0]h 1)c);".X"];c:d;e"Applied force : ",$w;e"Real force : ",$h 1];e"Par in ",($1+s)," shots!";} Could definitely golf this more. Incrementing/decrementing the indices is what kills me and could probably be done much simpler. (Slightly) more readable than the one liner: golf:{[] b:(10*!-_-(100)%10)_@[100#".";0,1?100;:;"XO"]; c:0 0;e:s:-1; while["O"in,/b; e@b; e"Score : ",$s+:1;
h:"SI"$" "\:0:0; h[1]:1|*(w:h 1)+1?-2+!5; b:.[;;:;]/[b;(c;d:9&((rdlu!({(0,x)+};{(x,0)+};{(0,-x)+};{(-x,0)+}))[h 0]h 1)c);".X"]; c:d; e"Applied force : ",$w;
e"Real force    : ",$h 1]; e"Par in ",($1+s)," shots!";
}

.

k)golf[]
X.........
..........
..........
..........
.......O..
..........
..........
..........
..........
..........
Score : 0
d 4
Applied force : 4
Real force    : 4
..........
..........
..........
..........
X......O..
..........
..........
..........
..........
..........
Score : 1
r 7
Applied force : 7
Real force    : 5
..........
..........
..........
..........
.....X.O..
..........
..........
..........
..........
..........
Score : 2
r 2
Applied force : 2
Real force    : 1
..........
..........
..........
..........
......XO..
..........
..........
..........
..........
..........
Score : 3
r 1
Applied force : 1
Real force    : 1
Par in 4 shots!
• Won't change the current outcome, but the colons for the two force lines should be aligned. Congrats! Commented Mar 25, 2014 at 14:23
• @Jonathan - made the change for an extra 3 chars. Commented Mar 25, 2014 at 15:07

# Scala, 605

Golfed:

import scala.util.Random;object G extends App{var b,h=(0,0);while(b==h)h =(Random.nextInt(10),Random.nextInt(10));var s=0;def f{for(y<- 0 to 9){for(x<- 0 to 9)print(if(b==(x,y))"X"else if(h==(x,y))"O"else ".");println};println("score : "+s)};f;while(b!=h){val i=readLine.split(" ");val x=i(1).toInt.ensuring(a=>a>0&&a<10)-1+Random.nextInt(3);b=i(0).toLowerCase match{case "d"=>(b._1,b._2+x) case "u"=>(b._1,b._2-x) case "r"=>(b._1+x,b._2) case "l"=>(b._1-x,b._2)};b=(b._1.min(9).max(0),b._2.min(9).max(0));s+=1;f;println("applied force : "+i(1));println("real force : "+x)};println("Par in "+s+" shots!")}

Just a straight-forwardly implemented ice-breaker. Input is read line-by-line from stdin and power lower then 1 or higher then 9 will result in an AssertionError to comply with the specs.

I am pretty sure there are shorter programs, but let's see how far we get ;-)

Ungolfed:

import scala.util.Random

object Golf extends App {
var b, h = (0, 0)
while (b == h) h = (Random.nextInt(10), Random.nextInt(10))
var s = 0

def field {
for (y <- 0 to 9) {
for (x <- 0 to 9)
print(if (b ==(x, y)) "X" else if (h ==(x, y)) "O" else ".")
println
}
println("score : " + s)
}
field
while (b != h) {
val i = readLine.split(" ")
val x = i(1).toInt.ensuring(a => a > 0 && a < 10) - 1 + Random.nextInt(3)
b = i(0).toLowerCase match {
case "d" => (b._1, b._2 + x)
case "u" => (b._1, b._2 - x)
case "r" => (b._1 + x, b._2)
case "l" => (b._1 - x, b._2)
}
b = (b._1.min(9).max(0), b._2.min(9).max(0))
s += 1
field
println("applied force : " + i(1))
println("real force : " + x)
}
println("Par in " + s + " shots!")
}

Random example run:

X......O..
..........
..........
..........
..........
..........
..........
..........
..........
..........
score : 0

r 8

.......OX.
..........
..........
..........
..........
..........
..........
..........
..........
..........
score : 1
applied force : 8
real force : 8

l 1

.......OX.
..........
..........
..........
..........
..........
..........
..........
..........
..........
score : 2
applied force : 1
real force : 0

l 1

.......X..
..........
..........
..........
..........
..........
..........
..........
..........
..........
score : 3
applied force : 1
real force : 1
Par in 3 shots!
• Sorry for the delay in choosing the answer! Commented Mar 24, 2014 at 16:21
• @MukulKumar no problem. still I am a bit surprised I got the accepted answer :D Commented Mar 24, 2014 at 22:23
• OH MY GOD!!! WHAT DID I DO I thought this is popularity-contest ......sorry pal. Commented Mar 25, 2014 at 13:51

## Julia, 427

This is pretty straightforward on the whole. Julia isn't really designed to deal with I/O very heavily, as she's intended for large-scale scientific computation. But for our limited purposes here, she obliges by accepting some STDIO and playing a little game of golf with us.

Edit: charged myself two additional characters because I had omitted the spaces after the colon for applied force and real force.

a=fill(".",10,10)
x,y,s,b,c=1,1,0,iceil(10rand()),iceil(10rand())
a[x,y],a[b,c]="X","O"
while(x,y)!=(b,c)
print(a)
d,f=m[1],m[2]
o=(d in"Ul"?-1:d in "Dr"?1:0)
g=clamp(round((f-48)+4rand()-2),1,9)*o
a[x,y]="."
x,y=(d in "UD"?clamp(x+g,1,10):x),(d in "lr"?clamp(y+g,1,10):y)
a[x,y]="X"
s+=1
print("score : ",s,"\napplied force : ",f,"\nreal force    : ",abs(g),"\n")
end
print(a,"\nPar in ",s," shots!")

Here's a sample game, in which the final putt becomes an increasingly trying situation for our golfer.

(Note that occasionally our Ints get converted to Floats on the fly...ah, the vagaries of dynamic typing.)

X . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . O . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
STDIN> r5
score : 1
applied force : 5
real force    : 7.0
. . . . . . . X . .
. . . . . . . . . .
. . . . . . . . . .
. . . . O . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
STDIN> D3
score : 2
applied force : 3
real force    : 2.0
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . X . .
. . . . O . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
STDIN> l3
score : 3
applied force : 3
real force    : 3.0
. . . . . . . . . .
. . . . . . . . . .
. . . . X . . . . .
. . . . O . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
STDIN> D1
score : 4
applied force : 1
real force    : 2.0
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . O . . . . .
. . . . X . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
STDIN> U1
score : 5
applied force : 1
real force    : 2.0
. . . . . . . . . .
. . . . . . . . . .
. . . . X . . . . .
. . . . O . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
STDIN> D1
score : 6
applied force : 1
real force    : 1
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . X . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .

Par in 6 shots!

# PHP - 563

<?php
$b=[0,0];do{$h=[rand(0,9),rand(0, 9)];}while(!($h[0]|$h[1]));
$s=0;function o(){global$b,$h,$s;for($y=0;$y<10;$y++){for($x=0;$x<10;$x++){$p=[$y,$x];if($p==$b)echo'X';elseif($p==$h)echo'O';else echo '.';}echo "\n";}echo "score :$s
";}o();while($h!=$b){list($d,$p)=explode(' ',fread(STDIN,9));$P=max(1,min(9,$p+rand(-2,2)));switch($d){case'u':$b[0]-=$P;break;case'd':$b[0]+=$P;break;case'l':$b[1]-=$P;break;case'r':$b[1]+=$P;break;}$b=[max(0,min(9,$b[0])),max(0,min(9,$b[1]))];$s++;o();echo "applied force :${p}real force    : $P ";}echo "Par in$s shots!";

X.........
......O...
..........
..........
..........
..........
..........
..........
..........
..........
score : 0
d 1
..........
X.....O...
..........
..........
..........
..........
..........
..........
..........
..........
score : 1
applied force : 1
real force    : 1
r 5
..........
......X...
..........
..........
..........
..........
..........
..........
..........
..........
score : 2
applied force : 5
real force    : 6
Par in 2 shots!

Ungolfed:

<?php
$ball = [0,0]; do {$hole = [ rand(0,9), rand(0, 9) ];
}while(!($hole[0]|$hole[1]));
$score = 0; function out() { global$ball, $hole,$score;
for($y=0;$y<10;$y++) { for($x=0;$x<10;$x++) {
$p=[$y,$x]; if($p==$ball)echo'X'; elseif($p==$hole)echo'O'; else echo '.'; } echo "\n"; } echo "score :$score
";
}
out();
while($hole!=$ball) {
list($direction,$power) = explode(' ',fread(STDIN,10));
$rPower=$power+rand(-2,2);
switch($direction) { case 'u':$ball[0]-=$rPower;break; case 'd':$ball[0]+=$rPower;break; case 'l':$ball[1]-=$rPower;break; case 'r':$ball[1]+=$rPower;break; }$ball[0]=max(0,min(9,$ball[0]));$ball[1]=max(0,min(9,$ball[1]));$score++;
out();
echo "applied force : ${power}real force :$rPower
";
}
echo "Par in \$score shots!";
• You may not have me beat for shortest code, but par in 2 shots! It took me (whispers) 6. Commented Mar 19, 2014 at 19:11
• -100 for using global :D Commented Jun 2, 2014 at 13:07