This article has shared the specific code for writing card games in C# for your reference. The specific content is as follows
using System; using ; using ; using ; 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))); public void getnum(int[] number, int minnum, int maxnum) { int flag; flag = number[i] = (0, 52); int f = 0; while (f < i) { if (number[f] == flag) getnum(number, 0, 52); f++; } } public void GetRandomNum()//Assign a different value from 0 to 51 to number randomly { while (i < 52) { int flag; flag = number[i] = (0, 52); int f = 0; while (f < i) { if (number[f] == flag) { getnum(number, 0, 52); break; } f++; } i++; } } public void EnterCard()// Assign each string array card value { 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)//Distribute the cards { 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)//Show the player's cards { ("Players{0}Card", PersonName[j]); ("1. {0}", str[a[j]]); ("2. {0}", str[b[j]]); ("3. {0}", str[c[j]]); ("4. {0}", str[d[j]]); ("5. {0}", str[e[j]]); } public void Print1(string[] PersonName, int j)//Show the player's cards { ("Players{0}剩下Card", PersonName[j]); ("1. {0}", str[a[j]]); ("2. {0}", str[b[j]]); ("3. {0}", str[c[j]]); ("4. {0}", str[d[j]]); ("5. {0}", str[e[j]]); ("Click ENTER to enter the next player"); (); } public bool CompareAns(int j, string[] PersonName, int n, bool bool2)//Compare the player's card with the victory result { 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) { (); ("恭喜Players{0}Win the game!", PersonName[j]); bool2 = false; } //Judge whether there are still trump cards for players to get else if (n==52) { (); ("No trump card!! Start over."); bool2 = false; } return bool2; } public void TakeCard(string[] PersonName, int PersonNum)// Players play cards { GetRandomNum(); int n = 1, botton=7; int f = 0; int up = 0, up1 = 0; bool bool2 = true; bool bool1 = false; // Distribute the card FaPai(PersonNum); // Players play cards n = 5 * PersonNum; while (bool2) { for (int j = 0; j < PersonNum; j++) { //Show the player's cards (); if (bool1) { ("The last player played the card"); ("{0}", str[up]); } bool1 = true; Print(PersonName, j); //The player takes the cards from the remaining cards or takes a card thrown by the player char D='f'; if (n == 5 * PersonNum) { try { ("Press D to get one card from the remaining cards"); D = (()); } catch (Exception e) { (); (1000); (0); } if (D == 'D' || D == 'd') { f = number[n]; n++; Print(PersonName, j); ("6. {0}", str[f]); } } else { ("Press D to get one card from the remaining cards or press T to get the card played by the previous player"); D = (()); if (D == 'D' || D == 'd') { f = number[n]; n++; Print(PersonName, j); ("6. {0}", str[f]); } else if (D == 't' || D == 'T') { Print(PersonName, j); ("6. {0}", str[up]); } } //play cards try { ("Enter the serial number you want to play:"); botton = Convert.ToInt32(()); } catch (Exception e) { (); (1000); (0); } switch (botton) { case 1: { ("被打出Card是{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: { ("被打出Card是{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: { ("被打出Card是{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: { ("被打出Card是{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: { ("被打出Card是{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: { ("被打出Card是{0}", str[f]); up = f; Print1(PersonName, j); break; } default: ("Input Error"); break; } //Judgement result bool2 = CompareAns(j,PersonName,n,bool2); if (bool2 == false) break; } } } } class ExeMain { static void Main(string[] args) { int PersonNum=0; Game obj = new Game(); ("The first name entered will be the first person to start taking the card:"); ("Number of the game:"); try { PersonNum = Convert.ToInt32(()); } catch (Exception e) { (); (1000); (0); } string[] PersonName = new string[PersonNum]; for (int i = 0; i < PersonNum; i++) { ("输入Players{0}Name", i + 1); PersonName[i] = (); } ();// Assign values to each string array (PersonName, PersonNum);//Distribute cards to every player (); } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.