public bool Rotate(bool left) { int oldX = X, oldY = Y; IEnumerable <Tuple <int, int> > reindex = new LinkedList <Tuple <int, int> >(); //erase old location for (int x = 0; x < 4; x++) { for (int y = 0; y < 4; y++) { if (Structure[x, y]) { TetrisState.Grid[x + X, y + Y] = Color.Transparent; ((LinkedList <Tuple <int, int> >)reindex).AddLast(Tuple.Create(x + X, y + Y)); } } } bool[,] candidateStructure = Rotate(Structure, left ? 1 : 3); //check wall collisions, and try to correct for them //is there a left collision? int leftBound = FindLeftSurfaces(candidateStructure).Min(); if (X + leftBound < 0) { X = -leftBound - 1; } int rightBound = FindRightSurfaces(candidateStructure).Max(); if (X + rightBound >= Tetris.Width) { X = Tetris.Width - rightBound; } int bottomBound = FindBottomSurfaces(candidateStructure).Max(); if (Y + bottomBound >= Tetris.Height) { Y = Tetris.Height - bottomBound; } //check block intersections bool failure = false; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (candidateStructure[i, j] && TetrisState.Grid[i + X, j + Y] != Color.Transparent) { failure = true; break; } } } if (!failure) { //we have a valid location, put the tetromino there #if diagnose Debug.WriteLine("tetra rotating"); #endif if (oldX != X || oldY != Y) { reindex = TetrisState.ReIndexTransitions(reindex, t => Tuple.Create(t.Item1 + (X - oldX), t.Item2 + (Y - oldY))); TranslationTransition.FromTetromino(TetrisState, this, oldX, oldY, TransitionTime); } Structure = candidateStructure; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (Structure[i, j]) { TetrisState.Grid[i + X, j + Y] = Color; } } } TetrisState.ReIndexTransitions(reindex, t => { Tuple <int, int> transformed = Tetromino.RotationTransform(Tuple.Create(t.Item1 - X, t.Item2 - Y), left ? 1 : 3); return(Tuple.Create(transformed.Item1 + X, transformed.Item2 + Y)); }); RotationTransition.FromTetromino(TetrisState, this, left ? -1 : 1, TransitionTime); return(true); } else { X = oldX; Y = oldY; for (int x = 0; x < 4; x++) { for (int y = 0; y < 4; y++) { if (Structure[x, y]) { TetrisState.Grid[x + X, y + Y] = Color; } } } return(false); } }