设计扫雷游戏的模式

huangapple go评论80阅读模式
英文:

Design patterns for minesweeper

问题

我正在尝试使用设计模式创建扫雷游戏,但在实现瓦片的创建模式时遇到了问题。

在扫雷游戏中,有两种类型的瓦片,数字瓦片和雷瓦片。数字瓦片包含一个数字或空白,具体取决于其旁边的雷瓦片数量。而雷瓦片包含一个雷的图像,加载到瓦片上。

我尝试使用工厂方法来创建瓦片,以下是Java的示例代码。

抽象类:

public abstract class Tile {
    boolean flag = false;
    boolean opened = false;

    abstract void open();

    void flag(){
        // 业务逻辑
    }

    boolean checkFlagged(){
        return flag;
    }

    boolean checkOpened(){
        return opened;
    }
}

数字瓦片类:

public class NumberTile extends Tile {
    int number;

    @Override
    void open(){
        // 业务逻辑
    }
    
    void setNumber(int number){
        this.number = number;
    }

    int getNumber(){
        return number;
    }
}

雷瓦片类:

public class MinesTile extends Tile {
    ImageIcon mineImg;

    @Override
    void open(){
        // 业务逻辑
        explode();
    }
    
    void explode(){
        // 告诉主游戏板结束游戏
    }
}

客户端代码:

Tile numberTile = new NumberTile(); // get和setNumber方法将不可访问
Tile minesTile  = new MinesTile();  // explode方法将不可访问

如何在不使子类方法不可访问的情况下使用相同的抽象Tile数据类型?

如果我将所有方法从NumberTileMinesTile抽象到抽象类中,那么它们还必须重写不会被使用的方法,这会导致"refused bequest"问题。

如何解决这个设计模式的问题?

英文:

I'm trying to create a minesweeper game with design patterns, but I have trouble implementing a creational pattern for the tiles in minesweeper.

In minesweeper, there are 2 types of tiles, the number tile and mines tile. The number tile contains a number or a blank depending on the amount of mines tiles beside them. While the mines tile contain a mine image to load on the tile.

How I approached this is by trying to use a Factory Method for the tiles, here is an example in java.

The abstract class

public abstract class Tile {
    boolean flag = false;
    boolean opened = false;

    abstract void open();

    void flag(){
        // business logic
    }

    boolean checkFlagged(){
        return flag;
    }

    boolean checkOpened(){
        return opened;
    }
}

The NumberTile class

public class NumberTile extends Tile {
    int number;

    @Override
    void open(){
        // business logic
    }
    
    void setNumber(int number){
        this.number = number;
    }

    int getNumber(){
        return number;
    }
}

The MinesTile class

public class MinesTile extends Tile {
    ImageIcon mineImg;

    @Override
    void open(){
        // business logic
        explode();
    }
    
    void explode(){
        // tell main board to end game
    }
    
}

Client Code

Tile numberTile = new NumberTile(); // get and setNumber method will not be accesible
Tile minesTile  = new MinesTile();  // explode method will not be accesible

How do I use the same abstract Tile data type without having the child class methods inaccessible?

If I would abstract all the methods from NumberTile and MinesTile into the abstract class, then they would also have to override methods that won't be used and cause refused bequest.

How do I solve this design pattern problem?

答案1

得分: 0

你无法真正解决这个问题,我不确定你要实现的确切设计模式,因为有许多变体。但你能够实现这个的唯一方法就是进行强制类型转换。

Tile someTile = new NumberTile();

if (someTile instanceof NumberTile) {
    NumberTile numberTile = (NumberTile) someTile;
    numberTile.getNumber();
}
英文:

You cannot really solve this, I am not sure on the exact design pattern you're trying to implement as there are many variants. But the only way you can achieve this is by casting.

Tile someTile = new NumberTile();

if(someTile instanceof NumberTile){
    NumberTile numberTile = (NumberTile) someTile;
    numberTile.getNumber();
}

huangapple
  • 本文由 发表于 2020年10月17日 10:33:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/64398485.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定