mirror of
https://github.com/RGBCube/nu_scripts
synced 2025-08-01 14:47:47 +00:00

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>
75 lines
2.2 KiB
Text
75 lines
2.2 KiB
Text
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
|
|
}
|
|
|