/// <summary> /// C'tor /// </summary> public Board() { this.VerticalAlignment = System.Windows.VerticalAlignment.Center; this.HorizontalAlignment = System.Windows.HorizontalAlignment.Center; m_FirstCellClicked = null; this.ShowGridLines = true; m_BoardStatus = eBoardState.BoardInit; }
private void InitCells(string[] i_Images, int i_CellSize) { #region DeclareVariables // number of cells in board int numberOfCells; int picIndex = 0; int numberOfPics; int length; int i; int[] imagesIndex; Random random; #endregion // init random = new Random(); numberOfCells = m_NumberOfRows * m_NumberOfColumns; numberOfPics = i_Images.Length; m_NumberOfCellsLeftVisible = numberOfCells; m_BoardStatus = eBoardState.BoardInitCellsInitStart; picIndex = 0; int[] usedIndex; int randomIndex; // creating an array with randomized index of images // array size is the number of the cells but will filled to the half imagesIndex = new int[numberOfCells]; usedIndex = new int[numberOfPics]; length = numberOfCells / 2; for (i = 0; i < length; i++) { randomIndex = random.Next(numberOfPics); imagesIndex[i] = randomIndex; } for (i = length; i < numberOfCells; i++) { imagesIndex[i] = imagesIndex[i - length]; } // shuffle according to Fisher–Yates shuffle // http://en.wikipedia.org/wiki/Fisher-Yates_shuffle#Example_implementations random = new Random(); for (int n = imagesIndex.Length ; n > 1; n--) { int k = random.Next(n); int tmp = imagesIndex[k]; imagesIndex[k] = imagesIndex[n - 1]; imagesIndex[n - 1] = tmp; } try { // checking if a valid board size if (m_NumberOfColumns > 0 && m_NumberOfRows > 0) { // checking if we have enough images to cover the board if ((numberOfCells / 2) <= i_Images.Length) { this.Children.Clear(); m_CellArray = new Cell[m_NumberOfRows, m_NumberOfColumns]; int x, y; for (x = 0; x < m_NumberOfRows; x++) { for (y = 0; y < m_NumberOfColumns; y++) { m_CellArray[x, y] = new Cell(x.ToString() + y.ToString(), i_CellSize); m_CellArray[x, y].ImagePressed += new Cell.DelegeateImageChecked(Board_EventCheck); m_CellArray[x, y].SetBackground(i_Images[imagesIndex[picIndex++]]); this.Children.Add(m_CellArray[x, y]); Grid.SetColumn(m_CellArray[x, y], y); Grid.SetRow(m_CellArray[x, y], x); } } } else { m_BoardStatus = eBoardState.BoardInitErrorNotEnoughImages; Debug.WriteLine("Number of images must be half of number of cells"); } } else { m_BoardStatus = eBoardState.BoardInitErrorColumnRowMustBePositive; Debug.WriteLine("Number of Rows and number of Columns must be positive"); } } catch (Exception e1) { Debug.WriteLine(e1.Message); m_BoardStatus = eBoardState.BoardInitGeneralError; } // if no error found if (m_BoardStatus == eBoardState.BoardInitCellsInitStart) { m_BoardStatus = eBoardState.BoardInitCellsinitEndedOkay; } BoardStatusChanged.Invoke(m_BoardStatus); }
/// <summary> /// Init every cell from using an array of images URLs /// </summary> /// <param name="i_Images">URLs of images</param> void Board_EventCheck(Cell i_Cell) { m_BoardStatus = eBoardState.FirstCellClick; // if this is the first cell opened if (m_FirstCellClicked == null) { m_FirstCellClicked = i_Cell; } // if already one cell is open, check if they are the same else if (m_FirstCellClicked.CellId == i_Cell.CellId && m_FirstCellClicked.Name != i_Cell.Name) { // cells are the same m_FirstCellClicked.LockVisible(true); i_Cell.LockVisible(true); m_FirstCellClicked = null; // decreasing 2 cells m_NumberOfCellsLeftVisible -= 2; m_BoardStatus = eBoardState.FoundMatch; } // if they are not the same, hide them else { m_FirstCellClicked.ForceHide(); i_Cell.ForceHide(); m_FirstCellClicked = null; m_BoardStatus = eBoardState.NotFoundMatch; } if (m_NumberOfCellsLeftVisible == 0) { m_BoardStatus = eBoardState.BoardNoAvailableCellsLeft | m_BoardStatus; } BoardStatusChanged.Invoke(m_BoardStatus); }