Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Langton ant #1463

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions Cellular-Automata/LangtonAnt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Langton's Ant
* Langton's Ant is a cellular automaton that involves an "ant" moving on a grid of cells.
* The ant changes the color of each cell it visits and turns based on the cell's color.
*/

/**
* Simulates Langton's Ant for a given number of steps.
* @param {number} steps - The number of steps to simulate.
*/

export function simulateLangtonAnt(steps, gridSize) {
// Initialize the grid
const grid = Array.from({ length: gridSize }, () =>
Array(gridSize).fill(false)
);

// Initialize the ant's position and direction
let antX = Math.floor(gridSize / 2);
let antY = Math.floor(gridSize / 2);
let antDirection = 'up'; // Initial direction (up, down, left, or right)

// Define the Langton's Ant rules
const turnRight = { up: 'right', right: 'down', down: 'left', left: 'up' };
const turnLeft = { up: 'left', left: 'down', down: 'right', right: 'up' };

// Simulate Langton's Ant for the specified number of steps
for (let step = 0; step < steps; step++) {
// Check the current cell's color
const isWhite = grid[antY][antX];

// Flip the color of the current cell
grid[antY][antX] = !isWhite;

// Turn the ant based on the cell's color
antDirection = isWhite ? turnRight[antDirection] : turnLeft[antDirection];

// Move the ant forward
if (antDirection === 'up') {
antY--;
} else if (antDirection === 'down') {
antY++;
} else if (antDirection === 'left') {
antX--;
} else if (antDirection === 'right') {
antX++;
}

// Handle ant's position wrapping around the grid edges
if (antX < 0) antX = gridSize - 1;
if (antX >= gridSize) antX = 0;
if (antY < 0) antY = gridSize - 1;
if (antY >= gridSize) antY = 0;
}

return grid;
}
30 changes: 30 additions & 0 deletions Cellular-Automata/test/LangtonAnt.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// LangtonAnt.test.js
import { simulateLangtonAnt } from './LangtonAnt';

describe('LangtonAnt', () => {
it('Simulates LangtonAnt for 1 step', () => {
const gridSize = 11;
const steps = 1;
const result = simulateLangtonAnt(steps, gridSize);

// Add your assertions here to check the grid state after 1 step
});

it('Simulates LangtonAnt for 10 steps', () => {
const gridSize = 21;
const steps = 10;
const result = simulateLangtonAnt(steps, gridSize);

// Add your assertions here to check the grid state after 10 steps
});

it('Simulates LangtonAnt for 50 steps', () => {
const gridSize = 31;
const steps = 50;
const result = simulateLangtonAnt(steps, gridSize);

// Add your assertions here to check the grid state after 50 steps
});

// Add more test cases as needed to cover different scenarios
});
Loading