简体   繁体   中英

Why is my code not counting my held cards correctly?

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);
    }
}

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();        
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM