c# - What is the best way to have a score value for cards in a deck -


i wanted know best way create deck of cards score or calculate score ? see below have calculate class deal cards score, check card , give value , calculate winner.

im creating blackjack game , have gotten cards randomly generate in having trouble score each card...

i going show classes have far can sense of im doing , im at.

i know lot of code not ailing people try , please try , bear me...

card class

     public static class card {     // should enumes eg. privat enume suite{}     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>();          (int s = 0; s < 4; s++ )         {             string sut = suite[s];              (int fv = 0; fv < 13; fv++)             {                 string value = facevalue[fv];                  deckofcards.add(sut + value);             }         }         // end of 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;         }     } } 

dealer class(this center class per dealer get, shuffle , deal cards out)

    class dealer {     private list<string> randomisedcards;      public dealer()     {         randomisedcards = card.createdeck();     }       public string dealcard()     {         string randcard = randomisedcards[0];         randomisedcards.removeat(0);           // creating object.         calculate c = new calculate(randcard);          return randcard;     } } 

player class

    class player {     // member variables.     private int newplayerscore;     private int newplayerwin;     private int newplayerlosses;     private int newplayerties;       // defalut constructor used set member variables default values , used reset     // players details.     public player()     {         newplayerscore = 0;         newplayerwin = 0;         newplayerlosses = 0;         newplayerties = 0;     }      // propertie used , set players score.     public int calcplayerscore     {                 {             return newplayerscore;         }         set         {             newplayerscore = value;         }     }       public void getplayerdetails()     {         calculate c = new calculate(newplayerscore);      } } 

calculate class(this using calculate winner , score each card, issue not best way things)

    class calculate {     player p = new player();      // member variable.     private string newcard;     private int pscore;     private int dscore;       // overloaded constructor.     public calculate(string card)     {         newcard = card;     }      public calculate(int playerscore)     {         pscore = playerscore;     }      public void calculatescore()     {         switch (newcard)         {             case "spades2":             case "hearts2":             case "diamonds2":             case "clubs2":                 pscore = 2;                 p.calcplayerscore = pscore;                 break;              case "spades3":             case "hearts3":             case "diamonds3":             case "clubs3":                 pscore = 3;                 p.calcplayerscore = pscore;                 break;              case "spades4":             case "hearts4":             case "diamonds4":             case "clubs4":                 pscore = 4;                 p.calcplayerscore = pscore;                 break;              case "spades5":             case "hearts5":             case "diamonds5":             case "clubs5":                 pscore = 5;                 p.calcplayerscore = pscore;                 break;              case "spades6":             case "hearts6":             case "diamonds6":             case "clubs6":                 pscore = 6;                 p.calcplayerscore = pscore;                 break;              case "spades7":             case "hearts7":             case "diamonds7":             case "clubs7":                 pscore = 7;                 p.calcplayerscore = pscore;                 break;              case "spades8":             case "hearts8":             case "diamonds8":             case "clubs8":                 pscore = 8;                 p.calcplayerscore = pscore;                 break;              case "spades9":             case "hearts9":             case "diamonds9":             case "clubs9":                 pscore = 9;                 p.calcplayerscore = pscore;                 break;              default:                 pscore = 10;                 p.calcplayerscore = pscore;                 break;         }     } } 

thanks in advance.

don't work simple strings. put them enums , create immutable class card take 3 properties suite, value, score:

public enum suite {     clubs,      hearts,      spades,      diamonds }  public enum value {     ace,      two,      three,      four,     five,     six,     seven,     eight,     nine,     ten,     jack,      queen,      king }  public class card {     public card(suite suite, value value, decimal score)     {         suite = suite;         value = value;         score = score;     }      public suite suite { get; private set; }      public value value { get; private set; }      public decimal score { get; private set; } } 

with stuff in hand can create desired cards individual score.

maybe score not property of card itself, player holds cards. can make sense if score of card can change depending on other cards player holding (e.g. ace in black jack can either 1 or eleven, if have 2 of them 21). in last case should maybe create static class specific game play able give things need:

public static class blackjack {     public static ienumerable<card> createnewdeck()     {         var suits = enum.getvalues(typeof(suite)).cast<suite>();         var values = enum.getvalues(typeof(value)).cast<value>();          // create new deck contains cards needed.         var simpledeck = suits.selecmany(suit => values.select(value => new card(suit, value)));         // e.g. in 1 blackjack deck you'll find cards 4 times (imho).         var deck = enumerable.repeat(simpledeck, 4).selectmany(x => x);                              .tolist();          deck.shuffle();         return deck;     }      public static decimal computescore(ienumerable<card> playercards)     {         // iterate through cards player holding         // , tell current player score.         throw new notimplementexception();     } } 

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -