Advertisement

c# console blackjack game

Started by February 27, 2019 12:52 AM
24 comments, last by Tom Sloper 5 years, 6 months ago

I want to use the if loop on a list but it does give a compiler error. I want to compare two list items with the && operator. well  I tried Convert.ToInt32() and it worked. 


        static void Main(string[] args)
        {
            List<int> cards = new List<int>(52);
            for(int i=0; i<52;i++)
            {
                cards.Add(i);
            }
            cards = Shuffle(cards);

            if (cards >=0 && cards <=8)
            {
                Console.Write(cards + 2);
                Console.WriteLine(suit.S);
            }
        }

 


        static void Main(string[] args)
        {
            List<int> cards = new List<int>();
            for(int i=0; i<52;i++)
            {
                cards.Add(i);
            }
            cards = Shuffle(cards);
            int card = Convert.ToInt32(cards);

            if (card >=0 && card <=8)
            {
                Console.Write(card + 2);
                Console.WriteLine(suit.S);
            }

the int card = ConvertToInt32(cards) throws a runtime error.

Advertisement

After over a decade posting on his forum, how have you not figured out how to use google yet?  List has built in functions that return the size...or Count() of the List in question.  I leave the rest up to you.

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

Let's be nice.

-- Tom Sloper -- sloperama.com

thanks tom, I think I have a valid question. I want to use the fisher-yates shuffle.

I don't know what your shuffle routine is doing but it looks to me after you shuffle the cards you are trying to retrieve a single card from the deck for display. Using Convert.ToInt32(cards) would not be the way to do this because it would be converting the entire list to an int when you want to be converting one item from the list to an int.

Advertisement

thanks kseh so how should I convert one item from  a list to an int?

I think what you intended was

int card = cards[0];

That would get the first item in the list you created at the beginning of your code.

From what I can see in the code you have, there's still work you needed to show more than just the first card in your list. Also, I suspect you'll find what you're writing to the screen isn't going to be the result you expect. Hopefully you'll be able to figure out the issues on your own.

If you are having a difficult time trying to simulate a deck of cards, a simpler plan to start with would be just generating random numbers from 1 to 10.

Good luck with your projects.

thanks kseh I will work on my game. I might try an array instead of a list.

I am implementing the fisher-yates shuffle function. the line int card=Shuffle() does not work.


        static Random r = new Random();

        static public void Shuffle(int[] deck)
        {
            for(int n=deck.Length-1; n>0;--n)
            {
                int k = r.Next(n + 1);
                int temp = deck[n];
                deck[n] = deck[k];
                deck[k] = temp;
            }
        }

        enum suit {S=1,H,D,C};
        enum face {J=11,Q,K,A};

        static void Main(string[] args)
        {
            int card = Shuffle();

            if (card >=0 && card <=8)
            {
                Console.Write(card + 2);
                Console.WriteLine(suit.S);
            }

 

This topic is closed to new replies.

Advertisement