Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there all,
I am trying to calculate the score value for each card.

What I have so far is a player, dealer and card class. Iv created a calculate class as to handle all the calculations and hand the score for each card back to the dealer or player class.

My theory is to send each card that is handed out to the calculate class, then check what card it is and then send the value for that specific card to the player class to increment the players score by the cards amount.

But now how do I check the card and give it a score ?

I hade this but its to messy

C#
switch (newCard)
{
    case "Spades2:"
    case "Hearts2:"
    case "Diamonds2:" 
    case "Clubs2:"
        pScore = 2;
        break;
  
}

And so on.
newCard = the card value coming in and pScore = player score.

Heads up : im still a junier so please no bashing me.
Thanks in advance.



EDIT :
This is the rest of my code as you guys can see what I have so far.
I know its a lot of code but please bear with me.

Card Class :
C#
public static class Card
    {
        private static string[] Suite = new string[4] {"Clubs", "Hearts", "Spades", "Diamonds" };
        private static string[] FaceValue = new string[13] {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };


        public static List<string> CreateDeck()
        {
            List<string> deckOfCards = new List<string>();

            for (int s = 0; s < 4; s++ )
            {
                string sut = Suite[s];

                for (int fV = 0; fV < 13; fV++)
                {
                    string value = FaceValue[fV];

                    deckOfCards.Add(sut + value);
                }
            }
            // End of For loop.
            deckOfCards.Shuffle();

            return deckOfCards;
        }

        public static void Shuffle<T>(this IList<T> list)
        {
            Random rng = new Random();
            int n = list.Count;
            while (n > 1)
            {
                n--;
                int k = rng.Next(n + 1);
                T value = list[k];
                list[k] = list[n];
                list[n] = value;
            }
        }
    }

I know that I should have used enume instead of an array.


Dealer Class :
C#
class Dealer
    {
        private List<string> randomisedCards;

        public Dealer()
        {
            randomisedCards = Card.CreateDeck();
        }


        public string dealCard()
        {
            string randCard = randomisedCards[0];
            randomisedCards.RemoveAt(0);

            return randCard;
        }
    }

I wanted to send the ranCard to the calculate class to calculate the score value for that card.


Calculate class :
C#
class Calculate
    {
        // Member variable.
        private string newCard;
        private int pScore;
        private int dScore;


        // Overloaded constructor.
        public Calculate(string card)
        {
            newCard = card;
        }

        public Calculate(int playerScore, int dealerScore)
        {
            pScore = playerScore;
            dScore = dealerScore;
        }

        public void calculateScore()
        {
            switch (newCard)
            {
                case "Spades":
                case "Hearts":
                    pScore = 11;
                    break;

                    // etc.
            }
        }
    }


Im receiving the random card in the first constructor and the player score in the second constructor so I can send the cards value to the player class.


Player class
C#
class Player
    {
        // Member variables.
        private int newPlayerScore;
        private int newPlayerWin;
        private int newPlayerLosses;
        private int newPlayerTies;

        // Defalut constructor used to set the member variables to their default values and will be used to reset
        // the players details.
        public Player()
        {
            newPlayerScore = 0;
            newPlayerWin = 0;
            newPlayerLosses = 0;
            newPlayerTies = 0;
        }

        // Propertie used to get and set the players score.
        public int calcPlayerScore
        {
            get
            {
                return newPlayerScore;
            }
            set
            {
                newPlayerScore += value;
            }
        }

    }


The calculate player score propertie will accumulate the players score.
Posted
Updated 2-Sep-14 23:12pm
v2
Comments
Richard MacCutchan 3-Sep-14 4:34am    
Create a card class that contains both the suit and the face value as properties. Then it's easy to add up the total that any player holds.
murkalkiran 3-Sep-14 5:38am    
(y)

We can't be precise, because we have no idea what teh rest of your system looks like. But...the way I would do it is to create a Card class which contained two values:
C#
public enum Suit
   {
   Diamond, Spade, Club, Heart
   }
public enum Face
   {
   One = 1,
   Ace = 1,
   Two,
   Three,
   ...
   King
   }
public class Card
   {
   public Suit Suit { get; set; }
   public Face Face { get; set; }
   }

And add a property which generated a "score" value (if it's fixed and not related to the other cards in the hand)
If it is related, I'd probably generate a Hand class which contained a collection of Card objects, and had the Score property.

That way, if the Face is "Two", then the "score" is (int) Face and so forth.
C#
public int Score
   {
   get { return (int) Face; }
   }
 
Share this answer
 
How about creating Card class and instantiating each card in the constructor giving it concrete value, adding it to the deck (collection of cards) and then simply sending concrete cards to players - easily calculating the score from that

Class Card
Public property Color
public property Value
public property PointValue
end class

public enum cardcolor
Spades
Hearts
Diamonds
Clubs
end enum

you can do similar enum for card values so you get cardValue.Jack or cardValue.Ace instead of numbers
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900