private void PerformOpponentMove(MinesweeperItemCellDefinition cell) { OpponentLost = true; MinesweeperItem item = Match.Board.FindItemAt(cell); Match.Board.EvaluateItem(item); }
private Button GetGridButton(MinesweeperItem item) { try { //creates a button Button button = new Button(); //stores the button on the item tag item.tag = button; //stores the item on the tag button button.Tag = item; button.Content = " "; button.Width = CELL_SIDE; button.Height = CELL_SIDE; button.FontSize = 16; button.FontWeight = FontWeights.Bold; button.BorderBrush = Brushes.White; button.BorderThickness = new Thickness(1, 1, 1, 1); button.Background = Brushes.LightGray; button.Click += GridButton_Click; return(button); } catch (Exception ex) { HandleException(ex); return(null); } }
private void GridButton_Click(object sender, RoutedEventArgs e) { OpponentLost = false; if (Mode == GameMode.Mode.Online) { if (!IsMyTurn()) { return; } } try { Button button = (Button)sender; MinesweeperItem item = (MinesweeperItem)button.Tag; if (Mode == GameMode.Mode.Online) { Thread notifyFinishTurn = new Thread(() => Client.FinishTurn(item.cell, Match.HomePlayer, Match.AwayPlayer, UserName)); notifyFinishTurn.Start(); FlipTurnImage(); string playerToNotfiy = (Type == PlayerType.Home) ? Match.AwayPlayer : Match.HomePlayer; Thread notifyFlipTurnImage = new Thread(() => Client.FlipTurnImage(playerToNotfiy)); notifyFlipTurnImage.Start(); } Match.Board.EvaluateItem(item); if (IsAllCellsOpened()) { if (Mode == GameMode.Mode.Online) { Thread notifyServerAboutTie = new Thread(() => Client.GameFinishInTie(Match.HomePlayer, Match.AwayPlayer)); notifyServerAboutTie.Start(); } else { MessageBox.Show("You successfully opened all cells :)", "Congratulations", MessageBoxButton.OK, MessageBoxImage.Information); this.Close(); } } } catch (Exception ex) { HandleException(ex); } }
private void GameGrid_gameOver(MinesweeperItem item) { if (Mode == GameMode.Mode.Online) { if (!OpponentLost) { Thread notifyServer = new Thread(() => Client.PlayerLose(Match.HomePlayer, Match.AwayPlayer, UserName)); notifyServer.Start(); MessageBox.Show("You lost the game :(", "Sorry", MessageBoxButton.OK, MessageBoxImage.Information); MatchFinish = true; this.Close(); } } else { MessageBox.Show("You lost the game :(", "Sorry", MessageBoxButton.OK, MessageBoxImage.Information); this.Close(); } }
private void GameGrid_itemAdded(MinesweeperItem item) { //TODO }
private void GameGrid_cellOpeningCompleted(MinesweeperItem item) { //this event will be raised after calling setAdjacentCells() method try { //gets the button from the item tag (see getGridButton method) Button button = (Button)item.tag; Image img = new Image(); switch (item.type) { case MinesweeperItemType.MinesweeperItem_Empty: button.Content = ""; button.Background = Brushes.Cyan; break; case MinesweeperItemType.MinesweeperItem_Mine: img.Source = new BitmapImage(new Uri(System.AppDomain.CurrentDomain.BaseDirectory + "/Resources/bomb.png")); button.Content = img; button.Background = Brushes.OrangeRed; break; case MinesweeperItemType.MinesweeperItem_MineWarning: button.Background = Brushes.DeepSkyBlue; if (item.value.Equals(1)) { img.Source = new BitmapImage(new Uri(System.AppDomain.CurrentDomain.BaseDirectory + "/Resources/one.png")); button.Content = img; } else if (item.value.Equals(2)) { img.Source = new BitmapImage(new Uri(System.AppDomain.CurrentDomain.BaseDirectory + "/Resources/two.png")); button.Content = img; } else if (item.value.Equals(3)) { img.Source = new BitmapImage(new Uri(System.AppDomain.CurrentDomain.BaseDirectory + "/Resources/three.png")); button.Content = img; } else if (item.value.Equals(4)) { img.Source = new BitmapImage(new Uri(System.AppDomain.CurrentDomain.BaseDirectory + "/Resources/four.png")); button.Content = img; } else if (item.value.Equals(5)) { img.Source = new BitmapImage(new Uri(System.AppDomain.CurrentDomain.BaseDirectory + "/Resources/five.png")); button.Content = img; } else if (item.value.Equals(6)) { img.Source = new BitmapImage(new Uri(System.AppDomain.CurrentDomain.BaseDirectory + "/Resources/six.png")); button.Content = img; } else if (item.value.Equals(7)) { img.Source = new BitmapImage(new Uri(System.AppDomain.CurrentDomain.BaseDirectory + "/Resources/seven.png")); button.Content = img; } else if (item.value.Equals(8)) { img.Source = new BitmapImage(new Uri(System.AppDomain.CurrentDomain.BaseDirectory + "/Resources/eight.png")); button.Content = img; } else if (item.value.Equals(9)) { img.Source = new BitmapImage(new Uri(System.AppDomain.CurrentDomain.BaseDirectory + "/Resources/nine.png")); button.Content = img; } break; case MinesweeperItemType.MinesweeperItemType_None: break; } //if items is opened, remove the click event handler from the button if (item.type != MinesweeperItemType.MinesweeperItemType_None) { button.Click -= GridButton_Click; } } catch (Exception ex) { HandleException(ex); } }