C#編寫控制臺(tái)程序紙牌游戲
本文實(shí)例為大家分享了C#編寫紙牌游戲的具體代碼,供大家參考,具體內(nèi)容如下
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隨機(jī)賦從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()//給每個(gè)字符串?dāng)?shù)組賦上牌值 { 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)//發(fā)牌 { 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建進(jìn)入下一個(gè)玩家"); Console.ReadLine(); } public bool CompareAns(int j, string[] PersonName, int n, bool bool2)//對(duì)玩家的牌與勝利的結(jié)果進(jìn)行比較 { 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; // 發(fā)牌 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("輸入你想要打出牌的序號(hào):"); 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("輸入錯(cuò)誤"); break; } //判斷結(jié)果 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("第一個(gè)輸入的名字將作為第一個(gè)開始拿牌的人:"); Console.WriteLine("游戲的人數(shù):"); 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();//給每個(gè)字符串?dāng)?shù)組賦值 obj.TakeCard(PersonName, PersonNum);//給每個(gè)玩家發(fā)牌 Console.ReadLine(); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:C#中使用FilleStream實(shí)現(xiàn)視頻文件的復(fù)制
欄 目:C#教程
本文標(biāo)題:C#編寫控制臺(tái)程序紙牌游戲
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/4676.html
您可能感興趣的文章
- 01-10WinForm實(shí)現(xiàn)程序一段時(shí)間不運(yùn)行自動(dòng)關(guān)閉的方法
- 01-10C#實(shí)現(xiàn)將程序鎖定到Win7任務(wù)欄的方法
- 01-10C#使用windows服務(wù)開啟應(yīng)用程序的方法
- 01-10C#一個(gè)簡(jiǎn)單的定時(shí)小程序?qū)崿F(xiàn)代碼
- 01-10C#實(shí)現(xiàn)程序等待延遲執(zhí)行的方法
- 01-10C#使用Mutex簡(jiǎn)單實(shí)現(xiàn)程序單實(shí)例運(yùn)行的方法
- 01-10C#計(jì)算程序執(zhí)行過程花費(fèi)時(shí)間的方法
- 01-10C#實(shí)現(xiàn)將應(yīng)用程序設(shè)置為開機(jī)啟動(dòng)的方法
- 01-10WPF實(shí)現(xiàn)類似360安全衛(wèi)士界面的程序源碼分享
- 01-1010個(gè)C#程序員經(jīng)常用到的實(shí)用代碼片段


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10delphi制作wav文件的方法
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery