public OptimizedCell GetCellFromDirection(OptimizedCell firstCell, OptimizedCell secondCell) { OptimizedCell destinationCell; //middle up if (firstCell.y == secondCell.y && (firstCell.x + 2) == secondCell.x) { destinationCell = GetCell(secondCell.x + 2, secondCell.y); if (destinationCell != null) { return(destinationCell); } } //middle down if (firstCell.y == secondCell.y && (firstCell.x - 2) == secondCell.x) { destinationCell = GetCell(secondCell.x - 2, secondCell.y); if (destinationCell != null) { return(destinationCell); } } //middle left if (firstCell.y - 1 == secondCell.y && firstCell.x == secondCell.x) { destinationCell = GetCell(secondCell.x, secondCell.y - 1); if (destinationCell != null) { return(destinationCell); } } //middle right if (firstCell.y + 1 == secondCell.y && firstCell.x == secondCell.x) { destinationCell = GetCell(secondCell.x, secondCell.y + 1); if (destinationCell != null) { return(destinationCell); } } int offsetFirstCell = firstCell.x % 2; int offsetSecondCell = secondCell.x % 2; //top left if ((firstCell.y - 1 + offsetFirstCell) == secondCell.y && (firstCell.x + 1) == secondCell.x) { destinationCell = GetCell(secondCell.x + 1, secondCell.y - 1 + offsetSecondCell); if (destinationCell != null) { return(destinationCell); } } //top right if ((firstCell.y + offsetFirstCell) == secondCell.y && (firstCell.x + 1) == secondCell.x) { destinationCell = GetCell(secondCell.x + 1, secondCell.y + offsetSecondCell); if (destinationCell != null) { return(destinationCell); } } //bottom left if ((firstCell.y - 1 + offsetFirstCell) == secondCell.y && (firstCell.x - 1) == secondCell.x) { destinationCell = GetCell(secondCell.x - 1, secondCell.y - 1 + offsetSecondCell); if (destinationCell != null) { return(destinationCell); } } //bottom right if ((firstCell.y + offsetFirstCell) == secondCell.y && (firstCell.x - 1) == secondCell.x) { destinationCell = GetCell(secondCell.x - 1, secondCell.y + offsetSecondCell); if (destinationCell != null) { return(destinationCell); } } return(null); }
public List <Move> GetAvailableMoves(CellColor color, bool enableJump = true, bool canOnlyJump = false) { List <Move> moves = new List <Move>(); int ballsLeft, i, j = 0; if (color == CellColor.Black) { ballsLeft = blackBallsLeft.Count; } else { ballsLeft = whiteBallsLeft.Count; } if (ballsLeft == 0) { for (i = 0; i < Cells.Length; ++i) { for (j = 0; j < Cells[i].Length; ++j) { if (Cells[i][j].color == color) { OptimizedCell actualCell = new OptimizedCell(); actualCell.x = i; actualCell.y = j; actualCell.color = color; List <OptimizedCell> cells = GetCellNeighbours(actualCell); foreach (OptimizedCell cell in cells) { if (cell.color == CellColor.None) { if (canOnlyJump) { continue; } Move move = new Move(); move.fromX = actualCell.x; move.fromY = actualCell.y; move.toX = cell.x; move.toY = cell.y; move.color = actualCell.color; move.isPoint = cell.isPoint; moves.Add(move); } else if (cell.color != CellColor.NOT_A_CELL && enableJump) { OptimizedCell potentialCell = GetCellFromDirection(actualCell, cell); if (potentialCell != null && potentialCell.color == CellColor.None) { Move move = new Move(); move.fromX = actualCell.x; move.fromY = actualCell.y; move.toX = potentialCell.x; move.toY = potentialCell.y; move.color = actualCell.color; move.isPoint = actualCell.isPoint; moves.Add(move); } } } } } } } else { for (i = 0; i < cells.Length; ++i) { for (j = 0; j < cells[i].Length; ++j) { if (cells[i][j].color == CellColor.None) { Move move = new Move(); move.fromX = -1; move.fromY = -1; move.toX = i; move.toY = j; move.color = color; move.isPoint = ((ballsLeft % 2) == 0) ? false : true; moves.Add(move); } } } } return(moves); }
public List <OptimizedCell> GetCellNeighbours(OptimizedCell cell) { List <OptimizedCell> neighbours = new List <OptimizedCell>(); OptimizedCell neighbour = new OptimizedCell(); //middle up neighbour = GetCell(cell.x + 2, cell.y); if (neighbour != null) { neighbours.Add(neighbour); } //middle down neighbour = GetCell(cell.x - 2, cell.y); if (neighbour != null) { neighbours.Add(neighbour); } //middle left neighbour = GetCell(cell.x, cell.y - 1); if (neighbour != null) { neighbours.Add(neighbour); } //middle right neighbour = GetCell(cell.x, cell.y + 1); if (neighbour != null) { neighbours.Add(neighbour); } int offset = cell.x % 2; //top left neighbour = GetCell(cell.x + 1, cell.y - 1 + offset); if (neighbour != null) { neighbours.Add(neighbour); } //top right neighbour = GetCell(cell.x + 1, cell.y + offset); if (neighbour != null) { neighbours.Add(neighbour); } //bottom left neighbour = GetCell(cell.x - 1, cell.y - 1 + offset); if (neighbour != null) { neighbours.Add(neighbour); } //bottom right neighbour = GetCell(cell.x - 1, cell.y + offset); if (neighbour != null) { neighbours.Add(neighbour); } return(neighbours); }