C# -- 模拟扑克牌发牌
程序员文章站
2022-04-19 19:31:42
C# -- 模拟扑克牌发牌 1. User 类: 玩家 public class User { private List listCard = new List(); public string Name { get; set; } public User ......
c# -- 模拟扑克牌发牌
1. user 类: 玩家
public class user { private list<papercard> listcard = new list<papercard>(); public string name { get; set; } public user(string strname) { this.name = strname; } public void add(papercard p) { listcard.add(p); } public void reset() { listcard.clear(); } public void introduce() { string strcards = ""; for (int i = 0; i < listcard.count; i++) { if (i == 0) { strcards += listcard[i].name; } else { strcards += ", " + listcard[i].name; } } console.writeline("\r\n--------------------------------------------"); console.write("我是{0},", name); console.write("我手中的扑克牌有{0}张:\r\n{1}\r\n", listcard.count,strcards); } }
2. papercard类:纸牌
public class papercard { public string type { get; set; } public string number { get; set; } public papercard(string strtype, string strnumber) { this.type = strtype; this.number = strnumber; } public string name { get { return string.format("{0}{1}", this.type, this.number); } private set { } } }
3. 模拟发牌过程
public void testplay() { //产生扑克牌 console.writeline("正在生成扑克牌..."); list<papercard> mycards = new list<papercard>(); string[] strtype = { "红桃", "黑桃", "梅花", "方块" }; string[] strnumber = { "a", "2", "3", "4", "5", "6", "7", "8", "9", "10", "j", "q", "k" }; papercard[] othercard = { new papercard("大王", ""), new papercard("小王", "") }; for (int i = 0; i < strnumber.length; i++) { for (int j = 0; j < strtype.length; j++) { papercard p = new papercard(strtype[j], strnumber[i]); mycards.add(p); } } for (int j = 0; j < othercard.length; j++) { mycards.add(othercard[j]); } //洗牌 console.writeline("正在洗牌..."); stack<papercard> stackcard = new stack<papercard>(); random r = new random(); while (mycards.count>0) { int iindex = r.next(0, mycards.count); stackcard.push(mycards[iindex]); mycards.removeat(iindex); } //发牌 console.writeline("开始发牌..."); list<user> listuser = new list<user>() { new user("大师兄"), new user("二师兄"), new user("三师弟") }; while (stackcard.count > 0) { for (int s = 0; s < listuser.count; s++) { listuser[s].add(stackcard.pop()); } } console.writeline("发牌完成!"); //看牌 foreach (var item in listuser) { item.introduce(); } }
测试:
上一篇: 喝什么缓解胃疼,让我们尽早远离胃疼的折磨
下一篇: 菠菜和什么不能一起吃,大家一起来看看