/// <summary> /// Checks whether the given tetrisBlock can be on given coords /// </summary> /// <param name="block">GameBlock</param> /// <param name="x">X coord</param> /// <param name="y">Y coord</param> /// <returns>True if block can be on given coords</returns> private bool Check(ByteMatrix block, int x, int y) { for (int row = 0; row < block.Rows; row++) { for (int col = 0; col < block.Columns; col++) { int newX = col + x; int newY = row + y; // if block is out of the field then return false if ((newX < 0 || newX > FIELD_COLS - 1 || newY < 0 || newY > FIELD_ROWS - 1) && block.GetCell(row, col) != 0) { return(false); } // if block is in the field but there is no free place to fall, return false if (!(newX < 0 || newX > FIELD_COLS - 1 || newY < 0 || newY > FIELD_ROWS - 1)) { if (field.GetCell(newY, newX) != 0 && block.GetCell(row, col) != 0) { return(false); } } } } return(true); }
/// <summary> /// Rotate current block /// </summary> public void Rotate() { ByteMatrix rotated = new ByteMatrix(currentBlock.Columns, currentBlock.Rows); // Rotate the object for (int row = 0; row < currentBlock.Rows; row++) { for (int col = 0; col < currentBlock.Columns; col++) { rotated.SetCell(rotated.Rows - col - 1, row, currentBlock.GetCell(row, col)); } } // Check whether rotate object fits the field if (Check(rotated, blockX, blockY)) { currentBlock = new ByteMatrix(rotated); } }
/// <summary> /// Checks whether the given tetrisBlock can be on given coords /// </summary> /// <param name="block">GameBlock</param> /// <param name="x">X coord</param> /// <param name="y">Y coord</param> /// <returns>True if block can be on given coords</returns> private bool Check(ByteMatrix block, int x, int y) { for (int row = 0; row < block.Rows; row++) for (int col = 0; col < block.Columns; col++) { int newX = col + x; int newY = row + y; // if block is out of the field then return false if ((newX < 0 || newX > FIELD_COLS - 1 || newY < 0 || newY > FIELD_ROWS - 1) && block.GetCell(row, col) != 0) return false; // if block is in the field but there is no free place to fall, return false if (!(newX < 0 || newX > FIELD_COLS - 1 || newY < 0 || newY > FIELD_ROWS - 1)) if (field.GetCell(newY, newX) != 0 && block.GetCell(row, col) != 0) return false; } return true; }