public bool MoveRight(Grid grid) { bool canMove = true; int[] newX = new int[Constants.BlockPerPiece]; // Calculate the new coordinates for (uint i = 0; i < Constants.BlockPerPiece ; i++) { newX[i] = _blocks[i]._x + 1 ; } // Check if the piece can be moved for (uint i = 0 ; i < Constants.BlockPerPiece ; i++) { if (grid.isOutOfGrid(newX[i], _blocks[i]._y) || grid.isBlock(newX[i], _blocks[i]._y)) { canMove = false; } } // Rotate the piece if it can be rotated for (uint i = 0 ; canMove && i < Constants.BlockPerPiece ; i++) { _blocks[i]._x = newX[i]; } return canMove; }
private bool pieceCanMove(Grid grid, int[] newX, int[] newY) { bool canRotate = true; if(grid != null) { // First we need to check if the piece is too high // In this case, we need to make it go down int offsetDown = 0; for(uint i = 0 ; i < Constants.BlockPerPiece; i++) { if(newY[i] - offsetDown > Constants.GridSizeYmax) offsetDown = newY[i] - Constants.GridSizeYmax; } if(offsetDown > 0) { for(uint i = 0 ; i < Constants.BlockPerPiece; i++) { newY[i] -= offsetDown; } } // We are going to make 3 iterations to find a position that is good // First with the position already calculated, then with a shift to the right // and finally with a shift to the left int iteration = 0; do { if(iteration == 1) { // We shift to the left for (uint i = 0 ; i < Constants.BlockPerPiece; i++) { newX[i]--; } canRotate = true; } else if(iteration == 2) { // We shift to the right for (uint i = 0 ; i < Constants.BlockPerPiece; i++) { newX[i] += 2; } canRotate = true; } for (uint i = 0 ; i < Constants.BlockPerPiece; i++) { if (grid.isOutOfGrid(newX[i], newY[i]) || grid.isBlock(newX[i], newY[i])) { canRotate = false; break; } } iteration++; } while(!canRotate && iteration < 3); } return canRotate; }
public bool MoveDown(Grid grid) { bool canMove = true; int[] newY = new int[Constants.BlockPerPiece]; // Calculate the new coordinates for (uint i = 0; i < Constants.BlockPerPiece; i++) { newY[i] = _blocks[i]._y - 1 ; } // Check if the piece can be moved for (uint i = 0 ; i < Constants.BlockPerPiece ; i++) { if (grid.isOutOfGrid(_blocks[i]._x, newY[i]) || grid.isBlock(_blocks[i]._x, newY[i])) { canMove = false; break; } } // Validate the new position if it is ok if(canMove) { for (uint i = 0 ; i < Constants.BlockPerPiece ; i++) { _blocks[i]._y = newY[i]; } } return canMove; }