public void OnCheckedLianLianButton(object sender, RoutedEventArgs e) { LianLianKanButton button = sender as LianLianKanButton; if (_pairButton[0] != null) { _pairButton[1] = button; _pairButton[0].IsChecked = false; _pairButton[1].IsChecked = false; //判断是否是一对 bool success = false; if (_pairButton[0].PairID == _pairButton[1].PairID) { //只有是一对时,才去找路径。 var path = this.FindPathByRecursion(_mapReflex[_pairButton[0].Row, _pairButton[0].Col], _mapReflex[_pairButton[1].Row, _pairButton[1].Col]); if (path != null) { _pairButton[0].Checked -= OnCheckedLianLianButton; _pairButton[1].Checked -= OnCheckedLianLianButton; mapGrid.Children.Remove(_pairButton[0]); mapGrid.Children.Remove(_pairButton[1]); _mapReflex[_pairButton[0].Row, _pairButton[0].Col].HasButton = false; _mapReflex[_pairButton[1].Row, _pairButton[1].Col].HasButton = false; _remainNumberOfButton -= 2; if (_remainNumberOfButton == 0) { GameOverSound.Play(); RaiseEvent(new RoutedEventArgs(GameOverEvent)); } else { succeedToMatchSound.Play(); } success = true; } } _pairButton[0] = null; _pairButton[1] = null; //播放声音 if (success == false) { failToMatchSound.Play(); } } else { _pairButton[0] = button; } }
public void ResetGame() { int mapRow = this.MapRow, mapCol = MapCol; //先停止游戏 StopGame(); //重置地图映像 for (int row = 0; row < mapRow; row++) { for (int col = 0; col < mapCol; col++) { _mapReflex[row, col].HasButton = false; } } //添加连连看按钮 _remainNumberOfButton = (this.MapRow - 2) * (this.MapCol - 2); var random = new Random(DateTime.Now.Millisecond); for (int num = 0; num < _remainNumberOfButton; num += 2) { int pairID = random.Next(LianLianKanBrush.NumberOfBrush); for (int count = 0; count < 2; count++) { int localRow, localCol; do { localRow = random.Next(1, mapRow - 1); localCol = random.Next(1, mapCol - 1); } while (_mapReflex[localRow, localCol].HasButton == true); var button = new LianLianKanButton(pairID, localRow, localCol, LianLianKanBrush.GetSolidColorBrush(pairID)); button.ToolTip = $"{pairID}"; button.Checked += OnCheckedLianLianButton; Grid.SetRow(button, localRow); Grid.SetColumn(button, localCol); mapGrid.Children.Add(button); _mapReflex[localRow, localCol].HasButton = true; _mapReflex[localRow, localCol].Row = localRow; _mapReflex[localRow, localCol].Col = localCol; } //for } //for }