3
\$\begingroup\$

I wrote my first code in Java and I need constructive criticism and code review.

This is an implementation of the well-known "2048" sliding-blocks game.

package com.example.simple2048game;

import static javafx.scene.text.Font.font;

public class GameBoard {
     static double PANE_SIZE = 100 * GameMatrix.getInstance().getMatrixSize();
      static final double RECTANGLE_SIZE = PANE_SIZE / GameMatrix.getInstance().getMatrixSize();

    public enum Direction {
        UP, DOWN, LEFT, RIGHT
    }
    public static final class GameMatrix {


        private final int matrixSize = 4;
        private int[][] gameMatrix;
        private static volatile GameMatrix INSTANCE;

        public int getMatrixSize() {
            return matrixSize;
        }

        public int[][] getGameMatrix() {
            return gameMatrix;
        }

        public void setGameMatrix(int[][] newMatrix) {
            this.gameMatrix = newMatrix;
        }
        private GameMatrix() {
            gameMatrix = new int[matrixSize][matrixSize];

        }
        public static GameMatrix getInstance() {
            if (INSTANCE == null) {
                synchronized (GameMatrix.class) {
                    if (INSTANCE == null) {
                        INSTANCE = new GameMatrix();
                    }
                }
            }
            return INSTANCE;
        }
        record Point(int x, int y) {
            Point(int x, int y) {
                this.x = x;
                this.y = y;
            }
        }
    }

}

Latest version is on GitHub.

\$\endgroup\$
3
  • \$\begingroup\$ Welcome to Code Review! Obviously the code is for a game of 2048, however it would benefit reviewers to have a bit more information about the code in the description. From the help center page How to ask: "You will get more insightful reviews if you not only provide your code, but also give an explanation of what it does. The more detail, the better." \$\endgroup\$ Commented Oct 24, 2023 at 21:40
  • \$\begingroup\$ @SᴀᴍOnᴇᴌᴀ Basically, I'm mainly concerned about the cleanliness of the code and not about the game's algorithms themselves. \$\endgroup\$
    – hathor
    Commented Oct 24, 2023 at 22:05
  • \$\begingroup\$ I'm not sure there's enough here to meaningfully review. Most of the code seems to be missing from the question. Remember that here on Code Review, we have quite generous limit on question size (~64k characters). \$\endgroup\$ Commented Oct 25, 2023 at 6:47

1 Answer 1

4
\$\begingroup\$

GameMatrix isn't easily constructible, as it enforces a singleton pattern. In my view, that's an antipattern, as it makes it impossible to run unit-tests in parallel.

In this code, I'd consider the singleton to be an anti-pattern, which is equivalent to (and inferior to) a simple global variable.

Even worse, the setGameMatrix allows callers to replace gameMatrix with a value that doesn't match matrixSize, potentially leading to erroneous operation (such as raising exceptions). If we believe we really need setGameMatrix(), it needs to be a lot more defensive about its arguments.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.