开发者

I have split a string into individual letters using Razor (C#) syntax in WebMatrix, now how can I populate an array with the result?

Task: To display a hand of bridge (13 cards). The hand comes from a database formatted as KQT5.KJ873..AJ52 where the suit order is spades, hearts, diamonds clubs and a full stop is used to separate the suits. I wish to create a 2D array of this hand, namely [S, K] [S, Q] [S, T] [S, 5] [H, K] [H, J] [H, 8] [H, 7] [H, 3] (There is a void in diamonds) [C, A] [C, J] [C, 5] [C, 2]

My code so far using Razor (C#) in WebMatrix is

@{ string westHand = "KQT5.KJ873..AJ52";

                foreach (string subString2 in westHand.Split('.')) {
       开发者_如何学Python             @subString2 <br />

                    foreach (char c in subString2){
                        @c <br />

                }
}}

The output is KQT5 K Q T 5 KJ873 K J 8 7 3

AJ52 A J 5 2

where the individual cards are now separated. As I said above, I want to put this into a 2-D array:

string[,] handData = new string[12,12]

Hey, I would be happy if I could even work out how to put the numbers into a 1-D array.

Edit: As mentioned below the dimensions of the required array should be [13,2] i.e 13 rows by 2 columns.


EDIT: Ok, I think this is exactly what you're asking for. At the end, hand contains an array of chars: hand[index of card, 0-12][0 is suit, 1 is card]

    char[] suits = { 'S', 'H', 'D', 'C' };
    char[,] hand = new char[13, 2];
    string westHand = "KQT5.KJ873..AJ52";
    String output = new String();
    int currentSuit = 0; //Iterator for suits (0-4)
    int currentCard = 0; //Current # of card from hand (0-12)
    foreach (string suitString in westHand.Split('.')) {
        foreach (char cardChar in suitString){
            hand[currentCard, 0] = suits[currentSuit];
            hand[currentCard, 1] = cardChar;
            currentCard++;
        }
        currentSuit++;
    }
    for(int x = 0; x < 13; x++)
    {
        output += "[" + hand[x,0] + "," + hand[x,1] + "]";
    }
}

value of output:

[S,K][S,Q][S,T][S,5][H,K][H,J][H,8][H,7][H,3][C,A][C,J][C,5][C,2]

Previous Answer, just in case you still need it: I think this is something along the lines of what you're trying to do. This is just straight C#, but uses a class since this is an object oriented language. :)

    char[] suits = { 'S', 'H', 'D', 'C' };
    String output = new String();
    List<Card> hand = new List<Card>();
    string westHand = "KQT5.KJ873..AJ52";
    int currentSuit = 0;

    foreach (string suitString in westHand.Split('.')) {
        foreach (char cardChar in suitString){
            Card newCard = new Card(suits[currentSuit], cardChar);
            hand.Add(newCard);
        }
        currentSuit++;
    }

    foreach (Card currentCard in hand)
    {
        output += currentCard.ToString();
    }

This is the Card class:

public class Card
{
    public char suit, type;

    public Card(char suit, char type)
    {
        this.suit = suit;
        this.type = type;
    }

    public String ToString()
    {
        return "[" + this.suit + ", " + this.type + "]";
    }

}

Output:

[S, K][S, Q][S, T][S, 5][H, K][H, J][H, 8][H, 7][H, 3][C, A][C, J][C, 5][C, 2]

Again, I think this is what you want but I'm not completely sure. Let me know if I'm way off base.


I am not sure if you want to display the cards like you wrote at the beginning of your post - or if you want to "put" them into an array to do something else. But for only displaying them in your desired format the following code would work:

@{ string westHand = "KQT5.KJ873..AJ52";

   char type = 'S'; //start with spades

                foreach (string subString2 in westHand.Split('.')) {

                    foreach (char c in subString2){
                        <text>[@type, @c]</text>
                }
                switch (type)
                {
                    case 'S': type = 'H'; break;
                    case 'H': type = 'D'; break;
                    case 'D': type = 'C'; break;
                }
}}

Edit: if you really only want to have them in a array with 13 rows and 2 columns use the following code. (the variable result contains the array with the right values)

string westHand = "KQT5.KJ873..AJ52";

        char type = 'S'; //start with spades

        string[,] result = new string[westHand.Length - 3, 2];

        int counter = 0;
        foreach (string subString2 in westHand.Split('.'))
        {

            foreach (char c in subString2)
            {
                result[counter, 0] = type.ToString();
                result[counter, 1] = c.ToString();
                counter++;
            }
            switch (type)
            {
                case 'S': type = 'H'; break;
                case 'H': type = 'D'; break;
                case 'D': type = 'C'; break;
            }
        }


Thanks Preli & chrsmtclf. Putting your solutions into Razor (C#) syntax for WebMatrix, I now have:

(1) Preli's solution; and

@{string westHand = "KQT5.KJ873..AJ52";
    var num = westHand.Length - 3;        
    char type = 'S'; //start with spades
    string[,] result = new string[num, 2];

    int counter = 0;
    foreach (string subString2 in westHand.Split('.'))
    {
        foreach (char card2 in subString2)
        {
             @: [@type, @card2]
            result[counter, 0] = type.ToString();
            result[counter, 1] = card2.ToString();
            counter++;
        }
        switch (type)
        {
            case 'S': type = 'H'; break;
            case 'H': type = 'D'; break;
            case 'D': type = 'C'; break;
        }
    }       
}
  <br /> You have @num cards. <br />        
  @for(var i = 0; i < num; i++)  
{ 
   @result[i,0] ; @result[i,1] ;<br /> 
}

(2) chrsmtclf's solution

@{  char[] suits = { 'S', 'H', 'D', 'C' };
char[,] hand = new char[13, 2];
string westHand = "KQT5.KJ873..AJ52";
int currentSuit = 0; //Iterator for suits (0-4)
int currentCard = 0; //Current # of card from hand (0-12)
foreach (string suitString in westHand.Split('.')) {
    foreach (char cardChar in suitString){
        hand[currentCard, 0] = suits[currentSuit];
        hand[currentCard, 1] = cardChar;
        currentCard++;
    }
    currentSuit++;
}
}

@for(var i = 0; i < 13; i++)  
{ 
  @hand[i,0] ; @hand[i,1] ;<br /> 
}

Both the above solutions give the following output contained in a 2-D array:
SK SQ ST S5 HK HJ H8 H7 H3 CA CJ C5 C2

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