设计一个类表达一组牌,设计牌组的初始化方式
//牌值
private string face;
//花色
private string suit;
public zupai(string suit, string face) {
this.face = face;
this.suit = suit;
}
//牌子
public string getFace() {
return face;
}
//花色
public string getSuit() {
return suit;
}
一个字段就可以了
class 牌
{
List<string> names = new List<string> { "红桃", "黑桃", "梅花", "方片" };
List<string> spec = new List<string> { "J", "Q", "K", "A" };
private int value;
public 牌(string 花色, string 牌)
{
int type = names.FindIndex(x => x == 花色);
int num = spec.Contains(牌) ? (spec.FindIndex(x => x == 牌) + 11) % 13 : int.Parse(牌);
value = num * 4 + type;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class 牌
{
List<string> names = new List<string> { "红桃", "黑桃", "梅花", "方片" };
List<string> spec = new List<string> { "J", "Q", "K", "A" };
private int value;
public 牌(string 花色, string 牌)
{
int type = names.FindIndex(x => x == 花色);
int num = spec.Contains(牌) ? (spec.FindIndex(x => x == 牌) + 11) % 13 : int.Parse(牌);
value = num * 4 + type;
}
public override string ToString()
{
int type = value % 4;
int num = value / 4;
string st = names[type];
string sn = (num == 1 || num > 10) ? spec[num == 1 ? 3 : num - 11] : num.ToString();
return st + sn;
}
}
class Program
{
static void Main(string[] args)
{
牌[] cards =
{
new 牌("红桃", "K"),
new 牌("方片", "4"),
new 牌("梅花", "A")
};
foreach (牌 i in cards)
Console.WriteLine(i);
}
}
}