/// <summary> /// It is used to call deserialization of a previously saved game. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnToLoadGame_Click(object sender, EventArgs e) { Sudoku game = BinaryDeserializeGame(); sudoku = game; if (game == null) { //MessageBox.Show("Game e null"); return; } else { game = BinaryDeserializeGame(); if (game is Standard) { standard = (Standard)game; //sudoku = standard; setStandardTableView(); } else { squiggly = (Squiggly)game; //sudoku = squiggly; setSquigglyTableView(); } timerlabel.Text = (new TimeSpan(game.ticks)).ToString(); setGrid(gameType.Standard, Difficulty.Easy); //can have any combination of parameters. changeView(1); timer.Start(); } }
/// <summary> /// Takes the selected values for gameType and Difficulty. /// Creates a new game with these parameters, /// sets the grid and starts the timer. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void startGame() { SaveScoreHasAppeared = false; standard = null; squiggly = null; sudoku = null; gameType type; if (radioSquigglyMode.Checked) { setSquigglyTableView(); type = gameType.Squiggly; } else { setStandardTableView(); type = gameType.Standard; } Difficulty level = Difficulty.Easy; if (radioMediumMode.Checked) { level = Difficulty.Medium; } else if (radioHardMode.Checked) { level = Difficulty.Hard; } changeView(1); dataGridView.Focus(); dataGridView.ClearSelection(); clearHighlight(); setGrid(type, level); ticks = sudoku.ticks; timer.Start(); }
/// <summary> /// This is used to create a new game object, according to the given parameters, /// to fill the dataGridView with the starting values, /// and to update the text shown on the form. /// </summary> /// <param name="type"></param> /// <param name="level"></param> public void setGrid(gameType type, Difficulty level) { for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { dataGridView.Rows[i].Cells[j].Value = ""; } } if (standard != null || type == gameType.Standard) { if (standard == null) { squiggly = null; standard = new Standard(level); sudoku = standard; } for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if (standard.mask[i, j] != 0) { dataGridView.Rows[i].Cells[j].Value = standard.mask[i, j]; } ColorMap[i, j] = Color.White; } } this.Text += " - Standard " + (standard.diff == Difficulty.Easy ? "(easy) " : (standard.diff == Difficulty.Medium ? "(medium)" : "(hard)")); } else { if (squiggly == null) { standard = null; schemeBuilder(); Random random = new Random(); squiggly = null; bool Completed = false; while (squiggly == null && !Completed) { squiggly = Limex(() => new Squiggly(level, Schemes[random.Next(0, Schemes.Count)]), 4000, out Completed); sudoku = squiggly; } } for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if (squiggly.mask[i, j] != 0) { dataGridView.Rows[i].Cells[j].Value = squiggly.mask[i, j]; } dataGridView.Rows[i].Cells[j].Style.BackColor = colors[squiggly.scheme[i, j]]; ColorMap[i, j] = colors[squiggly.scheme[i, j]]; } } this.Text += " - Squiggly " + (squiggly.diff == Difficulty.Easy ? "(easy) " : (squiggly.diff == Difficulty.Medium ? "(medium)" : "(hard)")); } LockCellMap(); }
/// <summary> /// Takes action according to the keyboard button pressed. /// - If it is a number from numpad or from the main keyboard, /// it writes it down or overwrites the current number in that field. /// - If it is up/down/left/right/tab/enter, /// it selects the respective cell. /// - If it is escape/backspace or delete, /// it deletes the value in the cell. /// - Else, it doesn't do anything. /// - It checks if the grid has been solved, /// and prompts a window according to the result. /// - If the grid is full but it isn't a valid solution to the game, /// it doesn't do anything. /// - If the grid is solved but the time does not make it in the top 5, /// it prompts a MessageBox /// - If the grid is solved AND the time makes it in the top 5, /// it prompts a new form, that asks for the player's name /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dataGridView_KeyDown(object sender, KeyEventArgs e) { if (standard == null && squiggly == null) { System.Media.SystemSounds.Asterisk.Play(); return; } if (dataGridView.SelectedCells.Count > 0) { var selected = dataGridView.SelectedCells[0]; int sel_i = selected.RowIndex; int sel_j = selected.ColumnIndex; if (CellMap[sel_i, sel_j] == LOCKED) { if (!(e.KeyCode == Keys.Enter || e.KeyCode == Keys.Tab || e.KeyCode == Keys.Up || e.KeyCode == Keys.Down || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right)) { System.Media.SystemSounds.Asterisk.Play(); } return; } if (!(e.KeyValue >= 49 && e.KeyValue <= 57 || e.KeyValue >= 97 && e.KeyValue <= 105)) { if (e.KeyValue == 27 || e.KeyValue == 8 || e.KeyValue == 46) // Same as e.KeyCode == Keys.Enter etc. { selected.Value = ""; if (standard != null) { standard.userGrid[selected.RowIndex, selected.ColumnIndex] = 0; } else { squiggly.userGrid[selected.RowIndex, selected.ColumnIndex] = 0; } } else if (!(e.KeyCode == Keys.Enter || e.KeyCode == Keys.Tab || e.KeyCode == Keys.Up || e.KeyCode == Keys.Down || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right)) { System.Media.SystemSounds.Asterisk.Play(); } } else { int value = -1; if (e.KeyValue >= 49 && e.KeyValue <= 57) { selected.Value = e.KeyValue - 48; value = e.KeyValue - 48; } else { selected.Value = e.KeyValue - 96; value = e.KeyValue - 96; } if (standard != null) { standard.userGrid[sel_i, sel_j] = value; } else if (squiggly != null) { squiggly.userGrid[sel_i, sel_j] = value; } highlightSelectedNumber(); } } if ((standard != null && Sudoku.isSolved(standard.userGrid, Standard.scheme)) || (squiggly != null && Sudoku.isSolved(squiggly.userGrid, squiggly.scheme))) { long time = ticks; timer.Stop(); DarkenGrid(); //MessageBox.Show("Congratulations!!!"); System.Media.SoundPlayer finishSoundPlayer = new System.Media.SoundPlayer(@"C:\Windows\Media\tada.wav"); System.Media.SoundPlayer finish_no_high_SoundPlayer = new System.Media.SoundPlayer(@"C:\Windows\Media\chimes.wav"); gameType type = standard == null ? gameType.Squiggly : gameType.Standard; Difficulty level = standard == null ? squiggly.diff : standard.diff; int d = level == Difficulty.Easy ? 0 : (level == Difficulty.Medium ? 1 : 2); SaveScoreHasAppeared = true; if (HS.HS[type][d].highScores.Count == 5 && ticks > HS.HS[type][d].highScores[4].time) { finish_no_high_SoundPlayer.Play(); MessageBox.Show("\t\tCongratulations!!! \n I'm sorry but you didn't make it in the top 5."); } else { finishSoundPlayer.Play(); Form2 form2 = new Form2(this, ticks, type, level); form2.Show(); form2.name.Focus(); } standard = null; squiggly = null; sudoku = null; } }