public bool IsFilledCell(Cell c) { return c.x >= 0 && c.x < Width && c.y >= 0 && c.y < Height && this[c.y][c.x]; }
public bool IsValidCell(Cell c) { return c.x >= 0 && c.x < Width && c.y >= 0 && c.y < Height && !this[c.y][c.x]; }
public VisitedInfo(Cell pivot, Cell[] members) { Pivot = pivot; Members = members.ToArray(); Array.Sort(Members); }
private bool CellHasJoint(Cell cell, HashSet<Cell> cellsSet) { return Moves.NeighborFunc.Select(fun => fun(cell)).Any(c => Field.IsFilledCell(c) || cellsSet.Contains(c)); }
public static Cell RelatedCell(Cell pivotOld, Cell pivotNew, Cell cellOld) { int dy = pivotNew.y - pivotOld.y; int dx = pivotNew.x - pivotOld.x; int d = -1; if (dy % 2 == 0 || (cellOld.y - pivotOld.y) % 2 == 0) d = 0; else if (pivotOld.y % 2 == 0) d = 1; return new Cell(cellOld.x + dx + d, cellOld.y + dy); }
public static string PrettyPrintBeforeUpdate(Field field, Unit unit, UnitState state, Dictionary<Cell, char> pathCells = null) { string res = ""; var cells = state.GetCells(unit); for (int y = 0; y < field.Height; ++y) { if (y % 2 == 1) res += " "; for (int x = 0; x < field.Width; ++x) { Cell c = new Cell(x, y); if (pathCells != null && pathCells.ContainsKey(c)) res += pathCells[c] + " "; else if (field[y][x]) res += "* "; else if (cells.Contains(c)) res += "@ "; else res += ". "; } res += "|\n"; } return res; }
public static Cell MoveW(Cell c) { return new Cell(c.x - 1, c.y); }
public bool Equals(Cell other) { return x == other.x && y == other.y; }
public static Cell MoveSE(Cell c) { return new Cell(c.x + c.y % 2, c.y + 1); }
public static Cell MoveSW(Cell c) { return new Cell(c.x - (c.y + 1) % 2, c.y + 1); }
public static Cell MoveE(Cell c) { return new Cell(c.x + 1, c.y); }
private static int HexY(double y, Cell pivot) { return Convert.ToInt32(pivot.y + y / Math.Sqrt(3) * 2); }
private static int HexX(double x, Cell pivot) { if (Math.Abs(x - Math.Round(x)) < 0.01) return Convert.ToInt32(Math.Round(pivot.x + x)); return Convert.ToInt32(Math.Round(pivot.x - 0.5 + pivot.y % 2 + x)); }
public void Update(Cell[] cells) { int points = 0; var usedRows = new HashSet<int>(); foreach (var c in cells) { Debug.Assert(!this[c.y][c.x]); this[c.y][c.x] = true; RowCells(c.y).Add(c.x); points++; TotalBricks++; BricksInField++; if (!usedRows.Contains(c.y)) usedRows.Add(c.y); if (c.y < MinFilledRow) MinFilledRow = c.y; } var del = new List<int>(); foreach (var row in usedRows) if (RowCells(row).Count == Width) del.Add(row); del.Sort(); for (int i = 0; i < del.Count; i++) { _cells.RemoveAt(Height - del[i] - 1); _cellsInRows.RemoveAt(Height - del[i] - 1); _cells.Add(new bool[Width]); _cellsInRows.Add(new HashSet<int>()); } MinFilledRow += del.Count; BricksInField -= del.Count * Width; points += 100 * (del.Count + 1) * del.Count / 2; if (_lsOld > 1) points += Convert.ToInt32((_lsOld - 1) * points / 10.0); Score += points; _lsOld = del.Count; if (del.Count > 0) { if (!RowsKilled.ContainsKey(del.Count)) RowsKilled.Add(del.Count, 1); else RowsKilled[del.Count]++; } }
public static Cell TurnCounter(Cell c, Cell pivot) { double x = EuclidX(c, pivot); double y = EuclidY(c, pivot); return new Cell(HexX(x / 2 + Math.Sqrt(3) * y / 2, pivot), HexY(-Math.Sqrt(3) * x / 2 + y / 2, pivot)); }
public InputUnit(Cell[] members, Cell pivot) { this.members = members; this.pivot = pivot; }
private static double EuclidX(Cell cell, Cell pivot) { int dy = cell.y - pivot.y; int dx = cell.x - pivot.x; if (dy % 2 == 0) return dx; return dx + 0.5 - pivot.y % 2; }
private bool TryAddVisitedState(UnitState newState, UnitState state, int funcIndex, out Cell[] cells, out bool isBlockingMove, char moveChar = ANY_CHAR) { cells = null; isBlockingMove = false; if (_visited.ContainsKey(newState)) return false; cells = newState.GetCells(_unit); if (!cells.All(_field.IsValidCell)) { isBlockingMove = true; return false; } //if (_IsDuplicateState(state, newState)) // return false; var vi = new VisitInfo { MoveFuncIndex = funcIndex, PrevState = state, MoveChar = moveChar, //AllPrevStates = _visited[state].AllPrevStates // AllPrevStates = new HashSet<UnitState>(_visited[state].AllPrevStates) }; // vi.AllPrevStates.Add(new UnitState(state.Pivot, _unit.GetCanonicalRotation(state.Rotation))); _visited[newState] = vi; return true; }
private static double EuclidY(Cell cell, Cell pivot) { int dy = cell.y - pivot.y; return dy * Math.Sqrt(3) / 2; }
public UnitState(Cell pivot, int rotation) { Pivot = pivot; Rotation = rotation; }
private static string _PrettyPrint(Cell[] cells, Cell pivot) { string res = ""; Cell minCell = new Cell(pivot.x, pivot.y); Cell maxCell = new Cell(pivot.x, pivot.y); foreach (var cell in cells) { minCell.x = Math.Min(minCell.x, cell.x); minCell.y = Math.Min(minCell.y, cell.y); maxCell.x = Math.Max(maxCell.x, cell.x); maxCell.y = Math.Max(maxCell.y, cell.y); } //var newMembers = cells.Select(cell => new Cell(cell.x - minCell.x, cell.y - minCell.y)).ToList(); //maxCell.x -= minCell.x; //maxCell.y -= minCell.y; //Cell newPivot = new Cell(pivot.x - minCell.x, pivot.y - minCell.y); //for (int y = 0; y < maxCell.y; ++y) //{ // for (int x ) //} for (int y = minCell.y; y <= maxCell.y; ++y) { if (y % 2 == 1) res += " "; for (int x = minCell.x; x <= maxCell.x; ++x) { Cell c = new Cell(x, y); if (cells.Contains(c)) { if (pivot == c) res += "& "; else res += "* "; } else if (pivot == c) res += "# "; else res += ". "; } res += "\n"; } return res; }