I tried to create a Game of Life copy with following OOD principles. What should I change in my code to make it cleaner?
import java.awt.Color;
import java.awt.Point;
public class Square {
public Point coordinate;
public static int squareDimension = 10;
public Square (int x, int y) {
coordinate = new Point(x, y);
}
}
The class that makes all computations:
import java.awt.Point;
import java.util.ArrayList;
public class Board {
public int sqrDimension ;
public int[][] allBoard;
public ArrayList<Point> emptySquare;
public ArrayList<Square> filledSquare;
public ArrayList<Square> temporarySquare;
public ArrayList<Square> sqrWillLive;
public Board() {
sqrDimension = Square.squareDimension;
allBoard = new int[200][200];
filledSquare = new ArrayList<Square>();
temporarySquare = new ArrayList<Square>();
sqrWillLive = new ArrayList<Square>();
emptySquare = new ArrayList<Point>();
}
public void move () {
increaseOneNghbrd();
theFutureOfFilledSquare();
theFutureOfEmptySqr();
makeBoardEmpty();
}
// adding one to value of neighberhood of filled square
public void increaseOneNghbrd() {
int sideOfBoard = allBoard.length -1;
for (Square kareler : filledSquare) {
// coordinate of filled square
int x = kareler.coordinate.x;
int y = kareler.coordinate.y;
if (x > 0 && x <sideOfBoard && y > 0 && y < sideOfBoard) {
for (int i = -1; i < 2; i++) {
for (int j = -1; j < 2; j++) {
allBoard[x+j][y+i]++;
addIntoEmpty(x+j, y+i);
}
}
// decrease value of filled square
// cause its increased as it is nghbrhood of itself
allBoard[x][y]--;
}
}
}
// neighberhood square is added to array
public void addIntoEmpty (int x, int y) {
boolean kopya = true;
//checking if it is in array or not
for (Point kareler : emptySquare) {
if (kareler.x == x && kareler.y == y) {
kopya = false;
break;
}
}
// checking if this square is filled square or empty sqr
if (kopya) {
for (Square kareler1 : filledSquare) {
if (kareler1.coordinate.x == x && kareler1.coordinate.y == y) {
kopya = false;
break;
}
}
}
//if it is not added before and not filled sqr then it is added
if (kopya) {
emptySquare.add(new Point(x, y)) ;
}
}
//putting coordinate of mouse clicking
public void addSquare(int x, int y){
boolean kopya = true;
//checking if clicked to filled square
for (Square kareler : filledSquare) {
if (kareler.coordinate.x == x && kareler.coordinate.y == y) {
kopya = false;
break;
}
}
if (kopya) {
filledSquare.add(new Square(x, y));
temporarySquare.add(new Square(x, y));
}
}
// values of each cell will return 0 after each move
public void makeBoardEmpty () {
allBoard = new int[200][200];
}
// filled sqr will die or live depending on its value
public void theFutureOfFilledSquare () {
// adding sqr that is filled and has enough ngbhrhd to live to temprry array
for (Square kareler : filledSquare) {
int komsu = allBoard[kareler.coordinate.x][kareler.coordinate.y] ;
if (komsu == 2 || komsu == 3) {
sqrWillLive.add(kareler);
}
}
// removing all filled square
filledSquare.clear();
temporarySquare.clear();
// square that has right to live is added to array again
for (Square point : sqrWillLive) {
filledSquare.add(point);
temporarySquare.add(point);
}
sqrWillLive.clear();
}
// empty array will be added to filled sqr array if it has 3 ngbhrhd
public void theFutureOfEmptySqr () {
for (Point kareler : emptySquare) {
int komsu = allBoard[kareler.x][kareler.y] ;
if (komsu == 3) {
filledSquare.add(new Square(kareler.x, kareler.y));
temporarySquare.add(new Square(kareler.x, kareler.y));
}
}
emptySquare.clear();
}
}
Drawing class:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DrawingBoard extends JPanel implements MouseListener, KeyListener,MouseMotionListener{
private static final long serialVersionUID = 1L;
public JFrame cerceve;
public Board board;
int squareDimension;
int SquareFrame ;
public DrawingBoard () {
board = new Board();
cerceve = new JFrame();
cerceve.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(getMaximumSize());
addMouseListener(this);
addMouseMotionListener(this);
addKeyListener(this);
setFocusable(true);
cerceve.add(this);
cerceve.setPreferredSize(getMaximumSize());
cerceve.setResizable(false);
cerceve.pack();
cerceve.setVisible(true);
squareDimension = board.sqrDimension;
SquareFrame = (board.sqrDimension+1);
}
public void paintComponent (Graphics g) {
super.paintComponent(g);
karelericiz(g);
}
public void karelericiz(Graphics g) {
g.setColor(Color.ORANGE);
for (Square kare : board.filledSquare) {
g.fillRect(kare.coordinate.x*SquareFrame+1,
kare.coordinate.y*SquareFrame+1,
board.sqrDimension, board.sqrDimension);
}
}
public static void main(String[] args) {
DrawingBoard tahta1 = new DrawingBoard();
}
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
int x = e.getX() / SquareFrame;
int y = e.getY() / SquareFrame;
board.addSquare(x, y);
repaint();
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_5) {
board.move();
repaint();
}
}
public void keyReleased(KeyEvent e) {
}
public void mouseDragged(MouseEvent e) {
int x = e.getX() / SquareFrame;
int y = e.getY() / SquareFrame;
board.addSquare(x, y);
repaint();
}
public void mouseMoved(MouseEvent e) {
}
}