static List <Point> GetCellLineComboItems(Match3Map actualMap, List <Point[]> bangLines, int targetBangLineLength) { var actualBangLines = bangLines.Where(line => line.Length == targetBangLineLength).ToArray(); var result = new List <Point>(); for (int i = 0; i < actualBangLines.Count(); i++) { if (actualBangLines[i] == null || actualBangLines[i].Length < 2 || actualBangLines[i][0].y != actualBangLines[i][1].y) { continue; } var bangLineY = GetBangLineCellsLine(actualMap, actualBangLines[i]); var possibleCells = new Point[actualMap.Width]; for (int j = 0; j < possibleCells.Length; j++) { possibleCells[j] = new Point(j, bangLineY); } possibleCells = possibleCells .Where(cell => !actualBangLines[i].Contains(cell)) .ToArray(); result.AddRange(possibleCells); } return(result); }
static List <Point> GetCellTypeComboItems( Match3Map actualMap , List <Point[]> bangLines , int targetBangLineLength ) { var actualBangLines = bangLines .Where(line => line.Length == targetBangLineLength) .ToArray(); var result = new List <Point>(); for (int i = 0; i < actualBangLines.Count(); i++) { var bangLineCellsType = GetBangLineCellsType(actualMap, actualBangLines[i]); var possibleCells = actualMap.GetCellIndexesByType(bangLineCellsType); possibleCells = possibleCells .Where(cell => !actualBangLines[i].Contains(cell)) .ToList(); result.AddRange(possibleCells); } return(result); }
static List <Point[]> GetAllVerticalBangLines(Match3Map actualMap) { var result = GetAllHorizontalBangLines(actualMap.TransposeMap()); return(result .Select(bangLine => Utils.GetPointsForTransposedMatrix(bangLine)) .ToList()); }
public static List <Point[]> GetAllBangLines(Match3Map actualMap) { var bangLines = new List <Point[]>(); bangLines.AddRange(GetAllHorizontalBangLines(actualMap)); bangLines.AddRange(GetAllVerticalBangLines(actualMap)); return(bangLines); }
static void MakeAllSteps(Match3Map actualMap, Point startPoint) { for (int x = startPoint.x; x < actualMap.Width - 1; x += Constants.checkingStep) { for (int y = startPoint.y; y < actualMap.Height; y += Constants.checkingStep) { actualMap.SwapCells(new Point(x, y), new Point(x + 1, y)); } } }
static List <Point> GetCellColumnComboItems(Match3Map actualMap, List <Point[]> bangLines, int targetBangLineLength) { var transposedMap = actualMap.TransposeMap(); var transposedBangLines = bangLines .Select(line => Utils.GetPointsForTransposedMatrix(line)) .ToList(); var result = GetCellLineComboItems(transposedMap, transposedBangLines, targetBangLineLength); return(Utils.GetPointsForTransposedMatrix(result)); }
public static List <Point> GetComboBangItems(Match3Map actualMap, List <Point[]> bangLines) { var result = new List <Point>(); result.AddRange(GetCellColumnComboItems(actualMap, bangLines, Constants.lineComboItemsLenght)); result.AddRange(GetCellLineComboItems(actualMap, bangLines, Constants.lineComboItemsLenght)); result.AddRange(GetCellTypeComboItems(actualMap, bangLines, Constants.cellTypeComboItemsLenght)); result = result.Distinct().ToList(); return(result); }
static List <Point[]> GetVerticalHints(Match3Map actualMap) { var hints = GetHorizontalHints(actualMap.TransposeMap()); var result = new List <Point[]>(); for (int i = 0; i < hints.Count; i++) { result.Add(Utils.GetPointsForTransposedMatrix(hints[i])); } return(result); }
public static void ResetMap(Match3Map actualMap, int cellTypesCount) { for (int i = 0; i < actualMap.Width; i++) { for (int j = 0; j < actualMap.Height; j++) { actualMap.DestroyCell(new Point(i, j)); } } FillAllEmptyCells(actualMap, cellTypesCount); }
public static void FillAllEmptyCells(Match3Map actualMap, int cellTypesCount) { for (int i = 0; i < actualMap.Width; i++) { for (int j = actualMap.Height - 1; j > -1; j--) { if (actualMap[i, j] == null) { actualMap.CreateCell(new Point(i, j), Utils.GetRandomValue(cellTypesCount - 1)); } } } }
static Point[] GetHint(Match3Map gameMap) { var hints = new List <Point[]>(); hints.AddRange(GetHorizontalHints(gameMap)); hints.AddRange(GetVerticalHints(gameMap)); if (hints.Count > 0) { var resultIndex = Utils.GetRandomValue(hints.Count - 1); return(hints[resultIndex]); } return(null); }
public Match3Map(Match3Map templateMap) { this.map = new Match3MapItem[templateMap.Width, templateMap.Height]; for (int i = 0; i < this.Width; i++) { for (int j = 0; j < this.Height; j++) { if (templateMap.map[i, j] != null) { this.map[i, j] = new Match3MapItem(templateMap.map[i, j]); } } } }
public Match3Map TransposeMap() { var result = new Match3Map(this.Height, this.Width); for (int i = 0; i < this.Width; i++) { for (int j = 0; j < this.Height; j++) { if (this.map[i, j] != null) { result.map[j, i] = new Match3MapItem(this.map[i, j]); } } } return(result); }
public static void DestroyStepCells( Match3Map actualMap , List <Point[]> bangLines , List <Point> comboCells ) { for (int i = 0; i < bangLines.Count; i++) { for (int j = 0; j < bangLines[i].Length; j++) { actualMap.DestroyCell(bangLines[i][j]); } } for (int i = 0; i < comboCells.Count; i++) { actualMap.DestroyCell(comboCells[i]); } }
static List <Point[]> GetAllHorizontalBangLines(Match3Map actualMap) { var result = new List <Point[]>(); for (int i = 0; i < actualMap.Width - 2; i++) { for (int j = 0; j < actualMap.Height; j++) { if (actualMap[i, j] == null || actualMap[i, j].IsBlocked) { continue; } var startPointType = actualMap[i, j].ItemType; int lineLength = 0; while ( actualMap.Width > i + lineLength && actualMap[i + lineLength, j].ItemType == startPointType ) { lineLength++; } if (lineLength < 3) { continue; } var bangLine = new Point[lineLength]; for (int k = 0; k < lineLength; k++) { bangLine[k] = new Point(i + k, j); } result.Add(bangLine); } } return(result); }
static bool IsAnyHorizontalStepsEffective(Match3Map actualMap) { for (int i = 0; i < Constants.checkingStep; i++) { for (int j = 0; j < Constants.checkingStep; j++) { var mapCopy = new Match3Map(actualMap); MakeAllSteps(mapCopy, new Point(i, j)); var bangLines = Match3BangController.GetAllBangLines(mapCopy); if (bangLines.Count > 0) { return(true); } } } return(false); }
static int GetBangLineCellsLine(Match3Map actualMap, Point[] bangLine) { var bangLineLines = new int[bangLine.Length]; for (int i = 0; i < bangLine.Length; i++) { bangLineLines[i] = bangLine[i].y; } var possibleLines = bangLineLines.Distinct().ToArray(); if (possibleLines.Length == 1) { return(possibleLines[0]); } else { throw new ArgumentException("Match3Lib.Match3BangController.GetBangLineCellsLine exception: bang line is invalid!"); } }
public static void MoveDownAllPossibleCells(Match3Map actualMap) { int swaps = 0; do { swaps = 0; for (int j = 0; j < actualMap.Height - 1; j++) { for (int i = 0; i < actualMap.Width; i++) { if (actualMap[i, j] == null && actualMap[i, j + 1] != null) { actualMap.SwapCells(new Point(i, j), new Point(i, j + 1)); swaps++; } } } }while (swaps != 0); }
static List <Point[]> GetHintsByStep(Match3Map actualMap, Point startPoint) { var result = new List <Point[]>(); for (int x = startPoint.x; x < actualMap.Width - 1; x += Constants.checkingStep) { for (int y = startPoint.y; y < actualMap.Height; y += Constants.checkingStep) { var mapCopy = new Match3Map(actualMap); mapCopy.SwapCells(new Point(x, y), new Point(x + 1, y)); if (Match3BangController.GetAllBangLines(mapCopy).Count > 0) { result.Add(new Point[] { new Point(x, y), new Point(x + 1, y) }); } } } return(result); }
static List <Point[]> GetHorizontalHints(Match3Map actualMap) { var result = new List <Point[]>(); for (int i = 0; i < Constants.checkingStep; i++) { for (int j = 0; j < Constants.checkingStep; j++) { var mapCopy = new Match3Map(actualMap); MakeAllSteps(mapCopy, new Point(i, j)); var bangLines = Match3BangController.GetAllBangLines(mapCopy); if (bangLines.Count > 0) { result.AddRange(GetHintsByStep(actualMap, new Point(i, j))); } } } return(result); }
public void NewGame(Point size, IGameEventsListener actualGameEventsListener, int actualCellTypesCount = 3) { if (size == null) { throw new NullReferenceException("Match3Lib.Match3Controller.NewGame exception: 'size' can't be null!"); } if (!Utils.IsCellInRange(size - Constants.minSize, Constants.maxSize - Constants.minSize)) { throw new ArgumentOutOfRangeException("Match3Lib.Match3Controller.NewGame exception: map size out of range!"); } if (actualCellTypesCount < 2) { throw new ArgumentOutOfRangeException("Match3Lib.Match3Controller.NewGame exception: cellTypesCount out of range!"); } map = new Match3Map(size.x, size.y, this); cellTypesCount = actualCellTypesCount; currentStepIndex = 0; gameEventsListener = actualGameEventsListener; ActualScore = 0; UpdateMapState(); }
public static bool IsMapValid(Match3Map gameMap) { return(IsAnyVerticalStepsEffective(gameMap) || IsAnyHorizontalStepsEffective(gameMap)); }
static bool IsAnyVerticalStepsEffective(Match3Map actualMap) { return(IsAnyHorizontalStepsEffective(actualMap.TransposeMap())); }