• C# 之 扑克游戏 -- 21点规则介绍和代码实现


    一,游戏介绍

    1.1 游戏规则

    21点又名黑杰克,该游戏由2到6个人玩,使用除大小王之外的52张牌,游戏者的目标是使手中的牌的点数之和不超过21点且尽量大。

    1.2 牌点计算

    A至10牌,按其原点数计算;J、Q、K都算作10点。

    1.3 判断胜负

    二十一点玩法规则和概率在二十一点游戏中,拥有最高点数的玩家获胜,其点数必须等于或低于21点;超过21点的玩家称为爆牌。拥有最高点数的玩家获胜,其点数必须等于或低于21点;超过21点之间判负。


    二,游戏设计

    2.1 游戏流程

    1. 发牌:
      玩家和AI每人发两张牌,由手牌点数和大的玩家优先选择是否在牌堆中取牌

    2. 取牌:
      手牌点数和小于21,等待1中优先选择后再顺时针轮到其他玩家选择是否取牌

    3. 取牌后:
      若牌点大于21则直接判负出局,场上只剩1人,直接游戏结束;否则重复2-3
      若牌点小于等于21则轮到下家取牌,重复2-3

    4. 游戏结束
      其他玩家取牌后都超过21点,只剩1人,直接获胜
      所有玩家都选择不取牌后,按规则比较所有玩家手牌点数和,牌点大的获胜。

    2.2 玩家类

    由玩家自己选择是否继续拿牌。(输入Y继续拿牌,N为不拿牌)

    2.3 AI类

    简化AI逻辑,发牌后AI手牌和为4-8时继续拿牌,一直到17点或17点以上不再拿牌;因为此时再拿牌就有一半以上的概率超过21点。


    三,参考代码

    using System;
    
    namespace _21dian
    {
        using System;
        using System.Collections.Generic;
    
        namespace Game21Points
        {
            class Project
            {
                static void Main(string[] args)
                {
                    Console.WriteLine("----- 游戏开始 -----");
    
                    // 扑克牌
                    List<int> cards = new List<int>()
                    {
                        1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13,
                        14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
                        27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
                        40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52
                    };
    
                    // 创建对象
                    Poker poker = new Poker(cards);
                    Player player = new Player();
                    AIPlayer ai = new AIPlayer();
    
                    // --> 玩家入场
                    player.playerName = "Czhenya";
                    ai.playerName = "AI";
    
                    poker.AddPlayer(player);
                    poker.AddPlayer(ai);
    
                    // 事件关系绑定
                    poker.GameSratrHandler += player.GameStart;
                    poker.GameSratrHandler += ai.GameStart;
    
                    // 游戏开始 
                    poker.GameStart();
    
                    // 每人发两张牌
                    poker.SendCard();
                    poker.SendCard();
    
                    // 询问取牌
                    poker.TaskCard();
    
                    Console.ReadKey();
                }
            }
    
            abstract class AbsPlayer
            {
                public string playerName;
                public bool IsContinueTakeCard = true;
                public List<int> handCards = new List<int>();
    
                public abstract void GameStart();
    
                public virtual void SendCard(int card)
                {
                    handCards.Add(card);
                }
    
                public abstract bool TakeCard();
    
                public bool GameOver()
                {
                    bool isGameOver;
                    if (HandCardsPoint > 21)
                    {
                        isGameOver = true;
                    }
                    else
                    {
                        isGameOver = !IsContinueTakeCard;
                    }
                    return isGameOver;
                }
    
                public int HandCardsPoint { get { return PokeTools.HandCardsSum(handCards); } }
            }
    
            class Player : AbsPlayer
            {
                public override void GameStart()
                {
                    handCards.Clear();
                    Console.WriteLine("玩家整理了一下衣服,准备开局;");
                }
    
                public override void SendCard(int card)
                {
                    handCards.Add(card);
                    Console.WriteLine("玩家发牌:" + PokeTools.PokerBrandByPoint(card));
                }
    
                public override bool TakeCard()
                {
                    Console.WriteLine("当前您的手牌点数和为:" + HandCardsPoint);
                    Console.WriteLine("是否继续取牌(Y/N)?");
                    string readStr = Console.ReadLine();
                    // 输入Y取牌,其他为不取牌
                    IsContinueTakeCard = readStr.Equals("Y");
                    return IsContinueTakeCard;
                }
            }
    
            class AIPlayer : AbsPlayer
            {
                public override void GameStart()
                {
                    handCards.Clear();
                    Console.WriteLine("AI:清理一下内存,与之一战;");
                }
    
                public override void SendCard(int card)
                {
                    base.SendCard(card);
                    Console.WriteLine("AI发牌:" + PokeTools.PokerBrandByPoint(card));
                }
    
                public override bool TakeCard()
                {
                    // 手牌数点数小于17,就继续取牌
                    return HandCardsPoint < 17;
                }
            }
    
            class Poker
            {
                List<AbsPlayer> players = new List<AbsPlayer>();
                public Action GameSratrHandler;
                public Action<int> SendCardHandler;
                public Func<int, bool> TaskCardHandler;
                // 发牌用
                List<int> sendCards;
    
                public Poker(List<int> cards)
                {
                    // 复制一份发牌用
                    sendCards = new List<int>(cards);
                }
    
                public void AddPlayer(AbsPlayer player)
                {
                    players.Add(player);
                }
    
                public void GameStart()
                {
                    for (int i = 0; i < players.Count; i++)
                    {
                        if (!players[i].GameOver())
                        {
                            players[i].GameStart();
                        }
                    }
                }
    
                /// 
                /// 发牌 -- 会剔除已经发过的牌
                /// 
                public void SendCard()
                {
                    for (int i = 0; i < players.Count; i++)
                    {
                        players[i].SendCard(SendOneCard());
                    }
                }
    
                int SendOneCard()
                {
                    // 随机发一张牌
                    Random random = new Random();
                    int index = random.Next(0, sendCards.Count);
                    // 取到牌值
                    int cardPoint = sendCards[index];
                    // 从手牌中移除 --> 为避免发到相同的牌
                    sendCards.RemoveAt(index);
    
                    return cardPoint;
                }
    
                public void TaskCard()
                {
                    for (int i = 0; i < players.Count; i++)
                    {
                        // 选择取牌后再发一张牌
                        if (players[i].TakeCard())
                        {
                            players[i].SendCard(SendOneCard());
                        }
                        Console.WriteLine($"玩家:{players[i].playerName} 手牌:{PokeTools.ShowHandCard(players[i].handCards)}");
                    }
    
                    if (!GameOver())
                    {
                        TaskCard();
                    }
                }
    
                public bool GameOver()
                {
                    int playerCount = 0;
                    for (int i = 0; i < players.Count; i++)
                    {
                        if (!players[i].GameOver())
                        {
                            playerCount++;
                        }
                    }
    
                    bool isGameOver = playerCount <= 1;
    
                    if (isGameOver)
                    {
                        Console.WriteLine("游戏结束:");
    
                        List<AbsPlayer> playerList = new List<AbsPlayer>();
                        int maxPoint = 0;
                        for (int i = 0; i < players.Count; i++)
                        {
                            if (players[i].HandCardsPoint > 21)
                            {
                                Console.WriteLine($"玩家:{players[i].playerName} 爆牌了"  );
                            }
                            else
                            {
                                playerList.Add(players[i]);
                                if (maxPoint < players[i].HandCardsPoint)
                                {
                                    maxPoint = players[i].HandCardsPoint;
                                }
                            }
                        }
    
                        if (playerList.Count == 0)
                        {
                            Console.WriteLine("平局");
                        } 
                        else if (playerList.Count == 1)
                        {
    
                            Console.WriteLine($"玩家:{playerList[0].playerName} 赢了");
                        }
                        else
                        {
                            for (int i = 0; i < playerList.Count; i++)
                            {
                                if (maxPoint == playerList[i].HandCardsPoint)
                                {
                                    Console.WriteLine($"玩家:{playerList[i].playerName} 赢了");
                                }
                            }
                        }
                    }
                    return isGameOver;
                }
            }
        }
    
        class PokeTools
        {
            /// 
            /// 根据牌点返回牌名 如:14 ->红桃3
            /// 
            /// 
            /// 
            public static string PokerBrandByPoint(int card)
            {
                if (card > 52 || card <= 0)
                {
                    Console.WriteLine("不是扑克牌点");
                    return "不是扑克牌点";
                }
    
                string[] flowerColor = new string[4] { "黑桃", "红桃", "梅花", "方片" };
                string[] points = new string[13] { "K", "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q" };
                int huaSe = (card - 1) / 13;
                int point = card % 13;
    
                // 返回花色 + 牌点 如:红桃3
                return flowerColor[huaSe] + points[point];
            }
    
            /// 
            /// 手牌求和
            /// 
            /// 
            /// 
            public static int HandCardsSum(List<int> handCards)
            {
                // "K", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q"
                int[] points = new int[13] { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10 };
                int sumRes = 0;
    
                for (int i = 0; i < handCards.Count; i++)
                {
                    sumRes += points[handCards[i] % 13];
                }
    
                return sumRes;
            }
    
            // 显示手牌
            public static (string, string) ShowHandCard(List<int> handCards)
            {
                string resStr = "";
                for (int i = 0; i < handCards.Count; i++)
                {
                    resStr += PokeTools.PokerBrandByPoint(handCards[i]);
                    if (handCards.Count - 1 != i)
                    {
                        resStr += ",";
                    }
                }
    
                return (resStr, "牌点和:" + PokeTools.HandCardsSum(handCards));
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325

    测试结果:
    在这里插入图片描述


  • 相关阅读:
    【Nginx32】Nginx学习:随机索引、真实IP处理与来源处理模块
    MySQL数据库基础知识回顾
    自主式模块化无人机设计
    uniapp项目初始化时报gyp ERR
    软件项目测试指南
    Seal库官方示例(二):encoders.cpp解析
    ps怎么对字体进行加粗?
    Java学习之继承的本质(重要)
    Vue基础知识——数据绑定、数据代理
    javascript中的polyfill是什么,polyfill和babel的关系
  • 原文地址:https://blog.csdn.net/Czhenya/article/details/123198795