Skip to content
Snippets Groups Projects
State.java 1.55 KiB
Newer Older
  • Learn to ignore specific revisions
  • Guyslain's avatar
    Guyslain committed
    package model;
    
    import javafx.scene.paint.Color;
    
    import java.util.List;
    
    
    /**
     * Represents a state of a cell in a cellular automaton, and the update rules for the cellular
     * automaton.
     *
    
     * @param <S> The type of the state itself, used for reflexivity: if a class {@code SomeState}
     *          wants to implement this interface, it should implement {@code State<SomeState>}.
    
    Guyslain's avatar
    Guyslain committed
    public interface State<S> {
    
    
        /**
         * Returns the color associated with this state.
         *
         * @return The color representing this state.
         */
    
    Guyslain's avatar
    Guyslain committed
        Color getColor();
    
    
        /**
         * Computes and returns the next state based on the rules of the cellular automaton.
         *
         * @return The next state.
         */
    
    Guyslain's avatar
    Guyslain committed
        S next();
    
    
        /**
         * Updates the state based on the states of its neighboring cells.
         *
         * @param neighbours A list of neighboring cell states.
         * @return The updated state based on the neighbors.
         */
    
        S update(List<S> neighbours);
    
    Guyslain's avatar
    Guyslain committed
    
    
        /**
         * Counts the occurrences of a specific state within a list of neighboring states.
         *
         * @param <T>       The type of state to count.
         * @param state     The state to count occurrences of.
         * @param neighbours A list of neighboring states to search within.
         * @return The number of times the specified state appears in the list of neighbors.
         */
        static <T> int count(T state, List<T> neighbours) {
    
            int count = 0;
            for (T neighbour : neighbours) {
                if (neighbour.equals(state)) {
                    count++;
                }
            }
            return count;