用c# 制作一个扑克牌小游戏

展示:

屏幕录制 2025-11-11 154914

界面:

cs 复制代码
namespace TexasPoker
{
    partial class Form1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要修改
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.button_start = new System.Windows.Forms.Button();
            this.panel2 = new System.Windows.Forms.Panel();
            this.Y5 = new System.Windows.Forms.TextBox();
            this.Y4 = new System.Windows.Forms.TextBox();
            this.Y3 = new System.Windows.Forms.TextBox();
            this.Y2 = new System.Windows.Forms.TextBox();
            this.Y1 = new System.Windows.Forms.TextBox();
            this.panel3 = new System.Windows.Forms.Panel();
            this.P3_5 = new System.Windows.Forms.TextBox();
            this.P3_4 = new System.Windows.Forms.TextBox();
            this.P3_3 = new System.Windows.Forms.TextBox();
            this.P3_2 = new System.Windows.Forms.TextBox();
            this.P3_1 = new System.Windows.Forms.TextBox();
            this.panel4 = new System.Windows.Forms.Panel();
            this.P1_5 = new System.Windows.Forms.TextBox();
            this.P1_4 = new System.Windows.Forms.TextBox();
            this.P1_3 = new System.Windows.Forms.TextBox();
            this.P1_2 = new System.Windows.Forms.TextBox();
            this.P1_1 = new System.Windows.Forms.TextBox();
            this.panel5 = new System.Windows.Forms.Panel();
            this.P2_5 = new System.Windows.Forms.TextBox();
            this.P2_4 = new System.Windows.Forms.TextBox();
            this.P2_3 = new System.Windows.Forms.TextBox();
            this.P2_2 = new System.Windows.Forms.TextBox();
            this.P2_1 = new System.Windows.Forms.TextBox();
            this.textBox_vlog = new System.Windows.Forms.TextBox();
            this.textBox_score = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.button_continue = new System.Windows.Forms.Button();
            this.button_Giveup = new System.Windows.Forms.Button();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.label5 = new System.Windows.Forms.Label();
            this.label6 = new System.Windows.Forms.Label();
            this.panel2.SuspendLayout();
            this.panel3.SuspendLayout();
            this.panel4.SuspendLayout();
            this.panel5.SuspendLayout();
            this.SuspendLayout();
            // 
            // button_start
            // 
            this.button_start.Location = new System.Drawing.Point(273, 575);
            this.button_start.Name = "button_start";
            this.button_start.Size = new System.Drawing.Size(214, 49);
            this.button_start.TabIndex = 0;
            this.button_start.Text = "开始游戏";
            this.button_start.UseVisualStyleBackColor = true;
            this.button_start.Click += new System.EventHandler(this.button_start_Click);
            // 
            // panel2
            // 
            this.panel2.Controls.Add(this.label5);
            this.panel2.Controls.Add(this.Y5);
            this.panel2.Controls.Add(this.Y4);
            this.panel2.Controls.Add(this.Y3);
            this.panel2.Controls.Add(this.Y2);
            this.panel2.Controls.Add(this.Y1);
            this.panel2.Location = new System.Drawing.Point(273, 407);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(529, 140);
            this.panel2.TabIndex = 2;
            // 
            // Y5
            // 
            this.Y5.Font = new System.Drawing.Font("宋体", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.Y5.Location = new System.Drawing.Point(426, 52);
            this.Y5.Name = "Y5";
            this.Y5.Size = new System.Drawing.Size(69, 42);
            this.Y5.TabIndex = 4;
            // 
            // Y4
            // 
            this.Y4.Font = new System.Drawing.Font("宋体", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.Y4.Location = new System.Drawing.Point(330, 52);
            this.Y4.Name = "Y4";
            this.Y4.Size = new System.Drawing.Size(69, 42);
            this.Y4.TabIndex = 3;
            // 
            // Y3
            // 
            this.Y3.Font = new System.Drawing.Font("宋体", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.Y3.Location = new System.Drawing.Point(233, 52);
            this.Y3.Name = "Y3";
            this.Y3.Size = new System.Drawing.Size(69, 42);
            this.Y3.TabIndex = 2;
            // 
            // Y2
            // 
            this.Y2.Font = new System.Drawing.Font("宋体", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.Y2.Location = new System.Drawing.Point(125, 52);
            this.Y2.Name = "Y2";
            this.Y2.Size = new System.Drawing.Size(69, 42);
            this.Y2.TabIndex = 1;
            // 
            // Y1
            // 
            this.Y1.Font = new System.Drawing.Font("宋体", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.Y1.Location = new System.Drawing.Point(27, 52);
            this.Y1.Name = "Y1";
            this.Y1.Size = new System.Drawing.Size(69, 42);
            this.Y1.TabIndex = 0;
            // 
            // panel3
            // 
            this.panel3.Controls.Add(this.label4);
            this.panel3.Controls.Add(this.P3_5);
            this.panel3.Controls.Add(this.P3_4);
            this.panel3.Controls.Add(this.P3_3);
            this.panel3.Controls.Add(this.P3_2);
            this.panel3.Controls.Add(this.P3_1);
            this.panel3.Location = new System.Drawing.Point(916, 93);
            this.panel3.Name = "panel3";
            this.panel3.Size = new System.Drawing.Size(186, 329);
            this.panel3.TabIndex = 3;
            // 
            // P3_5
            // 
            this.P3_5.Font = new System.Drawing.Font("宋体", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.P3_5.Location = new System.Drawing.Point(59, 274);
            this.P3_5.Name = "P3_5";
            this.P3_5.Size = new System.Drawing.Size(69, 42);
            this.P3_5.TabIndex = 17;
            // 
            // P3_4
            // 
            this.P3_4.Font = new System.Drawing.Font("宋体", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.P3_4.Location = new System.Drawing.Point(59, 209);
            this.P3_4.Name = "P3_4";
            this.P3_4.Size = new System.Drawing.Size(69, 42);
            this.P3_4.TabIndex = 16;
            // 
            // P3_3
            // 
            this.P3_3.Font = new System.Drawing.Font("宋体", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.P3_3.Location = new System.Drawing.Point(59, 143);
            this.P3_3.Name = "P3_3";
            this.P3_3.Size = new System.Drawing.Size(69, 42);
            this.P3_3.TabIndex = 15;
            // 
            // P3_2
            // 
            this.P3_2.Font = new System.Drawing.Font("宋体", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.P3_2.Location = new System.Drawing.Point(57, 77);
            this.P3_2.Name = "P3_2";
            this.P3_2.Size = new System.Drawing.Size(69, 42);
            this.P3_2.TabIndex = 14;
            // 
            // P3_1
            // 
            this.P3_1.Font = new System.Drawing.Font("宋体", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.P3_1.Location = new System.Drawing.Point(57, 19);
            this.P3_1.Name = "P3_1";
            this.P3_1.Size = new System.Drawing.Size(69, 42);
            this.P3_1.TabIndex = 13;
            // 
            // panel4
            // 
            this.panel4.Controls.Add(this.label2);
            this.panel4.Controls.Add(this.P1_5);
            this.panel4.Controls.Add(this.P1_4);
            this.panel4.Controls.Add(this.P1_3);
            this.panel4.Controls.Add(this.P1_2);
            this.panel4.Controls.Add(this.P1_1);
            this.panel4.Location = new System.Drawing.Point(35, 170);
            this.panel4.Name = "panel4";
            this.panel4.Size = new System.Drawing.Size(185, 357);
            this.panel4.TabIndex = 4;
            // 
            // P1_5
            // 
            this.P1_5.Font = new System.Drawing.Font("宋体", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.P1_5.Location = new System.Drawing.Point(50, 274);
            this.P1_5.Name = "P1_5";
            this.P1_5.Size = new System.Drawing.Size(69, 42);
            this.P1_5.TabIndex = 11;
            // 
            // P1_4
            // 
            this.P1_4.Font = new System.Drawing.Font("宋体", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.P1_4.Location = new System.Drawing.Point(50, 209);
            this.P1_4.Name = "P1_4";
            this.P1_4.Size = new System.Drawing.Size(69, 42);
            this.P1_4.TabIndex = 10;
            // 
            // P1_3
            // 
            this.P1_3.Font = new System.Drawing.Font("宋体", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.P1_3.Location = new System.Drawing.Point(50, 144);
            this.P1_3.Name = "P1_3";
            this.P1_3.Size = new System.Drawing.Size(69, 42);
            this.P1_3.TabIndex = 9;
            // 
            // P1_2
            // 
            this.P1_2.Font = new System.Drawing.Font("宋体", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.P1_2.Location = new System.Drawing.Point(50, 77);
            this.P1_2.Name = "P1_2";
            this.P1_2.Size = new System.Drawing.Size(69, 42);
            this.P1_2.TabIndex = 8;
            // 
            // P1_1
            // 
            this.P1_1.Font = new System.Drawing.Font("宋体", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.P1_1.Location = new System.Drawing.Point(50, 19);
            this.P1_1.Name = "P1_1";
            this.P1_1.Size = new System.Drawing.Size(69, 42);
            this.P1_1.TabIndex = 7;
            // 
            // panel5
            // 
            this.panel5.Controls.Add(this.label3);
            this.panel5.Controls.Add(this.P2_5);
            this.panel5.Controls.Add(this.P2_4);
            this.panel5.Controls.Add(this.P2_3);
            this.panel5.Controls.Add(this.P2_2);
            this.panel5.Controls.Add(this.P2_1);
            this.panel5.Location = new System.Drawing.Point(308, 12);
            this.panel5.Name = "panel5";
            this.panel5.Size = new System.Drawing.Size(460, 110);
            this.panel5.TabIndex = 5;
            // 
            // P2_5
            // 
            this.P2_5.Font = new System.Drawing.Font("宋体", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.P2_5.Location = new System.Drawing.Point(366, 32);
            this.P2_5.Name = "P2_5";
            this.P2_5.Size = new System.Drawing.Size(69, 42);
            this.P2_5.TabIndex = 12;
            // 
            // P2_4
            // 
            this.P2_4.Font = new System.Drawing.Font("宋体", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.P2_4.Location = new System.Drawing.Point(277, 32);
            this.P2_4.Name = "P2_4";
            this.P2_4.Size = new System.Drawing.Size(69, 42);
            this.P2_4.TabIndex = 11;
            // 
            // P2_3
            // 
            this.P2_3.Font = new System.Drawing.Font("宋体", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.P2_3.Location = new System.Drawing.Point(186, 32);
            this.P2_3.Name = "P2_3";
            this.P2_3.Size = new System.Drawing.Size(69, 42);
            this.P2_3.TabIndex = 10;
            // 
            // P2_2
            // 
            this.P2_2.Font = new System.Drawing.Font("宋体", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.P2_2.Location = new System.Drawing.Point(99, 32);
            this.P2_2.Name = "P2_2";
            this.P2_2.Size = new System.Drawing.Size(69, 42);
            this.P2_2.TabIndex = 9;
            // 
            // P2_1
            // 
            this.P2_1.Font = new System.Drawing.Font("宋体", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.P2_1.Location = new System.Drawing.Point(15, 32);
            this.P2_1.Name = "P2_1";
            this.P2_1.Size = new System.Drawing.Size(69, 42);
            this.P2_1.TabIndex = 8;
            // 
            // textBox_vlog
            // 
            this.textBox_vlog.Location = new System.Drawing.Point(376, 183);
            this.textBox_vlog.Multiline = true;
            this.textBox_vlog.Name = "textBox_vlog";
            this.textBox_vlog.Size = new System.Drawing.Size(346, 148);
            this.textBox_vlog.TabIndex = 6;
            // 
            // textBox_score
            // 
            this.textBox_score.Location = new System.Drawing.Point(934, 563);
            this.textBox_score.Name = "textBox_score";
            this.textBox_score.Size = new System.Drawing.Size(124, 25);
            this.textBox_score.TabIndex = 7;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(957, 532);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(67, 15);
            this.label1.TabIndex = 8;
            this.label1.Text = "你的筹码";
            // 
            // button_continue
            // 
            this.button_continue.Location = new System.Drawing.Point(536, 575);
            this.button_continue.Name = "button_continue";
            this.button_continue.Size = new System.Drawing.Size(105, 49);
            this.button_continue.TabIndex = 9;
            this.button_continue.Text = "加注";
            this.button_continue.UseVisualStyleBackColor = true;
            this.button_continue.Click += new System.EventHandler(this.button_continue_Click);
            // 
            // button_Giveup
            // 
            this.button_Giveup.Location = new System.Drawing.Point(674, 575);
            this.button_Giveup.Name = "button_Giveup";
            this.button_Giveup.Size = new System.Drawing.Size(105, 49);
            this.button_Giveup.TabIndex = 10;
            this.button_Giveup.Text = "弃牌";
            this.button_Giveup.UseVisualStyleBackColor = true;
            this.button_Giveup.Click += new System.EventHandler(this.button_Giveup_Click);
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(21, 159);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(23, 15);
            this.label2.TabIndex = 11;
            this.label2.Text = "P1";
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(211, 81);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(23, 15);
            this.label3.TabIndex = 12;
            this.label3.Text = "P2";
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(149, 154);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(23, 15);
            this.label4.TabIndex = 12;
            this.label4.Text = "P3";
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(246, 21);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(31, 15);
            this.label5.TabIndex = 12;
            this.label5.Text = "You";
            // 
            // label6
            // 
            this.label6.AutoSize = true;
            this.label6.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.label6.Location = new System.Drawing.Point(81, 28);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(69, 80);
            this.label6.TabIndex = 11;
            this.label6.Text = "梅花-C\r\n红心-H\r\n黑桃-S\r\n方块-D";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(1125, 655);
            this.Controls.Add(this.label6);
            this.Controls.Add(this.button_Giveup);
            this.Controls.Add(this.button_continue);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.textBox_score);
            this.Controls.Add(this.textBox_vlog);
            this.Controls.Add(this.panel5);
            this.Controls.Add(this.panel4);
            this.Controls.Add(this.panel3);
            this.Controls.Add(this.panel2);
            this.Controls.Add(this.button_start);
            this.Name = "Form1";
            this.Text = "Form1";
            this.panel2.ResumeLayout(false);
            this.panel2.PerformLayout();
            this.panel3.ResumeLayout(false);
            this.panel3.PerformLayout();
            this.panel4.ResumeLayout(false);
            this.panel4.PerformLayout();
            this.panel5.ResumeLayout(false);
            this.panel5.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button button_start;
        private System.Windows.Forms.Panel panel2;
        private System.Windows.Forms.Panel panel3;
        private System.Windows.Forms.Panel panel4;
        private System.Windows.Forms.Panel panel5;
        private System.Windows.Forms.TextBox textBox_vlog;
        private System.Windows.Forms.TextBox Y5;
        private System.Windows.Forms.TextBox Y4;
        private System.Windows.Forms.TextBox Y3;
        private System.Windows.Forms.TextBox Y2;
        private System.Windows.Forms.TextBox Y1;
        private System.Windows.Forms.TextBox P1_5;
        private System.Windows.Forms.TextBox P1_4;
        private System.Windows.Forms.TextBox P1_3;
        private System.Windows.Forms.TextBox P1_2;
        private System.Windows.Forms.TextBox P1_1;
        private System.Windows.Forms.TextBox P2_5;
        private System.Windows.Forms.TextBox P2_4;
        private System.Windows.Forms.TextBox P2_3;
        private System.Windows.Forms.TextBox P2_2;
        private System.Windows.Forms.TextBox P2_1;
        private System.Windows.Forms.TextBox P3_5;
        private System.Windows.Forms.TextBox P3_4;
        private System.Windows.Forms.TextBox P3_3;
        private System.Windows.Forms.TextBox P3_2;
        private System.Windows.Forms.TextBox P3_1;
        private System.Windows.Forms.TextBox textBox_score;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Button button_continue;
        private System.Windows.Forms.Button button_Giveup;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label6;
    }
}

//Clubs(梅花)Hearts(红心)Spades(黑桃)Diamonds(方块)

static string[] cards = { "C_2", "C_3","C_4","C_5", "C_6", "C_7", "C_8", "C_9", "C_10", "C_J", "C_Q", "C_K" ,"C_A",

"H_2", "H_3","H_4","H_5", "H_6", "H_7", "H_8", "H_9", "H_10", "H_J", "H_Q", "H_K" ,"H_A",

"S_2", "S_3","S_4","S_5", "S_6", "S_7", "S_8", "S_9", "S_10", "S_J", "S_Q", "S_K" ,"S_A",

"D_2", "D_3","D_4","D_5", "D_6", "D_7", "D_8", "D_9", "D_10", "D_J", "D_Q", "D_K" ,"D_A"

};

static int Round = 0;//游戏回合

static int score = 100;//初始下注100

static int Property = 1000;//玩家初始财产

static int allscore = 300;//人机的总注码

// 记录人机是否放弃

static bool P1_giveup = false;

static bool P2_giveup = false;

static bool P3_giveup = false;

string[] group1;//玩家Y1

string[] group2;//人机P1

string[] group3;//人机P2

string[] group4;//人机P3

//(A认作14.没有A2345这个顺子)

static Dictionary<string, int> cardValues = new Dictionary<string, int>

{

{"A", 14}, {"2", 2}, {"3", 3}, {"4", 4}, {"5", 5},

{"6", 6}, {"7", 7}, {"8", 8}, {"9", 9}, {"10", 10},

{"J", 11}, {"Q", 12}, {"K", 13}

};

static Dictionary<int, string> grouplevel = new Dictionary<int, string>

{

{1,"散牌" },{2,"一对"},{3,"两对"},{4,"三条"},{5,"顺子"},{6,"同花"},{7,"葫芦"},{8,"四条"},{9,"同花顺"},

};

通过random 打乱 排序

List<string> shuffled = cards.OrderBy(x => random.Next()).ToList();//洗牌

然后5张5张发牌。

group1 = shuffled.Take(5).ToArray();//玩家Y1

group2 = shuffled.Skip(5).Take(5).ToArray();//人机P1

group3 = shuffled.Skip(10).Take(5).ToArray();//人机P2

group4 = shuffled.Skip(15).Take(5).ToArray();//人机P3

判断牌型 函数如下:

//判断是什么等级

private int LevelGroup(string[] group)

{

bool tonghua = IsTonghua(group);

bool shunzi = IsShunzi(group);

if (tonghua && shunzi) return 9; // 同花顺

if (IsSitiao(group)) return 8; // 四条

if (IsHulu(group)) return 7; // 葫芦

if (tonghua) return 6; // 同花

if (shunzi) return 5; // 顺子

if (IsSantiao(group)) return 4; // 三条

if (IsLiangdui(group)) return 3; // 两对

if (IsYidui(group)) return 2; // 一对

return 1; // 散牌

}

对每种情况,进行判断.

完整代码:

cs 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace TexasPoker
{
    public partial class Form1 : Form
    {
        //Clubs(梅花)Hearts(红心)Spades(黑桃)Diamonds(方块)
        static string[] cards = { "C_2", "C_3","C_4","C_5", "C_6", "C_7", "C_8", "C_9", "C_10", "C_J", "C_Q", "C_K" ,"C_A",
                            "H_2", "H_3","H_4","H_5", "H_6", "H_7", "H_8", "H_9", "H_10", "H_J", "H_Q", "H_K" ,"H_A",
                            "S_2", "S_3","S_4","S_5", "S_6", "S_7", "S_8", "S_9", "S_10", "S_J", "S_Q", "S_K" ,"S_A",
                            "D_2", "D_3","D_4","D_5", "D_6", "D_7", "D_8", "D_9", "D_10", "D_J", "D_Q", "D_K" ,"D_A"

        };

        static int Round = 0;//游戏回合
        static int score = 100;//初始下注100
        static int Property = 1000;//玩家初始财产
        static int allscore = 300;//人机的总注码

        // 记录人机是否放弃
        static bool P1_giveup = false;
        static bool P2_giveup = false;
        static bool P3_giveup = false;

        string[] group1;//玩家Y1
        string[] group2;//人机P1
        string[] group3;//人机P2
        string[] group4;//人机P3

        //(A认作14.没有A2345这个顺子)
        static Dictionary<string, int> cardValues = new Dictionary<string, int>
            {
                {"A", 14}, {"2", 2}, {"3", 3}, {"4", 4}, {"5", 5},
                {"6", 6}, {"7", 7}, {"8", 8}, {"9", 9}, {"10", 10},
                {"J", 11}, {"Q", 12}, {"K", 13}
            };

        static Dictionary<int, string> grouplevel = new Dictionary<int, string>
            {
            {1,"散牌" },{2,"一对"},{3,"两对"},{4,"三条"},{5,"顺子"},{6,"同花"},{7,"葫芦"},{8,"四条"},{9,"同花顺"},
            };

        static Random random = new Random();
        public Form1()
        {
            InitializeComponent();
            textBox_score.Text = Property.ToString();
        }

        private void button_start_Click(object sender, EventArgs e)
        {
            score = 100;//重置注码
            allscore = 300;
            clear_text();
            textBox_vlog.AppendText("====开始游戏:从1千块 赢到三千七百万===\r\n");

            // 重置放弃状态
            P1_giveup = false;
            P2_giveup = false;
            P3_giveup = false;

            List<string> shuffled = cards.OrderBy(x => random.Next()).ToList();//洗牌

            group1 = shuffled.Take(5).ToArray();//玩家Y1
            group2 = shuffled.Skip(5).Take(5).ToArray();//人机P1
            group3 = shuffled.Skip(10).Take(5).ToArray();//人机P2
            group4 = shuffled.Skip(15).Take(5).ToArray();//人机P3

            //游戏开始 展示2张牌(每人一张暗牌,4张名牌)
            Y1.Text = group1[0];
            Y2.Text = group1[1];
            P1_2.Text = group2[1];
            P2_2.Text = group3[1];
            P3_2.Text = group4[1];

            Round = 1;
        }

        //判断是什么等级
        private int LevelGroup(string[] group)
        {
            bool tonghua = IsTonghua(group);
            bool shunzi = IsShunzi(group);

            if (tonghua && shunzi) return 9; // 同花顺
            if (IsSitiao(group)) return 8;    // 四条
            if (IsHulu(group)) return 7;      // 葫芦
            if (tonghua) return 6;            // 同花
            if (shunzi) return 5;             // 顺子
            if (IsSantiao(group)) return 4;   // 三条
            if (IsLiangdui(group)) return 3;  // 两对
            if (IsYidui(group)) return 2;     // 一对

            return 1; // 散牌
        }

        // 计算牌的分数(用于同级牌型比较)
        private int CalculateScore(string[] group, int level)
        {
            int[] values = StringToInt(group);
            Array.Sort(values);
            Array.Reverse(values); // 从大到小排序,便于比较

            switch (level)
            {
                case 9: // 同花顺
                case 5: // 顺子
                    return values[0] * 10000; // 比较最大牌

                case 8: // 四条
                    var fourOfAKind = values.GroupBy(v => v).First(g => g.Count() == 4).Key;
                    var kicker = values.First(v => v != fourOfAKind);
                    return fourOfAKind * 10000 + kicker;

                case 7: // 葫芦
                    var three = values.GroupBy(v => v).First(g => g.Count() == 3).Key;
                    var two = values.GroupBy(v => v).First(g => g.Count() == 2).Key;
                    return three * 10000 + two;

                case 6: // 同花
                case 1: // 散牌
                    return values[0] * 10000 + values[1] * 1000 + values[2] * 100 + values[3] * 10 + values[4];

                case 4: // 三条
                    var three2 = values.GroupBy(v => v).First(g => g.Count() == 3).Key;
                    var otherCards = values.Where(v => v != three2).OrderByDescending(v => v).ToArray();
                    return three2 * 10000 + otherCards[0] * 100 + otherCards[1];

                case 3: // 两对
                    var pairs = values.GroupBy(v => v).Where(g => g.Count() == 2).OrderByDescending(g => g.Key).ToArray();
                    var kicker2 = values.First(v => v != pairs[0].Key && v != pairs[1].Key);
                    return pairs[0].Key * 10000 + pairs[1].Key * 100 + kicker2;

                case 2: // 一对
                    var pair = values.GroupBy(v => v).First(g => g.Count() == 2).Key;
                    var otherCards2 = values.Where(v => v != pair).OrderByDescending(v => v).ToArray();
                    return pair * 10000 + otherCards2[0] * 100 + otherCards2[1] * 10 + otherCards2[2];

                default:
                    return 0;
            }
        }

        //转换成数组
        private int[] StringToInt(string[] group1)
        {
            int[] values = new int[5];
            for (int i = 0; i < 5; i++)
            {
                string cardValue = group1[i].Substring(2);
                values[i] = cardValues[cardValue];
            }
            return values;
        }

        //判断是否为同花
        private bool IsTonghua(string[] group1)
        {
            char hua = group1[0][0];
            for (int i = 1; i < 5; i++)
            {
                if (group1[i][0] != hua)
                {
                    return false;
                }
            }
            return true;
        }

        //判断是否为顺子
        private bool IsShunzi(string[] group)
        {
            int[] values = StringToInt(group);
            Array.Sort(values);

            // 检查A-2-3-4-5的特殊情况(A作为1)
            if (values.SequenceEqual(new[] { 2, 3, 4, 5, 14 }))
            {
                return true;
            }

            for (int i = 1; i < values.Length; i++)
            {
                if (values[i] != values[i - 1] + 1)
                {
                    return false;
                }
            }
            return true;
        }

        //判断是否为葫芦(三条+一对)
        private bool IsHulu(string[] group)
        {
            int[] values = StringToInt(group);
            var groups = values.GroupBy(v => v).Select(g => g.Count()).OrderByDescending(c => c).ToArray();
            return groups.Length == 2 && groups[0] == 3 && groups[1] == 2;
        }

        //判断是否为四条
        private bool IsSitiao(string[] group)
        {
            int[] values = StringToInt(group);
            return values.GroupBy(v => v).Any(g => g.Count() == 4);
        }

        //判断是否为三条
        private bool IsSantiao(string[] group)
        {
            int[] values = StringToInt(group);
            return values.GroupBy(v => v).Any(g => g.Count() == 3);
        }

        //判断是否为两对
        private bool IsLiangdui(string[] group)
        {
            int[] values = StringToInt(group);
            var pairCount = values.GroupBy(v => v).Count(g => g.Count() == 2);
            return pairCount == 2;
        }

        //判断是否为一对
        private bool IsYidui(string[] group)
        {
            int[] values = StringToInt(group);
            var pairCount = values.GroupBy(v => v).Count(g => g.Count() == 2);
            var threeCount = values.GroupBy(v => v).Count(g => g.Count() == 3);
            return pairCount == 1 && threeCount == 0;
        }

        // 人机决策方法 - 第二回合根据牌型决定是否加注
        private bool ShouldRobotRaise(string[] robotCards)
        {
            int level = LevelGroup(robotCards);

            // 有一对或以上的牌型就加注
            if (level >= 2)
            {
                return true;
            }

            // 散牌时,根据最大牌决定(如果最大牌是J、Q、K、A,有50%概率加注)
            int[] values = StringToInt(robotCards);
            int maxValue = values.Max();

            if (maxValue >= 11) // J、Q、K、A
            {
                return random.Next(2) == 1; // 50%概率
            }

            return false;
        }

        private void button_continue_Click(object sender, EventArgs e)
        {
            //玩家第二次选择加注
            if (Round == 2)
            {
                textBox_vlog.AppendText("你选择再次加注\r\n");
                score = score + 100;//加注100

                //人机进行思考
                Thread.Sleep(500);

                // P1决策
                if (!P1_giveup)
                {
                    if (ShouldRobotRaise(group2))
                    {
                        textBox_vlog.AppendText("P1加注\r\n");
                        allscore += 100;
                    }
                    else
                    {
                        textBox_vlog.AppendText("P1放弃\r\n");
                        P1_giveup = true;
                    }
                }

                Thread.Sleep(500);

                // P2决策
                if (!P2_giveup)
                {
                    if (ShouldRobotRaise(group3))
                    {
                        textBox_vlog.AppendText("P2加注\r\n");
                        allscore += 100;
                    }
                    else
                    {
                        textBox_vlog.AppendText("P2放弃\r\n");
                        P2_giveup = true;
                    }
                }

                Thread.Sleep(500);

                // P3决策
                if (!P3_giveup)
                {
                    if (ShouldRobotRaise(group4))
                    {
                        textBox_vlog.AppendText("P3加注\r\n");
                        allscore += 100;
                    }
                    else
                    {
                        textBox_vlog.AppendText("P3放弃\r\n");
                        P3_giveup = true;
                    }
                }

                //思考结束,开牌。
                // 创建参与比较的玩家列表
                var participants = new List<(string Name, string[] Cards, int Level, int Score)>();

                // 玩家总是参与
                participants.Add(("玩家", group1, LevelGroup(group1), CalculateScore(group1, LevelGroup(group1))));

                // 只有未放弃的人机才参与比较
                if (!P1_giveup) participants.Add(("P1", group2, LevelGroup(group2), CalculateScore(group2, LevelGroup(group2))));
                if (!P2_giveup) participants.Add(("P2", group3, LevelGroup(group3), CalculateScore(group3, LevelGroup(group3))));
                if (!P3_giveup) participants.Add(("P3", group4, LevelGroup(group4), CalculateScore(group4, LevelGroup(group4))));

                // 按等级和分数排序
                var sortedGroups = participants.OrderByDescending(g => g.Level)
                                              .ThenByDescending(g => g.Score)
                                              .ToList();

                // 找出胜者(可能有多个平局)
                var winners = sortedGroups.Where(g => g.Level == sortedGroups[0].Level && g.Score == sortedGroups[0].Score).ToList();

                // 显示所有暗牌
                P1_1.Text = group2[0];
                P2_1.Text = group3[0];
                P3_1.Text = group4[0];
                this.Refresh();

                if (winners.Count == 1)
                {
                    textBox_vlog.AppendText($"最终的胜者是:{winners[0].Name},是{grouplevel[winners[0].Level]}\r\n");
                }
                else
                {
                    textBox_vlog.AppendText($"最终是平局,胜者有:{string.Join("、", winners.Select(w => w.Name))}\r\n");
                }

                // 结算
                if (winners.Any(w => w.Name == "玩家"))
                {
                    Property = Property + allscore;
                    textBox_score.Text = Property.ToString();
                }
                else
                {
                    Property = Property - score;
                    textBox_score.Text = Property.ToString();
                }
            }

            //玩家第一次选择加注
            if (Round == 1)
            {
                score = score + 100;//加注100
                textBox_vlog.AppendText("你选择加注\r\n");

                //人机进行思考(第一回合人机总是加注)
                Thread.Sleep(500);
                textBox_vlog.AppendText("P1加注\r\n");
                Thread.Sleep(500);
                textBox_vlog.AppendText("P2加注\r\n");
                Thread.Sleep(500);
                textBox_vlog.AppendText("P3加注\r\n");
                allscore = allscore + 300;

                //第一轮结束,开牌
                Y3.Text = group1[2];
                Y4.Text = group1[3];
                Y5.Text = group1[4];

                P1_3.Text = group2[2];
                P1_4.Text = group2[3];
                P1_5.Text = group2[4];

                P2_3.Text = group3[2];
                P2_4.Text = group3[3];
                P2_5.Text = group3[4];

                P3_3.Text = group4[2];
                P3_4.Text = group4[3];
                P3_5.Text = group4[4];
                this.Refresh();

                Round = 2;
            }
        }

        private void button_Giveup_Click(object sender, EventArgs e)
        {
            textBox_vlog.AppendText("你选择放弃\r\n");
            Property = Property - score;
            textBox_score.Text = Property.ToString();

            //玩家放弃。人机进行思考
            if (Round == 1)
            {
                //人机进行思考(第一回合人机总是加注)
                Thread.Sleep(500);
                textBox_vlog.AppendText("P1加注\r\n");
                Thread.Sleep(500);
                textBox_vlog.AppendText("P2加注\r\n");
                Thread.Sleep(500);
                textBox_vlog.AppendText("P3加注\r\n");

                //第一轮结束,开牌
                Y3.Text = group1[2];
                Y4.Text = group1[3];
                Y5.Text = group1[4];

                P1_3.Text = group2[2];
                P1_4.Text = group2[3];
                P1_5.Text = group2[4];

                P2_3.Text = group3[2];
                P2_4.Text = group3[3];
                P2_5.Text = group3[4];

                P3_3.Text = group4[2];
                P3_4.Text = group4[3];
                P3_5.Text = group4[4];
                this.Refresh();
                Round = 2;
                Thread.Sleep(1000);
            }

            if (Round == 2)
            {
                //人机进行思考
                Thread.Sleep(500);

                // P1决策
                if (!P1_giveup)
                {
                    if (ShouldRobotRaise(group2))
                    {
                        textBox_vlog.AppendText("P1加注\r\n");
                        allscore += 100;
                    }
                    else
                    {
                        textBox_vlog.AppendText("P1放弃\r\n");
                        P1_giveup = true;
                    }
                }

                Thread.Sleep(500);

                // P2决策
                if (!P2_giveup)
                {
                    if (ShouldRobotRaise(group3))
                    {
                        textBox_vlog.AppendText("P2加注\r\n");
                        allscore += 100;
                    }
                    else
                    {
                        textBox_vlog.AppendText("P2放弃\r\n");
                        P2_giveup = true;
                    }
                }

                Thread.Sleep(500);

                // P3决策
                if (!P3_giveup)
                {
                    if (ShouldRobotRaise(group4))
                    {
                        textBox_vlog.AppendText("P3加注\r\n");
                        allscore += 100;
                    }
                    else
                    {
                        textBox_vlog.AppendText("P3放弃\r\n");
                        P3_giveup = true;
                    }
                }

                // 创建参与比较的玩家列表(玩家已放弃,不参与)
                var participants = new List<(string Name, string[] Cards, int Level, int Score)>();

                // 只有未放弃的人机才参与比较
                if (!P1_giveup) participants.Add(("P1", group2, LevelGroup(group2), CalculateScore(group2, LevelGroup(group2))));
                if (!P2_giveup) participants.Add(("P2", group3, LevelGroup(group3), CalculateScore(group3, LevelGroup(group3))));
                if (!P3_giveup) participants.Add(("P3", group4, LevelGroup(group4), CalculateScore(group4, LevelGroup(group4))));

                if (participants.Count > 0)
                {
                    // 按等级和分数排序
                    var sortedGroups = participants.OrderByDescending(g => g.Level)
                                                  .ThenByDescending(g => g.Score)
                                                  .ToList();

                    // 找出胜者(可能有多个平局)
                    var winners = sortedGroups.Where(g => g.Level == sortedGroups[0].Level && g.Score == sortedGroups[0].Score).ToList();

                    // 显示暗牌
                    P1_1.Text = group2[0];
                    P2_1.Text = group3[0];
                    P3_1.Text = group4[0];
                    this.Refresh();

                    if (winners.Count == 1)
                    {
                        textBox_vlog.AppendText($"最终的胜者是:{winners[0].Name},是{grouplevel[winners[0].Level]}\r\n");
                    }
                    else
                    {
                        textBox_vlog.AppendText($"最终是平局,胜者有:{string.Join("、", winners.Select(w => w.Name))}\r\n");
                    }
                }
                else
                {
                    textBox_vlog.AppendText("所有玩家都放弃了,没有胜者\r\n");
                }
            }
        }

        private void clear_text()
        {
            Y1.Clear();
            Y2.Clear();
            Y3.Clear();
            Y4.Clear();
            Y5.Clear();

            P1_1.Clear();
            P1_2.Clear();
            P1_3.Clear();
            P1_4.Clear();
            P1_5.Clear();

            P2_1.Clear();
            P2_2.Clear();
            P2_3.Clear();
            P2_4.Clear();
            P2_5.Clear();

            P3_1.Clear();
            P3_2.Clear();
            P3_3.Clear();
            P3_4.Clear();
            P3_5.Clear();

            textBox_vlog.Clear();
        }
    }
}
相关推荐
liu****16 小时前
八.函数递归
c语言·开发语言·数据结构·c++·算法
韭菜钟17 小时前
在Qt中使用QuickJS
开发语言·qt
β添砖java17 小时前
python第一阶段第六章python数据容器
开发语言·python
YouEmbedded17 小时前
解码C++基础——从C到C++
开发语言·从c到c++
yong999017 小时前
基于多普勒连续波雷达原理的MATLAB测速程序
开发语言·matlab
o***369317 小时前
python爬虫——爬取全年天气数据并做可视化分析
开发语言·爬虫·python
枫叶丹417 小时前
【Qt开发】Qt窗口(三) -> QStatusBar状态栏
c语言·开发语言·数据库·c++·qt·microsoft
亮子AI17 小时前
【JavaScript】修改数组的正确方法
开发语言·javascript·ecmascript
浮尘笔记17 小时前
Go语言中如何实现线程安全的map
开发语言·后端·安全·golang