Making a Top-Trumps Game, NEED HELP!

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 9456 Views - Last Post: 05 April 2013 - 01:04 PM Rate Topic: -----

#1 WrecKaGe   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-April 13

Making a Top-Trumps Game, NEED HELP!

Posted 05 April 2013 - 11:15 AM

I'm currently working on a task where I have to code a windows form application for a Top-Trumps game in which the player plays against a CPU opponent. For anyone who doesn't know, Top-Trumps is a game where each player is given a set of cards which each contain the same types of stats but with varying numbers depending on which card you have. Each player can only look at/use their top card and must choose a stat, at which point that stat is compared between the two players and whoever has the highest wins the other players card. For this task I had to include a stat 'luck', which is not a predetermined number and must instead be obtained through the roll of a dice (a section of code generates a random number)

I've only started studying Computer Science over the past year and subsequently I am very inexperienced and have found this task very frustrating to say the least. So far I have created each card as an object within my 'Cards' class, which I have done in the format:

Class card1 = new Class();

The above is not from my actual code because I've used a constructor to (based on my understanding of constructors) associate individual stat values to each card. Basically the main problem I've encountered is shuffling the cards so that they are completely randomised in order, then distributing 5 cards to the player and the other 5 to the CPU. In order to do this I'm guessing I need some sort of shuffling algorithm as well as an array for the players cards and another for the CPU cards. Sorry if I've been rambling, any help would be greatly appreciated and please bear in mind that I am very new to programming in general. Below is an excerpt from my code, which only contains 3 of the 10 cards I have in total and doesn't show my code for my random number generator (although I have one):

