Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Labyrinth Cell Automata in Java
import javax.swing.*;
import java.awt.*;
public class CellAutomataPanel extends JPanel {
public int[][] field = new int[40][40];
public Turmit turmit = new Turmit();
@Override
public void paint(Graphics g) {
super.paint(g);
drawField(g);
}
private void drawField(Graphics g) {
for(int x=0; x<40; x++){
for(int y=0; y<40; y++){
int colorNumber = field[x][y]; // получаем номер цвета клетки поля по x,y
Color color = colorWithNumber(colorNumber); // получаем цвет по номеру цвета
g.setColor(color); // устанавливаем цвет для рисования
g.fillRect(x*10, y*10, 10, 10); // рисуем квадрат
}
}
}
private Color colorWithNumber(int number){
Color[] colors = new Color[]{
Color.BLACK, Color.RED, Color.GREEN, Color.BLUE,
Color.GRAY, Color.YELLOW, Color.ORANGE, Color.PINK,
Color.CYAN, Color.MAGENTA, new Color(52, 99, 255), new Color(85, 159, 255),
new Color(36, 130, 100), new Color(130, 0, 255), new Color(196, 255, 101), new Color(255, 116, 153),
};
return colors[number];
}
public void nextStep(){
turmit.nextStep(field);
this.repaint();
}
}
import javax.swing.*;
import java.awt.event.ActionEvent;
public class LabyrinthCellAutomata {
public static void main(String[] args) {
JFrame frame = new JFrame("Лабиринт");
frame.setSize(400, 400);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
CellAutomataPanel myPanel = new CellAutomataPanel();
frame.setContentPane(myPanel);
frame.setVisible(true);
startTimer(myPanel);
}
private static void startTimer(CellAutomataPanel myPanel) {
Timer timer = new Timer(1, new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
myPanel.nextStep();
}
});
timer.start();
}
}
import java.util.HashMap;
public class Turmit {
public String state = "A";
public String direction = "up";
public int x = 20;
public int y = 20;
public void nextStep(int[][] field){
int color = field[x][y]; // получем цвет клетки где находится тьюрмит (x, y)
Rule rule = findRule(state, color);
field[x][y] = rule.nextColor; // красим клетку в новый цвет
state = rule.nextState; // меняем состояния тьюрмита на новый
if(rule.turn == -1){ // Если нужно повернуть налево
if(direction.equals("up")){ // Если текущее направление вверх
direction = "left"; // то меняем на лево
} else if(direction.equals("left")){ // Если текущее направление налево
direction = "down"; // то меняем вниз
} else if(direction.equals("down")){ // и т.д.
direction = "right";
} else if(direction.equals("right")){
direction = "up";
}
} else if(rule.turn == 1){ // Если нужно повернуть направо
if(direction.equals("up")){
direction = "right";
} else if(direction.equals("left")){
direction = "up";
} else if(direction.equals("down")){
direction = "left";
} else if(direction.equals("right")){
direction = "down";
}
}
// После того как повернули тьюрмит, двигаем его на одну клетка по направлению
if(direction.equals("up")){
y -= 1;
} else if(direction.equals("down")){
y += 1;
} else if(direction.equals("left")){
x -= 1;
} else if(direction.equals("right")){
x += 1;
}
}
public Rule findRule(String state, int color){
HashMap<String, Rule> rules = new HashMap<>();
rules.put("A0", new Rule(1, -1, "A"));
rules.put("A1", new Rule(2, -1, "A"));
rules.put("A2", new Rule(3, -1, "A"));
rules.put("A3", new Rule(4, -1, "A"));
rules.put("A4", new Rule(5, -1, "A"));
rules.put("A5", new Rule(6, 1, "B"));
rules.put("B0", new Rule(1, 1, "A"));
rules.put("B5", new Rule(6, -1, "B"));
rules.put("B6", new Rule(7, -1, "B"));
rules.put("B7", new Rule(8, -1, "B"));
rules.put("B8", new Rule(9, -1, "B"));
rules.put("B9", new Rule(10, -1, "B"));
rules.put("B10", new Rule(11, -1, "B"));
rules.put("B11", new Rule(12, -1, "B"));
rules.put("B12", new Rule(13, -1, "B"));
rules.put("B13", new Rule(14, -1, "B"));
rules.put("B14", new Rule(15, -1, "B"));
rules.put("B15", new Rule(0, -1, "B"));
return rules.get(state+color);
}
class Rule{
public int nextColor;
public int turn;
public String nextState;
public Rule(int color, int turn, String state){
this.nextColor = color;
this.turn = turn;
this.nextState = state;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment