0

I'm building a Tetris game in Java using some software design patterns. Basically, I created a factory algorithm that retrieves a string to figure out what type of tetris object to make when it's given a request by the main game loop, see code:

public class TVShapeFactory {

    protected TVShape tvShape = null;
    protected GameContainer gc;

    TVShapeFactory(GameContainer gc) {
        this.gc = gc;
    }

    public TVShape createShape(String shape) {
        if (shape=="TVIShape") {
            tvShape = new TVIShape(gc);
        }
        else if (shape=="TVOShape") {
            tvShape = new TVOShape(gc);
        }
        else if (shape=="TVLShape") {
            tvShape = new TVLShape(gc);
        }
        else if (shape=="TVZShape") {
            tvShape = new TVZShape(gc);
        }
        else if (shape=="TVJShape") {
            tvShape = new TVJShape(gc);
        }
        else if (shape=="TVSShape") {
            tvShape = new TVSShape(gc);
        }
        else if (shape=="TVTShape") {
            tvShape = new TVTShape(gc);
        }
        else {
            System.out.println("Error: invalid type of shape");
        }
        return tvShape;
    }
}

If you don't know what gc is, it's simply a part of Slick 2D's library and is used to work with the full game environment. Anyways, I created a few other methods to randomly generate a shape for the game (such that the main game loop receives a random shape every time, but I feel like RNG doesn't cut it, I want to make it harder. I noticed there's a famous tetris algorithm called Bastet tetris, but it didn't make sense to me. Any suggestions you guys have for making a HARD tetris shape generation algorithm? Here's my simple RNG algorithm:

public TVShape getRandomShape() {

    TVShape[] shapes = new TVShape[7];
    shapes[0] = createShape("TVIShape");
    shapes[1] = createShape("TVOShape");
    shapes[2] = createShape("TVLShape");
    shapes[3] = createShape("TVZShape");
    shapes[4] = createShape("TVJShape");
    shapes[5] = createShape("TVSShape");
    shapes[6] = createShape("TVTShape");

    int index = new Random().nextInt(shapes.length);
    return shapes[index];
}
4

2 回答 2

1

你们对制作硬俄罗斯方块形状生成算法有什么建议吗?

为了使它更难,您需要一种方法来评估玩家最需要哪些形状以及他们最不需要哪些形状,然后将您的随机数生成偏向于给他们不需要的形状。

在基本层面上,2x2 方形和长 4x1 形状是“最简单的”,因为您通常可以更轻松地堆叠它们。所以你可以制作一个随机形状生成器,它返回它们的频率是其他形状的一半:

public TVShape getRandomShape() {

    TVShape[] shapes = new TVShape[12];
    shapes[0] = createShape("TVIShape");
    shapes[1] = createShape("TVOShape");
    shapes[2] = createShape("TVLShape");
    shapes[3] = createShape("TVLShape");
    shapes[4] = createShape("TVZShape");
    shapes[5] = createShape("TVZShape");
    shapes[6] = createShape("TVJShape");
    shapes[7] = createShape("TVJShape");
    shapes[8] = createShape("TVSShape");
    shapes[9] = createShape("TVSShape");
    shapes[10] = createShape("TVTShape");
    shapes[11] = createShape("TVTShape");

    int index = new Random().nextInt(shapes.length);
    return shapes[index];
}

但是如果你想让它变得非常困难,你可以在每一步评估在当前游戏状态下哪个形状对玩家的帮助最大和最小:

对于每个形状,遍历可以放置它的所有有效位置,以及所有可能的旋转(每个形状 1、2 或 4 个),并根据几个标准对每个可能的位置进行评分:

  • 放置后堆栈在最高点的高度。每次身高增加负分。
  • 放置是否导致任何行被完全填充。每个已完成的行都是一个正分数。
  • 堆栈中有多少个孔(一列中最高占用方格下方的空白方格)。每个洞都是负分。
  • 你能想到的任何其他事情

形状的最佳放置分数是该形状的分数。然后根据分数对棋子进行排序,并生成一个随机数。使它有很高的机会获得最差的形状,获得第二差的形状的机会略小,等等。

因此,例如,您可以生成一个介于 0 和 27 之间的数字,然后根据该数字进行选择:

  • 0-6 最差形状
  • 7-12 次最差形状
  • 13-17第三差的形状
  • 18-21 倒数第四
  • 22-24第三佳身材
  • 25-26次最佳身材
  • 27最好的形状

你可以根据你想要的难度来改变分布。

于 2015-04-10T01:50:15.213 回答
1

而不是“==”符号使用 .equals() 函数来比较字符串。== 用于检查两个字符串是否具有相同的引用。而 .equals() 方法用于检查两个字符串是否具有相同的值。代替

(shape=="TVIShape")

利用

(shape.equals("TVIShape")
于 2015-04-10T01:30:39.223 回答