public Piece(Cell[] cells,int centralCellIndex = -1) { this.cells = ImmutableArray.Create(cells); this.centralCellIndex = centralCellIndex == -1 ? FindCentralCellIndex() : centralCellIndex; minX = int.MaxValue; minY = int.MaxValue; maxX = int.MinValue; maxY = int.MinValue; foreach (var point in cells) { //!!!!!!!!!!!!!!! if (minX > point.X) minX = point.X; if (maxX < point.X) maxX = point.X; if (minY > point.Y) minY = point.Y; if (maxY < point.Y) maxY = point.Y; } }
public void InitTheGrid() { for (int i = 0; i < HorizontalMax;i++) { for (int j = 0; j < VerticalMax; j++) { cells[i,j] = new Cell(); } } for (int i = 0; i < HorizontalMax; i++) { for (int j = 0; j < VerticalMax; j++) { if (j + 1 < VerticalMax) { cells[i, j].Up = cells[i, j + 1]; } if(j - 1 >= 0) { cells[i, j].Down = cells[i, j - 1]; } if( i + 1 < HorizontalMax) { cells[i, j].Right = cells[i + 1, j]; } if(i - 1 >= 0) { cells[i, j].Left = cells[i - 1, j]; } } } return; }
private Step(Step step, Tuple<IEnumerable<Cell>, int> pointsNCells) { height = step.height; width = step.width; commandIndex = step.commandIndex + 1; points = step.points + pointsNCells.Item2; Pieces = step.Pieces; CurrentPieceCells = Pieces[step.pieceIndex].Cells; UsedCells = pointsNCells.Item1.ToImmutableHashSet(); Center = GetShift(CurrentPieceCells); RunningCells = CurrentPieceCells.Select(c => new Cell(c + Center)).ToImmutableHashSet(); pieceIndex = (step.pieceIndex + 1) % step.Pieces.Count; }
private Step(Step step, IEnumerable<Cell> cells, IEnumerable<Cell> rotateCells, Cell newCenter) { height = step.height; width = step.width; pieceIndex = step.pieceIndex; commandIndex = step.commandIndex + 1; points = step.points; Center = newCenter; Pieces = step.Pieces; CurrentPieceCells = rotateCells.ToImmutableList(); UsedCells = step.UsedCells; RunningCells = cells.ToImmutableHashSet(); }
private Step(Step step) { height = step.height; width = step.width; pieceIndex = step.pieceIndex; commandIndex = step.commandIndex + 1; points = step.points; Center = step.Center; Pieces = step.Pieces; CurrentPieceCells = step.CurrentPieceCells; UsedCells = step.UsedCells; RunningCells = step.RunningCells; }
public Step(Field field) { height = field.Height; width = field.Width; UsedCells = ImmutableHashSet<Cell>.Empty; Pieces = field.Pieces; CurrentPieceCells = Pieces[0].Cells; Center = GetShift(CurrentPieceCells); RunningCells = CurrentPieceCells.Select(c => new Cell(c + Center)).ToImmutableHashSet(); pieceIndex = 1 % Pieces.Count; commandIndex = 0; points = 0; }
/// <summary> /// Draws the empty Tetris board /// </summary> private void drawBackgroundBoard() { foreach (KeyValuePair<string, Cell> c in boardCells) { c.Value.Dispose(); } boardCells.Clear(); for ( int row = 0; row < numRows; row++) { for (int col = 0; col < numCols; col++) { Cell cell = new Cell(row, col); cell.Parent = gameGrid; cell.Top = row * cellSize; cell.Left = col * cellSize; boardCells.Add(cellKey(row, col), cell); } } }
public FieldRenderer(GameWindow window, Vector3 position) : base(window) { this.width = 10; this.height = 20; this.tetrionBlock = -new Vector3(-(width / 2) + .5f, -(height / 2) + .5f, 0.0f); this.position = Vector3.Add(position, -tetrionBlock); shownCells = new Cell[width, height]; committedCells = new Cell[width, height]; UpdateUI = true; for (int x = 0; x < width; x++) for (int y = 0; y < height; y++) { committedCells[x, y] = new Cell(); shownCells[x, y] = new Cell(); } }
private Step Move(Cell to) { var cells = RunningCells.Select(c => c + to).ToList(); if (CheckBorders(cells) ) return new Step(this, cells, CurrentPieceCells, Center + to); return CollisionWork(); }
private Pieces CentralizedPieces(ImmutableArray<Piece> piecesArray,int width) { Piece[] centralizedPieces = new Piece[piecesArray.Length]; for (int i = 0; i < centralizedPieces.Length; i++) { int xShift = (width - (piecesArray[i].MaxX - piecesArray[i].MinX + 1)) / 2 - piecesArray[i].MinX; int yShift = piecesArray[i].MinY; Cell[] centralizedCells = new Cell[piecesArray[i].Cells.Length]; for (int j = 0; j < centralizedCells.Length; j++) { centralizedCells[j] = piecesArray[i].Cells[j].MoveTowards(xShift).MoveDown(-yShift); } centralizedPieces[i] = new Piece(centralizedCells,piecesArray[i].FindCentralCellIndex()); } return new Pieces(ImmutableArray.Create(centralizedPieces)); }
public override string ToString() { StringBuilder fieldInfo = new StringBuilder(); for (int i = 0; i < gameField.Length; i++) { for (int j = 0; j < width; j++) { Cell cell = new Cell(j, i); fieldInfo.Append((!gameField[i].Contains(cell) ? "." : gameField[i][gameField[i].IndexOf(cell)].ToString()) + " ");// gameField[i][j].ToString() } fieldInfo.Append("\n"); } return fieldInfo.ToString(); }
private Cell ReflectedCell(Cell cellToReflect,bool turnRight) { int cci = centralCellIndex; int reverse = turnRight ? 1 : -1; return new Cell(cells[cci].X - (cellToReflect.Y - cells[cci].Y) * reverse, cells[cci].Y + (cellToReflect.X - cells[cci].X) * reverse,true); }
public Piece TurnFigure(int width,int height,bool turnRight) { Cell[] turnedPoints = new Cell[cells.Length]; turnedPoints[centralCellIndex] = cells[centralCellIndex]; for (int i = 0; i < cells.Length; i++) { if (i == centralCellIndex) continue; turnedPoints[i] = ReflectedCell(cells[i], turnRight); if (turnedPoints[i].X >= width || turnedPoints[i].X < 0 || turnedPoints[i].Y >= height || turnedPoints[i].Y < 0) return null; } return new Piece(turnedPoints,centralCellIndex); }
public Piece MoveTowards(int deltaX,int width) { Cell[] movedPoints = new Cell[cells.Length]; for (int i = 0; i < cells.Length; i++) { movedPoints[i] = cells[i].MoveTowards(deltaX); if (movedPoints[i].X >= width || movedPoints[i].X < 0) return null; } return new Piece(movedPoints,centralCellIndex); }
public Piece MoveDown(int deltaY,int height) { Cell[] movedPoints = new Cell[cells.Length]; for (int i = 0; i < cells.Length; i++) { movedPoints[i] = cells[i].MoveDown(deltaY); if (movedPoints[i].Y >= height || movedPoints[i].Y < 0) return null; } return new Piece(movedPoints,centralCellIndex); }
public Cell(Cell other) { X = other.X; Y = other.Y; }
private bool CheckCell(Cell cell) { return !(cell.X < 0 || cell.X >= width || cell.Y < 0 || cell.Y >= height); }
public GameLine(Cell[] mutableLineCells, int cellsCount, int width) { lineCells = ImmutableArray.Create(mutableLineCells); this.cellsCount = cellsCount; this.width = width; }
private void PrintField() { for (int i = 0; i < height; ++i) { for (int j = 0; j < width; ++j) { var cell = new Cell(j, i); if (UsedCells.Contains(cell)) Console.Write('#'); else if (RunningCells.Contains(cell)) Console.Write('*'); else Console.Write('.'); } Console.WriteLine(); } }
public void Flip() { rotation++; if (rotation == 4) { rotation = 0; } switch (blockType) { case Blocks.I_Block: switch (rotation) { case 0: cells[0] = new Cell(0 + positionX, -1 + positionY); cells[1] = new Cell(-1 + positionX, -1 + positionY); cells[2] = new Cell(1 + positionX, -1 + positionY); cells[3] = new Cell(2 + positionX, -1 + positionY); foreach (Cell cell in cells) { cell.FillColor = Brushes.LightBlue; } break; case 1: cells[0] = new Cell(1 + positionX, 0 + positionY); cells[1] = new Cell(1 + positionX, 1 + positionY); cells[2] = new Cell(1 + positionX, -1 + positionY); cells[3] = new Cell(1 + positionX, 2 + positionY); foreach (Cell cell in cells) { cell.FillColor = Brushes.LightBlue; } break; case 2: cells[0] = new Cell(0 + positionX, 1 + positionY); cells[1] = new Cell(1 + positionX, 1 + positionY); cells[2] = new Cell(-1 + positionX, 1 + positionY); cells[3] = new Cell(-2 + positionX, 1 + positionY); foreach (Cell cell in cells) { cell.FillColor = Brushes.LightBlue; } break; case 3: cells[0] = new Cell(-1 + positionX, 0 + positionY); cells[1] = new Cell(-1 + positionX, -1 + positionY); cells[2] = new Cell(-1 + positionX, 1 + positionY); cells[3] = new Cell(-1 + positionX, 2 + positionY); foreach (Cell cell in cells) { cell.FillColor = Brushes.LightBlue; } break; } break; case Blocks.J_Block: switch (rotation) { case 0: cells[0] = new Cell(0 + positionX, 0 + positionY); cells[1] = new Cell(-1 + positionX, 0 + positionY); cells[2] = new Cell(1 + positionX, 0 + positionY); cells[3] = new Cell(1 + positionX, 1 + positionY); foreach (Cell cell in cells) { cell.FillColor = Brushes.Blue; } break; case 1: cells[0] = new Cell(0 + positionX, 0 + positionY); cells[1] = new Cell(0 + positionX, -1 + positionY); cells[2] = new Cell(0 + positionX, 1 + positionY); cells[3] = new Cell(-1 + positionX, 1 + positionY); foreach (Cell cell in cells) { cell.FillColor = Brushes.Blue; } break; case 2: cells[0] = new Cell(0 + positionX, 0 + positionY); cells[1] = new Cell(1 + positionX, 0 + positionY); cells[2] = new Cell(-1 + positionX, 0 + positionY); cells[3] = new Cell(-1 + positionX, -1 + positionY); foreach (Cell cell in cells) { cell.FillColor = Brushes.Blue; } break; case 3: cells[0] = new Cell(0 + positionX, 0 + positionY); cells[1] = new Cell(0 + positionX, 1 + positionY); cells[2] = new Cell(0 + positionX, -1 + positionY); cells[3] = new Cell(1 + positionX, -1 + positionY); foreach (Cell cell in cells) { cell.FillColor = Brushes.Blue; } break; } break; case Blocks.L_Block: switch (rotation) { case 0: cells[0] = new Cell(0 + positionX, 0 + positionY); cells[1] = new Cell(-1 + positionX, 0 + positionY); cells[2] = new Cell(1 + positionX, 0 + positionY); cells[3] = new Cell(-1 + positionX, 1 + positionY); foreach (Cell cell in cells) { cell.FillColor = Brushes.Orange; } break; case 1: cells[0] = new Cell(0 + positionX, 0 + positionY); cells[1] = new Cell(0 + positionX, 1 + positionY); cells[2] = new Cell(0 + positionX, -1 + positionY); cells[3] = new Cell(-1 + positionX, -1 + positionY); foreach (Cell cell in cells) { cell.FillColor = Brushes.Orange; } break; case 2: cells[0] = new Cell(0 + positionX, 0 + positionY); cells[1] = new Cell(-1 + positionX, 0 + positionY); cells[2] = new Cell(1 + positionX, 0 + positionY); cells[3] = new Cell(1 + positionX, -1 + positionY); foreach (Cell cell in cells) { cell.FillColor = Brushes.Orange; } break; case 3: cells[0] = new Cell(0 + positionX, 0 + positionY); cells[1] = new Cell(0 + positionX, -1 + positionY); cells[2] = new Cell(0 + positionX, 1 + positionY); cells[3] = new Cell(1 + positionX, 1 + positionY); foreach (Cell cell in cells) { cell.FillColor = Brushes.Orange; } break; } break; case Blocks.T_Block: switch (rotation) { case 0: cells[0] = new Cell(0 + positionX, 0 + positionY); cells[1] = new Cell(-1 + positionX, 1 + positionY); cells[2] = new Cell(0 + positionX, 1 + positionY); cells[3] = new Cell(1 + positionX, 1 + positionY); foreach (Cell cell in cells) { cell.FillColor = Brushes.Purple; } break; case 1: cells[0] = new Cell(0 + positionX, 0 + positionY); cells[1] = new Cell(-1 + positionX, 0 + positionY); cells[2] = new Cell(-1 + positionX, 1 + positionY); cells[3] = new Cell(-1 + positionX, -1 + positionY); foreach (Cell cell in cells) { cell.FillColor = Brushes.Purple; } break; case 2: cells[0] = new Cell(0 + positionX, 0 + positionY); cells[1] = new Cell(0 + positionX, -1 + positionY); cells[2] = new Cell(1 + positionX, -1 + positionY); cells[3] = new Cell(-1 + positionX, -1 + positionY); foreach (Cell cell in cells) { cell.FillColor = Brushes.Purple; } break; case 3: cells[0] = new Cell(0 + positionX, 0 + positionY); cells[1] = new Cell(1 + positionX, 0 + positionY); cells[2] = new Cell(1 + positionX, 1 + positionY); cells[3] = new Cell(1 + positionX, -1 + positionY); foreach (Cell cell in cells) { cell.FillColor = Brushes.Purple; } break; } break; case Blocks.S_Block: switch (rotation) { case 0: cells[0] = new Cell(0 + positionX, 0 + positionY); cells[1] = new Cell(0 + positionX, 1 + positionY); cells[2] = new Cell(-1 + positionX, 1 + positionY); cells[3] = new Cell(1 + positionX, 0 + positionY); foreach (Cell cell in cells) { cell.FillColor = Brushes.Red; } break; case 1: cells[0] = new Cell(0 + positionX, 0 + positionY); cells[1] = new Cell(-1 + positionX, 0 + positionY); cells[2] = new Cell(0 + positionX, 1 + positionY); cells[3] = new Cell(-1 + positionX, -1 + positionY); foreach (Cell cell in cells) { cell.FillColor = Brushes.Red; } break; case 2: cells[0] = new Cell(-1 + positionX, 0 + positionY); cells[1] = new Cell(0 + positionX, 0 + positionY); cells[2] = new Cell(0 + positionX, -1 + positionY); cells[3] = new Cell(1 + positionX, -1 + positionY); foreach (Cell cell in cells) { cell.FillColor = Brushes.Red; } break; case 3: cells[0] = new Cell(-1 + positionX, 0 + positionY); cells[1] = new Cell(0 + positionX, 0 + positionY); cells[2] = new Cell(-1 + positionX, -1 + positionY); cells[3] = new Cell(0 + positionX, 1 + positionY); foreach (Cell cell in cells) { cell.FillColor = Brushes.Red; } break; } break; case Blocks.Z_Block: switch (rotation) { case 0: cells[0] = new Cell(0 + positionX, 0 + positionY); cells[1] = new Cell(0 + positionX, 1 + positionY); cells[2] = new Cell(-1 + positionX, 0 + positionY); cells[3] = new Cell(1 + positionX, 1 + positionY); foreach (Cell cell in cells) { cell.FillColor = Brushes.Green; } break; case 1: cells[0] = new Cell(1 + positionX, 0 + positionY); cells[1] = new Cell(0 + positionX, 0 + positionY); cells[2] = new Cell(0 + positionX, 1 + positionY); cells[3] = new Cell(1 + positionX, -1 + positionY); foreach (Cell cell in cells) { cell.FillColor = Brushes.Green; } break; case 2: cells[0] = new Cell(0 + positionX, 1 + positionY); cells[1] = new Cell(0 + positionX, 0 + positionY); cells[2] = new Cell(1 + positionX, 1 + positionY); cells[3] = new Cell(-1 + positionX, 0 + positionY); foreach (Cell cell in cells) { cell.FillColor = Brushes.Green; } break; case 3: cells[0] = new Cell(0 + positionX, 0 + positionY); cells[1] = new Cell(1 + positionX, 0 + positionY); cells[2] = new Cell(1 + positionX, -1 + positionY); cells[3] = new Cell(0 + positionX, 1 + positionY); foreach (Cell cell in cells) { cell.FillColor = Brushes.Green; } break; } break; } }