/// <summary> /// Clonning constructor /// </summary> /// <param name="sourceMatrix">Matrix to make copy from</param> public ByteMatrix(ByteMatrix sourceMatrix) { int length = sourceMatrix.BaseArray.Length; this._baseArray = new byte[length]; Array.Copy(sourceMatrix.BaseArray, this._baseArray, length); this._rows = sourceMatrix.Rows; this._columns = sourceMatrix.Columns; }
public GameBlocks() { _blocks = new ByteMatrix[7]; // O _blocks[0] = new ByteMatrix(2, 2); _blocks[0].SetCells(0, 0, new byte[4] { 1, 1, 1, 1}); // J _blocks[1] = new ByteMatrix(3, 2); _blocks[1].SetCells(0, 0, new byte[6] { 2, 2, 2, 0, 2, 0}); // L _blocks[2] = new ByteMatrix(3, 2); _blocks[2].SetCells(0, 0, new byte[6] { 3, 0, 3, 0, 3, 3}); // T _blocks[3] = new ByteMatrix(2, 3); _blocks[3].SetCells(0, 0, new byte[6] { 0, 4, 0, 4, 4, 4}); // Z _blocks[4] = new ByteMatrix(2, 3); _blocks[4].SetCells(0, 0, new byte[6] { 5, 5, 0, 0, 5, 5}); // S _blocks[5] = new ByteMatrix(2, 3); _blocks[5].SetCells(0, 0, new byte[6] { 0, 6, 6, 6, 6, 0}); // I _blocks[6] = new ByteMatrix(4, 1); _blocks[6].SetCells(0, 0, new byte[4] { 7, 7, 7, 7,}); }
/// <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> /// Creates new block from 'next-block' and generates new nex-block /// </summary> private void NewBlock() { int next; // nextBlock is null on first execution of the game if (nextBlock == null) { next = rnd.Next(7); //Microsoft.SPOT.Math.Random(7); nextBlock = GameBlocks.Instance.GetBlock(next); } currentBlock = new ByteMatrix(nextBlock); next = rnd.Next(7);// Microsoft.SPOT.Math.Random(7); nextBlock = GameBlocks.Instance.GetBlock(next); blockX = 4; blockY = 0; }
/// <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); }