任务:显示一手牌(13张牌)。手牌来自格式为KQT5.KJ873..AJ52的数据库,其中的套装是黑桃,心形,钻石俱乐部和完全停止用于分离西装。我希望创建一个这个手的2D数组,即 [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]
到目前为止,我在WebMatrix中使用Razor(C#)的代码是
@{ string westHand = "KQT5.KJ873..AJ52";
foreach (string subString2 in westHand.Split('.')) {
@subString2 <br />
foreach (char c in subString2){
@c <br />
}
}}
输出是 KQT5 ķ Q Ť 五 KJ873 ķ Ĵ 8 7 3
AJ52 一个 Ĵ 五 2
现在各个卡片已分开。正如我上面所说,我想把它放到一个二维数组中:
string[,] handData = new string[12,12]
嘿,如果我能弄清楚如何将数字放入一维数组,我会很高兴。
编辑:如下所述,所需数组的尺寸应为[13,2],即13行乘2列。
答案 0 :(得分:1)
编辑: 好吧,我认为这正是你所要求的。最后,hand包含一系列字符:hand [卡片索引,0-12 ] [ 0适合,1是卡]
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] + "]";
}
}
输出值:
[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]
上一个答案,以防您仍然需要它: 我认为这与你想要做的事情类似。这只是直接的C#,但是使用类,因为这是一种面向对象的语言。 :)
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();
}
这是卡类:
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 + "]";
}
}
输出:
[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]
同样,我认为这是你想要的,但我不完全确定。如果我离开基地,请告诉我。
答案 1 :(得分:1)
我不确定您是否要显示您在帖子开头写的卡片,或者您是否想要将它们“放入”数组中以执行其他操作。但是,为了仅以您想要的格式显示它们,以下代码将起作用:
@{ 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;
}
}}
编辑:如果您真的只想将它们放在包含13行和2列的数组中,请使用以下代码。 (变量result包含具有正确值的数组)
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;
}
}
答案 2 :(得分:0)
感谢Preli&amp; chrsmtclf。将您的解决方案用于WebMatrix的Razor(C#)语法,我现在有:
(1)Preli的解决方案;和
@{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的解决方案
@{ 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 />
}
上述两种解决方案都给出了二维数组中包含的以下输出:
SK
SQ
ST
S5
HK
HJ
H8
H7
H3
CA
CJ
C5
C2