0

I am writing a method to handle direction for Tetris and am confused about the logic to write hard drop. the move method is called whenever a key is pressed.

public void move(Direction direction) {
    if (canMove(direction)) {
        switch (direction) {
        case DOWN:
            row = row + 1;
            break;
        case LEFT:
            col = col - 1;
            break;
        case RIGHT:
            col = col + 1;
            break;  
        case DROP:
            for(int i = row; i < Grid.HEIGHT; i ++){
                if(!grid.isSet(i,col)){
                    row =  row + 1;
                }       
            }
            break;
        }
    }
}

My idea is to find the furthest open space where the block can hard drop and it will repeat going down rows until it hits that space to hard drop.

Edit: This is my canMove method and I've changed my case drop, instant drop does work, however, there seems to be a problem with collision when drop key is used. I dont quite understand why

public boolean canMove(Direction direction) {
    if (!ableToMove)
        return false;

    boolean move = true;
    // if the given direction is blocked, we can't move
    // remember to check the edges of the grid
    switch (direction) {
    case DOWN:
        if (row == (Grid.HEIGHT - 1) || grid.isSet(row + 1, col))
            move = false;
        break;

    case DROP:
        if (row == (Grid.HEIGHT - 1) || grid.isSet(row + 1, col))
            move = false;
        break;  
    // currently doesn't support checking LEFT or RIGHT
    // MODIFY so that it correctly returns if it can move left or right
    case LEFT:
        if (col == (0) || grid.isSet(row, col-1))
            move = false;
        break;

    case RIGHT:
        if (row == (Grid.WIDTH - 1) || grid.isSet(row, col+1))
            move = false;
        break;


    }
    return move;
}
greg-449
  • 109,219
  • 232
  • 102
  • 145
  • As someone mentioned, _(I couldn't catch his name, he deleted his post too fast)_ have you looked here http://stackoverflow.com/questions/16592898/tetris-hard-drop-logic ? – frederick99 Apr 15 '17 at 21:08
  • Yes, i have and have no idea how to do that in my code –  Apr 15 '17 at 21:19
  • 1
    Please don't vandalize your own posts. When you post here, you give SO the right to distribute the content under CC-by SA 4.0. Any vandalism will be reverted. – greg-449 Mar 12 '20 at 08:12

1 Answers1

0

The required change in DROPcase is

case DROP:
        while(canMove(DOWN)){
            row =  row + 1;  
        }
        break;
frederick99
  • 1,033
  • 11
  • 18
  • this works however, i now have a problem with collision when using drop –  Apr 15 '17 at 21:42
  • _collision?_ `canMove` returns `true` only when the block can move. right? – frederick99 Apr 15 '17 at 23:15
  • I don't understand the problem here. If `canMove` works fine, there should not be any issues. What do you mean by _problem with collision_? – frederick99 Apr 15 '17 at 23:43
  • for example, L shape piece when dropped will just be 2 squares after dropped. and when also dropped pieces wont "stick" to their shape, like the piece should be above a empty space but instead it goes down to that spot. –  Apr 15 '17 at 23:51
  • That's for you to figure out. If you can't, edit the question and include what you had tried. This answer answers the current question. (: – frederick99 Apr 16 '17 at 00:26