-1

Currently i have the move left function

int8_t move_piece_left(piece_type* piecePtr) {

int8_t row;
/*
 * Check if piece is all the way to the left If so, return.
 * (Remember, column number is based on bit position - higher
 * numbers to the left.
 */
if(piecePtr->right_column + piecePtr->x_dimension >= BOARD_WIDTH) {
    return 0;
}

/*
 * Make the move.
 */
for(row=0; row < piecePtr->y_dimension; row++) {
    piecePtr->rowdata[row] <<= 1;
}
piecePtr->right_column++;
return 1;

}

I assumed for the move right it would be a simple change, which I am sure it is but I don't think its going so well. So obviously I need to check if the piece is all the way to the right not the left, and perhaps its

piecePtr->right_column--;

To move right since left is ++ right would be -- I think?

I've tried a few changes here and there but not much has worked, so I am beginning to think I'm not fully understanding the code.

Can someone give a more of a deeper explain what the code is doing (I can read the comments I wanted a more of an in depth explanation).

EDIT there are appropriate checks in order to move left or right. e.g. checking if a piece is there

Jim
  • 482
  • 1
  • 5
  • 20
  • The existing code seems flawed or misleading. In order to move left, one needs to compute the left side coordinate and check if it will be less than the left side of the board, then move left. In order to move to right, one would compute the right side coordinate, check if it will be greater than the right side of the board, then move right. As @Medinoc points out, there are no checks against other pieces, either. – nurettin May 16 '13 at 07:36
  • There are, but i haven't show the code, i will edit my original post though – Jim May 16 '13 at 07:45

1 Answers1

0

rowData seems to be key here: It appears to contain all pixels of the piece, in binary form (one integer per row). So, if you move the piece to the right, you have to shift its values right instead of left, in addition to changing the test and incrementation.

Also note that this code does not check for existing pieces in the way: It only checks for bounds.

The moving code shold be something like this:

int8_t move_piece_right(piece_type* piecePtr) {

    int8_t row;
    /*
     * Check if piece is all the way to the right If so, return.
     * (Remember, column number is based on bit position - higher
     * numbers to the left.
     */
    if(piecePtr->right_column == 0) {
        return 0;
    }

    /*
     * Make the move.
     */
    for(row=0; row < piecePtr->y_dimension; row++) {
        piecePtr->rowdata[row] >>= 1;
    }
    piecePtr->right_column--;
    return 1;
}
Medinoc
  • 6,577
  • 20
  • 42
  • Yeah, i've got another bit that checks for existing pieces at the moment, trying to get that bit to allow me to drop one piece to the bottom after i enable the move right, how would i go about changing the test in this case however, thats the main bit that is stumping me – Jim May 16 '13 at 07:36
  • I added a rough code sample. – Medinoc May 16 '13 at 07:45
  • oh right i see what you mean now... don't know why i didn't think of that, thank you. – Jim May 16 '13 at 07:54