public class Cards
{
// Cards created as objects
Stats bigL = new Stats("Big L", 9, 10, 10, 8, 0);
Stats biggieSmalls = new Stats("Biggie Smalls", 9, 8, 9, 9, 0);
Stats tupac = new Stats("Tupac", 8, 10, 8, 10, 0);
}
public class Stats
{
// Ignore this, it is only relevant to the 'luck' stat 
static Dice luckCard = new Dice();
// Variables for each stat declared
public String statName;
public int statLyrics;
public int statFlow;
public int statCred;
public int statFame;
public int statLuck;

public Stats() { statName = ""; statLyrics = 0; statFlow = 0; statCred = 0; statFame = 0; statLuck = luckCard.topNumber }
public Stats(string cardName, int cardLyrics, int cardFlow, int cardCred, int cardFame, int cardLuck)
{
// swap empty stats with those from Cards class and vice versa
statName = cardName;
statLyrics = cardLyrics;
statFlow = cardFlow;
statCred = cardCred;
statFame = cardFame;
cardLuck = statLuck;
}


So far it makes sense to me, although obviously it could be done better. If possible I'd like to know of a way to shuffle the cards and allocate half of them to each player without having to change everything I've already done. If this is not possible then it's understandably due to my poor coding ability. Thanks for reading and please help!

This post has been edited by tlhIn`toq: 05 April 2013 - 11:18 AM
Reason for edit:: fixed broken closing code tag


Is This A Good Question/Topic? 0
  • +

Replies To: Making a Top-Trumps Game, NEED HELP!

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14426
  • View blog
  • Posts: 57,833
  • Joined: 12-June 08

Re: Making a Top-Trumps Game, NEED HELP!

Posted 05 April 2013 - 11:23 AM

Where's your individual card objects being held at? In theory you would have a single object called 'card' that holds an id, stats, etc for that specific card. Then you would have a class called 'cards' or 'deck' that holds a collection of 'card' objects.. and houses the shuffle function, etc.
Was This Post Helpful? 1
  • +
  • -

#3 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Making a Top-Trumps Game, NEED HELP!

Posted 05 April 2013 - 11:26 AM

Your Cards class makes no sense.
A Card class would include properties pertaining to a card... Like suit, value, IsWild, ArtworkBitmap

Class card1 = new Class();
That's just gibberish.

public String statName;
Don't use fields like that, use properties.

Naming all your properties within the stats class with stat in the name is silly. Do you see that in .NET? Does a button have a .ButtonBackColor .ButtonText ? No Its just Button.Backcolor and Button.Text

Just stop.

Read and work the classes tutorial and properties tutorial linked in my signature block.
Was This Post Helpful? 1
  • +
  • -

#4 WrecKaGe   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-April 13

Re: Making a Top-Trumps Game, NEED HELP!

Posted 05 April 2013 - 11:27 AM

View Postmodi123_1, on 05 April 2013 - 11:23 AM, said:

Where's your individual card objects being held at? In theory you would have a single object called 'card' that holds an id, stats, etc for that specific card. Then you would have a class called 'cards' or 'deck' that holds a collection of 'card' objects.. and houses the shuffle function, etc.

Isn't that sort of what I have at the moment? The values for each individual stat for each card are stored in each card object and are then accessed by the 'stats' class which associates the values to the type of stat, is this wrong? It would seem I used a constructor where it wasn't necessary.
Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Making a Top-Trumps Game, NEED HELP!

Posted 05 April 2013 - 11:29 AM

There is a NuGet package which encapsulates the basics of cards. (NuGet: Cards)
NuGet Cards.Sample

Make Trump Card class implement the ICard interface and you'll be able to use the predefined shuffling, decks, hands.
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14426
  • View blog
  • Posts: 57,833
  • Joined: 12-June 08

Re: Making a Top-Trumps Game, NEED HELP!

Posted 05 April 2013 - 11:33 AM

I got no idea what you are doing there champ. Is class 'Stats' the individual card object?
Was This Post Helpful? 0
  • +
  • -

#7 WrecKaGe   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-April 13

Re: Making a Top-Trumps Game, NEED HELP!

Posted 05 April 2013 - 11:39 AM

View Postmodi123_1, on 05 April 2013 - 11:33 AM, said:

I got no idea what you are doing there champ. Is class 'Stats' the individual card object?

In all honesty I'm not sure what I'm doing either, I based this entire section of code on some examples of constructors that I read up on and then modified for my project. I guess it just won't work this way.
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14426
  • View blog
  • Posts: 57,833
  • Joined: 12-June 08

Re: Making a Top-Trumps Game, NEED HELP!

Posted 05 April 2013 - 11:44 AM

Then time to scrap it, refactor your thoughts, and start differently.

I would go with the 'Card' class that holds the information for a single card.. then a class called 'deck' and add like I mentioned above.
Was This Post Helpful? 0
  • +
  • -

#9 WrecKaGe   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-April 13

Re: Making a Top-Trumps Game, NEED HELP!

Posted 05 April 2013 - 11:49 AM

View Postmodi123_1, on 05 April 2013 - 11:44 AM, said:

Then time to scrap it, refactor your thoughts, and start differently.

I would go with the 'Card' class that holds the information for a single card.. then a class called 'deck' and add like I mentioned above.

So should I create an object for the card in the 'Card' class or just the fields for each of the stats? Thanks for the help btw

View PosttlhIn`toq, on 05 April 2013 - 11:26 AM, said:

Your Cards class makes no sense.
A Card class would include properties pertaining to a card... Like suit, value, IsWild, ArtworkBitmap

Class card1 = new Class();
That's just gibberish.

public String statName;
Don't use fields like that, use properties.

Naming all your properties within the stats class with stat in the name is silly. Do you see that in .NET? Does a button have a .ButtonBackColor .ButtonText ? No Its just Button.Backcolor and Button.Text

Just stop.

Read and work the classes tutorial and properties tutorial linked in my signature block.

Looking through the tutorials now, thanks for the suggestion

View PostAdamSpeight2008, on 05 April 2013 - 11:29 AM, said:

There is a NuGet package which encapsulates the basics of cards. (NuGet: Cards)
NuGet Cards.Sample

Make Trump Card class implement the ICard interface and you'll be able to use the predefined shuffling, decks, hands.

Thanks, I'll check it out
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14426
  • View blog
  • Posts: 57,833
  • Joined: 12-June 08

Re: Making a Top-Trumps Game, NEED HELP!

Posted 05 April 2013 - 11:59 AM

Ahrm, no?

The 'card' class should be a singular instance of a card. List out the properties of a card. If I am looking at a deck of regular playing cards I would figure I would want to know the suit, the number, and a way to print that out, right?
Was This Post Helpful? 0
  • +
  • -

#11 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14426
  • View blog
  • Posts: 57,833
  • Joined: 12-June 08

Re: Making a Top-Trumps Game, NEED HELP!

Posted 05 April 2013 - 12:25 PM

To clarify here's a quick regular playing card class setup.

The first class is just the card, the second is a 'deck of cards', and some examples of it working.

Shuffling would just be an exercise in using the 'random number' namespace, pick two numbers between 0 and 52, and swap their places.

Dealing would require two more decks to be created - one for the player A and one for player B. You would take your master deck use the 'deal' method to return the first card in the collection, remove said card from the collection, and add the card to either player A or player B's deck.

Of course all of this is just for plain ol' playing cards. Your other options can be bolted in with changes here and there, but it shouldn't be too bad.


