我有一个任务是用Java做一个更好的俄罗斯方块大脑。我对这个程序相当陌生,我遇到了一些困难,我想出一个循环来帮助我将碎片放在任何没有任何碎片的空间中。
我试过这个循环,但它只是使游戏崩溃。
我的任务类似于我在这里的任务。http://courses.cs.vt.edu/~cs1705/Fall03/programs/p04.php
这是我放在CleverBrain中的循环。能帮我个忙吗?
import cs5044.tetris.*;
public class CleverBrain implements Brain {
public void bestMove(
Board board, Piece piece, int heightLimit, Move move){
move.setScore(1e20);
int rotationCount = 0;
while (rotationCount < piece.numRotations()){
// For this rotation of the piece, try to drop it from every
// possible column and see which result scores the best
tryAllColumns(board, piece, heightLimit, move);
piece = piece.nextRotation();
++rotationCount;
}
}
public void tryAllColumns(
Board board, Piece piece, int heightLimit, Move move)
{
i int xIndex = 0;
int yIndex = 0;
while (xIndex < board.getWidth() - piece.getWidth() + 1) {
if (board.getColumnHeight(xIndex) == 1 || board.getColumnHeight(xIndex) <= yIndex) {
move.setPiece(piece);
move.setX(xIndex);
move.setScore(100000.0);
xIndex++;
}
if (board.getBlocksInRow(yIndex) == board.getWidth()) {
yIndex++;
}
move.setX(0);
}
}
我不需要这些碎片来旋转。我只是不想让它们直接落在中间。有没有一个循环让碎片在落地时散开?当我激活聪明的大脑时,我的代码就一直使游戏崩溃。提前谢谢。
很抱歉,我是个新手。我们得到了运行游戏所需的所有相关类。这样做的目的是更改LameBrain类,该类在运行时会导致所有片段倒下,从而使其散布开来。我可能又弄错了,请你耐心点。几乎所有的代码都给出了。讲师要求使用一个循环,让"public void tryAllColumns“方法运行一个循环,让这些片段分散开来。如果还有什么我需要进一步解释的,我很乐意去做。我觉得我说的好像你能读懂我的心思,为此我很抱歉,我仍然在努力寻找一种更好地解释自己的方法。谢谢
发布于 2015-10-07 01:45:00
正如一条评论所说,很难准确推断出你遇到的问题是什么,但仅基于代码,我有一种感觉,这是你的问题之一:
while (xIndex < board.getWidth() - piece.getWidth() + 1) {
你很可能会得到一个超出范围的索引异常。您可能需要:
while (xIndex < board.getWidth() - piece.getWidth() - 1) {
或者这样:
while (xIndex < board.getWidth() - piece.getWidth()) {
https://stackoverflow.com/questions/32976125
复制相似问题