C#编写控制台程序纸牌游戏
程序员文章站
2022-06-21 20:22:14
本文实例为大家分享了c#编写纸牌游戏的具体代码,供大家参考,具体内容如下
using system;
using system.collections.gene...
本文实例为大家分享了c#编写纸牌游戏的具体代码,供大家参考,具体内容如下
using system; using system.collections.generic; using system.text; using system.threading; namespace consoleapplication2 { class game { string[] str = new string[52]; int[] number = new int[52]; int i = 0; int[] a = new int[4]; int[] b = new int[4]; int[] c = new int[4]; int[] d = new int[4]; int[] e = new int[4]; random ra = new random(unchecked((int)datetime.now.ticks)); public void getnum(int[] number, int minnum, int maxnum) { int flag; flag = number[i] = ra.next(0, 52); int f = 0; while (f < i) { if (number[f] == flag) getnum(number, 0, 52); f++; } } public void getrandomnum()//给number随机赋从0到51不同的值 { while (i < 52) { int flag; flag = number[i] = ra.next(0, 52); int f = 0; while (f < i) { if (number[f] == flag) { getnum(number, 0, 52); break; } f++; } i++; } } public void entercard()//给每个字符串数组赋上牌值 { str[0] = "1 of the club"; str[1] = "2 of the club"; str[2] = "3 of the club"; str[3] = "4 of the club"; str[4] = "5 of the club"; str[5] = "6 of the club"; str[6] = "7 of the club"; str[7] = "8 of the club"; str[8] = "9 of the club"; str[9] = "10 of the club"; str[10] = "jack of the club"; str[11] = "queen of the club"; str[12] = "king of the club"; str[13] = "1 of the diamond"; str[14] = "2 of the diamond"; str[15] = "3 of the diamond"; str[16] = "4 of the diamond"; str[17] = "5 of the diamond"; str[18] = "6 of the diamond"; str[19] = "7 of the diamond"; str[20] = "8 of the diamond"; str[21] = "9 of the diamond"; str[22] = "10 of the diamond"; str[23] = "jack of the diamond"; str[24] = "queen of the diamond"; str[25] = "king of the diamond"; str[26] = "1 of the heart"; str[27] = "2 of the heart"; str[28] = "3 of the heart"; str[29] = "4 of the heart"; str[30] = "5 of the heart"; str[31] = "6 of the heart"; str[32] = "7 of the heart"; str[33] = "8 of the heart"; str[34] = "9 of the heart"; str[35] = "10 of the heart"; str[36] = "jack of the heart"; str[37] = "queen of the heart"; str[38] = "king of the heart"; str[39] = "1 of the spade"; str[40] = "2 of the spade"; str[41] = "3 of the spade"; str[42] = "4 of the spade"; str[43] = "5 of the spade"; str[44] = "6 of the spade"; str[45] = "7 of the spade"; str[46] = "8 of the spade"; str[47] = "9 of the spade"; str[48] = "10 of the spade"; str[49] = "jack of the spade"; str[50] = "queen of the spade"; str[51] = "king of the spade"; } public void fapai(int personnum)//发牌 { for (int j = 0; j < personnum; j++) { a[j] = number[j * 5]; b[j] = number[j * 5 + 1]; c[j] = number[j * 5 + 2]; d[j] = number[j * 5 + 3]; e[j] = number[j * 5 + 4]; } } public void print(string[] personname, int j)//显示玩家的牌 { console.writeline("玩家{0}的牌", personname[j]); console.writeline("1. {0}", str[a[j]]); console.writeline("2. {0}", str[b[j]]); console.writeline("3. {0}", str[c[j]]); console.writeline("4. {0}", str[d[j]]); console.writeline("5. {0}", str[e[j]]); } public void print1(string[] personname, int j)//显示玩家的牌 { console.writeline("玩家{0}剩下的牌", personname[j]); console.writeline("1. {0}", str[a[j]]); console.writeline("2. {0}", str[b[j]]); console.writeline("3. {0}", str[c[j]]); console.writeline("4. {0}", str[d[j]]); console.writeline("5. {0}", str[e[j]]); console.writeline("按enter建进入下一个玩家"); console.readline(); } public bool compareans(int j, string[] personname, int n, bool bool2)//对玩家的牌与胜利的结果进行比较 { int min = 0, max = 0; max = (a[j] > b[j]) ? a[j] : b[j]; max = (c[j] > max) ? c[j] : max; max = (d[j] > max) ? d[j] : max; max = (e[j] > max) ? e[j] : max; min = (a[j] < b[j]) ? a[j] : b[j]; min = (c[j] < min) ? c[j] : min; min = (d[j] < min) ? d[j] : min; min = (e[j] < min) ? e[j] : min; if (min >= 0 && max < 13 || min >= 13 && max < 26 || min >= 26 && max < 39 || min >= 39 && max < 52) { console.clear(); console.writeline("恭喜玩家{0}赢得游戏!", personname[j]); bool2 = false; } //判断是否还有底牌可供玩家拿取 else if (n==52) { console.clear(); console.writeline("没有底牌了!!重新开始。"); bool2 = false; } return bool2; } public void takecard(string[] personname, int personnum)//玩家打牌 { getrandomnum(); int n = 1, botton=7; int f = 0; int up = 0, up1 = 0; bool bool2 = true; bool bool1 = false; // 发牌 fapai(personnum); //玩家打牌 n = 5 * personnum; while (bool2) { for (int j = 0; j < personnum; j++) { //显示玩家的牌 console.clear(); if (bool1) { console.write("上一玩家打出的牌是"); console.writeline("{0}", str[up]); } bool1 = true; print(personname, j); //玩家从剩下的牌中拿牌或拿上一玩家扔出来的牌 char d='f'; if (n == 5 * personnum) { try { console.writeline("按d从剩下的牌中拿一张牌"); d = convert.tochar(console.readline()); } catch (exception e) { console.writeline(e.message); thread.sleep(1000); system.environment.exit(0); } if (d == 'd' || d == 'd') { f = number[n]; n++; print(personname, j); console.writeline("6. {0}", str[f]); } } else { console.writeline("按d从剩下的牌中拿一张牌或者按t拿被上一玩家打的牌"); d = convert.tochar(console.readline()); if (d == 'd' || d == 'd') { f = number[n]; n++; print(personname, j); console.writeline("6. {0}", str[f]); } else if (d == 't' || d == 't') { print(personname, j); console.writeline("6. {0}", str[up]); } } //打牌 try { console.writeline("输入你想要打出牌的序号:"); botton = convert.toint32(console.readline()); } catch (exception e) { console.writeline(e.message); thread.sleep(1000); system.environment.exit(0); } switch (botton) { case 1: { console.writeline("被打出的牌是{0}", str[a[j]]); up1 = up; up = a[j]; a[j] = b[j]; b[j] = c[j]; c[j] = d[j]; d[j] = e[j]; if (d == 'd' || d == 'd') e[j] = f; else if (d == 't' || d == 't') e[j] = up1; print1(personname, j); break; } case 2: { console.writeline("被打出的牌是{0}", str[b[j]]); up1 = up; up = b[j]; b[j] = c[j]; c[j] = d[j]; d[j] = e[j]; if (d == 'd' || d == 'd') e[j] = f; else if (d == 't' || d == 't') e[j] = up1; print1(personname, j); break; } case 3: { console.writeline("被打出的牌是{0}", str[c[j]]); up1 = up; up = c[j]; c[j] = d[j]; d[j] = e[j]; if (d == 'd' || d == 'd') e[j] = f; else if (d == 't' || d == 't') e[j] = up1; print1(personname, j); break; } case 4: { console.writeline("被打出的牌是{0}", str[d[j]]); up1 = up; up = d[j]; d[j] = e[j]; if (d == 'd' || d == 'd') e[j] = f; else if (d == 't' || d == 't') e[j] = up1; print1(personname, j); break; } case 5: { console.writeline("被打出的牌是{0}", str[e[j]]); up1 = up; up = e[j]; if (d == 'd' || d == 'd') e[j] = f; else if (d == 't' || d == 't') e[j] = up1; print1(personname, j); break; } case 6: { console.writeline("被打出的牌是{0}", str[f]); up = f; print1(personname, j); break; } default: console.writeline("输入错误"); break; } //判断结果 bool2 = compareans(j,personname,n,bool2); if (bool2 == false) break; } } } } class exemain { static void main(string[] args) { int personnum=0; game obj = new game(); console.writeline("第一个输入的名字将作为第一个开始拿牌的人:"); console.writeline("游戏的人数:"); try { personnum = convert.toint32(console.readline()); } catch (exception e) { console.writeline(e.message); thread.sleep(1000); system.environment.exit(0); } string[] personname = new string[personnum]; for (int i = 0; i < personnum; i++) { console.writeline("输入玩家{0}的名字", i + 1); personname[i] = console.readline(); } obj.entercard();//给每个字符串数组赋值 obj.takecard(personname, personnum);//给每个玩家发牌 console.readline(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。