0

I'm a newbie so please don't scald me too much as I'd just like to follow the good OOP trail from the very beginning :) So I'm coding a minesweeper in Java with Swing and for now, my code looks like this:

  • a class with main() only which starts the game by creating an object Minesweeper()
  • the Minesweeper class in which I create a JFRame, JPanel for menu (and ActionListener for it) and create a Grid(x,y) object
  • the Grid(int width, int height) class which extends JPanel using which I create a grid with the given dimensions, put mines on it and handle all the playing

I have some worries about the Grid(), though. Is it OK to handle everything from drawing the desired number of JButtons, through setting mines on them and listening for clicks (and also addressing those clicks) to the finding_empty_cells algorithm in case user clicks on something other than bomb and we have to show the surrounding empties in one class? Doens't that violate the single responsibility principle? Or is it OK?

Andremoniy
  • 34,031
  • 20
  • 135
  • 241
Straightfw
  • 2,143
  • 5
  • 26
  • 39
  • 1
    Taking it to a higher OOP level, a cell in the Grid would be represented by a class instance as well. – Niklas R Jan 11 '13 at 09:53
  • So even when I don't have anything to add to the JButton class, I should create some Cell() extends JButton class anyway and pop some vairbles like isThereBomb, isThereFlag and such inside? – Straightfw Jan 11 '13 at 09:57
  • *"the Grid(int width, int height) class which extends JPanel.."* Why extend panel rather than simply hold a reference to (or just use) one? – Andrew Thompson Jan 11 '13 at 10:00
  • Andrew - that's what I did first but I read some codes on Minesweeper online and found that some of them (who were praised) used this technique. I just thought it'd be a good way to go. If it's not, good for me, less code to refactor :) – Straightfw Jan 11 '13 at 10:02
  • I would say `Grid` is a good thing too have (generally speaking). Most of the games will consist just of one Grid and in those it will be less of an advantage, but having the class allows you more freedom (for example, would ease you creating a game with two minesweeper grids, one next to the other). – SJuan76 Jan 11 '13 at 10:08
  • 1
    Why don't you show your code to the guys at http://codereview.stackexchange.com/? I'm pretty sure they will help you. – Thomas Jungblut Jan 11 '13 at 10:22
  • Thomas - Thanks, I think I may do it as soon as I code the whole logic to not show some unpolished code :) – Straightfw Jan 11 '13 at 10:24
  • Also consider MVC, illustrated [here](http://stackoverflow.com/a/3072979/230513). – trashgod Jan 11 '13 at 11:11

1 Answers1

1

I am not familiar with swing, so I can only give you some pseudo-java code. However, it should serve the demonstrational purpose. When you want to reach the next level of OOP, I would recommend creating a class for a cell in the Minesweeper grid.

public class Cell extends JPanel {

    private MinesweepController controller;
    private int points;
    private boolean revealed;

    // Index in the grid.
    private int x, y;

    public Cell(MinesweepController controller_, int x_, int y_, int points_) {
        controller = controller_;
        points = points_;
        x = x_;
        y = y_;
    }

    public void onClick(MouseEvent event) {
        controller.reveal(x, y);
    }

    public void paint(Graphics gfx) {
        if (revealed) {
            if (points < 0) {
                drawBomb(getX(), getY())
            }
            else {
                drawPoints(getX(), getY(), points);
            }
        }
        else {
            drawRect(getX(), getY());
        }
    }

    public int getPoints() {
        return points;
    }

    public boolean isRevealed() {
        return revealed;
    }

    public void reveal() {
        revealed = true;
    }

}

public class MinesweepController {

    private Grid grid;
    private int score;

    // ...

    public boid reveal(int x, int y) {
        Cell cell = grid.getCell(x, y);
        if (cell.getPoints() < 0) {
            // End game.
        }
        else {
            score += cell.getPoints();
            // Reveal ascending cells.
        }
    }

}
Niklas R
  • 16,299
  • 28
  • 108
  • 203
  • dont draw in `paint(..)`, rather override `paintComponent(Graphics g)` and dont forget to call its `super.XXX` implementation before drawing to the graphics object. – David Kroukamp Jan 11 '13 at 11:24
  • @DavidKroukamp: Thank you. As I said, I'm not familiar with swing and I'm sorry for this issue. – Niklas R Jan 11 '13 at 11:38