home
/
code
/
cellular-automata.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
7b9dc10
)
get/set/do prefix notation.
author
Siraaj Khandkar
<siraaj@khandkar.net>
Sun, 26 Aug 2012 04:38:07 +0000
(
00:38
-0400)
committer
Siraaj Khandkar
<siraaj@khandkar.net>
Sun, 26 Aug 2012 04:38:07 +0000
(
00:38
-0400)
004/life.awk
patch
|
blob
|
blame
|
history
diff --git
a/004/life.awk
b/004/life.awk
index
714f209
..
ff80828
100755
(executable)
--- a/
004/life.awk
+++ b/
004/life.awk
@@
-6,23
+6,23
@@
function CHAR_ALIVE() {return "o"}
function CHAR_DEAD() {return " "}
function CHAR_DEAD() {return " "}
-function init_cell() {
+function
get_
init_cell() {
return int(2 * rand())
}
return int(2 * rand())
}
-function init_board(n) {
+function
get_
init_board(n) {
board = "";
for (i=1; i <= n; i++) {
board = "";
for (i=1; i <= n; i++) {
- board = sprintf("%s%d", board, init_cell())
+ board = sprintf("%s%d", board,
get_
init_cell())
};
return board
}
};
return board
}
-function print_border(x) {
+function
do_
print_border(x) {
for (i=1; i <= x; i++) {
printf CHAR_BORDER()
};
for (i=1; i <= x; i++) {
printf CHAR_BORDER()
};
@@
-30,7
+30,7
@@
function print_border(x) {
}
}
-function char_of_state(state) {
+function
get_
char_of_state(state) {
if (state == 1) {
return CHAR_ALIVE()
} else if (state == 0) {
if (state == 1) {
return CHAR_ALIVE()
} else if (state == 0) {
@@
-39,22
+39,22
@@
function char_of_state(state) {
}
}
-function print_board(x, n, board) {
- print_border(x);
+function
do_
print_board(x, n, board) {
+
do_
print_border(x);
for (i=1; i <= n; i++) {
for (i=1; i <= n; i++) {
- printf "%s", char_of_state(substr(board, i, 1));
+ printf "%s",
get_
char_of_state(substr(board, i, 1));
if (i % x == 0) {
printf "\n"
}
};
if (i % x == 0) {
printf "\n"
}
};
- print_border(x);
+
do_
print_border(x);
}
}
-function new_state(state, live_neighbors) {
+function
get_
new_state(state, live_neighbors) {
if (state == 1 && live_neighbors < 2) {
return 0
} else if (state == 1 && live_neighbors < 4) {
if (state == 1 && live_neighbors < 2) {
return 0
} else if (state == 1 && live_neighbors < 4) {
@@
-69,7
+69,7
@@
function new_state(state, live_neighbors) {
}
}
-function new_generation(x, n, board) {
+function
get_
new_generation(x, n, board) {
offsets["N" ] = - x ;
offsets["NE"] = -(x - 1);
offsets["E" ] = 1 ;
offsets["N" ] = - x ;
offsets["NE"] = -(x - 1);
offsets["E" ] = 1 ;
@@
-95,7
+95,7
@@
function new_generation(x, n, board) {
}
}
}
}
- new_cell_state = new_state(cell_state, live_neighbors);
+ new_cell_state =
get_
new_state(cell_state, live_neighbors);
new_board = sprintf("%s%d", new_board, new_cell_state);
};
new_board = sprintf("%s%d", new_board, new_cell_state);
};
@@
-111,11
+111,11
@@
function life() {
y = stty_size[1] - 3; # Minus 1 char for each: border, border, cursor
n = x * y;
y = stty_size[1] - 3; # Minus 1 char for each: border, border, cursor
n = x * y;
- board = init_board(n);
+ board =
get_
init_board(n);
while (1) {
while (1) {
- print_board(x, n, board);
- board = new_generation(x, n, board);
+
do_
print_board(x, n, board);
+ board =
get_
new_generation(x, n, board);
}
}
}
}
This page took
0.024793 seconds
and
4
git commands to generate.