  class Card
    {
        // suits are static, so might as well use an enumeration
        // it is also nice that the suit is a name *AND* a number!  (it helps filling them)
        public enum Suit
        {
            SPADE = 0,
            HEART = 1,
            CLUB = 2,
            DIAMOND = 3
        }
        
        // ranks are static, so might as well use an enumeration
        public enum Rank
        {
            ACE = 0,
            TWO = 1,
            THREE = 2,
            FOUR = 3,
            FIVE = 4,
            SIX = 5,
            SEVEN = 6,
            EIGHT = 7,
            NINE = 8,
            TEN = 09,
            JACK = 10,
            QUEEN = 11,
            KING = 12
        }
        // varialbes to hold the specified suit and rank
        private Suit _enSuit;
        private Rank _enRank;

        //read only so we cannot accidentially change them.. but we can tell what the properties are.
        public Suit MySuit { get { return _enSuit; } }
        public Rank MyRank { get { return _enRank; } }

        //only construct the card if it has a suit and rank.
        public Card(Suit theSuit, Rank theRank)
        {
            _enSuit = theSuit;
            _enRank = theRank;
        }
        //display, in a nice format, what the card is.
        public string Print()
        {
            return string.Format("{0} of {1}", _enRank, _enSuit);
        }
    }



    class Deck
    {
        // collection of all the cards
        private List<Card> _collectionOfCards;

        // sometimes it is nice to know how many cards in a deck.
        public int DeckCount{ get{return _collectionOfCards.Count;}}
        
        //bland construtor.
        public Deck()
        {
            _collectionOfCards = new List<Card>();
        }
        
        //loop through the available numbers and assign them.
        public void FillDeck()
        {
            for (int lcvSuit = 0; lcvSuit < 4; lcvSuit++)// we know there's four suits, right?  so just fill them out.
            {
                for (int lcvRank = 0; lcvRank < 13; lcvRank++)// we know there's 14 card ranks
                {
                    _collectionOfCards.Add(new Card((Card.Suit)lcvSuit, (Card.Rank)lcvRank));//create card objects and add them to the collection.
                }
            }
        }

        //may want to empty the deck out
        public void Clear()
        {
            _collectionOfCards.Clear();
        }

        //quick print dump of all the cards in the deck.
        public void PrintDeck()
        {

            if ((_collectionOfCards == null) || 
                (_collectionOfCards.Count == 0))
            {
                Console.WriteLine("Deck is empty");
            }
            else
            {
                foreach (Card temp in _collectionOfCards)
                {
                    Console.WriteLine(temp.Print());
                }
            }
        }

        public void ShuffleDeck()
        {
            // what would you use to shuffle?
        }

        public Card DealCard()
        {
            Card temp = null;
            // need to pull a card off the deck, remove it from the collection, and return it.

            return temp;
        }

        public void AddCard(Card incomingCard)
        {
            _collectionOfCards.Add(incomingCard);
        }

    }



        public static void Main(string[] args)
        {
            // shows how to create a single card object.
            Card fff = new Card(Card.Suit.SPADE, Card.Rank.ACE);
            Console.WriteLine(fff.Print());
            Console.WriteLine("--------------");

            Deck aaa = new Deck(); // create our deck
            aaa.FillDeck(); // fill it

            Console.WriteLine("How many cards in the deck: " + aaa.DeckCount);// access a nice property
            Console.WriteLine("--------------");
            
            aaa.PrintDeck();// print out the whole deck.
}

Was This Post Helpful? 3
  • +
  • -

#12 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Making a Top-Trumps Game, NEED HELP!

Posted 05 April 2013 - 12:33 PM

Nice Modi - If you massage that a little it would make a nice tutorial to point people towards. <hint>
Was This Post Helpful? 0
  • +
  • -

#13 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14426
  • View blog
  • Posts: 57,833
  • Joined: 12-June 08

Re: Making a Top-Trumps Game, NEED HELP!

Posted 05 April 2013 - 12:40 PM

What - for basic object/class operations? I don't think I have anything new in C#.. I may..
Was This Post Helpful? 0
  • +
  • -

#14 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Making a Top-Trumps Game, NEED HELP!

Posted 05 April 2013 - 12:46 PM

Hey think of this tutorial?
Was This Post Helpful? 0
  • +
  • -

#15 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14426
  • View blog
  • Posts: 57,833
  • Joined: 12-June 08

Re: Making a Top-Trumps Game, NEED HELP!

Posted 05 April 2013 - 12:48 PM

Ha.. a well versed problem.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2