private int GetNewCell(Cell.Direction dir) { if (!Enum.IsDefined(typeof(Cell.Direction), dir)) { return(_currentCellNo); } var newCellNo = _currentCellNo + Cell.CellNoDifference[dir]; if (Cell.CellNoMap.ContainsKey(newCellNo)) { return(newCellNo); } foreach (var diff in Cell.NearCellDifference) { var nearCellNo = newCellNo + diff; if (Cell.CellNoMap.ContainsKey(nearCellNo)) { return(nearCellNo); } } return(_currentCellNo); }
/// <summary> /// Метод размещения элементарной клетки на поле /// </summary> /// <param name="target"></param> private void PlaceNum(Cell target) { var zero = map.GetNum(0); target.Actual = true; target.CheckPlace(); if (target.Placed) { target.Actual = false; return; } while (!target.Placed) //двигаем пока не поставим на своё место { map.MoveToNeighbourhood(target); //пока не будем рядом с целью Cell.Direction destination_side = target.GetDirectionToMove(); /* * определяем, в каком направлении двигаться * приоритет: лево-право, вверх-вниз. * определяем кратчайший для передвижения с учётом уже размещённых */ switch (zero.GetDirection(target)) //определяем, с какой стороны граничит с целью 0 { case Cell.Direction.Up: //0 снизу switch (destination_side) //определяем в каком направлении мы хотели бы передвинуть целевую клетку { case Cell.Direction.Up: //если надо вверх map.MoveRight(zero); map.MoveUp(zero); break; case Cell.Direction.Right: //надо вправо map.MoveRight(zero); map.MoveUp(zero); break; case Cell.Direction.Down: //надо вниз map.MoveUp(zero); break; case Cell.Direction.Left: //надо влево if (zero.CanLeft()) { map.MoveLeft(zero); map.MoveUp(zero); } else { map.MoveRight(zero); map.MoveUp(zero); } break; default: break; } break; case Cell.Direction.Right: //0 слева switch (destination_side) //определяем в каком направлении мы хотели бы передвинуть целевую клетку { case Cell.Direction.Up: //если надо вверх if (zero.CanUp()) { map.MoveUp(zero); map.MoveRight(zero); } else { map.MoveDown(zero); map.MoveRight(zero); } break; case Cell.Direction.Right: //надо вправо if (zero.CanUp()) { map.MoveUp(zero); map.MoveRight(zero); } else { map.MoveDown(zero); map.MoveRight(zero); } break; case Cell.Direction.Down: //надо вниз if (zero.CanDown()) { map.MoveDown(zero); map.MoveRight(zero); } else if (zero.CanUp()) { map.MoveUp(zero); map.MoveRight(zero); } break; case Cell.Direction.Left: //надо влево map.MoveRight(zero); break; default: break; } break; case Cell.Direction.Down: //0 сверху switch (destination_side) //определяем в каком направлении мы хотели бы передвинуть целевую клетку { case Cell.Direction.Up: //если надо вверх map.MoveDown(zero); break; case Cell.Direction.Right: //надо вправо map.MoveRight(zero); map.MoveDown(zero); break; case Cell.Direction.Down: //надо вниз map.MoveRight(zero); map.MoveDown(zero); break; case Cell.Direction.Left: //надо влево if (zero.CanLeft() && target.CanLeft()) //и слева не установленная клетка и целевая может идти влево { map.MoveLeft(zero); map.MoveDown(zero); } else { map.MoveRight(zero); map.MoveDown(zero); } break; default: break; } break; case Cell.Direction.Left: //0 справа switch (destination_side) //определяем в каком направлении мы хотели бы передвинуть целевую клетку { case Cell.Direction.Up: //если надо вверх map.MoveUp(zero); map.MoveLeft(zero); break; case Cell.Direction.Right: //надо вправо map.MoveLeft(zero); break; case Cell.Direction.Down: //надо вниз map.MoveDown(zero); map.MoveLeft(zero); break; case Cell.Direction.Left: //надо влево if ((zero.CanDown() && zero.Previous != "up") || target.Row == 0) { map.MoveDown(zero); map.MoveLeft(zero); } else { map.MoveUp(zero); map.MoveLeft(zero); } break; default: break; } break; default: break; } target.CheckPlace(); } target.Actual = false; }
public String Walk(Cell.Direction dir) { _currentCellNo = GetNewCell(dir); return(Cell.CellNoMap[_currentCellNo]); }
/// <summary> /// размещение клетки /// </summary> /// <param name="row_of_num"></param> /// <param name="col_of_num"></param> void PlaceCell(int row_of_num, int col_of_num) { Cell placed_cell = GetCell(field, row_of_num * size + col_of_num + 1); Cell empty = GetCell(field, size * size); placed_cell.CheckPlace(); if (placed_cell.GetPlaced()) //если размещена { return; } MoveEmptyToClose(field, placed_cell); //пока не будем рядом с целью while (!placed_cell.GetPlaced()) //двигаем пока не поставим на своё место { Cell.Direction destination_side = placed_cell.GetDirectionToMove(); /* * определяем, в каком направлении двигаться * приоритет: лево-право, вверх-вниз. * определяем кратчайший для передвижения с учётом уже размещённых */ switch (empty.GetDirection(placed_cell)) //определяем, с какой стороны граничит с целью 0 { case Cell.Direction.Up: //0 снизу switch (destination_side) //определяем в каком направлении мы хотели бы передвинуть целевую клетку { case Cell.Direction.Up: //если надо вверх MoveRight(field, empty); MoveUp(field, empty); break; case Cell.Direction.Right: //надо вправо MoveRight(field, empty); MoveUp(field, empty); break; case Cell.Direction.Down: //надо вниз MoveUp(field, empty); break; case Cell.Direction.Left: //надо влево if (empty.CanLeft()) { MoveLeft(field, empty); MoveUp(field, empty); } else { MoveRight(field, empty); MoveUp(field, empty); } break; default: break; } break; case Cell.Direction.Right: //0 слева switch (destination_side) //определяем в каком направлении мы хотели бы передвинуть целевую клетку { case Cell.Direction.Up: //если надо вверх if (empty.CanUp()) { MoveUp(field, empty); MoveRight(field, empty); } else { MoveDown(field, empty); MoveRight(field, empty); } break; case Cell.Direction.Right: //надо вправо if (empty.CanUp()) { MoveUp(field, empty); MoveRight(field, empty); } else { MoveDown(field, empty); MoveRight(field, empty); } break; case Cell.Direction.Down: //надо вниз if (empty.CanDown()) { MoveDown(field, empty); MoveRight(field, empty); } else if (empty.CanUp()) { MoveUp(field, empty); MoveRight(field, empty); } break; case Cell.Direction.Left: //надо влево MoveRight(field, empty); break; default: break; } break; case Cell.Direction.Down: //0 сверху switch (destination_side) //определяем в каком направлении мы хотели бы передвинуть целевую клетку { case Cell.Direction.Up: //если надо вверх MoveDown(field, empty); break; case Cell.Direction.Right: //надо вправо MoveRight(field, empty); MoveDown(field, empty); break; case Cell.Direction.Down: //надо вниз MoveRight(field, empty); MoveDown(field, empty); break; case Cell.Direction.Left: //надо влево if (empty.CanLeft() && placed_cell.CanLeft()) //и слева не установленная клетка и целевая может идти влево { MoveLeft(field, empty); MoveDown(field, empty); } else { MoveRight(field, empty); MoveDown(field, empty); } break; default: break; } break; case Cell.Direction.Left: //0 справа switch (destination_side) //определяем в каком направлении мы хотели бы передвинуть целевую клетку { case Cell.Direction.Up: //если надо вверх MoveUp(field, empty); MoveLeft(field, empty); break; case Cell.Direction.Right: //надо вправо MoveLeft(field, empty); break; case Cell.Direction.Down: //надо вниз MoveDown(field, empty); MoveLeft(field, empty); break; case Cell.Direction.Left: //надо влево if ((empty.CanDown() && empty.GetPrevious() != Cell.Direction.Up) || placed_cell.GetRow() == 0) { MoveDown(field, empty); MoveLeft(field, empty); } else { MoveUp(field, empty); MoveLeft(field, empty); } break; default: break; } break; default: break; } placed_cell.CheckPlace(); } }