/// <summary> /// 向xxx方向移动空键 /// </summary> /// <param name="i"></param> private void MovePanNull(int i) { PanKey p = new PanKey(); switch (i) { case 0: //下 p = pans.FirstOrDefault(o => (o.X == panNull.X && o.Y == panNull.Y + 1)); break; case 1: //上 p = pans.FirstOrDefault(o => (o.X == panNull.X && o.Y == panNull.Y - 1)); break; case 2: //左 p = pans.FirstOrDefault(o => (o.X == panNull.X - 1 && o.Y == panNull.Y)); break; case 3: //右 p = pans.FirstOrDefault(o => (o.X == panNull.X + 1 && o.Y == panNull.Y)); break; default: break; } if (p != null) { TrySwapWithNull(p); } }
/// <summary> /// 创建提示 /// </summary> private void CreateTooltip() { gdTip.Children.Clear(); gdTip.ColumnDefinitions.Clear(); gdTip.RowDefinitions.Clear(); for (int i = 0; i < Ban.Columns; i++) { gdTip.ColumnDefinitions.Add(new ColumnDefinition() { }); } for (int i = 0; i < Ban.Rows; i++) { gdTip.RowDefinitions.Add(new RowDefinition() { }); } for (int i = 0; i < Ban.Columns; i++) { for (int j = 0; j < Ban.Rows; j++) { PanKey p = new PanKey(); p.Value = i + j * Ban.Columns + 1; Grid.SetColumn(p, i); Grid.SetRow(p, j); gdTip.Children.Add(p); if (Ban.PBrushes.Count > i * Ban.Columns + j) { p.Background = Ban.PBrushes[i * Ban.Columns + j]; } } } }
/// <summary> /// 点击棋子 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void pk_Click(object sender, RoutedEventArgs e) { PanKey pan = sender as PanKey; //交换空块 if (TrySwapToNull(pan)) { //验证是否完成魔板 VertifyFinished(); } }
/// <summary> /// 单键与空键交换位置 /// </summary> /// <param name="pan"></param> private void TrySwapWithNull(PanKey pan) { if (Math.Abs(pan.X - panNull.X) + Math.Abs(pan.Y - panNull.Y) == 1 && panNull.Template == null) { int x = pan.X; int y = pan.Y; pan.X = panNull.X; pan.Y = panNull.Y; panNull.X = x; panNull.Y = y; changed = true; } }
/// <summary> /// 尝试向空格移动 /// </summary> /// <param name="pan"></param> private bool TrySwapToNull(PanKey pan) { if (panNull.Template != null) { return(false); } if (pan.X - panNull.X != 0 && pan.Y - panNull.Y != 0) { return(false); } int a = pan.X - panNull.X; int b = pan.Y - panNull.Y; if (a > 0) { for (int i = 0; i < a; i++) { MovePanNull(3); } } else if (a < 0) { for (int i = a; i < 0; i++) { MovePanNull(2); } } else if (b > 0) { for (int i = 0; i < b; i++) { MovePanNull(0); } } else if (b < 0) { for (int i = b; i < 0; i++) { MovePanNull(1); } } changed = true; return(true); }
/// <summary> /// 创建新盘 /// </summary> public void CreatePan() { count = 0; pans.Clear(); #region 生成对象 //生成对象 for (int i = 0; i < Columns; i++) { for (int j = 0; j < Rows - 1 || (j < Rows && i < Columns - 1); j++) { PanKey pk = new PanKey(); pk.X = i; pk.Y = j; pk.Value = i + Columns * j + 1; pk.Click += pk_Click; pans.Add(pk); } } panNull = new PanKey() { Template = null, Value = this.Rows * this.Columns, IsEnabled = false, X = Columns - 1, Y = Rows - 1 }; pans.Add(panNull); #endregion #region 打乱棋盘 //打乱 for (int s = 0; s < 10000; s++) { int i = random.Next(0, 4); MovePanNull(i); } #endregion changed = true; timer.Start(); }