XNA Creators Club Online
Page 1 of 1 (8 items)
Sort Posts: Previous Next

Need Help :: I want to build a card deck of non-standard cards stature.

Last post 09-04-2008 4:44 PM by The ZMan. 7 replies.
  • 09-03-2008 7:32 PM

    Need Help :: I want to build a card deck of non-standard cards stature.

    I am trying to build a deck of cards, but not regular playing cards, and I am having problems figuring out how to code it.

    I built a class called "Cards" and a class called "BuildDeck"... I would like it to build the deck then shuffle it, then draw a card... but I don't know what kind of logic or method to go about this.  I am new to c# so please bare with my idiocracy.  I thought I figured it all out by assigning each method to a delegate array... but now I don't know how to call the individual array later....

    I used "BuildDeck deck = new BuildDeck();" then tried to perform something like "deck.tileDeck[21];" and the code through a "Only assignment, call, increment, decrement, and new object expressions can be used as a statement."  which i looked up and translated to the program saying "I don't get what you want me to do, this is a useless statement."

    Any help?



         class Tiles 
        { 
            string RoadType; 
            string TileName; 
     
            int BldgLifeTiles; 
            int BldgBulletTiles; 
            int BldgZombies; 
            int BldgSpaces; 
            int BlankSpaces; 
            int InvalidSpaces; 
            int RoadSpaces; 
            int nZombies; 
     
            public Tiles() 
            { 
                RoadType = ""
     
                TileName = ""
     
                BldgLifeTiles = 0; 
                BldgBulletTiles = 0; 
                BldgZombies = 0; 
                BldgSpaces = 0; 
                BlankSpaces = 0; 
                InvalidSpaces = 0; 
                RoadSpaces = 0; 
                nZombies = 0; 
            } 
     
            public void TownSquare() 
            { 
                RoadType = "Xroad"
     
                TileName = "Town Square"
     
                nZombies = 4; 
     
                BldgLifeTiles = 0; 
                BldgBulletTiles = 0; 
                BldgSpaces = 0; 
     
                BlankSpaces = 0; 
                InvalidSpaces = 4; 
                RoadSpaces = 5; 
     
                // return TileName; 
            }        
            public void TRoad() 
            { 
                RoadType = "TRoad"
     
                TileName = "T Road"
     
                nZombies = 3; 
     
                BldgLifeTiles = 0; 
                BldgBulletTiles = 0; 
                BldgSpaces = 0; 
     
                BlankSpaces = 0; 
                InvalidSpaces = 5; 
                RoadSpaces = 4; 
                // return TileName; 
            } 
        class BuildDeck 
        { 
            public delegate void TileDelegate(); 
     
            public TileDelegate[ tileDeck; 
     
            public BuildDeck() 
            { 
                cardDeck = new CardDelegate[50]; 
                tileDeck = new TileDelegate[28]; 
            } 
     
           public void BuildTileDeck(int i) 
            {  
                Tiles floorTiles = new Tiles(); 
     
     
                tileDeck[0] = floorTiles.ArmySurplusStore; 
                tileDeck[1] = floorTiles.DrugStore; 
                tileDeck[2] = floorTiles.FireStation; 
                tileDeck[3] = floorTiles.FloristShop; 
                tileDeck[4] = floorTiles.GasStation; 
                tileDeck[5] = floorTiles.HardwareStore; 
                tileDeck[6] = floorTiles.Hospital; 
                tileDeck[7] = floorTiles.LawnGardenStore; 
                tileDeck[8] = floorTiles.PoliceStation; 
                tileDeck[9] = floorTiles.SkateShop; 
                tileDeck[10] = floorTiles.SportingGoodsStore; 
                tileDeck[11] = floorTiles.ToyStore; 
                tileDeck[12] = floorTiles.Corner; 
                tileDeck[13] = floorTiles.Corner; 
                tileDeck[14] = floorTiles.Corner; 
                tileDeck[15] = floorTiles.Corner; 
                tileDeck[16] = floorTiles.Straight; 
                tileDeck[17] = floorTiles.Straight; 
                tileDeck[18] = floorTiles.Straight; 
                tileDeck[19] = floorTiles.Straight; 
                tileDeck[20] = floorTiles.TRoad; 
                tileDeck[21] = floorTiles.TRoad; 
                tileDeck[22] = floorTiles.TRoad; 
                tileDeck[23] = floorTiles.TRoad; 
                tileDeck[24] = floorTiles.XRoad; 
                tileDeck[25] = floorTiles.XRoad; 
                tileDeck[26] = floorTiles.XRoad; 
                tileDeck[27] = floorTiles.XRoad; 
            } 
  • 09-03-2008 7:58 PM In reply to

    Re: Need Help :: I want to build a card deck of non-standard cards stature.

    changing "deck.tileDeck[21]" to "deck.tileDeck[21]()" successfully compiles but then the code runs and stops at it's line with the statement "Object reference not set to an instance of an object." but i cant figure out what that means... I read the help on it and it didn't exactly make sense either.
  • 09-03-2008 8:02 PM In reply to

    Re: Need Help :: I want to build a card deck of non-standard cards stature.

    To shuffle the deck, all you really need is a way to pick a random card. This could be something as simple as:

        public class Card 
        { 
            public string Name; 
        } 
     
        public class Deck 
        { 
            private Card[ _cards; 
            private List<int> _indexes; 
     
            private Random _rnd; 
     
            public Deck() 
            { 
                _rnd = new Random(); 
     
                _cards = new Card[27]; 
     
                for (int i = 0; i < 27; i++) 
                { 
                    _cards[i] = new Card(); 
                    _cards[i].Name = "Card" + i.ToString(); 
                } 
     
                InitIndexes(); 
            } 
     
            private void InitIndexes() 
            { 
     
                _indexes = new List<int>(); 
     
                for (int i = 0; i < 27; i++) 
                    _indexes.Add(i); 
     
            } 
     
            public void Shuffle() 
            { 
                InitIndexes(); 
            } 
     
            public Card GetRandomCard() 
            { 
                int index = _rnd.Next(0, _indexes.Count); 
     
                Card card = _cards[_indexes[index]]; 
                _indexes.RemoveAt(index); 
     
                return card; 
     
            } 
        } 
     

    The above is off the top of my head and not tested, but you should get the idea.


    Jim Perry - Microsoft XNA MVP
    Here's what I'm up to.
    If people spent a minute searching the forums before posting I'd be out of a job.
  • 09-03-2008 8:05 PM In reply to

    Re: Need Help :: I want to build a card deck of non-standard cards stature.

    Ohashtan:
    changing "deck.tileDeck[21]" to "deck.tileDeck[21]()" successfully compiles but then the code runs and stops at it's line with the statement "Object reference not set to an instance of an object." but i cant figure out what that means... I read the help on it and it didn't exactly make sense either.

    I don't see this line in the code you posted. It's telling you either "deck" or "tileDeck[21]" doesn't exist.



    Jim Perry - Microsoft XNA MVP
    Here's what I'm up to.
    If people spent a minute searching the forums before posting I'd be out of a job.
  • 09-03-2008 9:20 PM In reply to

    Re: Need Help :: I want to build a card deck of non-standard cards stature.

    You have Tiles floorTiles = new Tiles(); declared in BuildDeck(). As soon as BuildDeck is complete floorTiles is destroyed since it's a local variable. I'm guessing that means the methods you pointed to are also not valid. Try declaring floorTiles in the class, and create it in the BuildDeck() and see if that helps.
  • 09-03-2008 9:27 PM In reply to

    Re: Need Help :: I want to build a card deck of non-standard cards stature.

    Actually by doing

     

                BuildDeck deck = new BuildDeck();

                deck.BuildTileDeck(0);
                deck.tileDeck[20]();

     

    I was able to get it to work. I had to comment out the methods that you don't have defined to get it to compile though. Maybe you just didn't post those though.


  • 09-04-2008 3:27 PM In reply to

    Re: Need Help :: I want to build a card deck of non-standard cards stature.

    This is no longer an issue, but i did find out that:

    Yes Slickman your method worked nicely, I too found out I was missing the "()" at the end of the array request.

    I eventually deleted all the code and started over, instead of declaring each tile as a method I made them object and declared each of the objects when i built the array.  This has worked out great so far. *cross fingers*

     Thank you guys for your help and support! :) And quick responses!


  • 09-04-2008 4:44 PM In reply to

    Re: Need Help :: I want to build a card deck of non-standard cards stature.

    Jim Perry:
    To shuffle the deck, all you really need is a way to pick a random card.

    The above is off the top of my head and not tested, but you should get the idea.

    Be very careful implementing shuffling... as this wikipedia article states its VERY easy to get it wrong and end up with a biased deck. I didn't look at Jim's code long enough to work out if it has the problem but there is plenty of examples in that link to get it right.

    If the game is just for fun nobody will notice... but if you are making an online casino with real money you can bet somebody will...



    The ZBuffer News and information for XNA

    Please read the forum FAQs - Bug reporting
Page 1 of 1 (8 items) Previous Next