public static bool neighbor(CellPostion cell1, CellPostion cell2) { if (cell2.x == cell1.x) { if (System.Math.Abs(cell2.y - cell1.y) == 1) { return(true); } else { return(false); } } if (cell2.y == cell1.y) { if (System.Math.Abs(cell2.x - cell1.x) == 1) { return(true); } else { return(false); } } return(false); }
private void updateDragCell() { //需要计算出开始的cell、转向的cell、结束的cell Cell begin = null; Cell turning = null; Cell end = null; begin = dragCells[0]; for (int i = 0; i < dragCellCount; i++) { Cell tempCell = dragCells[i]; if (!turning) { if (!CellPostion.equal(begin.cellPostion, tempCell.cellPostion)) { turning = tempCell; } } if (!end) { if (!CellPostion.equal(turning.cellPostion, tempCell.cellPostion)) { end = tempCell; } } } }
public static int moveDirection(CellPostion cell1, CellPostion cell2) { if (cell2.x == cell1.x) { if (cell2.y > cell1.y) { return((int)Direction.Up); } else { return((int)Direction.Down); } } if (cell2.y == cell1.y) { if (cell2.x > cell1.x) { return((int)Direction.Right); } else { return((int)Direction.Left); } } return((int)Direction.Error); }
private Cell getCell(CellPostion postion) { if (postion.x >= 1 && postion.x <= GameArgs.cols && postion.y >= 1 && postion.y <= GameArgs.rows) { return(cells [(int)postion.x - 1, (int)postion.y - 1]); } return(null); }
public static bool equal(CellPostion cell1, CellPostion cell2) { if ((cell1.x == cell2.x) && (cell1.y == cell2.y)) { return(true); } return(false); }
public static CellPostion getRight(CellPostion cell) { CellPostion newCellPostion; newCellPostion.x = cell.x + 1; newCellPostion.y = cell.y; if (newCellPostion.x > GameArgs.cols) { return(ErrorCellPostion()); } return(newCellPostion); }
public static CellPostion getLeft(CellPostion cell) { CellPostion newCellPostion; newCellPostion.x = cell.x - 1; newCellPostion.y = cell.y; if (newCellPostion.x < 1) { return(ErrorCellPostion()); } return(newCellPostion); }
public static CellPostion getDown(CellPostion cell) { CellPostion newCellPostion; newCellPostion.x = cell.x; newCellPostion.y = cell.y - 1; if (newCellPostion.y < 1) { return(ErrorCellPostion()); } return(newCellPostion); }
public static CellPostion getUp(CellPostion cell) { CellPostion newCellPostion; newCellPostion.x = cell.x; newCellPostion.y = cell.y + 1; if (newCellPostion.y > GameArgs.rows) { return(ErrorCellPostion()); } return(newCellPostion); }
private void addDragCell(Cell cell) { if (dragCellCount >= maxDragCellCount) { return; } if (dragCellCount == 0) { dragCells [dragCellCount] = cell; dragCellCount++; return; } if (dragCellCount >= 1) { begin = dragCells [0]; if (CellPostion.equal(dragCells [dragCellCount - 1].cellPostion, cell.cellPostion)) { return; } } if (dragCellCount == 1 && CellPostion.neighbor(dragCells [dragCellCount - 1].cellPostion, cell.cellPostion)) { dragCells [dragCellCount] = cell; dragCellCount++; end = dragCells [dragCellCount - 1]; return; } if (dragCellCount >= 2) { fristDirection = (Direction)CellPostion.moveDirection(dragCells [0].cellPostion, dragCells [1].cellPostion); if (fristDirection == Direction.Error) { cleanDragCell(); return; } } for (int i = 3; i <= dragCellCount; i++) { Direction tempDire = (Direction)CellPostion.moveDirection(dragCells [i - 2].cellPostion, dragCells [i - 1].cellPostion); if (tempDire == Direction.Error) { cleanDragCell(); return; } if (tempDire != fristDirection) { turning = dragCells [i - 2]; //第二次的方向 secondDirection = tempDire; break; } } //新加入的cell的方向 Direction newCellDirection = (Direction)CellPostion.moveDirection(dragCells [dragCellCount - 1].cellPostion, cell.cellPostion); // print ("fristDirection:"+fristDirection.ToString()); // print ("secondDirection:"+secondDirection.ToString()); // print ("newCellDirection:"+newCellDirection.ToString()); if (secondDirection != Direction.Error) { if (newCellDirection == secondDirection && CellPostion.neighbor(dragCells [dragCellCount - 1].cellPostion, cell.cellPostion)) { dragCells [dragCellCount] = cell; dragCellCount++; } } else { if (CellPostion.neighbor(dragCells [dragCellCount - 1].cellPostion, cell.cellPostion)) { if (newCellDirection != fristDirection) { turning = dragCells [dragCellCount - 1]; secondDirection = newCellDirection; } dragCells [dragCellCount] = cell; dragCellCount++; } } end = dragCells[dragCellCount - 1]; }
//,Cell[] fristSet,Cell[] secondSet public bool CanMove(Cell begin, Cell turning, Cell end, Cell[] cells) { CellPostion beginPos = CellPostion.ErrorCellPostion(); CellPostion turningPos = CellPostion.ErrorCellPostion(); CellPostion endPos = CellPostion.ErrorCellPostion(); Direction fristDirection = Direction.Error; Direction secondDirection = Direction.Error; if (begin) { beginPos = begin.cellPostion; begin.selected(true); } else { return(false); } if (end) { endPos = end.cellPostion; } else { return(false); } if (begin && turning && end) { turningPos = turning.cellPostion; fristDirection = (Direction)CellPostion.moveDirection(beginPos, turningPos); secondDirection = (Direction)CellPostion.moveDirection(turningPos, endPos); } else if (begin && end) { fristDirection = (Direction)CellPostion.moveDirection(beginPos, endPos); } //标记出第一条线的格子 Cell tempCell = begin; CellPostion nextPos = CellPostion.ErrorCellPostion(); if (turning) { //第一条线 while (tempCell != turning) { switch (fristDirection) { case Direction.Up: nextPos = CellPostion.getUp(tempCell.cellPostion); break; case Direction.Down: nextPos = CellPostion.getDown(tempCell.cellPostion); break; case Direction.Left: nextPos = CellPostion.getLeft(tempCell.cellPostion); break; case Direction.Right: nextPos = CellPostion.getRight(tempCell.cellPostion); break; default: break; } if (CellPostion.equal(nextPos, CellPostion.ErrorCellPostion())) { return(false); } else { tempCell = getCell(nextPos); tempCell.selected(true); } } // turning.selected (true); //第二条线 tempCell = turning; while (tempCell != end) { switch (secondDirection) { case Direction.Up: nextPos = CellPostion.getUp(tempCell.cellPostion); break; case Direction.Down: nextPos = CellPostion.getDown(tempCell.cellPostion); break; case Direction.Left: nextPos = CellPostion.getLeft(tempCell.cellPostion); break; case Direction.Right: nextPos = CellPostion.getRight(tempCell.cellPostion); break; default: break; } if (CellPostion.equal(nextPos, CellPostion.ErrorCellPostion())) { return(false); } else { tempCell = getCell(nextPos); tempCell.selected(true); } } } else { tempCell = begin; while (tempCell != end) { switch (fristDirection) { case Direction.Up: nextPos = CellPostion.getUp(tempCell.cellPostion); break; case Direction.Down: nextPos = CellPostion.getDown(tempCell.cellPostion); break; case Direction.Left: nextPos = CellPostion.getLeft(tempCell.cellPostion); break; case Direction.Right: nextPos = CellPostion.getRight(tempCell.cellPostion); break; default: break; } if (CellPostion.equal(nextPos, CellPostion.ErrorCellPostion())) { return(false); } else { tempCell = getCell(nextPos); tempCell.selected(true); } } } // end.selected (true); // // foreach (Cell cell in cells) { // cell.selected (true); // } return(true); }