protected Figure ExecuteCommand(Commander.CommandType command, Figure figure, PlayingField playingField) { switch (command) { case Commander.CommandType.A: figure = figure.ShiftXLeft(); break; case Commander.CommandType.D: figure = figure.ShiftXRight(); break; case Commander.CommandType.S: figure = figure.ShiftYDown(); break; case Commander.CommandType.Q: figure = figure.RotateCounterClockwise(); break; case Commander.CommandType.E: figure = figure.RotateClockwise(); break; case Commander.CommandType.P: var fieldOut = playingField.Field.Select(row => row.ToArray()).ToArray(); foreach (var cell in figure.Cells) fieldOut[cell.ProjectionY][cell.ProjectionX] = PlayingField.StateCoord.CurrentFigure; foreach (var row in fieldOut) { foreach (var cell in row) Console.Write ( cell == PlayingField.StateCoord.Free ? '.' : cell == PlayingField.StateCoord.Busy ? '#' : '*' ); Console.Write(Environment.NewLine); } break; } if (СheckCollision(figure, playingField)) throw new CollisionException(); return figure; }
protected Figure ProjectNewFigure(Figure figure, PlayingField playingField) { int rightEndFigure = figure.Cells[0].X; foreach (var cell in figure.Cells.Skip(1)) if (rightEndFigure < cell.X) rightEndFigure = cell.X; int leftEndFigure = figure.Cells[0].X; foreach (var cell in figure.Cells.Skip(1)) if (leftEndFigure > cell.X) leftEndFigure = cell.X; int lenFigure = rightEndFigure - leftEndFigure + 1; int positionOffsetLeft = (playingField.CountX - lenFigure) / 2; int upEndFigure = figure.Cells[0].Y; foreach (var cell in figure.Cells.Skip(1)) if (cell.Y < upEndFigure) upEndFigure = cell.Y; var newFigure = figure.ShiftXRight(Math.Abs(leftEndFigure) + positionOffsetLeft).ShiftYDown(Math.Abs(upEndFigure)); if (СheckCollision(newFigure, playingField)) throw new CollisionException(); return newFigure; }