/// <summary> /// 数字单元移动. /// </summary> /// <param name="step"></param> /// <param name="direction"></param> private void NumberItemMoving(MoveStep step, MoveDirection direction) { Invoke(new Action(() => { // 取得指定坐标的 按钮控件. // 注意参数的顺序, 先列后行! Control ctl = this.tlpMain.GetControlFromPosition(step.J, step.I); // 将按钮从表格删除. this.tlpMain.Controls.Remove(ctl); // 根据移动方向,将按钮加到指定的位置. // 注意参数的顺序, 先列后行! switch (direction) { case MoveDirection.Left: // 左 this.tlpMain.Controls.Add(ctl, step.J - 1, step.I); break; case MoveDirection.Up: // 上 this.tlpMain.Controls.Add(ctl, step.J, step.I - 1); break; case MoveDirection.Right: // 右 this.tlpMain.Controls.Add(ctl, step.J + 1, step.I); break; case MoveDirection.Down: // 下 this.tlpMain.Controls.Add(ctl, step.J, step.I + 1); break; } this.Refresh(); Thread.Sleep(500); if (puzzle8.IsFinish) { // 停止计时! this.timer1.Stop(); EndAuto(); MessageBox.Show("完成拼图处理!"); } })); }
/// <summary> /// 移动 坐标为 (i,j) 上面的元素. /// </summary> /// <param name="i"></param> /// <param name="j"></param> /// <returns></returns> public bool Move(int i, int j) { //Console.WriteLine("Move({0}, {1})", i, j); if (PuzzleArray[i, j] == 0) { // 空格不能移动. return false; } // 左边是0 (空格). if ( j > 0 && PuzzleArray[i, j - 1] == 0) { // 左移. PuzzleArray[i, j-1] = PuzzleArray[i, j]; PuzzleArray[i, j] = 0; MoveStep ms = new MoveStep(i, j); MoveStepList.Add(ms); if (Moving != null) { Moving(ms, MoveDirection.Left); } return true; } // 上边是0 (空格). if (i > 0 && PuzzleArray[i - 1, j] == 0) { // 上移. PuzzleArray[i - 1, j] = PuzzleArray[i, j]; PuzzleArray[i, j] = 0; MoveStep ms = new MoveStep(i, j); MoveStepList.Add(ms); if (Moving != null) { Moving(ms, MoveDirection.Up); } return true; } // 右边是0 (空格). if ( j<PuzzleSize-1 && PuzzleArray[i, j + 1] == 0) { // 右移. PuzzleArray[i, j + 1] = PuzzleArray[i, j]; PuzzleArray[i, j] = 0; MoveStep ms = new MoveStep(i, j); MoveStepList.Add(ms); if (Moving != null) { Moving(ms, MoveDirection.Right); } return true; } // 下边是0(空格). if (i < PuzzleSize - 1 && PuzzleArray[i + 1, j] == 0) { // 下移. PuzzleArray[i + 1, j] = PuzzleArray[i, j]; PuzzleArray[i, j] = 0; MoveStep ms = new MoveStep(i, j); MoveStepList.Add(ms); if (Moving != null) { Moving(ms, MoveDirection.Down); } return true; } // 上左下右都不是空格. return false; }
/// <summary> /// 移动 坐标为 (i,j) 上面的元素. /// </summary> /// <param name="i"></param> /// <param name="j"></param> /// <returns></returns> public bool Move(int i, int j) { //Console.WriteLine("Move({0}, {1})", i, j); if (PuzzleArray[i, j] == 0) { // 空格不能移动. return(false); } // 左边是0 (空格). if (j > 0 && PuzzleArray[i, j - 1] == 0) { // 左移. PuzzleArray[i, j - 1] = PuzzleArray[i, j]; PuzzleArray[i, j] = 0; MoveStep ms = new MoveStep(i, j); MoveStepList.Add(ms); if (Moving != null) { Moving(ms, MoveDirection.Left); } return(true); } // 上边是0 (空格). if (i > 0 && PuzzleArray[i - 1, j] == 0) { // 上移. PuzzleArray[i - 1, j] = PuzzleArray[i, j]; PuzzleArray[i, j] = 0; MoveStep ms = new MoveStep(i, j); MoveStepList.Add(ms); if (Moving != null) { Moving(ms, MoveDirection.Up); } return(true); } // 右边是0 (空格). if (j < PuzzleSize - 1 && PuzzleArray[i, j + 1] == 0) { // 右移. PuzzleArray[i, j + 1] = PuzzleArray[i, j]; PuzzleArray[i, j] = 0; MoveStep ms = new MoveStep(i, j); MoveStepList.Add(ms); if (Moving != null) { Moving(ms, MoveDirection.Right); } return(true); } // 下边是0(空格). if (i < PuzzleSize - 1 && PuzzleArray[i + 1, j] == 0) { // 下移. PuzzleArray[i + 1, j] = PuzzleArray[i, j]; PuzzleArray[i, j] = 0; MoveStep ms = new MoveStep(i, j); MoveStepList.Add(ms); if (Moving != null) { Moving(ms, MoveDirection.Down); } return(true); } // 上左下右都不是空格. return(false); }