Public Group

# c# game

## Recommended Posts

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 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 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 on other sites

I do want to use the fisher yates shuffle algorithm. I will do more research on shuffling.

##### 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 = Shuffle(cards);
if (e.Button == MouseButtons.Left)
{
MessageBox.Show(Convert.ToString(cards));
Graphics g1 = this.CreateGraphics();
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 on other sites

Doesn't appear to be very random...

I put it online here, have a look

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

Was an extension method (which is better).

Edited by duke_meister

##### 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 on other sites

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

##### 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)

{

• 16
• 11
• 24
• 42
• 75