One-dimensional cellular automata: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (→{{header|Ring}}: Remove vanity tags) |
|||
Line 880: | Line 880: | ||
=={{header|Ceylon}}== |
=={{header|Ceylon}}== |
||
<lang ceylon>shared |
<lang ceylon>shared abstract class Cell(character) of alive | dead { |
||
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) { |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
class Automata1D<Cell>({Cell*} data, Cell alive, Cell dead) |
|||
given Cell satisfies Object { |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
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) { |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
function isAlive(Cell c) => c == alive; |
|||
function flipped(Cell c) => c == alive then dead else alive; |
|||
⚫ | |||
⚫ | |||
*cells.indexed.map((Integer->Cell element) { |
|||
⚫ | |||
⚫ | |||
⚫ | |||
if(isAlive(left) && isAlive(right)) { |
|||
return flipped(cell); |
|||
} |
|||
if(isAlive(cell) && !isAlive(left) && !isAlive(right)) { |
|||
⚫ | |||
} |
|||
⚫ | |||
} |
|||
⚫ | |||
value changed = buffer != cells; |
|||
⚫ | |||
⚫ | |||
} |
} |
||
⚫ | |||
⚫ | |||
return true; |
|||
} |
} |
||
⚫ | |||
} |
|||
shared Automata1D? automata1d(String string) => |
|||
value automata = Automata1D("_###_##_#_#_#_#__#__", '#', '_'); |
|||
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; |
variable value generation = 0; |
||
print("generation ``generation`` ``automata``"); |
print("generation ``generation`` ``automata``"); |
||
while(automata.evolve() && generation |
while (automata.evolve() && generation<10) { |
||
print("generation ``++generation`` ``automata``"); |
print("generation `` ++generation `` ``automata``"); |
||
} |
} |
||
}</lang> |
}</lang> |