I'm creating a poker game in Unity using C#. I have an array card sprites which I assign values to, and then randomly deal. Despite getting the correct random cards, my code is setting their value to 1, 2, 3, etc.
public class DeckScript : MonoBehaviour
{
public Sprite[] cardSprites;
int[] cardValues = new int[53];
int currentIndex = 0;
public int cardValueNum = 0;
[...]
void GetCardValues()
{
int num = 0;
// Loop to assign values
for (int i = 0; i < cardSprites.Length; i++)
{
num = i;
// Count up to the amount of cards, 52
num %= 13;
// If there is a remainder after x/13, then remainder
// is used as the value, unless over 10, then use 10
if (num > 10 || num == 0)
{
num = 10;
}
cardValues[i] = num++;
}
currentIndex = 1;
}
public void Shuffle()
{
for(int i = cardSprites.Length -1; i > 0; --i)
{
int j = Mathf.FloorToInt(Random.Range(0.0f, 1.0f) * cardSprites.Length - 1) + 1;
Sprite face = cardSprites[i];
cardSprites[i] = cardSprites[j];
cardSprites[j] = face;
int value = cardValues[j];
cardValues[j] = value;
}
}
public int DealCard(CardScript cardScript)
{
cardScript.SetSprite(cardSprites[currentIndex]);
cardScript.SetValue(cardValues[currentIndex]);
Debug.Log("Card value is " + cardScript.GetValueOfCard());
currentIndex++;
return cardScript.GetValueOfCard();
}
[...]
public int GetCard()
{
// Get a card, use deal card to assign sprite and value to card on table
int cardValue = deckScript.DealCard(hand[cardIndex].GetComponent<CardScript>());
// Show card on game screen
hand[cardIndex].GetComponent<Renderer>().enabled = true;
// Add card value to running total of the hand
handValue += cardValue;
cardIndex++;
return handValue;
}
This is one of 4 scripts I'm using, but I believe it has everything relevant. (This is also my first time using StackOverflow, so if I missed something I apologize)
The intended result should be 4 random numbers, all between 1 and 10.
I've traced through my functions to find where the shuffle stops working and starts just listing 1-4. The current placement of the debug in DealCard prints 1, 2, 3, 4, so I'm guessing there may be an issue here.
I loosely followed a tutorial for a lot of the this code, but I'm fairly certain I got everything correct. My best guess is there is something wrong in either GetCard or DealCard.