コードゴルフゴルフゴルフ


24

ゴルフチャレンジ

以下のASCII「グリーン」が与えられます。

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

レッツ|示す壁
レッツ|表す半分旗ポール
レッツ>示すポールの旗
レッツO示す穴
レッツo表すボール

「グリーン」の寸法は10x10です。2つの壁の間には10のスペースがあります|
また、緑の上部と下部の間には、10個のスペースがあります。

チャレンジ

xとyの値を入力するか、2つの乱数を生成してゴルフボールをグリーンに「発射」します。
生成されたx、yがホールまたはフラグポール/フラグ出力「Try Again!」に触れない場合
生成されたx、yがホール出力「Hole in One!」にヒットした場合
生成された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

そのままにしたり、楽しんだり移動したりできます。動き回るのは苦痛だと思っていましたが、楽しい挑戦になると思います。移動する場合は、2 <h <= 10を確認します。hは穴の高さのインデックスです。そうすれば、フラグは画面から消えません。
jacksonecac

2
0 <i <= 10および0 <k <= 10の2つの
パラメーター

1
@ corvus_192絶対
jacksonecac

1
これらの出力文字列は、コードゴルフでは苦痛です。何の答えはまだ存在しないので、入力としてそれらを取ることができるように考える
ルイスMendo

回答:


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

デモ


8

ゼリー、78 バイト

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

プレイスキルゲームやAをクラップ・シュート TryItOnlineを!

(Crap-shootのバイト数が増えます)。

どうやって?

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

パイソン2、290 264 262 252 248 245バイト

それはきれいではなく、短くはありませんが、私は疲れているとその最初の Pythonの答えです。X、Y形式でショットを入力します。

編集

リストの作成方法を再定義することにより、26を打ち​​切りました。しかし、長いif文にはまだ運がありません。

-2長いifを辞書に置き換え、短いifを置き換えます。

-10 @ Noodle9のおかげで-私はその1つを逃した:)

-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

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)が2番目の文字であるため1つのオフセットであるため、式を使用して正しいインデックスを計算しました。


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

2つのリテラル改行はそれぞれ1バイトを節約します。かなり標準。宣言、次にゲームボードを印刷します。


3

、99バイト

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

stdinで、スペースで区切られた1ベースの入力を受け取ります。ほとんどのコードは、4つのメッセージ(の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はい。私は最新のアップデートでそれを修正しました。ご指摘いただきありがとうございます。
メガトム

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バイト

私の最初の試み-ショットのXおよびY座標として0から9までの2つの整数を受け入れるグルーヴィーなクロージャー。

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

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バイト

繰り返しになりますが、最もシンプルなもの(数字を増やすなど)でのゴルフの美しさと冗長性の間のタートレドのバランスを示すと、タートレドはゴルフラング以外のすべてを打ち負かします。

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はインデックスが1つ、yはインデックスが0です。3,3は1つの穴


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

-2バイトの@billywobに感謝します。 M[a,b]M[c]いくつかの場合と同等です。

厄介なことに、2つのcat呼び出し(!)catを1つに接続することはできません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)}
。– Billywob

ああ、結構です。気を付けてください、f=とにかく自分のソリューションに必要があるとは思いません。削除されました。
JDL
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.