Anonymous user
One-dimensional cellular automata: Difference between revisions
m
→{{header|Ceylon}}
Thundergnat (talk | contribs) m (→{{header|Ring}}: Remove vanity tags) |
|||
Line 880:
=={{header|Ceylon}}==
<lang ceylon>shared
shared Character character;
string => character.string;
shared formal Cell opposite;
}
shared object alive extends Cell('#') {
opposite => dead;
}
shared object dead extends Cell('_') {
opposite => alive;
}
shared Map<Character, Cell> cellsByCharacter = map { for (cell in `Cell`.caseValues) cell.character->cell };
shared class Automata1D({Cell*} initialCells) {
assert(data.every((Cell element) => element == alive || element == dead));▼
neighbours = [left, right],
bothAlive = neighbours.every(alive.equals),
bothDead = neighbours.every(dead.equals))
if (bothAlive)
then cell.opposite
else if (cell == alive && bothDead)
if (newCells == cells) {
▲ value imaginaryFirstCell = data.first else dead;
▲ value imaginaryLastCell = data.last else dead;
▲ value cells = Array {*data.rest.exceptLast};
▲ shared Boolean evolve() {
▲ value buffer = Array {
▲ value index->cell = element;
▲ value left = cells[index - 1] else imaginaryFirstCell;
▲ value right = cells[index + 1] else imaginaryLastCell;
▲ return dead;
▲ return cell;
▲ )};
buffer.copyTo(cells);▼
▲ return changed;
}
string => imaginaryFirstCell.string + "".join(cells) + imaginaryLastCell.string;▼
return true;
}
▲
}
shared Automata1D? automata1d(String string) =>
let (cells = string.map((Character element) => cellsByCharacter[element]))
then Automata1D(cells.coalesced)
else null;
shared void run() {
assert (exists automata = automata1d("__###__##_#_##_###__######_###_#####_#__##_____#_#_#######__"));
variable value generation = 0;
print("generation ``generation`` ``automata``");
while (automata.evolve() && generation
print("generation `` ++generation `` ``automata``");
}
}</lang>
|