public StepResult PutShape(TypeShape shape, int x, int y, bool checkEmpty = true) { if (x >= countCellWidth || y >= countCellHeight || x < 0 || y < 0) { return(StepResult.Illegal); } if (checkEmpty && fields[y, x] != EmptyCell && shape != TypeShape.Empty) { return(StepResult.Illegal); } if (shape == TypeShape.Empty) { return(RemoveShape(x, y)); } TypeShape[] newShapes = new TypeShape[Shapes.Length + 1]; byte newIndex = (byte)(newShapes.Length - 1); if (newIndex == 0) { currentShapeLocation = new ShapeLocation(x, y); } Shapes.CopyTo(newShapes, 0); newShapes[newIndex] = (TypeShape)shape; fields[y, x] = newIndex; Shapes = newShapes; return(StepResult.Ok); }
public void Deserialize(byte[] binBoard) { for (byte y = 0; y < countCellHeight; y++) { for (byte x = 0; x < countCellWidth; x++) { fields[y, x] = EmptyCell; } } if (binBoard.Length == 0) { return; } if (binBoard.Length % 3 != 0) { throw new Exception("Неверный формат доски"); } int shapeCount = binBoard.Length / 3; Shapes = new TypeShape[shapeCount]; for (byte i = 0; i < shapeCount; i++) { Shapes[i] = (TypeShape)binBoard[i * 3 + 2]; fields[binBoard[i * 3 + 1], binBoard[i * 3]] = i; } currentShapeLocation = new ShapeLocation(binBoard[0], binBoard[1]); }
public double Score(ShapeLocation location) { double dx = countCellWidth - 1 - location.X; double dy = countCellHeight - 1 - location.Y; double maxd = Math.Sqrt((countCellWidth - 1) * (countCellWidth - 1) + (countCellHeight - 1) * (countCellHeight - 1)); int neighborsCount = GetNeighbours(location).Count; return(Math.Round(maxd - Math.Sqrt(dx * dx + dy * dy)) * 10 + neighborsCount); }
public StepResult MoveShape(int x, int y) { if (x >= countCellWidth || y >= countCellHeight || x < 0 || y < 0) { return(StepResult.Illegal); } if (fields[y, x] != EmptyCell) { return(StepResult.Illegal); } ShapeLocation locCur = currentShapeLocation; fields[y, x] = (byte)(Shapes.Length - 1); fields[currentShapeLocation.Y, currentShapeLocation.X] = EmptyCell; TypeShape curShape = Shapes[0]; int i; for (i = 0; i <= Shapes.Length - 2; i++) { Shapes[i] = Shapes[i + 1]; } Shapes[Shapes.Length - 1] = curShape; int x1, y1; for (y1 = 0; y1 < countCellHeight; y1++) { for (x1 = 0; x1 < countCellWidth; x1++) { i = fields[y1, x1]; if (i != EmptyCell) { if (i <= Shapes.Length - 1 && !(y == y1 && x == x1)) { fields[y1, x1]--; if (i == 1) { currentShapeLocation = new ShapeLocation(x1, y1); } } } } } return(StepResult.Ok); }
public StepResult RemoveShape(int x, int y) { byte removedShapeIndex = fields[y, x]; if (removedShapeIndex == EmptyCell) { return(StepResult.Ok); } if (removedShapeIndex == 0) { IsNextStepped = true; } TypeShape[] newShapes = new TypeShape[Shapes.Length - 1]; int i, ni = 0; for (i = 0; i < Shapes.Length; i++) { if (i != removedShapeIndex) { newShapes[ni++] = Shapes[i]; } } Shapes = newShapes; fields[y, x] = EmptyCell; for (y = 0; y < countCellHeight; y++) { for (x = 0; x < countCellWidth; x++) { ni = fields[y, x]; if (ni != EmptyCell) { if (ni > removedShapeIndex) { fields[y, x]--; } // Если удалили текущую фигуру if (removedShapeIndex == 0 && ni == 1) { currentShapeLocation = new ShapeLocation(x, y); } } } } return(StepResult.Ok); }
public virtual List <ShapeLocation> GetNeighbours(ShapeLocation curLocation) { List <ShapeLocation> neighbors = new List <ShapeLocation>(); int NewLocX; int NewLocY; foreach (StepOffset offset in neighborOffsets) { NewLocX = curLocation.X + offset.X; NewLocY = curLocation.Y + offset.Y; if (NewLocX < countCellWidth && NewLocY < countCellHeight && NewLocX >= 0 && NewLocY >= 0) { if (fields[NewLocY, NewLocX] != EmptyCell) { neighbors.Add(new ShapeLocation(NewLocX, NewLocY)); } } } return(neighbors); }