Domanda

So I have this class that draws a random card for a blackjack game. When I try to calculate the score of the starting hand , theoretically it should work but I don't know why it doesn't . And , after I run the program a few times and it runs well, it throws a StackOverflowException.

public static class DrawCard
{
    static string[] culoare = new string[4] { "r", "g", "b", "v" };
    static string[] numere = new string[13] { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" };
    public static Rectangle carteRec = new Rectangle(50, 50, 100, 100);
    public static Rectangle sourceRec = new Rectangle(0, 0, 220, 314);
    public static GraphicsUnit unit = GraphicsUnit.Pixel;
    public static Image carte1, carte2;
    private static string carte;
    private static int _nr, _cul;
    public static int scor1, scor2, _scor;


    public static string randomcard(Random r, int scor)
    {

        //string carte = "";
        //randomizam culoarea si numarul cartii
        _nr = r.Next(0, 13);
        _cul = r.Next(0, 4);
        if (_nr >= 0 && _nr <= 8)
        {
            scor += int.Parse(numere[_nr]);
        }
        else if (_nr > 8 && _nr <= 12)
        {
            scor += 10;
        }
        carte = (string)numere[_nr] + (string)culoare[_cul];
        return carte;
    }



    public static int Scor
    {
        get { return _scor; }
        set { _scor = value; }
    }

    public static void StartHand(Graphics desen, Rectangle destRec1, Rectangle destRec2)
    {
        string k = randomcard(new Random(), scor1);
        string j = randomcard(new Random(), scor2);
        //_scor = scor1 + scor2;
        if (k != j)
        {
            carte2 = Image.FromFile(@"C:\Users\Raul\Documents\visual studio 2010\Projects\Blackjack\Blackjack\Carti\" + j + ".jpg");
            carte1 = Image.FromFile(@"C:\Users\Raul\Documents\visual studio 2010\Projects\Blackjack\Blackjack\Carti\" + k + ".jpg");
            desen.DrawImage(carte1, destRec1, sourceRec, unit);
            desen.DrawImage(carte2, destRec2, sourceRec, unit);
        }
        else
        {
            StartHand(desen, destRec1, destRec2);

        }
    }
  }
È stato utile?

Soluzione

You must not create multiple new instances of Random in that way. The documentation says:

The default seed value is derived from the system clock and has finite resolution. As a result, different Random objects that are created in close succession by a call to the default constructor will have identical default seed values and, therefore, will produce identical sets of random numbers. This problem can be avoided by using a single Random object to generate all random numbers.

This means that at the start of StartHand you typically have k == j and hence make the recursive call. In turn this leads to the stack overflow.

Create exactly one instance of the random number generator. You might like to use a better seed than the system clock. All random numbers should come from this single instance of Random.

You would be well advised to use a loop rather than recursion as a means to ensure that k differs from j.

Your attempts to score are completely wrong. You appear to attempt that in randomcard. But you only score into a local variable whose value is not remembered. I think you meant for the score parameter of randomcard to be an out parameter.

I would suggest that you need to adopt a more functional style of coding. Have a function that draws a card. Have the card be represented by a struct with suit and value members. Have a function that returns score given a value. Stop using so many static members and use locals and return values where possible.

Altri suggerimenti

Your random suit-number has five values (_cul = r.Next(0, 4);) and your random card-number fourteen values (_nr = r.Next(0, 13);), one too many of each.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top