7 Replies - 814 Views - Last Post: 29 January 2013 - 03:20 PM Rate Topic: -----

#1 bboyzeez  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 142
  • Joined: 16-December 12

randomise array for "match" card game problem

Posted 29 January 2013 - 08:44 AM

hi all ok so i trying to make a match card game , the rules i am given are

36 cars 9 sets of 4 ranging from 2 - 10 , now i have looked at two ways of doing this as below shows

int[,] cards = new int[9,4]



and

int[] cards = new int[36]



but then when i use the bottom one and then have to shuffle the deck this is the code i am using but what i want to do is check if the random number generated has already been used then a bool varibale available equals false and if it is true then i declare in the new array the card and value from the original cards array

 int shufflecount = 0;

            do
            {
                int RNum = r.Next(0, 36);
                for (int i = 0; i <= shufflecount; i++)
                {
                    if (shuffledcards[i] == RNum)
                    {
                        available = false;
                    }

                }
                if (available == true)
                {
                    shuffledcards[shufflecount] =RNum;
                    shufflecount++;
                }
                if (shufflecount == 20)
                { count++; }
            } while (shufflecount != 36);



but what this is doing is comparing the actual value inside the array with the random number and i need it to check if the random number matches the array index

any pointers or advice please? i know in next chapter it gives me a more mathematical way of doing this but for now i have to make a basic way with the if staetments for checking if its been used etc so please try dumb it down :)

Is This A Good Question/Topic? 0
  • +

Replies To: randomise array for "match" card game problem

#2 bboyzeez  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 142
  • Joined: 16-December 12

Re: randomise array for "match" card game problem

Posted 29 January 2013 - 09:18 AM

ahh think i have done it just debugging to check it works
Was This Post Helpful? 0
  • +
  • -

#3 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1174
  • View blog
  • Posts: 1,659
  • Joined: 24-August 11

Re: randomise array for "match" card game problem

Posted 29 January 2013 - 09:27 AM

Rather than using an array, look into using a List<int>. It would give you greater control over your deck of cards. There are tutorials in this site on making card games, so you might want to search for some. The deck of cards should be represented by a class. The class should have your collection of cards, and also a "shuffle" type method that randomizes the order of the cards in the collection (that is for you to figure out how to do). If you use a List, you can call RemoveAt() and actually remove cards from your deck once they've been used. That would help you avoid repetition, and also creates a little bit better object oriented representation of the game you're creating. A new instance of that class (or a method that returns the existing instance to its beginning state) is all you would need to get the whole collection of cards back.

EDIT: I just reread your original post and realized that I had skipped the last paragraph and just answered how I think you should approach the problem. If this isn't what you're looking for at this point, then please disregard. However, come back to it when you move to the next chapters and are trying to employ "better" ways of solving this problem.

This post has been edited by h4nnib4l: 29 January 2013 - 09:30 AM

Was This Post Helpful? 0
  • +
  • -

#4 bboyzeez  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 142
  • Joined: 16-December 12

Re: randomise array for "match" card game problem

Posted 29 January 2013 - 09:37 AM

ahh ok i see the problem basically this code below gets random numbers in a seperate array then when its a legal outcome it transfers that into another array but with the actual value... only problem is the number "0" obviously it needs to be in the stack of cards somewhere but it will always ignore it as
if (shufflearray[i] == RNum)
will never allow it , i could cheat and only loop this for 35 cards and then always have last card as the "0" card but i rather find a proper way to work it out any advice please?

int shufflecount = 0;

            do
            {
                int RNum = r.Next( 36);
                for (int i = 0; i <= shufflecount; i++)
                {
                    if (shufflearray[i] == RNum)
                    {
                        available = false;
                        break;
                    }

                }
                if (available == true)
                {
                    shufflearray[shufflecount] = RNum;
                    shuffledcards[shufflecount] = cards[RNum];
                    shufflecount++;
                }
               // if (shufflecount == 20)
               // { count++; }
                available = true;
            } while (shufflecount != 36);


Was This Post Helpful? 0
  • +
  • -

#5 bboyzeez  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 142
  • Joined: 16-December 12

Re: randomise array for "match" card game problem

Posted 29 January 2013 - 09:42 AM

i added another bool and if loop and got it to work sorry to be a pain

int shufflecount = 0;

            do
            {
                int RNum = r.Next( 36);
                for (int i = 0; i <= shufflecount; i++)
                {
                    if (RNum == 0 && allowonce == true)
                    {
                        available = true;
                        allowonce = false;
                        break;
                    }
                    if (shufflearray[i] == RNum)
                    {
                        available = false;
                        break;
                    }

                }
                if (available == true)
                {
                    shufflearray[shufflecount] = RNum;
                    shuffledcards[shufflecount] = cards[RNum];
                    shufflecount++;
                }
               // if (shufflecount == 20)
               // { count++; }
                available = true;
            } while (shufflecount != 36);




its weird how you can leave the computer for half hour then come back and have worked it out without staring at the screen haha, now i guess there are better ways if anyone could show me them so i can learn better coding please?
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2875
  • View blog
  • Posts: 9,540
  • Joined: 12-December 12

Re: randomise array for "match" card game problem

Posted 29 January 2013 - 09:56 AM

int[] cards = new int[36];

By default all elements will have a 0 value. You could set this to 1 if the card has been selected already:

if (cards[someRandomNumber] == 0) {
    cards[someRandomNumber] = 1;
}

To empty the array, before another shuffle, use:

Array.Clear(cards, 0, 35);


NB new int[36] creates an array of 36 elements, indexed 0 to 35, so trying to reference cards[36] will generate an error.

This post has been edited by andrewsw: 29 January 2013 - 10:01 AM

Was This Post Helpful? 1
  • +
  • -

#7 Michael26  Icon User is offline

  • Radar love
  • member icon

Reputation: 332
  • View blog
  • Posts: 1,419
  • Joined: 08-April 09

Re: randomise array for "match" card game problem

Posted 29 January 2013 - 02:41 PM

@bboyzeez
Have you heard about the Fisher-Yates algorithm for shuffling?
Was This Post Helpful? 0
  • +
  • -

#8 bboyzeez  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 142
  • Joined: 16-December 12

Re: randomise array for "match" card game problem

Posted 29 January 2013 - 03:20 PM

yes i admit i looked ahead in book and i did see something about this aswell as on internet when i searched but when i look at the code i know im learning it next chapter so its kinda cheating to use that now for this when next chapter ill have to modoify what i make now to be better and nicer coding.... i will work with this for now as its what i been taught.... also i am going to use a listbox for both players and this way i can remove cards that have been put into center pile and then when winner wins center and cards need to be put back at bottom of pile i can do this easy with the items.add.... ,i can use this and just make them invisible opn the form because next chapter also is text files which im guessing would be the real way of doing it?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1