0

I have an assignment to make a better tetris brain in Java. I am fairly new to the program and I am having some difficulties coming up with a loop to help me place the pieces in any space that doesn't have any pieces there already.

I tried this loop but it just crashes the game.

my assignment is similar to what I have here. http://courses.cs.vt.edu/~cs1705/Fall03/programs/p04.php

This is the loop I put inside my CleverBrain. Can i get some help please?

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);
    }
}

I don't need the pieces to rotate. I just don't want them falling right on top of each other directly in the middle. is there a loop to have the pieces spread around as they fall? My code just keep crashing the game when I activate the clever brain. Thanks in advance.

So sorry, I am very new to this. We are given all the related classes to run the game. The objective is to change the LameBrain class, which causes all the pieces to fall down when it is run so that it spreads around. I may probably get this wrong again, I ask you be patient with me. Pretty much all the code is given. instructor asked for a loop that'll make the "public void tryAllColumns" method run a loop to have the pieces spread around. if there is anything else I need to explain further I will gladly do it. I feel as though I am speaking as if you can read my mind and I'm sorry for that I'm still trying to find a way to explain myself better. thanks

  • 2
    "I tried this loop but it just crashes the game" -- You haven't provided the error you get, nor a runnable code example. Are we supposed to guess what the problem is? – azurefrog Oct 06 '15 at 17:39

1 Answers1

0

As one comment stated, it's hard to deduce exactly what the problem you're running into is but based on just the code, I have a feeling this is one of your problems:

while (xIndex < board.getWidth() - piece.getWidth() + 1) {

You are most likely going to get an index out of range exception. You probably want:

while (xIndex < board.getWidth() - piece.getWidth() - 1) {

or this:

while (xIndex < board.getWidth() - piece.getWidth()) {
Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
  • Hello, I tried that and it did not work. I edited the first post. if you don't mind can you give it one more read to see if I'm making much sense? – Prince Amponsah Oct 06 '15 at 21:48
  • You still haven't given any details about the crash. Please post a full stack trace - that usually tells you exactly what the error is and where it is happening in the code – Mike Dinescu Oct 06 '15 at 23:44