Jump to content
  • Advertisement
phil67rpg

c# game

Recommended Posts

Advertisement

I think, Snake game must be next because it is a litttle more complicated then Tic-Tac-Toe and Pong. But you should not forget about your previous games. It is better to improve them in process when your skills grow up.

Share this post


Link to post
Share on other sites

I have done a lot of research on shuffle a list but I don't know which way is the best and easiest.

Share this post


Link to post
Share on other sites
18 minutes ago, phil67rpg said:

I have done a lot of research on shuffle a list but I don't know which way is the best and easiest.

Question for you. Do you want to actually learn to write a shuffle algorithm, or do you just want to get your game done? :) If the latter, you can surely find some code online in no time. If the former, then please tell us what research you have done so we know what not to suggest. Have you lookedat this https://www.i-programmer.info/programming/theory/2744-how-not-to-shuffle-the-kunth-fisher-yates-algorithm.html

Do you even really want to shuffle the cards, or just give the illusion? You could just choose (and remove) a card from your card collection/deck each time.

Share this post


Link to post
Share on other sites

I have almost solved my problem, I am trying to shuffle a list.

            List<int> cards = new List<int>(52);

            for (int i = 0; i < 2; i++)
            {
                cards.Add(i);
            }
            cards = Shuffle(cards);
            if (e.Button == MouseButtons.Left)
            {
                MessageBox.Show(Convert.ToString(cards));
                Graphics g1 = this.CreateGraphics();
                g1.DrawImage(bitmap_king_spade, 360, 20);
                g1.Dispose();
            }
        }
        public static List<T> Shuffle<T>(List<T> list)
        {
            for (int i = 0; i < list.Count; i++)
            {
                Random rnd = new Random();
                int k = rnd.Next(0, i);
                T value = list[k];
                list[k] = list[i];
                list[i] = value;
            }
            return list;
        }

 

Share this post


Link to post
Share on other sites

Doesn't appear to be very random... 

I put it online here, have a look

https://rextester.com/live/ZCP55965

Try to implement the algorithm at the link I sent you previously.

 

edit: LINQ is pretty cool.. Basically 1 line to shuffle :)

        public static List<T> Shuffle<T>(List<T> list)
        {
            Random rnd = new Random(DateTime.Now.Millisecond);
            var randomised = list.Select(item => new { item, order = rnd.Next() })
                .OrderBy(x => x.order)
                .Select(x => x.item)
                .ToList();

            return randomised;
        }

Adapted from here https://kwilson.io/blog/randomise-a-list-of-anything-in-c-with-linq/

Was an extension method (which is better).

Edited by duke_meister

Share this post


Link to post
Share on other sites

My mistake, you are implementing that algorithm. 

Change the line as such:

int k = i + rnd.Next(0, list.Count - i);

Edited by duke_meister

Share this post


Link to post
Share on other sites

I get a messagebox.show value of System.Collections.Generic.List'1[System.Int32], is this a correct output?

Share this post


Link to post
Share on other sites

No that's only showing you the object reference. Have a look at the code at that link https://rextester.com/live/ZCP55965

It's live code by the way, you can edit and hit run :)

edit: ok if you want it in a message box, add the output to a string and display that.


something like this

            string temp = "";

            foreach(var card in cards)

            {

                temp += $"{card},";              

            }

            MessageBox.Show( temp);

or better

var temp = string.Join(",", cards);

 
Edited by duke_meister
extra method

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!