4 Shorten code by 19 more bytes
source | link

Kotlin, 361 355 353353 334 bytes

6 bytes saved thanks to Jonathan
2 bytes saved changing to when
19 bytes saved switching to lambda & tracking outer edges

fun o({s:Int,n:Int){var->var a=Array(s,{_->Array(s,{_->' '})})
var r=0
var c=0
var d=0
var e=0
var f=1
var g=s-1
var h=g
for(i in 1..n){a[r][c]='*'
when(d){0->if(c<s-1&&a[r][c+1]<'*'c<g)c++
else{d=1
r++
g--}
1->if(r<s-1&&a[r+1][c]<'*'r<h)r++
else{d=2
c--
h--}
2->if(c>0&&a[r][c-1]<'*'c>e)c--
else{d=3
r--
e++}
3->if(r>0&&a[r-1][c]<'*'r>f)r--
else{d=0
c++
f++}}}
for(i in 0..s-1){for(j in 0..s-1)print(a[i][j])
println()}}

Try it online!Try it online!

Kotlin, 361 355 353 bytes

6 bytes saved thanks to Jonathan
2 bytes saved changing to when

fun o(s:Int,n:Int){var a=Array(s,{_->Array(s,{_->' '})})
var r=0
var c=0
var d=0
for(i in 1..n){a[r][c]='*'
when(d){0->if(c<s-1&&a[r][c+1]<'*')c++
else{d=1
r++}
1->if(r<s-1&&a[r+1][c]<'*')r++
else{d=2
c--}
2->if(c>0&&a[r][c-1]<'*')c--
else{d=3
r--}
3->if(r>0&&a[r-1][c]<'*')r--
else{d=0
c++}}}
for(i in 0..s-1){for(j in 0..s-1)print(a[i][j])
println()}}

Try it online!

Kotlin, 361 355 353 334 bytes

6 bytes saved thanks to Jonathan
2 bytes saved changing to when
19 bytes saved switching to lambda & tracking outer edges

{s:Int,n:Int->var a=Array(s,{_->Array(s,{_->' '})})
var r=0
var c=0
var d=0
var e=0
var f=1
var g=s-1
var h=g
for(i in 1..n){a[r][c]='*'
when(d){0->if(c<g)c++
else{d=1
r++
g--}
1->if(r<h)r++
else{d=2
c--
h--}
2->if(c>e)c--
else{d=3
r--
e++}
3->if(r>f)r--
else{d=0
c++
f++}}}
for(i in 0..s-1){for(j in 0..s-1)print(a[i][j])
println()}}

Try it online!

3 Shorten code by 2 more bytes
source | link

Kotlin,  361 355355 353 bytes

6 bytes saved thanks to Jonathan
2 bytes saved changing to when

fun o(s:Int,n:Int){var a=Array(s,{_->Array(s,{_->' '})})
var r=0
var c=0
var d=0
for(i in 1..n){a[r][c]='*'
ifwhen(d<1d){if0->if(c<s-1&&a[r][c+1]<'*')c++
else {d=1
r++}
if(d==1){if1->if(r<s-1&&a[r+1][c]<'*')r++
else {d=2
c--}
if(d==2){if2->if(c>0&&a[r][c-1]<'*')c--
else {d=3
r--}
if(d>2){if3->if(r>0&&a[r-1][c]<'*')r--
else{d=0
c++}}}
for(i in 0..s-1){for(j in 0..s-1)print(a[i][j])
println()}}

Try it online!Try it online!

Kotlin,361 355 bytes

6 bytes saved thanks to Jonathan

fun o(s:Int,n:Int){var a=Array(s,{_->Array(s,{_->' '})})
var r=0
var c=0
var d=0
for(i in 1..n){a[r][c]='*'
if(d<1){if(c<s-1&&a[r][c+1]<'*')c++
else d=1}
if(d==1){if(r<s-1&&a[r+1][c]<'*')r++
else d=2}
if(d==2){if(c>0&&a[r][c-1]<'*')c--
else d=3}
if(d>2){if(r>0&&a[r-1][c]<'*')r--
else{d=0
c++}}}
for(i in 0..s-1){for(j in 0..s-1)print(a[i][j])
println()}}

Try it online!

Kotlin,  361 355 353 bytes

6 bytes saved thanks to Jonathan
2 bytes saved changing to when

fun o(s:Int,n:Int){var a=Array(s,{_->Array(s,{_->' '})})
var r=0
var c=0
var d=0
for(i in 1..n){a[r][c]='*'
when(d){0->if(c<s-1&&a[r][c+1]<'*')c++
else{d=1
r++}
1->if(r<s-1&&a[r+1][c]<'*')r++
else{d=2
c--}
2->if(c>0&&a[r][c-1]<'*')c--
else{d=3
r--}
3->if(r>0&&a[r-1][c]<'*')r--
else{d=0
c++}}}
for(i in 0..s-1){for(j in 0..s-1)print(a[i][j])
println()}}

Try it online!

2 Shorten code by 6 bytes
source | link

Kotlin, 361361 355 bytes

6 bytes saved thanks to Jonathan

fun o(s:Int,n:Int){var a=Array(s,{_->Array(s,{_->' '})})
var r=0
var c=0
var d=0
for(i in 1..n){a[r][c]='*'
if(d==0d<1){if(c<s-1&&a[r][c+1]==' '1&&a[r][c+1]<'*')c++
else d=1}
if(d==1){if(r<s-1&&a[r+1][c]==' '1&&a[r+1][c]<'*')r++
else d=2}
if(d==2){if(c>0&&a[r][c-1]==' '1]<'*')c--
else d=3}
if(d==3d>2){if(r>0&&a[r-1][c]==' '1][c]<'*')r--
else{d=0
c++}}}
for(i in 0..s-1){for(j in 0..s-1)print(a[i][j])
println()}}

Try it online!Try it online!

Kotlin, 361 bytes

fun o(s:Int,n:Int){var a=Array(s,{_->Array(s,{_->' '})})
var r=0
var c=0
var d=0
for(i in 1..n){a[r][c]='*'
if(d==0){if(c<s-1&&a[r][c+1]==' ')c++
else d=1}
if(d==1){if(r<s-1&&a[r+1][c]==' ')r++
else d=2}
if(d==2){if(c>0&&a[r][c-1]==' ')c--
else d=3}
if(d==3){if(r>0&&a[r-1][c]==' ')r--
else{d=0
c++}}}
for(i in 0..s-1){for(j in 0..s-1)print(a[i][j])
println()}}

Try it online!

Kotlin,361 355 bytes

6 bytes saved thanks to Jonathan

fun o(s:Int,n:Int){var a=Array(s,{_->Array(s,{_->' '})})
var r=0
var c=0
var d=0
for(i in 1..n){a[r][c]='*'
if(d<1){if(c<s-1&&a[r][c+1]<'*')c++
else d=1}
if(d==1){if(r<s-1&&a[r+1][c]<'*')r++
else d=2}
if(d==2){if(c>0&&a[r][c-1]<'*')c--
else d=3}
if(d>2){if(r>0&&a[r-1][c]<'*')r--
else{d=0
c++}}}
for(i in 0..s-1){for(j in 0..s-1)print(a[i][j])
println()}}

Try it online!

1
source | link