(ref)
Author: adamo
Conway’s Game of Life implemented in Processing
Yesterday’s ugliness in implemented in Processing:
class Cell {
int x;
int y;
int size;
int live;
Cell(int tx, int ty, int ts, int tl) {
x = tx;
y = ty;
size = ts;
live = tl;
}
void display() {
stroke(0);
if (live == 0) {
fill(209);
} else {
fill(255);
}
rect(x, y, size, size);
}
void display(int c) {
stroke(0);
fill(c);
rect(x, y, size, size);
}
}
Cell[][] grid;
int[][] next;
int maxI;
int maxJ;
int size = 10;
int start = 0;
void setup() {
size(900, 500);
background(255);
maxI = floor(width / size) - 1;
maxJ = floor(height / size ) -1;
grid = new Cell[maxI][maxJ];
next = new int[maxI][maxJ];
noLife();
}
void noLife() {
for (int i = 0; i < maxI; i++) {
for (int j = 0; j < maxJ; j++) {
grid[i][j] = new Cell(i * size, j * size, size, 0);
next[i][j] = 0;
grid[i][j].display();
}
}
}
void draw() {
if (start == 1) {
runLife();
delay(200);
}
}
void mouseClicked() {
if (start == 1) {
noLife();
start = 0;
}
if ((mouseX < maxI * size) && (mouseY < maxJ * size)) {
int i = floor(mouseX / size);
int j = floor(mouseY / size);
if (grid[i][j].live == 1) {
grid[i][j].live = 0;
} else {
grid[i][j].live = 1;
}
grid[i][j].display();
} else {
start = 1;
}
}
void runLife() {
int i0;
int i1;
int j0;
int j1;
int alive;
for (int i = 0; i < maxI; i++) {
for (int j = 0; j < maxJ; j++) {
i0 = i - 1;
i1 = i + 1;
j0 = j - 1;
j1 = j + 1;
if (i0 < 0) i0 = maxI - 1;
if (j0 < 0) j0 = maxJ - 1;
if (i1 == maxI) i1 = 0;
if (j1 == maxJ) j1 = 0;
alive = 0;
if (grid[i][j0].live == 1) alive++;
if (grid[i1][j0].live == 1) alive++;
if (grid[i1][j].live == 1) alive++;
if (grid[i1][j1].live == 1) alive++;
if (grid[i][j1].live == 1) alive++;
if (grid[i0][j1].live == 1) alive++;
if (grid[i0][j].live == 1) alive++;
if (grid[i0][j0].live == 1) alive++;
if ((grid[i][j].live == 1) && ((alive == 2) || (alive == 3))) {
next[i][j] = 1;
} else {
next[i][j] = 0;
}
if ((grid[i][j].live == 0) && (alive == 3)) {
next[i][j] = 1;
}
}
}
for (int i = 0; i < maxI; i++) {
for (int j = 0; j < maxJ; j++) {
grid[i][j].live = next[i][j];
grid[i][j].display();
next[i][j] = 0;
}
}
}
Two things that I find cool about Processing:
- You can export your application as a Java applet.
- Processing.js
Conway’s Game of Life implemented in SmallBasic
At work we have a new junior system administrator. Given the fact that he is junior and lacks even programming experience, I gave him the following assignment:
– Implement Conway’s Game of Life. Pick the programming language of your choice, the user interface of your choice, read anything you believe is relevant, ask anyone (including me) for advice, make the assumptions that suit you (and document them). Just someday come back with a demo.
Life’s rules are simple:
- Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
- Any live cell with more than three live neighbours dies, as if by overcrowding.
- Any live cell with two or three live neighbours lives on to the next generation.
- Any dead cell with exactly three live neighbours becomes a live cell.
His language of choice is PHP (as I said he lacks programming experience and PHP is the only language he has ever written anything meaningful in). Given that a system administrator must be proficient at least in sh, awk, Perl and some Python (and VBScript or Windows PowerShell if working in a Windows environment) I expect a change of choice soon. He will see the light of C later.
Upon hearing about the assignment a colleague made an unfortunate humorous attempt to challenge me: If I am to put an assignment to the junior administrator, why don’t I implement it too? Due to the law of unintended consequences, this was an unfortunate remark that was done in front of the junior administrator (more on why this was a bad choice of timing later).
Continue reading “Conway’s Game of Life implemented in SmallBasic”
Bank 399
re: Όχι άλλο Μίχο!
Πέτρο Μίχο-
Υπήρξες αρχηγός μας και σε ευχαριστούμε για αυτό. Σεβάσου λοιπόν όσους που σε είδαμε να παίζεις και σε χειροκροτήσαμε και σταμάτα τις τηλεοπτικές εμφανίσεις. Δεν είναι ότι κάνεις κακό μόνο στον εαυτό σου: Κάνεις κακό στον Ολυμπιακό · κάνεις κακό και σε εμάς που σε χειροκροτήσαμε κάποτε.
Δεν είμαστε όλοι οι Ολυμπιακοί σαν κι εσένα. Μερικοί από εμάς διαβάζουμε και τους κανονισμούς πριν πούμε την άποψή μας.
Για το καλό της ομάδας μας, της ομάδας που ηγήθηκες, είναι καιρός να σταματήσεις.
Με σεβασμό (ακόμα, αλλά όχι για πολύ)
Γ.Α.
atan2() implemented in bc
Dave Taylor thought that it would be a good idea to calculate in Perl the distance between two longitude / latitude points on the Earth. He also thought it would be awesome if the same could be implemented in bc.
I replied that one could easily transform in Perl the cost function from the TSP page. Dave Taylor pointed out that unfortunately atan2() is not available in bc and atan2() is used in the cost function.
“So what?” I thought. All one needs is the definition of atan2(). It turns out that one needs to define in bc the sign function and abs() too.
define sgn(x) {
if (x == 0) {
return(0);
} else if (x < 0) {
return(-1);
} else if (x > 0) {
return(1);
}
}
define abs(x) {
if (x < 0) {
return(-1 * x);
} else {
return(x);
}
}
define atan2(y, x) {
auto pi, fi;
pi = 4 * a(1);
if (y == 0) {
if (x > 0) {
return(0);
} else if (x == 0) {
print "undefined\n";
halt;
} else if (x < 0) {
return(pi);
}
}
fi = a(abs(y/x));
if (x > 0) {
return(fi * sgn(y));
} else if (x == 0) {
return(pi * sgn(y) / 2);
} else if (x < 0) {
return((pi - fi) * sgn(y));
}
}
Put the function definitions in a file (say atan2.bc) and call it from the command line as:
$ bc -l atan2.bc
The above code was written in less than ten minutes of time. I am sure that one can come up with better implementations (not calculating pi (== 4 * atan(1)) and then dividing it by 2 is an optimization directly observable). All in all it was a fun exercise, plus while searching I got to read about CORDIC.
essentials of game theory
Someone landed on this blog looking for introductory material to Game Theory (The search term was “θεωρια παιγνιων βιβλιο εισαγωγικο”).
Well “Essentials of Game Theory” seems like a good candidate and is available via scribd too.
PS: I am not a Game Theorist.
MKD
Κορυφαία κίνηση! Όταν δεν βλέπω κάτι δεν υπάρχει (έρχονται και εκλογές).
(Άσε που ένας τέτοιος χειρισμός μοιάζει με σιωπηρή αποδοχή διπλής, διμερούς ονομασίας.)
Update: Και τώρα διαβάστε “Το Μακεδονικό στον Κυβερνοχώρο” (Thanks @stazybohorn for reminding this).
“You’re into computers, right?”
– You’re into computers, right?
– Right.
– I’ve got this page on Facebook and this guy that I had as a friend, but now I do not and …
First when someone was “into computers” people thought that meant free porn downloads. Then they thought it meant free Microsoft Word support (as I was told once “What the hell did you spend 5 years for in the University and you do not know Word?“) and now it means Facebook support.
In between it has always meant free support anytime, anyplace for everyone and for every crappy piece of software ever imagined.
I guess next time I will not be into computers.
Moleskine
Έχω γράψει παλιότερα πως μετά από μια σειρά από PDAs και SmartPhones, γύρισα σε ένα Moleskine notebook. Από τότε και για πρακτικούς λόγους το αντικατέστησα με ένα Filofax Personal. Για περιπτώσεις όμως που χρειάζομαι εκτεταμένες σημειώσεις, κείμενα που δε γράφω σε υπολογιστή (οτιδήποτε προκαλεί αυτό το “On Paper” feeling) χρησιμοποιώ πάλι Moleskine (όχι τα μικρά σημειωματάρια, αλλά τα μεγάλα squared τετράδια). Έτσι δεν είναι απορίας άξιο που έσκασα στα γέλια διαβάζοντας στο StuffWhitePeopleLike:
“Well, if a white person has a great idea, they write it by hand, if they have a good idea, it goes into the computer.”
