4

Hey so i am coding for an assesment and it is due very soon, i made a piece of code that is suppose to add up the value of my held cards, i am using unity and i have put all the sprites in a certain order(I put all of the aces first, then the 2's and so on) so this way when i count it i just divide by 4(this is the way my teacher told me to do it. So it does count but not properly, sometimes it counts backwards and i cannot find out how it is actually counting, can someone please point out what is wrong with it. here is the link to the whole project. https://drive.google.com/file/d/1Karttf7_zmNlASE4bjKVjRAuinMrLMdw/view?usp=sharing

And here is the code if you just want to look at it, its not done though im only up to the counter part.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameManager : MonoBehaviour
{
    [SerializeField]
    int[] Cards = new int[52];
    
    [SerializeField]
    GameObject[] PlayerCards;
    
    [SerializeField]
    GameObject[] DealerCards;
    
    [SerializeField]
    Sprite[] CardSprite;
    
    [SerializeField]
    Text HeldValue;
    int temp, Card1, Card2, dealt, PlayerDealt, DealerDealt, CardValue, DealCardValue;
    
    void Start()
    {
        shuffle();
        RestartGame();
        Dealcards();
        CardValue = 0;
        DealCardValue = 0;
    }
    
    void RestartGame()
    {
        for (int i = 0; i < PlayerCards.Length; i++)
        {
            PlayerCards[i].SetActive(false);
            DealerCards[i].SetActive(false);
        }
    }
    
    void shuffle()
    {
        dealt = 0;
    
        for (int i = 0; i < 52; i++)
        {
            Cards[i] = i + 1;
        }
        for (int i = 0; i < 100; i++)
        {
            Card1 = Random.Range(0, 51);
            Card2 = Random.Range(0, 51);
            temp = Cards[Card1];
            Cards[Card1] = Cards[Card2];
            Cards[Card2] = temp;
        }
        
    }
    
    void Dealcards()
    {
        PlayerCards[0].SetActive(true);
        PlayerCards[0].GetComponent<Image>().sprite = CardSprite[Cards[0]];
        CardValue += CalcCardValue(0);
        PlayerCards[1].SetActive(true);
        PlayerCards[1].GetComponent<Image>().sprite = CardSprite[Cards[1]];
        CardValue += CalcCardValue(1);
        DealerCards[0].SetActive(true);
        DealerCards[0].GetComponent<Image>().sprite = CardSprite[Cards[2]];
        DealCardValue += CalcCardValue(2);
        DealerCards[1].SetActive(true);
        DealerCards[1].GetComponent<Image>().sprite = CardSprite[Cards[3]];
        DealCardValue += CalcCardValue(3);
        HeldValue.text = CardValue.ToString();
    
        dealt += 4;
        PlayerDealt = 2;
        DealerDealt = 2;
    }
    
    public void Hit()
    {
        PlayerCards[PlayerDealt].SetActive(true);
        PlayerCards[PlayerDealt].GetComponent<Image>().sprite = CardSprite[Cards[dealt]];
    
        CardValue += CalcCardValue(dealt);
        PlayerDealt += 1;
        dealt += 1;
    
        HeldValue.text = CardValue.ToString();
    }
    
    int CalcCardValue(int i)
    {
        return Mathf.CeilToInt(Cards[i] / 4);
    }
}
Xetera
  • 1,390
  • 1
  • 10
  • 17
That Guy
  • 55
  • 1
  • 6
  • Hi, I would highly recommend that you remove the link to your whole project. If this is an assessment then it could be found and taken by fellow classmates which would get you in trouble for sharing/stealing work (even if it is your own) – The Grand J Aug 07 '20 at 05:58
  • @TheGrandJ I tried finding a solution and even my teacher doesnt know what is going on and he doesnt want to help me anymore. – That Guy Aug 07 '20 at 06:00
  • what do you mean by "counting wrong"? – Patrick Artner Aug 07 '20 at 06:01
  • @PatrickArtner https://streamable.com/vx487m – That Guy Aug 07 '20 at 06:07

1 Answers1

3

You are only ever calculating

CalcCardValue(1)  ...  CalcCardValue(4)

inside Dealcards() because you only ever call it with values from 1 to 4. You need to call CalcCardValue with the value of the Card (0...51) though.

You probably ment to do:

void Dealcards()
{
    for (int i = 0; i < 2; i++)
    {
        PlayerCards[i].SetActive(true);
        PlayerCards[i].GetComponent<Image>().sprite = CardSprite[Cards[i]];
        CardValue += CalcCardValue(Cards[i]);  # fix here ... and below the same

        DealerCards[i].SetActive(true);
        DealerCards[i].GetComponent<Image>().sprite = CardSprite[Cards[i+2]];
        DealCardValue += CalcCardValue(Cards[i+2]);
    }
    HeldValue.text = CardValue.ToString();

    dealt += 4;
    PlayerDealt = 2;
    DealerDealt = 2;
}

public void Hit()
{
    PlayerCards[PlayerDealt].SetActive(true);
    PlayerCards[PlayerDealt].GetComponent<Image>().sprite = CardSprite[Cards[dealt]];

    CardValue += CalcCardValue(Cards[dealt]);  # and here as well
    PlayerDealt += 1;
    dealt += 1;

    HeldValue.text = CardValue.ToString();        
}
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • 1
    Now you where faster ;) just wanted to propose a loop as well – derHugo Aug 07 '20 at 06:16
  • So instead of having to add 2 more lines for each and the player card dealing, i just use what you have there? @Patrick Artner – That Guy Aug 07 '20 at 06:29
  • @derHugo that one still doesnt work im not sure why, am i just suppose to copy and paste that in to my code replacing the code before? – That Guy Aug 07 '20 at 06:35
  • @OGXirvin yes. As a sidenote, for testing you might want to skip shuffling (comment that part out) so you know exactly in which order the cards in Cards are. If I understood you correctly that would be 1 for Cards 0-3, 2 for Cards 4-7, 3 for Cards 8-11 etc. Without shuffling you can check your numbers more easily. – Patrick Artner Aug 07 '20 at 06:43
  • @PatrickArtner i did that and all it gives me are aces – That Guy Aug 07 '20 at 06:48
  • @OGXir .. the values inside Cards are 1...52 to start with, `CalcCardValue(int i)` for 1..4 will give you 0,0,0,1 - - and `CardSprite[Cards[0]]` when `Cards[0]==52`will crash your game - you might want to stick using numbers 0...51 instead of switching to 1...52 in `shuffle()` when preparing your array. I'll take a look into it after work again. – Patrick Artner Aug 07 '20 at 06:55