Question

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

Was it helpful?

Solution

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top