public void button_click(object sender, EventArgs e) { // If first button clicked, start the clock if (startTimer == false) { startStopWatch(); Thread t = new Thread(doWork); t.IsBackground = true; t.Name = "TIME_THREAD"; Debug.WriteLine("[PlayWindow] Starting clock"); t.Start(); startTimer = true; } // Have to work out whether or not it was a correct click Button b = sender as Button; string res1 = sudokuGrid.getValueOfButtonInGrid(b.Name).ToString(); string res2 = gameControl.getSelectedToolkitButtonName()[7].ToString(); // Determining whether or not user has guessed correctly if (sudokuGrid.getValueOfButtonInGrid(b.Name).ToString() == gameControl.getSelectedToolkitButtonName()[7].ToString()) { b.Text = sudokuGrid.getValueOfButtonInGrid(b.Name).ToString(); b.Enabled = false; gameControl.updateNumberInstance(b.Text); // Also highlight new number gameControl.highlightOrDehighlightDisabledButtons(b.Text, true); // Perform a win-check if (gameControl.checkForPlayerWin()) { // Deal with player winning gameControl.highlightOrDehighlightDisabledButtons(b.Text, false); gameAlive = false; TimeSpan ts = stopWatch.Elapsed; string secs, mins; if (ts.Seconds.ToString().Length == 1) { secs = "0" + ts.Seconds; } else { secs = ts.Seconds.ToString(); } if (ts.Minutes.ToString().Length == 1) { mins = "0" + ts.Minutes; } else { mins = ts.Minutes.ToString(); } string finalResult = mins + ":" + secs; int totalSecs = (ts.Minutes * 60) + ts.Seconds; string penaltyEnding = ""; int penaltyNum = gameControl.getPlayerPenalty(); int finalScore = getFinalScore(ts.Minutes, ts.Seconds, penaltyNum, gridDifficulty); penaltyEnding = (gameControl.getPlayerPenalty() == 1) ? "intento" : "intentos"; MessageBox.Show("Sudoku completado! Terminado en " + finalResult + " con " + penaltyNum + " " + penaltyEnding + ".\n" + "Marcador final: " + finalScore); // Add score to leaderboard Leaderboard lb = new Leaderboard(); if (lb.scoreMadeItOntoLeaderboard(finalScore)) { MessageBox.Show("Felicitaciones estan en el top 10!"); NameRequest nr = new NameRequest(lb, gridDifficulty, totalSecs, finalScore); nr.FormClosed += (s, args) => Close(); Hide(); nr.Show(); } // Show leaderboard } } // Show the user they got it wrong - give penalty else { gameControl.incrementPenaltyLabel(this); } }
public void showStartingNumbers(int difficulty, SudokuGrid sg) { // Size of nums dependant on difficulty List <string> nums = new List <string>(); int size = 0; Random rnd = new Random(); switch (difficulty) { case 0: { size = rnd.Next(36, 40); // Below is for testing only //size = rnd.Next(80, 81); break; } case 1: { size = rnd.Next(20, 25); break; } case 2: { size = rnd.Next(10, 15); break; } } // Add our random squares to show by using random row/cols for (int i = 0; i < size; i++) { bool placementOk = false; while (!placementOk) { int row = rnd.Next(0, 9); int col = rnd.Next(0, 9); string res = row + "" + col; if (!nums.Contains(res)) { nums.Add(res); placementOk = true; } } } // Now we get the values at each location, and change playerViewGrid to show these numbers for (int i = 0; i < size; i++) { // Val is the value which will be shown string val = sg.getValueOfButtonInGrid(nums[i]).ToString(); updateNumberInstance(val); string currentNums = nums[i]; string row = currentNums[0].ToString(); string col = currentNums[1].ToString(); int colAsInt = int.Parse(col); int rowAsInt = int.Parse(row); playerViewGrid[rowAsInt][colAsInt].Enabled = false; playerViewGrid[rowAsInt][colAsInt].Text = val; } }