我的任务是用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类,这会导致所有碎片在运行时掉落以使其四处传播。我可能会再次弄错,我请你耐心等待我。几乎所有代码都给出了。教练要求一个循环,以便公共无效的tryAllColumns&#34;方法运行循环以使各个部分散布。如果还有什么我还需要进一步解释,我很乐意这样做。我觉得好像在说话,好像你能读懂我的想法,我很抱歉,我仍然想找到一种更好地解释自己的方法。谢谢
答案 0 :(得分:0)
正如一条评论所说,很难准确推断出你遇到的问题是什么,但仅基于代码,我觉得这是你的问题之一:
while (xIndex < board.getWidth() - piece.getWidth() - 1) {
您最有可能获得超出范围异常的索引。你可能想要:
while (xIndex < board.getWidth() - piece.getWidth()) {
或者这个:
#include <SDL2/SDL.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define SCREENH 768
#define SCREENW 1366
SDL_Window *window = NULL;
SDL_Surface *screenSurface = NULL;
SDL_Surface *windowSurface = NULL;
int init_SDL() {
int success = 0;
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL could not initialize! ");
printf("SDL_Error: %s\n",SDL_GetError());
success = -1;
}
else {
window = SDL_CreateWindow("SDL2_Tutorial02",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,SCREENW,SCREENH,SDL_WINDOW_SHOWN);
if(window == NULL) {
printf("Window could not be created! ");
printf("SDL Error: %s\n",SDL_GetError());
}
else {
screenSurface = SDL_GetWindowSurface(window);
}
}
return success;
}
int loadMedia() {
int success = 0;
windowSurface = SDL_LoadBMP("Images/Hallo.bmp");
if(windowSurface == NULL) {
printf("Unable to load image! ");
printf("SDL Error: %s\n",SDL_GetError());
success = -1;
}
return success;
}
void close() {
SDL_FreeSurface(windowSurface);
windowSurface = NULL;
SDL_DestroyWindow(window);
window = NULL;
SDL_Quit();
}
int main(int argc,char *argv[]) {
assert(init_SDL() == 0);
assert(loadMedia() == 0);
SDL_BlitSurface(windowSurface,NULL,screenSurface,NULL);
SDL_UpdateWindowSurface(window);
SDL_Delay(3000);
close();
exit(EXIT_SUCCESS);
}