private void ShowCards(bool turn) { try { var worker = new BackgroundWorker(); worker.WorkerSupportsCancellation = true; worker.DoWork += (sender, args) => { Thread.Sleep(3000); }; worker.RunWorkerCompleted += (sender, args) => { if (FirstCard == null) { return; } FirstCard.Hidden = true; FirstCard.Label.Content = FirstCard.ToString(); SecondCard.Hidden = true; SecondCard.Label.Content = SecondCard.ToString(); SetTurn(turn); FirstCard = null; SecondCard = null; }; worker.RunWorkerAsync(); } catch (Exception e) { Console.WriteLine(e); } }
private void Label_OnClick(object sender, RoutedEventArgs e) { if (!Turn) { return; } var label = sender as Label; if (!(label?.DataContext is GridCard card)) { return; } if (!CheckCard(card)) { return; } label.Content = Board[card.Row, card.Column]; var message = new GameMessage { SenderNick = Client.Nick, ReceiverNick = OpponentNick, GameId = GameId, Row = card.Row, Column = card.Column }; Client.CardRevealed(message); if (FirstCard != null && SecondCard != null) { if (FirstCard.Value != SecondCard.Value) { SetTurn(false); ShowCards(false); } else { ChallengerScoreL.Content = ++MyScore; if (CheckEndOfGame()) { SetTurn(false); EndOfTheGame(message); } FirstCard = null; SecondCard = null; } } }
private void InitGameBoard(InvitationMessage message) { Board = new GridCard[message.GameSize.Height(), message.GameSize.Width()]; var index = 0; for (var i = 0; i < message.GameSize.Height(); i++) { for (var j = 0; j < message.GameSize.Width(); j++) { Board[i, j] = new GridCard(i, j, message.GameCards[index++]); } } }
private void GameOver() { GameId = 0; Board = null; FirstCard = null; SecondCard = null; Turn = false; MyScore = 0; ChallengerScoreL.Content = MyScore; OpponentNick = ""; OpponentNickL.Content = OpponentNick; OpponentScore = 0; OpponentScoreL.Content = OpponentScore; GameScoreGrid.Visibility = Visibility.Hidden; GameBoardGrid.Visibility = Visibility.Hidden; MenuPnl.Visibility = Visibility.Visible; }
private bool CheckCard(GridCard card) { if (!card.Hidden) { return(false); } if (FirstCard == null) { FirstCard = card; FirstCard.Hidden = false; return(true); } SecondCard = card; SecondCard.Hidden = false; return(true); }
private void OnRevealCard(GameMessage message) { if (Turn) { return; } var card = Board[message.Row, message.Column]; if (!CheckCard(card)) { return; } card.Label.Content = Board[card.Row, card.Column]; if (FirstCard != null && SecondCard != null) { if (FirstCard.Value != SecondCard.Value) { ShowCards(true); } else { OpponentScoreL.Content = ++OpponentScore; if (CheckEndOfGame()) { SetTurn(false); EndOfTheGame(message); } FirstCard = null; SecondCard = null; } } }