Programkod från föreläsning 13

Detta är den programkod jag skrev:

import se.lth.cs.pt.dots.*;
import se.lth.cs.pt.dots.events.*;
import se.lth.cs.pt.io.*;
import se.lth.cs.pt.debug.*;
import se.lth.cs.pt.util.*;
import java.util.*;

class PoliticalSimulation {

    public static void main(String[] args) {
        new PoliticalSimulation().run();
    }

    Community community;
    DotWindow w;

    void run() {
        setup();
        mainLoop();
    }

    void mainLoop() {
        while (true) {
            community.display(w);
            GameEvent e = w.getNextEvent();
            switch (e.getKind()) {
            case GameEvent.KEY_PRESSED:
                keyPressed(e.getKey());
                break;
            case GameEvent.MOUSE_CLICKED:
                mouseClicked(e.getX(), e.getY());
                break;
            }
        }
    }

    void keyPressed(char key) {
        Debug.println("Tangent");
        switch (key) {
        case 'r':
            community.randomize();
            break;
        case ' ':
            community.nextDay();
            break;
        }
    }

    void mouseClicked(int x, int y) {
        Debug.println("Mus");
        community.changeOpinion(x, y);
    }

    void setup() {
        int size = 40;   // Keyboard.nextInt("Storlek: ");
        community = new Community(size);
        w = new DotWindow(community.size(), community.size(),
                          800 / community.size());
        w.checkKeys(true, false, false);
        w.checkMouse(true, false, false, false, false);
        w.useCircularDots(Color.BLACK);
    }
}

class Community {

    private int size;
    private int[][] voters;

    private final int RED = 0, BLUE = 1;
    private Random rng = new Random();

    public Community  (int size) {
        this.size = size;
        voters = new int[size][size];
        randomize();
    }

    public void randomize() {
        for (int y = 0; y < size; y++) {
            for (int x = 0; x < size; x++) {
                voters[x][y] = randomParty();
            }
        }
    }

    private int randomParty() {
        if (rng.nextBoolean()) {
            return RED;
        } else {
            return BLUE;
        }
    }

    public void display(DotWindow w) {
        final Color RED_COLOR = new Color("#D93636");
        final Color BLUE_COLOR = new Color("#368CD9");

        for (int x = 0; x < size; x++) {
            for (int y = 0; y < size; y++) {
                if (voters[x][y] == RED) {
                    w.setDot(x, y, RED_COLOR);
                } else {
                    w.setDot(x, y, BLUE_COLOR);
                }
            }
        }
    }

    public void nextDay() {
        int[][] nextDay = new int[size][size];
        for (int k = 0; k < size; k++) {
            nextDay[k][0] = voters[k][0];
            nextDay[k][size-1] = voters[k][size-1];
            nextDay[0][k] = voters[0][k];
            nextDay[size-1][k] = voters[size-1][k];
        }

        for (int x = 0; x < size; x++) {
            for (int y = 0; y < size; y++) {
                nextDay[x][y] = nextOpinion(x, y);
            }
        }
        voters = nextDay;
    }

    private int nextOpinion(int xPos, int yPos) {
        int nbrOfReds = 0;
        for (int x = xPos - 1; x <= xPos + 1; x++) {
            for (int y = yPos - 1; y <= yPos + 1; y++) {
                if (voters[CSMath.mod(x, size)][CSMath.mod(y, size)] == RED) {
                    nbrOfReds++;
                }
            }
        }
        if (voters[xPos][yPos] == RED) {
            nbrOfReds--;
        }
        if (nbrOfReds > 4) {
            return RED;
        } else if (nbrOfReds < 4) {
            return BLUE;
        } else {
            return randomParty();
        }
    }

    public void changeOpinion(int x, int y) {
        if (voters[x][y] == RED) {
            voters[x][y] = BLUE;
        } else {
            voters[x][y] = RED;
        }
    }

    public int size() {
        return size;
    }
}