Skip to content

Commit

Permalink
Port before_v0.60/fun folder (issue #221) (#835)
Browse files Browse the repository at this point in the history
This PR is part of porting all old scripts #221 and ports `fun` folder

<details><summary>Summary</summary>

### star.nu
```yaml
from: before_v0.60/fun/star.nu
to: modules/fun/star.nu
```

### spark.nu
The script has already been ported. I've removed old version.
```yaml
from: before_v0.60/fun/spark.nu
to: sourced/fun/spark.nu
functions:
  spark: sourced/fun/spark.nu:1:spark
```

### life.nu
```yaml
from: before_v0.60/fun/life.nu
to: modules/fun/life.nu
```

### lisp_mode.nu
There is a problem with this module because `-` is not allowed in names
anymore, however `def "-"` is a valid syntax, but you will be unable to
call this function.
```yaml
from: before_v0.60/fun/lisp_mode.nu
to: sourced/fun/lisp_mode.nu
```


### nyancat.nu
I also fixed animation frames from https://github.com/klange/nyancat
```yaml
from: before_v0.60/fun/nyancat.nu
to: modules/fun/nyancat.nu
```


</details>
  • Loading branch information
39555 committed May 13, 2024
1 parent b40ead9 commit 2fe0756
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 236 deletions.
82 changes: 0 additions & 82 deletions before_v0.60/fun/life.nu

This file was deleted.

35 changes: 0 additions & 35 deletions before_v0.60/fun/lisp_mode.nu

This file was deleted.

23 changes: 0 additions & 23 deletions before_v0.60/fun/spark.nu

This file was deleted.

75 changes: 75 additions & 0 deletions modules/fun/life.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
def alive [x_pos: int, y_pos: int, $grid] {
let width = ($grid | get width)
let height = ($grid | get height)
let data = ($grid | get data)

let $left_x = (if $x_pos == 0 { $width - 1} else { $x_pos - 1})
let $right_x = (if $x_pos == ($width - 1) { 0 } else { $x_pos + 1})
let $up_y = (if $y_pos == 0 { $height - 1} else { $y_pos - 1})
let $down_y = (if $y_pos == ($height - 1) { 0 } else { $y_pos + 1})
let $n = ($data | get ($x_pos + $up_y * $width) | into int)
let $nw = ($data | get ($left_x + $up_y * $width) | into int)
let $w = ($data | get ($left_x + $y_pos * $width) | into int)
let $sw = ($data | get ($left_x + $down_y * $width) | into int)
let $s = ($data | get ($x_pos + $down_y * $width) | into int)
let $se = ($data | get ($right_x + $down_y * $width) | into int)
let $e = ($data | get ($right_x + $y_pos * $width) | into int)
let $ne = ($data | get ($right_x + $up_y * $width) | into int)

let total = $n + $nw + $w + $sw + $s + $se + $e + $ne

let $curr = ($data | get ($x_pos + $y_pos * $width))

if ($total == 3) or ($total == 2 and $curr) {
true
} else {
false
}
}

def generation [$grid] {
let width = ($grid | get width)
let height = ($grid | get height)
let data = ($grid | get data)

let next_generation = (
0..<$height | each {|y|
0..<$width | each {|x|
alive $x $y $grid
}
}
) | flatten
{ width: $width, height : $height, data: $next_generation }
}

def print-grid [$grid] {
$grid.data | flatten | group ($grid | get width) | each {|x|
$x | each {|item|
if $item {
"*"
} else {
"."
}
} | append (char nl) | str join
} | str join ""
}

def main [] {
let width = 15
let height = 15

let data = (0..<($width * $height) | each {
random bool
})

let grid = { width: $width, height : $height, data: $data }

print (print-grid $grid)

1..100 | reduce --fold ($grid) {|it acc|
let next_grid = (generation $acc)
print $"(char -u '1b')[2J" (print-grid $next_grid)
$next_grid
} | ignore
}

Loading

0 comments on commit 2fe0756

Please sign in to comment.