1. 程式人生 > >自動發牌(C#版)

自動發牌(C#版)

using ide bsp eric read over void log 成員

利用數組實現發牌過程

一副牌去掉大小王,還剩52張。一共東、南、西、北四家,每家隨機發13張牌。

提示:

  1. 東、南、西、北四家用一維數組表示
  2. 每家的牌采用一維數組表示(13張)
  3. 花色:enum Suit { Clubs, Diamonds, Hearts, Spades }
  4. 牌面:enum Value { Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace }
  5. 每張牌可以獨立作為一個類來實現,該類中包含兩個成員,即花色和牌面
 1 using System;
2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace Poke 7 { 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 Poker[] poker = new Poker[52]; 13 shuffle(poker); 14 15 //
發牌 16 Person[] person = new Person[4]; 17 for (int i = 0; i < 4; i++) 18 { 19 person[i] = new Person(); 20 person[i].perPoker = new Poker[13]; 21 } 22 for (int i = 0; i < 52; i++) 23 {
24 if (i % 4 == 0) 25 person[0].perPoker[person[0].count++] = poker[i]; 26 else if (i % 4 == 1) 27 person[1].perPoker[person[1].count++] = poker[i]; 28 else if (i % 4 == 2) 29 person[2].perPoker[person[2].count++] = poker[i]; 30 else if (i % 4 == 3) 31 person[3].perPoker[person[3].count++] = poker[i]; 32 } 33 //顯示每個人的牌 34 for (int i = 0; i < 4; i++) 35 { 36 Console.Write("第{0}個人的牌為:", i + 1); 37 for (int j = 0; j < 13; j++) 38 { 39 Console.Write(person[i].perPoker[j] + "\t"); 40 } 41 Console.WriteLine(); 42 } 43 Console.ReadKey(); 44 } 45 //洗牌 46 static void shuffle(Poker[] poker) 47 { 48 //設置52張牌 49 for (int i = 0; i < 4; i++) 50 for (int j = 0; j < 13; j++) 51 poker[i * 13 + j] = new Poker((Suit)i, (Value)j+1); 52 for (int i = 1; i <= 52; i++) 53 { 54 Random random = new Random(); 55 int num=random.Next(0, 53); 56 Poker temp = poker[i - 1]; 57 poker[i - 1] = poker[num-1]; 58 poker[num-1] = temp; 59 } 60 } 61 } 62 63 //花色 64 enum Suit { Clubs, Diamonds, Hearts, Spades } 65 66 //牌的值 67 enum Value { Ace = 1, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King } 68 69 //撲克牌類 70 class Poker 71 { 72 public Suit suit; 73 public Value value; 74 75 public Poker() { } 76 public Poker(Suit i, Value v) 77 { 78 suit = i; 79 value = v; 80 } 81 82 public override string ToString() 83 { 84 return (suit.ToString()+","+value.ToString()); 85 } 86 } 87 88 class Person 89 { 90 public Person(){} 91 public Poker[] perPoker; 92 public int count; 93 } 94 }

自動發牌(C#版)