private void checkGuessButton_Click(object sender, EventArgs e) { Guess thisGuess = new Guess(numberOfSpaces); // check for empty cells for (int i = 0; i < numberOfSpaces; i++) { if (guessInProgress[i] == backgroundColor) { checkGuessResult.Text = "Submission contains empty cells"; return; } } // check for repeated colors if (!repeatsAllowed) { System.Drawing.Color[] colorRepeatCheck = new System.Drawing.Color[numberOfSpaces]; int j = 0; for (int i = 0; i < numberOfSpaces; i++) { for (int k = 0; k < j; k++) { if (colorRepeatCheck[k] == guessInProgress[i]) { checkGuessResult.Text = "Submission contains repeated colours, which are currently not allowed"; return; } } colorRepeatCheck[j] = guessInProgress[i]; j++; } } int[] results = new int[2]; for (int i = 0; i < numberOfSpaces; i++) { thisGuess.setColor(i, guessInProgress[i]); } int m = 1; foreach (Guess g in guessHistory) { results = g.compare(thisGuess); if (results[0] != g.getRightColorRightPlace()) { checkGuessResult.Text = "No match: Right Color Right Place, guess " + m + "."; return; } else if (results[1] != g.getRightColorWrongPlace()) { checkGuessResult.Text = "No match: Right Color Wrong Place, guess " + m + "."; return; } m++; } checkGuessResult.Text = "OK!"; }
private void submitGuess() { checkGuessResult.Text = ""; submit.Hide(); cluesDGV.Invalidate(); // initialize currentGuess int guessHistoryRowNumber = numberOfAllowedGuesses - 1 - numberOfGuesses; currentGuess = new Guess(numberOfSpaces); for (int i = 0; i < numberOfSpaces; i++) { currentGuess.setColor(i, guessInProgress[i]); guessInProgress[i] = backgroundColor; } // get results of guess int[] guessResult = solution.compare(currentGuess); int rightColorRightPlace = guessResult[0]; int rightColorWrongPlace = guessResult[1]; int j = 0; for (int k = 0; k < rightColorRightPlace; k++) { j++; } for (int k = 0; k < rightColorWrongPlace; k++) { j++; } currentGuess.setRightColorRightPlace(rightColorRightPlace); currentGuess.setRightColorWrongPlace(rightColorWrongPlace); // update possibleSolutions guessHistory.Add(currentGuess); List <Guess> toRemove = new List <Guess>(); int[] results; foreach (Guess g in allSolutions) { results = g.compare(currentGuess); if (results[0] != currentGuess.getRightColorRightPlace() || results[1] != currentGuess.getRightColorWrongPlace()) { toRemove.Add(g); } } foreach (Guess g in toRemove) { allSolutions.Remove(g); } numPossibleSolutions = allSolutions.Count(); label4.Text = numPossibleSolutions.ToString(); // end of game if (rightColorRightPlace == numberOfSpaces || numberOfGuesses == numberOfAllowedGuesses - 1) { revealSolution = true; solutionDGV.Invalidate(); cluesDGV.Invalidate(); submit.Enabled = false; if (rightColorRightPlace != numberOfSpaces) { MessageBox.Show("Better luck next time."); } else { MessageBox.Show("You win!"); } panel3.Show(); //submit.Hide(); } else { numberOfGuesses++; selectCell(0, numberOfAllowedGuesses - numberOfGuesses - 1); submit.Location = new Point(submit.Location.X, submit.Location.Y - rowHeight); submit.Show(); } cluesDGV.Invalidate(); }
private void newGame() { numberOfGuesses = 0; revealSolution = false; solutionDGV.Hide(); colorDataGridView.Hide(); guessHistoryDGV.Hide(); cluesDGV.Hide(); guessHistoryDGV.RowCount = numberOfAllowedGuesses; guessHistoryDGV.ColumnCount = numberOfSpaces; foreach (DataGridViewColumn c in guessHistoryDGV.Columns) { c.Width = guessHistoryDGV.Width / guessHistoryDGV.ColumnCount; } foreach (DataGridViewRow r in guessHistoryDGV.Rows) { r.Height = guessHistoryDGV.Height / guessHistoryDGV.RowCount; } rowHeight = guessHistoryDGV.Height / guessHistoryDGV.RowCount; cluesDGV.RowCount = numberOfAllowedGuesses; cluesDGV.ColumnCount = numberOfSpaces; foreach (DataGridViewColumn c in cluesDGV.Columns) { c.Width = cluesDGV.Width / cluesDGV.ColumnCount; for (int i = 0; i < numberOfAllowedGuesses; i++) { cluesDGV[c.Index, i].Style.BackColor = backgroundColor; cluesDGV[c.Index, i].Style.SelectionBackColor = backgroundColor; } } foreach (DataGridViewRow r in cluesDGV.Rows) { r.Height = cluesDGV.Height / cluesDGV.RowCount; } solutionDGV.RowCount = 1; solutionDGV.ColumnCount = numberOfSpaces; foreach (DataGridViewColumn c in solutionDGV.Columns) { c.Width = solutionDGV.Width / solutionDGV.ColumnCount; } foreach (DataGridViewRow r in solutionDGV.Rows) { r.Height = solutionDGV.Height / solutionDGV.RowCount; } // initialize the list of possible colors and guess history grid guessHistory = new List <Guess>(); colorDataGridView.RowCount = 1; colorDataGridView.ColumnCount = numberOfColours; foreach (DataGridViewColumn c in colorDataGridView.Columns) { c.Width = colorDataGridView.Width / colorDataGridView.ColumnCount; } foreach (DataGridViewRow r in colorDataGridView.Rows) { r.Height = colorDataGridView.Height / colorDataGridView.RowCount; } guessInProgress = new System.Drawing.Color[numberOfSpaces]; for (int i = 0; i < numberOfSpaces; i++) { guessInProgress[i] = backgroundColor; } // initialize the list of possible solutions allSolutions = new List <Guess>(); int factor; //int current_guess_index = 0; for (int i = 0; i < totalSolutionsWithRepeats; i++) { Guess newGuess = new Guess(numberOfSpaces); factor = 1; for (int j = numberOfSpaces - 1; j >= 0; j--) { double d = i / factor; int mod = Convert.ToInt32(Math.Floor(d)); int color_index = mod % numberOfColours; if (!repeatsAllowed) { for (int k = numberOfSpaces - 1; k > j; k--) { // need to go to next i because this color already exists in this guess if (newGuess.getColor(k) == colorArray[color_index]) { goto newI; } } } newGuess.setColor(j, colorArray[color_index]); factor = factor * numberOfColours; } allSolutions.Add(newGuess); newI :; // skipped combinations (due to repeats not being allowed) jump to here } // pick one of the solutions as The Solution int solutionIndex = rnd.Next(0, totalSolutions); solution = allSolutions.ElementAt(solutionIndex); // show and hide the appropriate items label4.Text = label2.Text; submit.Enabled = true; submit.Location = new Point(submit.Location.X, cluesDGV.Location.Y + cluesDGV.Height - rowHeight); submit.Show(); solutionDGV.Show(); showSolutions.Show(); repeatsAllowedDisplayCheckbox.Show(); panel3.Hide(); checkGuessButton.Show(); checkGuessResult.Show(); cluesDGV.Show(); guessHistoryDGV.Show(); colorDataGridView.Show(); selectCell(0, numberOfAllowedGuesses - 1); }