/// <summary> /// Find and updates the textbox in the sudoku by given coordinates and what to write (string). /// </summary> /// <param name="row"></param> /// <param name="column"></param> /// <param name="text"></param> private void UpdateTextBox(int row, int column, string text) { TextBox textBox = GridContainer.FindName("TxtX" + row + "Y" + column) as TextBox; if (textBox != null) { if (text == "0") { textBox.Text = ""; } else { textBox.Text = text; } } }
/// <summary> /// Utility function called when table initialization is completed. /// </summary> private void InitializationComplete() { // Enables the solve button and menu option. BtnSolve.IsEnabled = true; MenuSolve.IsEnabled = true; // Enables invokation of the OnTextChanged event. isInitialized = true; // Makes the sudoku board editable, except for the originally given boxes. for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { TextBox textbox = GridContainer.FindName("TxtX" + i + "Y" + j) as TextBox; if (textbox != null) { textbox.IsReadOnly = false; textbox.IsEnabled = true; } if (immutableMatrix[j, i] == 1) { textbox.IsReadOnly = true; } } } // Reset undo & redo stacks, because we're starting a new game. undoStack.Clear(); redoStack.Clear(); BtnRedo.IsEnabled = false; MenuRedo.IsEnabled = false; BtnUndo.IsEnabled = false; MenuUndo.IsEnabled = false; MenuSave.IsEnabled = true; }
public void UpdatePlayers(List <Player> players, Dictionary <ulong, int> teams, bool dm) { try { if (players == null) { players = new List <Player>(); } int index = 1; foreach (var player in players) { Application.Current.Dispatcher.Invoke(() => { try { var totalGamesLabel = ((Label)GridContainer.FindName("Label" + index + "PlayerTotalGames")); var dropsLabel = ((Label)GridContainer.FindName("Label" + index + "PlayerDrops")); var winsLabel = ((Label)GridContainer.FindName("Label" + index + "PlayerWins")); var locationLabel = ((Label)GridContainer.FindName("Label" + index + "PlayerLocation")); var gamesLabel = ((Label)GridContainer.FindName("Label" + index + "PlayerGames")); var badRepLabel = ((Label)GridContainer.FindName("Label" + index + "PlayerBadRep")); var goodRepLabel = ((Label)GridContainer.FindName("Label" + index + "PlayerGoodRep")); var teamLabel = ((Label)GridContainer.FindName("Label" + index + "PlayerTeam")); var rank = ""; ((Label)GridContainer.FindName("Label" + index + "PlayerName")).Content = player.Name; if (player.Rank > 0) { rank = player.Rank.ToString(); } ((Label)GridContainer.FindName("Label" + index + "PlayerRank")).Content = rank; if (player.GameStats != null && player.Profile != null) { int totalGames = !dm ? player.GameStats.TotalGamesRM : player.GameStats.TotalGamesDM; int winRatio = !dm ? player.GameStats.WinRatioRM : player.GameStats.WinRatioDM; int dropRatio = !dm ? player.GameStats.DropRatioRM : player.GameStats.DropRatioDM; var games = ""; var wins = ""; var drops = ""; if (player.Profile.ProfilePrivate) { if (player.Profile.ProfileDataFetched.HasValue) { games = "[" + totalGames.ToString() + "]"; wins = "[" + winRatio + "%" + "]"; drops = "[" + dropRatio + "%" + "]"; } else { games = "PRIVATE ACC"; } } else { games = totalGames.ToString(); wins = winRatio + "%"; drops = dropRatio + "%"; } totalGamesLabel.Content = games; winsLabel.Content = wins; dropsLabel.Content = drops; } else { totalGamesLabel.Content = ""; winsLabel.Content = ""; dropsLabel.Content = ""; } if (player.Profile != null && player.Profile.Location != null) { locationLabel.Content = player.Profile.Location; } else { locationLabel.Content = ""; } if (teams.TryGetValue(player.SteamId, out int value)) { if (value > 0) { teamLabel.Content = value; } else { teamLabel.Content = ""; } } else { teamLabel.Content = ""; } if (player.ReputationStats != null) { gamesLabel.Content = player.ReputationStats.Games; badRepLabel.Content = player.ReputationStats.NegativeReputation; goodRepLabel.Content = player.ReputationStats.PositiveReputation; } else { gamesLabel.Content = ""; badRepLabel.Content = ""; goodRepLabel.Content = ""; } UpdateFieldColor(gamesLabel, player, PlayerFields.GAMES); UpdateFieldColor(goodRepLabel, player, PlayerFields.POSITIVE_REPUTATION); UpdateFieldColor(badRepLabel, player, PlayerFields.NEGATIVE_REPUTATION); UpdateFieldColor(totalGamesLabel, player, PlayerFields.TOTAL_GAMES); UpdateFieldColor(winsLabel, player, PlayerFields.WIN_RATIO); UpdateFieldColor(dropsLabel, player, PlayerFields.DROP_RATIO); index++; } catch (Exception e) { Console.WriteLine("Error while updating overlay slot: " + index); Console.WriteLine(e.ToString()); } }); } } catch (Exception e) { Console.WriteLine("Error while updating overlay list"); Console.WriteLine(e.ToString()); } }
/// <summary> /// Event used to update matrix when the used changes a value on the gameboard. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnTextChanged(object sender, TextChangedEventArgs e) { // Checking if it's user input and not input during initialization of the game. if (isInitialized == true && BtnSolve.IsEnabled == true) { // Assign sender textbox to a field and get its coordinates. TextBox textbox = ((TextBox)sender); int col = (int)textbox.GetValue(Grid.ColumnProperty); int row = (int)textbox.GetValue(Grid.RowProperty); row = row - 2; if (textbox.Text != "") { // Checking that value is valid (1-9) before proceeding. bool conversionPassed; int valueToCheck; conversionPassed = int.TryParse(textbox.Text, out valueToCheck); if (conversionPassed && valueToCheck >= 1 && valueToCheck <= 9 && textbox.Text.Length == 1) { // Toggling visibility of button & menu option for Undo. BtnUndo.IsEnabled = true; MenuUndo.IsEnabled = true; var oldValue = visibleMatrix[row, col]; visibleMatrix[row, col] = int.Parse(textbox.Text); var tempTuple = new Tuple <int, int, int>(row, col, oldValue); undoStack.Push(tempTuple); } else { isInitialized = false; textbox.Text = visibleMatrix[row, col] == 0 ? "" : visibleMatrix[row, col].ToString(); isInitialized = true; } } else { // Toggling visibility of button & menu option for Undo. BtnUndo.IsEnabled = true; MenuUndo.IsEnabled = true; var oldValue = visibleMatrix[row, col]; visibleMatrix[row, col] = 0; var tempTuple = new Tuple <int, int, int>(row, col, oldValue); undoStack.Push(tempTuple); } } // end isInitialized check if (IsFilledUp()) { if (IsSolved()) { if (BtnSolve.IsEnabled) { MessageBox.Show(Application.Current.MainWindow, "Congratulations! You've solved the Sudoku!", "Game won", MessageBoxButton.OK); } // Add +1 to the statistics counter for number of games solved in this session. solvedCount++; // Turning buttons off, as the played is expected to start/load a new game. BtnUndo.IsEnabled = false; BtnRedo.IsEnabled = false; BtnSolve.IsEnabled = false; MenuSolve.IsEnabled = false; MenuUndo.IsEnabled = false; MenuRedo.IsEnabled = false; MenuSave.IsEnabled = false; // Making the whole board uneditable. for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { TextBox textbox = GridContainer.FindName("TxtX" + i + "Y" + j) as TextBox; textbox.IsEnabled = false; } } // Stopping the timer. timer.Stop(); } else { if (BtnSolve.IsEnabled == true) { MessageBox.Show(Application.Current.MainWindow, "There's a mistake, game not finished.", "Game not finished", MessageBoxButton.OK); } } } }