private void secondCardChosenSequence(CardIndex i_CurrentCardIndex, char i_ValueOnCard) { Image valueOnFirstCard = valueOnFirstCard = (this.m_CardButtons[this.m_FirstChosenCard.Row, this.m_FirstChosenCard.Column]).BackgroundImage; Image valueOnSecondCard = valueOnFirstCard = (this.m_CardButtons[i_CurrentCardIndex.Row, i_CurrentCardIndex.Column]).BackgroundImage;; valueOnFirstCard = (this.m_CardButtons[this.m_FirstChosenCard.Row, this.m_FirstChosenCard.Column]).BackgroundImage; if (!valueOnSecondCard.Equals(valueOnFirstCard)) { swapCurrentPlayer(); System.Threading.Thread.Sleep(500); flipBothCardsDownSequence(i_CurrentCardIndex); playComputerTurnIfHisTurn(); } else { if (this.m_GameIncludesComputerPlayer) { this.m_CurrentGame.Computer.RemovePairFromStack(i_ValueOnCard); } this.m_CurrentPlayer.Score++; updateScores(); } if (this.m_CurrentGame.CurrentBoard.IsFull()) { System.Threading.Thread.Sleep(400); this.Close(); } }
/** * If needed, activates the update of the computer player's memory. **/ private void syncDataToCompterLogic(CardIndex i_ChosenCard, char i_OnChosenCard) { if (this.m_GameIncludesComputerPlayer) { this.m_CurrentGame.Computer.SyncDataToCompterLogic(i_ChosenCard, i_OnChosenCard); } }
internal ComputerLogic() { m_BoardMemory = new Dictionary <Char, CardIndex[]>(); m_KnownPairs = new Stack <char>(); m_PrevChoseCardIndex = null; m_CardToChooseNext = null; }
internal char FlipUpCard(CardIndex i_CardLocation) { char cardTrueValue = this.m_TrueBoard.GetCardValue(i_CardLocation); this.r_CurrentBoard.OpenCard(cardTrueValue, i_CardLocation); return(cardTrueValue); }
/* * Shows the card on button and syncs with the computer memory */ private char computerChoseSequence(CardIndex i_CardChosen) { char onCard = this.m_CurrentGame.FlipUpCard(i_CardChosen); showValueOnButton(this.m_CardButtons[i_CardChosen.Row, i_CardChosen.Column], onCard); syncDataToCompterLogic(i_CardChosen, onCard); System.Threading.Thread.Sleep(500); return(onCard); }
private void flipBothCardsDownSequence(CardIndex i_CurrentCardIndex) { this.m_CurrentGame.FlipDownCard(i_CurrentCardIndex); this.m_CurrentGame.FlipDownCard(this.m_FirstChosenCard); this.m_CardButtons[this.m_FirstChosenCard.Row, this.m_FirstChosenCard.Column].Enabled = true; this.m_CardButtons[this.m_FirstChosenCard.Row, this.m_FirstChosenCard.Column].BackgroundImage = null; this.m_CardButtons[this.m_FirstChosenCard.Row, this.m_FirstChosenCard.Column].FlatAppearance.BorderColor = SystemColors.ControlDarkDark; this.m_CardButtons[i_CurrentCardIndex.Row, i_CurrentCardIndex.Column].Enabled = true; this.m_CardButtons[i_CurrentCardIndex.Row, i_CurrentCardIndex.Column].BackgroundImage = null; this.m_CardButtons[i_CurrentCardIndex.Row, i_CurrentCardIndex.Column].FlatAppearance.BorderColor = SystemColors.ControlDarkDark; Application.DoEvents(); }
/** * Checks if a specific card is already known to copmuter memory **/ internal bool CheckIfCardWasPrevioslyFlipped(int i_Row, int i_Column) { CardIndex toCheckIfWasFlipped = new CardIndex(i_Row, i_Column); bool answer = true; foreach (KeyValuePair <char, CardIndex[]> cardUnit in m_BoardMemory) { answer &= !(cardUnit.Value[0].Equals(toCheckIfWasFlipped) || (cardUnit.Value[1] != null && cardUnit.Value[1].Equals(toCheckIfWasFlipped))); } return(answer); }
private void computerTurn() { CardIndex secondChosenCardIndex; char onFirstCard; char onSecondCard; //Computer chooses first card CardIndex firstChosenCardIndex = this.m_CurrentGame.Computer.ChooseFirstCard(this.m_CurrentGame.CurrentBoard); onFirstCard = computerChoseSequence(firstChosenCardIndex); syncDataToCompterLogic(firstChosenCardIndex, onFirstCard); this.m_CurrentGame.Computer.UpdateFirstCardValue(onFirstCard); this.m_FirstChosenCard = firstChosenCardIndex; //Computer chooses second card secondChosenCardIndex = this.m_CurrentGame.Computer.ChooseSecondCard(this.m_CurrentGame.CurrentBoard); onSecondCard = computerChoseSequence(secondChosenCardIndex); secondCardChosenSequence(secondChosenCardIndex, onSecondCard); }
/** * Updates class data structures to contain the info of the last flipped card. **/ internal void SyncDataToCompterLogic(CardIndex i_ChosenCardLocation, char i_OnCard) { CardIndex[] locationsOnBoard = new CardIndex[2]; if (m_BoardMemory.ContainsKey(i_OnCard)) { if (m_BoardMemory.TryGetValue(i_OnCard, out locationsOnBoard)) { if (!locationsOnBoard[0].Equals(i_ChosenCardLocation)) { locationsOnBoard[1] = i_ChosenCardLocation; m_KnownPairs.Push(i_OnCard); } } } else { locationsOnBoard[0] = i_ChosenCardLocation; m_BoardMemory.Add(i_OnCard, locationsOnBoard); } }
private void onButtonClick(object sender, EventArgs e) { if (this.m_CurrentPlayer.IsHuman && (sender as CardButton).Text.Equals("")) { CardIndex currentCardIndex = new CardIndex((sender as CardButton).X, (sender as CardButton).Y); char valueOnCard = this.m_CurrentGame.FlipUpCard(currentCardIndex); showValueOnButton(sender as CardButton, valueOnCard); this.m_AmountOfTotalClicks++; syncDataToCompterLogic(currentCardIndex, valueOnCard); //Enters only if first chosen card of pair if (this.m_AmountOfTotalClicks % 2 == 1) { this.m_FirstChosenCard = currentCardIndex; } else { secondCardChosenSequence(currentCardIndex, valueOnCard); } } }
/** * Chooses second card according to the current info in the data structures including info of first card's flip. **/ internal CardIndex ChooseSecondCard(Board i_CurrentBoard) { CardIndex secondCardChoise = null; CardIndex[] locationsOnBoard = new CardIndex[2]; if (m_CardToChooseNext != null) { secondCardChoise = m_CardToChooseNext; m_CardToChooseNext = null; } else if (m_BoardMemory.TryGetValue(m_OnPrevChosenCard, out locationsOnBoard) && (locationsOnBoard[1] != null)) { if (!locationsOnBoard[0].Equals(m_PrevChoseCardIndex)) { secondCardChoise = locationsOnBoard[0]; m_KnownPairs.Pop(); } } return((secondCardChoise == null) ? ChooseFirstCard(i_CurrentBoard) : secondCardChoise); }
/** * Chooses first card according to the current info in data structures: * if there is a known pair in the stack- flips the first card in the pair. * else- flips the first card on the board that hasn't been flipped yet. **/ internal CardIndex ChooseFirstCard(Board i_CurrentBoard) { CardIndex chosenCardIndex = null; if (m_KnownPairs.Count == 0) { for (int row = 0; row < i_CurrentBoard.NumberOfRows; row++) { for (int column = 0; column < i_CurrentBoard.NumberOfColumns; column++) { if (i_CurrentBoard.IsLegalCardIndex(row, column) && CheckIfCardWasPrevioslyFlipped(row, column)) { chosenCardIndex = new CardIndex(row, column); m_PrevChoseCardIndex = chosenCardIndex; break; } } if (chosenCardIndex != null) { break; } } } else { char charValueToFlip = m_KnownPairs.Pop(); CardIndex[] cardLocations; m_BoardMemory.TryGetValue(charValueToFlip, out cardLocations); m_CardToChooseNext = cardLocations[1]; chosenCardIndex = cardLocations[0]; } return(chosenCardIndex); }
internal char GetCardValue(CardIndex i_CardLocation) { return(this.m_Board[i_CardLocation.Row, i_CardLocation.Column]); }
internal void FlipDownCard(CardIndex i_CardLocation) { this.r_CurrentBoard.CloseCard(i_CardLocation); }
internal bool IsCardLegalIndex(CardIndex i_chosenCard) { return(this.r_CurrentBoard.IsLegalCardIndex(i_chosenCard.Row, i_chosenCard.Column)); }
internal void OpenCard(char i_Letter, CardIndex i_CardLocation) { this.m_Board[i_CardLocation.Row, i_CardLocation.Column] = i_Letter; m_OpenCards++; }
internal void CloseCard(CardIndex i_CardLocation) { this.m_Board[i_CardLocation.Row, i_CardLocation.Column] = '\0'; m_OpenCards--; }