高尔夫高尔夫


24

高尔夫挑战赛

给定以下ASCII“绿色”。

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

让我们代表|一堵墙
让我们|代表旗杆的一半
让我们>代表杆子上的国旗
让我们O代表孔
让我们o代表球

“绿色”的尺寸为10x10。两堵墙之间有十个间隔|
在果岭的顶部和底部之间还有十个空间,是否为空。

挑战

输入x和y值或生成两个随机数以将高尔夫球“击中”到果岭上。
如果生成的x,y没有碰到孔或旗杆/旗标输出“再试一次!”。
如果生成的x,y碰到孔输出“孔合一!”。
如果生成的x,y击中了极点输出“幸运射击!”
如果生成的x,y到达标志输出“ Close One!”(关闭一个!),

投篮后,用,在果岭上输出球的位置,并o替换击中的任何字符。还要输出上面的相应说法。

例子:

//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       |
|          |
|          |
|          |
|          |
|          |
|          |

祝您好运和好运,因为这是所以最短的代码赢了!


旗杆是否始终在同一位置?
corvus_192 2016年

您可以将其保留在原处,或对其进行一些有趣的移动。我认为移动它会很痛苦,但是我认为这增加了一个有趣的挑战。如果移动它,我将确保2 <h <= 10,其中h是孔的高度指数。这样,标志就不会出现在屏幕上。
jacksonecac

2
取两个参数i和k,其中0 <i <= 10和0 <k <= 10或使用随机数生成来设置i和k
jacksonecac 16-10-25

1
@ corvus_192绝对是
jacksonecac

1
这些输出字符串使代码打高尔夫球很痛苦。由于没有解答,考虑允许把他们作为一个输入
路易斯Mendo

Answers:


10

的JavaScript(ES6)210 208 193 184个字节

f=(a,b)=>((s=[...(`
|          |`).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字节thanx至Hedi

演示版


8

果冻,78 字节

ċЀ®Ḍị“ȷþḄ7Ẋ“þẹƊ⁴ḳL&Ṛ“qĠṂ®““ÞzḊṁġ“»;”!Ṅṛ
⁶ẋ“€¡®µC‘ż“|>|O”©F”o⁸¦Ç
ṭḌ‘Çs⁵j@€⁾||Y

播放技巧型游戏,或者废话拍摄TryItOnline!

(废话处理需要更多字节)。

怎么样?

ṭḌ‘Ç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)

8

Python 2中,290个 264 262 252 248 245字节

它不漂亮,也不短,但是我很累,它是第一个唯一的Python答案。输入x,y格式的镜头。

编辑

通过重新定义列表的构建方式,打入26。但是,如果使用长的if语句仍然没有运气。

-2通过用字典替换long if和一个if较短。

-10感谢@ Noodle9-我错过了一个:)

-4-再次感谢:)

再3折。谢谢。

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])

对于任何对逻辑感兴趣的人,请不要加上注释(1316字节,但如果有人记得它们,仍可以轻松地放在3.5英寸磁盘上):

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

绝对对我来说,字典是高尔夫挑战中最好的数据格式。


您可以将任何可散列的内容用作字典键
Noodle9 '16

6

C,236字节

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);}

取消高尔夫:

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);
}

3

Scala,238个字节

