private void AnimateRemoveAndRearangeCards(ThreeIndexes threeIndexes) { Storyboard storyBoard = new Storyboard(); NameScope.SetNameScope(gridMain, new NameScope()); for (int i = 0; i < 3; i++) { DoubleAnimation doubleAnimation = new DoubleAnimation(0, TimeSpan.FromSeconds(0.5)); doubleAnimation.FillBehavior = FillBehavior.Stop; int iLocationIndex = threeIndexes.LocationIndex[i]; gridMain.RegisterName("Canvas" + iLocationIndex.ToString(), GetCanvas(iLocationIndex)); Storyboard.SetTargetName(doubleAnimation, "Canvas" + iLocationIndex.ToString()); Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath(Control.OpacityProperty)); storyBoard.Children.Add(doubleAnimation); } storyBoard.Completed += delegate { RemoveCard(threeIndexes.LocationIndex[0]); RemoveCard(threeIndexes.LocationIndex[1]); RemoveCard(threeIndexes.LocationIndex[2]); RearangeCards(); }; storyBoard.Begin(gridMain); }
private void gridMain_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { DependencyObject dep = (DependencyObject)e.OriginalSource; while ((dep != null) && !(dep is Canvas)) { dep = VisualTreeHelper.GetParent(dep); } Canvas canvasCurrent = dep as Canvas; if (canvasCurrent != null) { int iRow = (int)((Canvas)dep).GetValue(Grid.RowProperty); int iCol = (int)((Canvas)dep).GetValue(Grid.ColumnProperty); if (m_arrCardState[iRow * 3 + iCol] == eCatdState.eCardNotSelected) { SelectCard(iRow * 3 + iCol); } else if (m_arrCardState[iRow * 3 + iCol] == eCatdState.eCardSelected) { UnselectCard(iRow * 3 + iCol); UpdateScore(iLowPenelty); } ThreeIndexes threeIndexesSelectedMax = GetSelectedCardsLocation(); if (threeIndexesSelectedMax.GetValidIndexes() == 3) { CheckAndReplaceCards(threeIndexesSelectedMax); } } }
private int GetSetsForCard(int iIndex, out ThreeIndexes threeIndexes) { int iTotalSetsForCard = 0; threeIndexes = new ThreeIndexes(iIndex, -1, -1); for (int j = 0; j < iCardsOnScreen; j++) { if (j == iIndex) { continue; } for (int k = j + 1; k < iCardsOnScreen; k++) { if (k == iIndex) { continue; } if (m_arrCardState[iIndex] != eCatdState.eCardDoesNotExists && m_arrCardState[j] != eCatdState.eCardDoesNotExists && m_arrCardState[k] != eCatdState.eCardDoesNotExists) { int iCardIndex1 = m_Cards[iIndex].Index; int iCardIndex2 = m_Cards[j].Index; int iCardIndex3 = m_Cards[k].Index; if (Card.CheckThreeCards(iCardIndex1, iCardIndex2, iCardIndex3) == true) { threeIndexes.LocationIndex[1] = j; threeIndexes.LocationIndex[2] = k; iTotalSetsForCard++; } } } } return(iTotalSetsForCard); }
private void CheckAndReplaceCards(ThreeIndexes threeIndexes) { int iIndex1 = threeIndexes.LocationIndex[0]; int iIndex2 = threeIndexes.LocationIndex[1]; int iIndex3 = threeIndexes.LocationIndex[2]; if (Card.CheckThreeCards(m_Cards[iIndex1].Index, m_Cards[iIndex2].Index, m_Cards[iIndex3].Index)) { UpdateScore(CalculateAddedPoints()); m_timeLastSuccess = DateTime.Now; int iTotalCardOnBorad = GetTotalCardsOnBoard(); if (iTotalCardOnBorad == iCardsPlayNumber && GetUnusedCardsNumber() > 0) { AnimateReplaceCard(iIndex1, false); AnimateReplaceCard(iIndex2, false); AnimateReplaceCard(iIndex3, false); } else if (GetUnusedCardsNumber() == 0 || iTotalCardOnBorad != iCardsPlayNumber) { AnimateRemoveAndRearangeCards(threeIndexes); } } else { UpdateScore(iHighPenelty); MessageBox.Show("Try Again..."); } UnselectAllCards(); }
private ThreeIndexes GetSelectedCardsLocation() { ThreeIndexes threeIndexesMax = new ThreeIndexes(); int iSelected = 0; for (int i = 0; i < iCardsOnScreen; i++) { if (m_arrCardState[i] == eCatdState.eCardSelected && iSelected < 3) { threeIndexesMax.LocationIndex[iSelected] = i; iSelected++; } } return(threeIndexesMax); }
private void buttonHelpMe_Click(object sender, RoutedEventArgs e) { int iTotalSets = 0; ThreeIndexes threeIndexesOfBestSet = GetSetsParticipationForAllTheCards(out iTotalSets); if (iTotalSets > 0) { int iBestCard = threeIndexesOfBestSet.LocationIndex[0]; if (m_arrCardState[iBestCard] == eCatdState.eCardNotSelected && GetSelectedCardsLocation().GetValidIndexes() > 0) { UnselectAllCards(); } if (m_arrCardState[iBestCard] == eCatdState.eCardNotSelected && GetSelectedCardsLocation().GetValidIndexes() == 0) { SelectCard(iBestCard); UpdateScore(iHelpPenetly); } else if (m_arrCardState[iBestCard] == eCatdState.eCardSelected && GetSelectedCardsLocation().GetValidIndexes() == 1) { SelectCard(threeIndexesOfBestSet.LocationIndex[1]); UpdateScore(iSecondHelpPenetly); if (DEBUG == true) { SelectCard(threeIndexesOfBestSet.LocationIndex[2]); ThreeIndexes threeIndexesSelectedMax = GetSelectedCardsLocation(); CheckAndReplaceCards(threeIndexesSelectedMax); } } } else { UpdateScore(iNoSetsFoundScore); AnimateReplaceCard(12, true); AnimateReplaceCard(13, true); AnimateReplaceCard(14, true); } }
private ThreeIndexes GetSetsParticipationForAllTheCards(out int iTotalSets) { int iMaxSetsPerCard = 0; iTotalSets = 0; ThreeIndexes threeIndexesForBestCard = new ThreeIndexes(); for (int i = 0; i < iCardsOnScreen; i++) { ThreeIndexes threeIndexes; int iCurrSetsPerCard = GetSetsForCard(i, out threeIndexes); iTotalSets += iCurrSetsPerCard; if (iMaxSetsPerCard < iCurrSetsPerCard) { iMaxSetsPerCard = iCurrSetsPerCard; threeIndexesForBestCard.LocationIndex[0] = threeIndexes.LocationIndex[0]; threeIndexesForBestCard.LocationIndex[1] = threeIndexes.LocationIndex[1]; threeIndexesForBestCard.LocationIndex[2] = threeIndexes.LocationIndex[2]; } } iTotalSets = iTotalSets / 3; return(threeIndexesForBestCard); }
private ThreeIndexes GetSetsParticipationForAllTheCards(out int iTotalSets) { int iMaxSetsPerCard = 0; iTotalSets = 0; ThreeIndexes threeIndexesForBestCard = new ThreeIndexes(); for (int i = 0; i < iCardsOnScreen; i++) { ThreeIndexes threeIndexes; int iCurrSetsPerCard = GetSetsForCard(i, out threeIndexes); iTotalSets += iCurrSetsPerCard; if (iMaxSetsPerCard < iCurrSetsPerCard) { iMaxSetsPerCard = iCurrSetsPerCard; threeIndexesForBestCard.LocationIndex[0] = threeIndexes.LocationIndex[0]; threeIndexesForBestCard.LocationIndex[1] = threeIndexes.LocationIndex[1]; threeIndexesForBestCard.LocationIndex[2] = threeIndexes.LocationIndex[2]; } } iTotalSets = iTotalSets / 3; return threeIndexesForBestCard; }
private int GetSetsForCard(int iIndex, out ThreeIndexes threeIndexes) { int iTotalSetsForCard = 0; threeIndexes = new ThreeIndexes(iIndex, -1, -1); for (int j = 0; j < iCardsOnScreen; j++) { if (j == iIndex) { continue; } for (int k = j + 1; k < iCardsOnScreen; k++) { if (k == iIndex) { continue; } if (m_arrCardState[iIndex] != eCatdState.eCardDoesNotExists && m_arrCardState[j] != eCatdState.eCardDoesNotExists && m_arrCardState[k] != eCatdState.eCardDoesNotExists) { int iCardIndex1 = m_Cards[iIndex].Index; int iCardIndex2 = m_Cards[j].Index; int iCardIndex3 = m_Cards[k].Index; if (Card.CheckThreeCards(iCardIndex1, iCardIndex2, iCardIndex3) == true) { threeIndexes.LocationIndex[1] = j; threeIndexes.LocationIndex[2] = k; iTotalSetsForCard++; } } } } return iTotalSetsForCard; }
private ThreeIndexes GetSelectedCardsLocation() { ThreeIndexes threeIndexesMax = new ThreeIndexes(); int iSelected = 0; for (int i = 0; i < iCardsOnScreen; i++) { if (m_arrCardState[i] == eCatdState.eCardSelected && iSelected < 3) { threeIndexesMax.LocationIndex[iSelected] = i; iSelected++; } } return threeIndexesMax; }
private void AnimateRemoveAndRearangeCards(ThreeIndexes threeIndexes) { Storyboard storyBoard = new Storyboard(); NameScope.SetNameScope(gridMain, new NameScope()); for (int i = 0; i < 3; i++) { DoubleAnimation doubleAnimation = new DoubleAnimation(0, TimeSpan.FromSeconds(0.5)); doubleAnimation.FillBehavior = FillBehavior.Stop; int iLocationIndex = threeIndexes.LocationIndex[i]; gridMain.RegisterName("Canvas" + iLocationIndex.ToString(), GetCanvas(iLocationIndex)); Storyboard.SetTargetName(doubleAnimation, "Canvas"+iLocationIndex.ToString()); Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath(Control.OpacityProperty)); storyBoard.Children.Add(doubleAnimation); } storyBoard.Completed += delegate { RemoveCard(threeIndexes.LocationIndex[0]); RemoveCard(threeIndexes.LocationIndex[1]); RemoveCard(threeIndexes.LocationIndex[2]); RearangeCards(); }; storyBoard.Begin(gridMain); }