static const int FIELD_Y = 20; static const int FIELD_X = 10; static const int BLOCK_X = 4; static const int BLOCK_Y = 4; static const int BLOCK_TYPES= 7; static const int ROTATIONS = 4; typedef int GameBlock[BLOCK_Y][BLOCK_X]; /// indices to our block field: /// look at the rows to see, which blocks are requested for each rotation /// look at each row to see the indices of the individual blocks for a single rotation static const int blockIndex[BLOCK_TYPES*ROTATIONS] = { // line cube S L T etc. 0, 2, 3, 7, 9, 11, 15, // rotation 0 1, 2, 4, 8, 10, 12, 16, // rotation 1 0, 2, 5, 7, 9, 13, 17, // rotation 2 1, 2, 6, 8, 10, 14, 18, // rotation 3 }; /// unique blocks - since not all blocks really rotate (e.g. the cube doesn't), /// only individual blocks are stored in this field ('1' means blocked, '0' means empty) /// you can re-arrange the arrays to visualise the blocks (: static const GameBlock blocks[] = { { {1,0,0,0}, {1,0,0,0}, {1,0,0,0}, {1,0,0,0} }, { {0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {1,1,1,1} }, { {0,0,0,0}, {0,0,0,0}, {1,1,0,0}, {1,1,0,0} }, { {0,0,0,0}, {1,1,0,0}, {1,0,0,0}, {1,0,0,0} }, { {0,0,0,0}, {0,0,0,0}, {1,1,1,0}, {0,0,1,0} }, { {0,0,0,0}, {0,0,1,0}, {0,0,1,0}, {0,1,1,0} }, { {0,0,0,0}, {0,0,0,0}, {1,0,0,0}, {1,1,1,0} }, { {0,0,0,0}, {0,0,0,0}, {0,1,1,0}, {1,1,0,0} }, { {0,0,0,0}, {0,1,0,0}, {0,1,1,0}, {0,0,1,0} }, { {0,0,0,0}, {0,0,0,0}, {1,1,0,0}, {0,1,1,0} }, { {0,0,0,0}, {0,0,1,0}, {0,1,1,0}, {0,1,0,0} }, { {0,0,0,0}, {0,0,0,0}, {0,1,0,0}, {1,1,1,0} }, { {0,0,0,0}, {0,1,0,0}, {0,1,1,0}, {0,1,0,0} }, { {0,0,0,0}, {0,0,0,0}, {1,1,1,0}, {0,1,0,0} }, { {0,0,0,0}, {0,1,0,0}, {1,1,0,0}, {0,1,0,0} }, { {0,0,0,0}, {1,1,0,0}, {0,1,0,0}, {0,1,0,0} }, { {0,0,0,0}, {0,0,0,0}, {0,0,1,0}, {1,1,1,0} }, { {0,0,0,0}, {0,1,0,0}, {0,1,0,0}, {0,1,1,0} }, { {0,0,0,0}, {0,0,0,0}, {1,1,1,0}, {1,0,0,0} } }; /// Try to rotate the current block static void RotateBlock(const int x, const int y) { // skip to next line in index array int temp = currentBlock + BLOCK_TYPES; // adjust on overflow if (temp >= BLOCK_TYPES*ROTATIONS) { temp -= BLOCK_TYPES*ROTATIONS; } // test if we can do this if (CanPutBlock(x, y, blocks[blockIndex[temp]])) { currentBlock = temp; } }
I'd always choose the simple solution over the most elegant first.