The below if condition triggers somehow the execution of the else section. I’d highly appreciate any input that could help me understand why this happens.
In my turnmanager.AskForCard, verify if the current player’s guess is correct, i.e. the if segment is for the case of a correct guess, i.e. the selectedPlayer has the SelectedCard. The else condition, considers a wrong guess. In general, the intention is to allow another guess to the currentPlayer if the guess is correct and end the turn otherwise.
The guess is executed by Turn UI objects that allow the currentPlayer to select a given card from a given player and ask for it via a click on the guess button which calls turnmanager.AskForCard.
In a case of wrong guess it works as expected. However, a correct guess triggers somehow another execution, “probably” that of the else/false segment. “probably” as the probability of a wrong guess is much higher than that of correct one. Accordingly, while it seems that the second ‘guess’ executes the else section, I cannot confirm if that is inherently so, or a probable cause of an arbitrary guess.
This ‘secondary’ guess isn’t a reflection of the currentPlayer’s action via the Turn UI objects, but rather an arbitrary call, executed in case of correct guess.
Here are the relevant snippets, in case you can consider them:
TurnManager:
private void HandlePlayerTurn(Player currentPlayer)
{
if (selectedCard == null && selectedPlayer == null)
{
EnableUIMakeGuess();
}
else
{
AskForCard(selectedCard, selectedPlayer);
}
}
private void EnableUIMakeGuess()
{
Debug.Log("MakeGuess method is called");
}
private void AskForCard(Card selectedCard, Player selectedPlayer)
{
if (selectedPlayer != null && selectedCard != null)
{
if (selectedPlayer.HandCards.Contains(selectedCard))
{
Debug.Log("AskForCar guess is correct");
TransferCard(selectedCard, currentPlayer);
CheckForQuartets();
// If the guess is correct and the player's hand isn't empty, allow another guess.
if (!IsPlayerHandEmpty(currentPlayer))
{
// Allow the player to make another guess without drawing a card or ending the turn.
HandlePlayerTurn(currentPlayer);
}
else if (deckCards.Count > 0)
{
// If the player's hand is empty but the deck isn't, draw a card from the deck.
DrawCardFromDeck();
// After drawing a card, re-evaluate the hand.
if (!IsPlayerHandEmpty(currentPlayer))
{
// Allow the player to make another guess.
HandlePlayerTurn(currentPlayer);
}
}
// No need to check for the deck being empty here, as we've already handled that case.
}
else
{
// If the guess is wrong, draw a card from the deck and end the turn.
DisplayMessage($"{selectedPlayer.playerName} does not have {selectedCard.cardName}.");
DrawCardFromDeck();
EndTurn(); // This is the correct place to call EndTurn for an incorrect guess.
}
}
}
PlayerController:
public void OnEventGuessClickRenamed()
{
guessButton.interactable = false;
int selectedPlayerIndex = playersDropdown.value;
int selectedCardIndex = cardsDropdown.value;
int selectedPlayerID = playerIDs[selectedPlayerIndex];
int selectedCardID = cardIDs[selectedCardIndex];
// Find the corresponding Card and Player objects based on IDs
Card selectedCard = CardsPlayerCanAsk.Find(card => card.ID == selectedCardID);
Player selectedPlayer = PlayerToAsk.Find(player => player.ID == selectedPlayerID);
TurnManager turnManager = GameObject.Find("Managers").GetComponent<TurnManager>();
if (turnManager != null)
{
turnManager.OnEventGuessClickRenamed(selectedCard, selectedPlayer);
}
else
{
Debug.LogError("TurnManager not found.");
}
// Re-enable the button after the method is called
guessButton.interactable = true;
}