}//键盘填数交互 private void TestButton_Click(object sender, RoutedEventArgs e) { if (!GameStart) { ClearPuzzleBoard(); MidLabel.Content = "数独生成中"; MidLabel.Visibility = Visibility.Visible; Thread newGameThread = new Thread(NewGame); newGameThread.Start(); } else { dispatcherTimer.Stop(); if (MessageBox.Show("现有数据将被清空,是否确定开始新游戏?", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.OK) { ClearPuzzleBoard(); dif = null; System.GC.Collect(); MidLabel.Content = "数独生成中"; MidLabel.Visibility = Visibility.Visible; Thread newGameThread = new Thread(NewGame); newGameThread.Start(); } else { dispatcherTimer.Start(); return; } } }//开始游戏按钮交互
}//计算板子中有几个0 private void NewGame() { GameBoard = new Board(); PuzzleBoard = new Board(); DifficultyChooseWindow difchos = null; Dispatcher.Invoke(() => { difchos = new DifficultyChooseWindow { Left = this.Left, Top = this.Top + 150 }; difchos.ShowDialog(); }); Stopwatch sw = new Stopwatch(); if (difchos.IsClosing()) { Dispatcher.Invoke(() => { TestButton.IsEnabled = false; }); sw.Start(); if (difchos.getDifficulty() != 0) { dif = new DifficultyChanger(); dif.GeneratePuzzleBoard((Difficulty)difchos.getDifficulty()); Dispatcher.Invoke(() => { ClearPuzzleBoard(); DrawPuzzleBoard(dif.TargetBoard); }); GameBoard.copyBoard(dif.TargetBoard); PuzzleBoard.copyBoard(dif.TargetBoard); zerocount = CountZero(PuzzleBoard); sw.Stop(); TimeSpan ts = sw.Elapsed; string[] strs = { "简单", "中等", "困难" }; Dispatcher.Invoke(() => { StatusLabel.Content = string.Format("成功生成了{0}难度数独,用时{1}毫秒", strs[difchos.getDifficulty() - 1], ts.TotalMilliseconds); EnableLabel(); }); return; } else { Dispatcher.Invoke(() => { ClearPuzzleBoard(); }); sw.Stop(); Dispatcher.Invoke(() => { MidLabel.Content = "游戏未开始"; }); return; } } }//创建新游戏