我是新手,所以请不要给我太多烫伤,因为我只想从一开始就遵循优秀的OOP路径:)所以我用Swing编写了一个用Java编写的扫雷艇,现在,我的代码看起来像这样:
答案 0 :(得分:1)
我不熟悉swing,所以我只能给你一些伪java代码。 但是,它应该起到示范作用。当你想要达到 下一级OOP,我建议为一个单元格创建一个类 扫雷网格。
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.
}
}
}