/// <summary> /// Creates a PexesoBoard representation on UI /// </summary> /// <param name="rowCount">Number of rows</param> /// <param name="columnCount">Number of columns</param> /// <param name="board">PexesoBoard object to which we add PictureBoxes representing Pexeso cards</param> /// <exception cref="ArgumentException">RowCount or columnCount number is not the same as in PexesoBoard object</exception> private void GeneratePexesoTable(int rowCount, int columnCount, PexesoBoard board) { if (rowCount != board.getPexesoBoardRows()) { throw new ArgumentException("UI board rowCount and PexesoBoard rowCount is not the same.", "rowCount"); } if (columnCount != board.getPexesoBoardColumns()) { throw new ArgumentException("UI board columnCount and PexesoBoard coulmnCount is not the same.", "columnCount"); } //Clear out the existing controls, we are generating a new table layout pexesoLayoutPanel.Controls.Clear(); //Clear out the existing row and column styles pexesoLayoutPanel.ColumnStyles.Clear(); pexesoLayoutPanel.RowStyles.Clear(); //Now we will generate the table, setting up the row and column counts first pexesoLayoutPanel.ColumnCount = columnCount; pexesoLayoutPanel.RowCount = rowCount; for (int column = 0; column < columnCount; column++) { //First add a column pexesoLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F)); for (int row = 0; row < rowCount; row++) { //Next, add a row. Only do this once, when creating the first column if (column == 0) { pexesoLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 100F)); } int width = pexesoLayoutPanel.GetColumnWidths()[column]; int height = pexesoLayoutPanel.GetRowHeights()[row]; //Create the control, in this case we will add a button PictureBox picture = new PictureBox { Name = $"pictureBox[{row}][{column}]", Image = ResourcesLibrary.Resource1.question_mark, Size = new Size(width, height), SizeMode = PictureBoxSizeMode.StretchImage, Anchor = AnchorStyles.None }; picture.MouseClick += Picture_Click; pexesoLayoutPanel.Controls.Add(picture, column, row); board.AddToPexesoBoard(row, column); } } }
/// <summary> /// Creates a new game /// </summary> private void CreateNewGame() { //Garbage collection from previous game if (_board != null) { _board.CleanPexesoBoard(); _board = null; _gameDifficulty = null; completedCounter = 0; GC.Collect(); GC.WaitForPendingFinalizers(); } //Shows a difficulty form to select difficulty DifficultyForm diffForm = new DifficultyForm(transferDelegate); diffForm.ShowDialog(); diffForm.Dispose(); try { switch (_gameDifficulty) { case "Easy": _board = new PexesoBoard(4, 4); GeneratePexesoTable(4, 4, _board); break; case "Hard": _board = new PexesoBoard(8, 8); GeneratePexesoTable(8, 8, _board); break; default: MessageBox.Show("Warning: Difficulty was not selected."); break; } } catch (ArgumentException ex) { MessageBox.Show(ex.Message); } }