public void Reset() { ConvertToColor ctc = new ConvertToColor(); Board = new Tile[7, 7]; BoardState = BoardStateEnum.Normal; RowPicked = ColPicked = -1; LayoutRoot.Children.Clear(); for (int r = 0; r < 7; r++) { for (int c = 0; c < 7; c++) { Board[r, c] = new Tile(r, c); Shape rect = new Ellipse(); rect.Margin = new Thickness(5); Grid.SetRow(rect, r); Grid.SetColumn(rect, c); Binding bb = new Binding(); bb.Source = Board[r, c]; bb.Converter = ctc; bb.Path = new PropertyPath("State"); rect.SetBinding(Ellipse.FillProperty, bb); LayoutRoot.Children.Add(rect); if (Board[r, c].State != TileStateEnum.Blocked) { rect.Stroke = Brushes.Black; rect.StrokeThickness = 1; } rect.MouseDown += new MouseButtonEventHandler(rect_MouseDown); } } }
void rect_MouseDown(object sender, MouseButtonEventArgs e) { Shape r = sender as Shape; BindingExpression be = r.GetBindingExpression(Ellipse.FillProperty); Tile t = be.DataItem as Tile; BoardState = t.MakeMove(BoardState, ref RowPicked, ref ColPicked, Board); CheckAnyMorePossible(); }
internal BoardStateEnum MakeMove(BoardStateEnum bs, ref int rowPicked, ref int colPicked, Tile[,] Board) { BoardStateEnum rbs = bs; if (bs == BoardStateEnum.Normal) { if (State == TileStateEnum.Filled) { State = TileStateEnum.Picked; rowPicked = m_row; colPicked = m_col; rbs = BoardStateEnum.Picked; } } else { switch (State) { case TileStateEnum.Blocked: break; // Not allowed case TileStateEnum.Filled: break; // Not allowed case TileStateEnum.Picked: // Dropping it back { State = TileStateEnum.Filled; rowPicked = -1; colPicked = -1; rbs = BoardStateEnum.Normal; break; } case TileStateEnum.Empty: // We need to check if dropping here is allowed { if ((m_row == rowPicked) && (Math.Abs(m_col - colPicked) == 2) && (Board[m_row, (m_col + colPicked) / 2].State == TileStateEnum.Filled) ) { State = TileStateEnum.Filled; Board[m_row, (m_col + colPicked) / 2].State = TileStateEnum.Empty; } if ((m_col == colPicked) && (Math.Abs(m_row - rowPicked) == 2) && (Board[(m_row + rowPicked) / 2, m_col].State == TileStateEnum.Filled) ) { State = TileStateEnum.Filled; Board[(m_row + rowPicked) / 2, m_col].State = TileStateEnum.Empty; } if (State == TileStateEnum.Filled) { Board[rowPicked, colPicked].State = TileStateEnum.Empty; rowPicked = -1; colPicked = -1; rbs = BoardStateEnum.Normal; } break; } default: // Do nothing break; } } return rbs; }