Skip to content

Commit

Permalink
- Added the String.numbers() and String.each_number{} methods
Browse files Browse the repository at this point in the history
modified:   scripts/Games/forest_fire.sf
  • Loading branch information
trizen committed Dec 2, 2015
1 parent e9f70c7 commit 1f7991d
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 54 deletions.
39 changes: 34 additions & 5 deletions lib/Sidef/Types/String/String.pm
Original file line number Diff line number Diff line change
Expand Up @@ -592,14 +592,43 @@ package Sidef::Types::String::String {
__PACKAGE__->new($acc);
}

sub words {
my ($self) = @_;
Sidef::Types::Array::Array->new(map { __PACKAGE__->new($_) } CORE::split(' ', $$self));
}

sub each_word {
my ($self, $obj) = @_;
my $array = Sidef::Types::Array::Array->new(map { __PACKAGE__->new($_) } CORE::split(' ', $$self));
$obj // return $array;
$array->each($obj);
my ($self, $code) = @_;

foreach my $word (CORE::split(' ', $$self)) {
if (defined(my $res = $code->_run_code(__PACKAGE__->new($word)))) {
return $res;
}
}

$self;
}

sub numbers {
my ($self) = @_;
Sidef::Types::Array::Array->new(map { Sidef::Types::Number::Number->new($_) } CORE::split(' ', $$self));
}

*nums = \&numbers;

sub each_number {
my ($self, $code) = @_;

foreach my $num (CORE::split(' ', $$self)) {
if (defined(my $res = $code->_run_code(Sidef::Types::Number::Number->new($num)))) {
return $res;
}
}

$self;
}

*words = \&each_word;
*each_num = \&each_number;

sub bytes {
my ($self) = @_;
Expand Down
116 changes: 67 additions & 49 deletions scripts/Games/forest_fire.sf
Original file line number Diff line number Diff line change
@@ -1,58 +1,76 @@
#!/usr/bin/ruby

define w = `tput cols`.to_i-1;
define h = `tput lines`.to_i-1;
define r = "\033[H";

define red = "\033[31m";
define green = "\033[32m";
define yellow = "\033[33m";

define chars = [' ', green+'*', yellow+'&', red+'&'];

define tree_prob = 0.05;
define burn_prob = 0.0002;

enum |Empty, Tree, Heating, Burning|;

define dirs = [
%n(-1 -1), %n(-1 0), %n(-1 1), %n(0 -1),
%n(0 1), %n(1 -1), %n(1 0), %n(1 1),
];

var forest = h.of { w.of { 1.rand < tree_prob ? Tree : Empty } };

func iterate {
var new = h.of{ w.of(0) };
range(0, h-1).each { |i|
range(0, w-1).each { |j|
given (new[i][j] = forest[i][j]) {
when (Tree) {
1.rand < burn_prob && (new[i][j] = Heating; next);
dirs.each { |pair|
var y = pair[0]+i;
y ~~ range(0, h-1) || next;
var x = pair[1]+j;
x ~~ range(0, w-1) || next;
forest[y][x] == Heating && (new[i][j] = Heating; break);
}
}
when (Heating) { new[i][j] = Burning }
when (Burning) { new[i][j] = Empty }
when (1.rand < tree_prob) { new[i][j] = Tree }
define RED = "\e[1;31m";
define YELLOW = "\e[1;33m";
define GREEN = "\e[1;32m";

define DIRS = [
[-1, -1], [0, -1], [1, -1],
[-1, 0], [1, 0],
[-1, 1], [0, 1], [1, 1],
]

enum (Empty, Tree, Heating, Burning);
define pix = [' ', GREEN + "*", YELLOW + "*", RED + "*"];

class Forest(p=0.01, f=0.001, height, width) {

has coords = [];
has spot = [];
has neighbors = [];

method init {
coords = (0..height ~X 0..width)
spot = height.of { width.of { [true, false].pick ? Tree : Empty } }
self.init_neighbors
}

method init_neighbors {
for pair in coords {
var(i, j) = @pair;
neighbors[i][j] = gather {
for dir in DIRS {
take(\(spot[i + dir[0]][j + dir[1]] \\ next));
}
}
}
}
forest = new;
}

func init_forest {
print r;
forest.each { |row|
print chars@[row];
print "\033[E\033[1G";
method step {
var heat = [];

for pair in coords {
var(i, j) = @pair;
given (spot[i][j]) {
when Empty { spot[i][j] = Tree if (1.rand < p) }
when Tree { spot[i][j] = Heating if (1.rand < f) }
when Heating { spot[i][j] = Burning; heat << [i, j] }
when Burning { spot[i][j] = Empty }
}
}

for pair in heat {
var(i, j) = @pair;
neighbors[i][j].each { |ref|
*ref = Heating if (*ref == Tree);
}
}
}

method show {
range(0, height-1).each { |i|
say pix.@[spot[i]];
}
}
iterate();
}

loop { init_forest() };
local $| = 1;
var(height, width) = `stty size`.nums...;

var forest = Forest(height: height, width: width);
print "\e[2J";
loop {
print "\e[H";
forest.show;
forest.step;
}

0 comments on commit 1f7991d

Please sign in to comment.