(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'))}

使用零索引。

这太可读了:(

说明:

(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
  )
}

我使用了公式1+x+13*y来计算正确的索引,因为每行的长度为13个字符(2个边框,一个换行符和10个空格)加上一个偏移量,因为(0,0)应该是第二个字符。


3

Perl中,225个 209字节

$_="|".$"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

两个文字换行符每个保存一个字节。很标准。打印声明,然后打印游戏板。


3

木炭,99字节

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

在stdin上使用空格分隔的基于1的输入。大多数代码用于打印这四个消息(之一)。在线尝试!

注意:木炭仍在开发中。此代码从当前提交起生效。如果以后停止运行(特别是TIO链接无法按预期运行),请ping通我,我将尝试添加一个有效的非竞争性更新版本。

说明

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
             Else:
¦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

3

Brain-Flak,1466 1938字节

(<()>)<>((()()()()()){}){({}[()]<(((((((()()()()()){})){}{}()){}){})<((()()()()()){}){({}[()]<(((((()()){}){}){}){})>)}{}>)((()()()()()){})>)}{}((((()()){}){}){}()){({}[()]<({}<>)<>>)}{}{}{}(((((()()()()()){})){}{}()){})(((((((()()()()()){})){}{}()){}){})<(((()()()){}){}()){({}[()]<({}<>)<>>)}{}{}>)(((()()()){}){}()){({}[()]<({}<>)<>>)}{}{}(((((()()()){}){}){}){}){<>({}<>)}(<>{}((((({}[()])){}){})){}{}{}()<>{}){({}[()]<({}<>)<>>)}{}({}<(((((((()()()){}){})){}{}())){}{}){<>({}<>)}>)(({}<((({}(((()()){}){}){}()){})[()])>)[((((()()){}){}){}){}]){({}[(((()()){}){}){}]){({}[((()()()){}()){}]){{}{}(((((((((()()()){}()){}){}()){}){})[()()()()()])[(()()()){}()])<(((((()()()()()){}){}){}()){}())(((((()()){}){}){}){})>(((()()){}){}){}())(([((()()()){}()){}](({})<>)<>)[((()()){}){}])((<>{}<>[()()()()])[(((()()()()()){}){}){}()])<>}{}{{}((((((((((()()()){}){}){}()){}){}())<>)<>((()()())){}{})[(((()()()()()){})){}{}()])<(((((()()){}){}){}){})((((<>{}<>)((()()()){}()){})[()()()()])[()()()])>[((()()()){}){}])<>}}{}{{}((((((((()()()){}){}){}()){}){}())((()()())){}{})[(((()()()()()){})){}{}()])((((((()()){}){}){}){})<(((((()()()()()){}){({}[()])}{}){})[()()()()()])>)((((((((()()()){}){}){}()){}){}())(()()()){}())()()())((((((()()()){}){}){})){}{})<>}{}}{}{{}(((((((()()()()()){}){({}[()])}{}){})[()()()()()])[((()()){}){}])(()()()){})(((((((((((()()){}){}){}){})))({}<({}{}())>)[()()()()]){}())[(()()()){}()])[(((()()()()()){})){}{}])<>}<>(((((()()){}){}){}()){})

在线尝试!


我赢了吗


您似乎在输出的第一行末尾打印了一个空字节。
0'

@ 1000000000是的。我已经通过最新更新修复了该问题。感谢您指出这一点。
MegaTom

2

TI-Basic,183 个字节

Input X
Input Y
X+1➡X
ClrHome
For(I,1,10
Output(I,1,"|
Output(I,12,"|
End
Output(2,4,"|>
Output(3,4,"|
Output(4,4,"O
Output(Y,X,"o
13
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!

谢天谢地,TI-Basic使用令牌。

|不能正常键入,但它是在字符集。

请让我知道拍摄结果是否绝对必须是小写。

稍后,我将添加示例程序结果的屏幕截图。


2

Groovy-235字节

我的第一次尝试-常规的闭包接受0到9之间的2个整数作为镜头的X和Y坐标。

{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):'再试一次!',(b):'幸运射击!',(f):'关闭一个!', (h):“孔合一!”] [“” + l [j]]; l [j] ='o'}; c + = l}; c + = m}

2

Dyalog APL,147(或127)字节

以(y,x)作为参数。

{G10 10''
G[⍳4;3]←' ||O'
G[2;4]←'>'
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

从16.0版开始,我们几乎可以使用new @运算符将字节数减半;

@ 将左操作数放在右参数的右操作数位置: 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!'}

稍作修改的代码即可在TryAPL中使用:

一杆进洞幸运射击1幸运射击2近距离一随机


1

乌龟,164字节

Turtlèd再次展示了Turtlèd在高尔夫和冗长性之间的最简单的平衡(例如增加数字),击败了所有高尔夫球手。

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_]

在线尝试

注意它是半零索引和半一索引;x是一个索引,y是零索引;3,3是一个洞


1

R,230个 226字节

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)}

感谢@billywob -2个字节,注意M[a,b]等同于M[c]在两种情况下的。

令人讨厌的是,两个cat调用(!)不能被cat限制为一个,因为该fill参数弄乱了消息。啊!


1
将矩阵的创建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)}
移到

哦,足够公平了。提醒您,我认为我也不需要f=解决方案。已移除。
JDL
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.