简体   繁体   English

为什么我的代码没有正确计算我持有的牌?

[英]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嘿,所以我正在编写评估代码,并且很快就要到期了,我编写了一段代码,假设可以将我持有的卡片的价值相加,我正在使用统一,并且我已将所有精灵按特定顺序放置(我把所有的 A 先放,然后是 2 等等)所以这样当我数它时,我只需除以 4(这是我的老师告诉我的方法。所以它确实计数但不正确,有时它倒数,我不知道它实际上是如何计数的,有人可以指出它有什么问题。这是整个项目的链接。https://drive.google.com/file/d/1Karttf7_zmNlASE4bjKVjRAuinMrLMdw/view ?usp=分享

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.Dealcards()中,因为您只使用 1 到 4 的值调用它。不过,您需要使用 Card (0...51) 的值调用 CalcCardValue。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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