首页 > 代码库 > 自动发牌(C#版)

自动发牌(C#版)

利用数组实现发牌过程

一副牌去掉大小王,还剩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 Poker71     {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 =http://www.mamicode.com/ v;80         }81 82         public override string  ToString()83         {84              return (suit.ToString()+","+value.ToString());85         }86     }87 88     class Person89     {90         public Person(){}91         public Poker[] perPoker;92         public int count;93     }94 }

 

自动发牌(C#版)