• 自动发牌(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] + "	");
    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 }

    作者:耑新新,发布于  博客园

    转载请注明出处,欢迎邮件交流:zhuanxinxin@aliyun.com

  • 相关阅读:
    Flask使用mysql数据池
    Flask之WTForms
    Flask用Flask-SQLAlchemy连接MySQL
    Flask之中间件
    Flask之session相关
    Flask之请求和响应
    Flask路由系统与模板系统
    Flask之基本使用与配置
    Flask知识总汇
    Flask之视图函数
  • 原文地址:https://www.cnblogs.com/Arthurian/p/6117719.html
Copyright © 2020-2023  润新知