জাভাস্ক্রিপ্ট (ES6) 257 288 321
পদক্ষেপগুলি একত্রিত করা হয়েছে Edit আরও কিছু চর কাটাতে গল্ফযুক্ত কোড ফিডলিং
সম্পাদনা করুন
বর্তমান এক্স এবং ওয়াই অবস্থান এবং বর্তমান দিকের ট্র্যাক রেখে আউটপুটটি পুনরায় পুনরায় অ্যারেতে তৈরি করুন। যখন x বা y অবস্থানটি <0 হয় তখন পুরো আর অ্যারেটি সামঞ্জস্য হয়।
প্রধান পরিবর্তনশীল:
- r ফলাফল অ্যারে বা সারি
- এক্স, ওয়াই বর্তমান অবস্থান।
- এর বর্তমান দিকনির্দেশ (0..7) (বা বর্তমান অবস্থা)
- (0..3) -> '| \ _ /' আঁকতে d বর্তমান প্রতীক
- আমি বর্তমান ক্রম উপর রাননিগ অবস্থান (নীচে 0 থেকে)
- ডাব্লু বর্তমান সর্পিল ব্যাসার্ধ (কম বা কম)
F=n=>
(w=>{
for(r=b=[],s=y=x=d=0;n--;
d&&--l||((s=s+1&7,d=s&3)?l=d-2?w:s/2-2+w+w:w+=!s))
s>0&s<4?++x:s>4?x?--x:r=r.map(v=>' '+v):b+=' ',
q=r[s>2&s<6?++y:y]||b,
r[y]=(q+b).slice(0,x)+'|/_\\'[d]+q.slice(x+1),
s<2|s>6?y?--y:r=[,...r]:x+=!d*2,x-=!d
})(1)||r.join('\n')
Ungolfed
F=n=>{
var r=[], s,x,y,d,w,l, q
for(l=w=1, s=x=y=d=0; n--;)
{
if (s>2 && s<6) ++y; // right side, inc y before drawing
if (x < 0) // too left, adjust
{
r = r.map(v=>' '+v) // shift all to right
++x; // move current position to right
}
if (y < 0) // too up
{
r = [q='',...r] // shift all to bottom
++y; // move current position to bottom
}
q = r[y] || ''; // current row, if undefined convert to empty string
r[y] = (q+' '.repeat(x)).slice(0,x) + '|/_\\'[d] + q.slice(x+1); // add current symbol in the x column
if (s<2 || s>6) --y; // left side, dec y after drawing
if (s>0 && s<4) // always change x after drawing
++x;
else if (s > 4)
--x;
--l; // decrement current run
if (l == 0) // if 0, need to change direction
{
s = (s+1) % 8; // change direction
d = s % 4; // change symbol
if (d == 0)
{
// vertical direction, adjust x and if at 0 increase radius
l = 1 // always 1 vertical step
if (s == 0)
++x, ++w
else
--x
}
else
{
if (d != 2)
{
l = w; // diaagonal length is always w
}
else if (s == 2)
{
l = w+w-1 // top is radius * 2 -1
}
else
{
l = w+w+1 // bottom is radius * 2 +1
}
}
}
}
return r.join('\n')
}
টেস্ট সালে ফায়ারফক্স / ফায়ারবাগ কনসোলটি (অথবা JSFiddle ধন্যবা @Rainbolt)
;[1, 2, 10, 20, 155, 278].forEach(x=>console.log(F(x)))
আউটপুট
|
/
|
_
/ \
| |
\___/
___
/ _ \
/ / \ \
| | | |
\___/ /
___________
/ _________ \
/ / _______ \ \
/ / / _____ \ \ \
/ / / / ___ \ \ \ \
/ / / / / _ \ \ \ \ \
/ / / / / / \ \ \ \ \ \
| | | | | | | | | | | |
\ \ \ \ \___/ / / / /
\ \ \ \_____/ / / /
\ \ \_______/ / /
\ \_________/ /
\___________/
_______________
/ _____________ \
/ / ___________ \ \
/ / / _________ \ \ \
/ / / / _______ \ \ \ \
/ / / / / _____ \ \ \ \ \
/ / / / / / ___ \ \ \ \ \ \
/ / / / / / / _ \ \ \ \ \ \ \
/ / / / / / / / \ \ \ \ \ \ \ \
| | | | | | | | | | | | | | | |
\ \ \ \ \ \ \___/ / / / / / / /
\ \ \ \ \ \_____/ / / / / / /
\ \ \ \ \_______/ / / / / /
\ \ \ \_________/ / / / /
\ \ \___________/ / / /
\ \_____________/ / /
\_______________/ /