//event handler for when a button is clicked on from the main view. opens a new dialogue to pick a value for that button private void ButtonClick(object sender, EventArgs e) { //initialize variables SudokuButton clickedButton; clickedButton = (SudokuButton)sender; int[,,] currentState = state.getVals(); numberPicked = 0; //create a new dialogue with a 3x3 grid of numbers to pick for the new value Form pickNum = new Form(); TableLayoutPanel numRegion = buildRegion(); int buttonNumber = 1; for (int r = 0; r < 3; ++r) { for (int c = 0; c < 3; ++c) { SudokuButton addButton = new SudokuButton(0, 0, 0); addButton.Text = buttonNumber++.ToString(); addButton.Click += new EventHandler(PickedButtonClicked); numRegion.Controls.Add(addButton, c, r); } } pickNum.Controls.Add(numRegion); pickNum.ShowDialog(); //update the button clicked with the text of the new number chosen if (numberPicked != 0) { clickedButton.Text = numberPicked.ToString(); clickedButton.value = numberPicked; } else { clickedButton.Text = ""; clickedButton.value = 0; } //update the board state with the new value and validate the new board state currentState[clickedButton.region, clickedButton.col, clickedButton.row] = numberPicked; state.updateState(currentState); bool[,] validation = state.validateBoard(); colorValid(validation); victoryCheck(); }
//construct the primary interface private void buildForm(BoardState buildState) { Form mainView = new Form(); mainView.FormBorderStyle = FormBorderStyle.Fixed3D; mainView.Size = new Size(500, 600); List <TableLayoutPanel> subRegions = new List <TableLayoutPanel>(); //build the parent table that will hold each of the 3x3 subregions TableLayoutPanel regions = buildRegion(); //build each subregion for (int i = 0; i < 10; i++) { subRegions.Add(buildRegion()); } //Fill out each region's value based on the board state that was passed at intitialization for (int i = 0; i < 9; ++i) { //for each row and column in a region, create and add a button showing the corresponding value //from the board state. If the value is 0, display nothing. for (int j = 0; j < 3; ++j) { for (int k = 0; k < 3; ++k) { SudokuButton addButton = new SudokuButton(i, j, k, buildState.getVals()[i, j, k]); addButton.Dock = DockStyle.Fill; if (!buildState.getFixed()[i, k, j]) { addButton.Click += new EventHandler(ButtonClick); addButton.BackColor = Color.White; } else { addButton.ForeColor = Color.White; addButton.BackColor = Color.Black; } if (buildState.getVals()[i, k, j] > 0) { addButton.Text = buildState.getVals()[i, k, j].ToString(); } subRegions[i].Controls.Add(addButton, k, j); buttons.Add(addButton); } } } //add each subregion to the parent region container regions.Controls.Add(subRegions[0], 0, 0); regions.Controls.Add(subRegions[1], 1, 0); regions.Controls.Add(subRegions[2], 2, 0); regions.Controls.Add(subRegions[3], 0, 1); regions.Controls.Add(subRegions[4], 1, 1); regions.Controls.Add(subRegions[5], 2, 1); regions.Controls.Add(subRegions[6], 0, 2); regions.Controls.Add(subRegions[7], 1, 2); regions.Controls.Add(subRegions[8], 2, 2); //create parent container to hold parent region and menu bar TableLayoutPanel parent = new TableLayoutPanel(); parent.Dock = DockStyle.Fill; parent.ColumnCount = 1; parent.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100)); parent.RowCount = 2; parent.RowStyles.Add(new RowStyle(SizeType.Absolute, 40)); parent.RowStyles.Add(new RowStyle(SizeType.Percent, 100)); //create the menu bar, then add the menu and the parent region parent.Controls.Add(buildMenu()); parent.Controls.Add(regions); mainView.Controls.Add(parent); NewGame(false); //display constructed board mainView.ShowDialog(); }