游戏21点(一个月前)

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;


namespace exercise0502
{
    public partial class Game21 : Form
    {
        private int[] str;
        public Random ran;
        private int[] pers;
        private int[] computer;
        private int countPer;
        private int countComputer;
        private int sumPer;
        private int sumComputer;
        private int numPer;
        private int numComputer;
        private int k;


        public Game21()
        {
            //字段初始化
            k = sumPer = sumComputer = countPer = countComputer = 0;
            str = new int[52];
            pers = new int[11];
            computer = new int[11];
            ran = new Random();
            InitializeComponent();
            //洗牌操作
            for (int i = 1; i < 53; i++)
            {
                while (true)
                {
                    int index = ran.Next(0, 52);
                    if (str[index] == 0)
                    {
                        str[index] = i;
                        break;
                    }
                    else
                        continue;
                }
            }           
        }


        private void Game21_Load(object sender, EventArgs e)
        {           
            //打印52张背面牌
            for (int i = 0; i < 52; i++)
            {
                PictureBox pic = new PictureBox();
                pic.Size = new Size(71, 96);
                pic.Image = Image.FromFile(Application.StartupPath + @"\images\rear.bmp");
                pic.Location = new Point(60 + 5 * i, 190);
                this.Controls.Add(pic);
                pic.BringToFront();
            }
            //点击下注前叫牌和开牌操作不可用
            this.callCard.Enabled = false;
            this.openCard.Enabled = false;
        }


        /// <summary>
        /// 下注操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bottomPour_Click(object sender, EventArgs e)
        {
            //点击下注后叫牌和开牌操作可用
            this.callCard.Enabled = true;
            this.openCard.Enabled = true;
            //判断余额是否大于下注金额
            if (int.Parse(inputMoney.Text.Trim()) > int.Parse(savings.Text.Trim()))
                MessageBox.Show("余额不足,请输入小于" + savings.Text.Trim() + "的数字");
            else
            {
                //下注只可点击一次操作
                this.bottomPour.Enabled = false;
                //获取上一回合取出的牌的个数,即要覆盖的牌的数量
                int n = countPer + countComputer - k;
                if (countPer + countComputer - 1 < 0)
                    k = 0;
                else
                    k = countPer + countComputer - 1;
                if (k > 48)
                {
                    MessageBox.Show("游戏结束!!");
                    Application.Exit();
                }
                else
                {
                    sumComputer = sumPer = 0;
                    numPer = numComputer = 2;
                    for (int i = 0; i < pers.Length; i++)
                        pers[i] = 0;
                    for (int i = 0; i < computer.Length; i++)
                        computer[i] = 0;
                    pers[0] = str[k];
                    pers[1] = str[k + 1];
                    computer[0] = str[k + 2];
                    computer[1] = str[k + 3];
                    for (int i = k; i < k + 4 + n; i++)
                    {
                        if (this.Controls.Count <= 4)
                        {
                            MessageBox.Show("游戏结束!!");
                            Application.Exit();
                            break;
                        }
                        //发牌时,背面牌对应减少
                        this.Controls.RemoveAt(0);
                    }
                    for (int i = 0; i < 2; i++)
                    {
                        PictureBox pic = new PictureBox();
                        pic.Size = new Size(71, 96);
                        pic.Image = Image.FromFile(Application.StartupPath + @"\images\" + pers[i] + ".bmp");
                        pic.Location = new Point(300 + 20 * i, 350);
                        this.Controls.Add(pic);
                        pic.BringToFront();
                        countPer++;
                    }
                    for (int i = 0; i < 2; i++)
                    {
                        PictureBox pic = new PictureBox();
                        pic.Size = new Size(71, 96);
                        if (i == 1)
                            pic.Image = Image.FromFile(Application.StartupPath + @"\images\rear.bmp");
                        else
                            pic.Image = Image.FromFile(Application.StartupPath + @"\images\" + computer[i] + ".bmp");
                        pic.Location = new Point(300 + 20 * i, 50);
                        this.Controls.Add(pic);
                        pic.BringToFront();
                        countComputer++;
                    }
                }
            }
        }


        /// <summary>
        /// 要牌操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void callCard_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.Controls.Count; i++)
            {
                if ((this.Controls[i] as PictureBox).Location.Y == 190)
                {
                    if (this.Controls.Count == 0)
                    {
                        MessageBox.Show("游戏结束!!");
                        Application.Exit();
                        break;
                    }
                    this.Controls.RemoveAt(i);
                    break;
                }
            }
            pers[numPer] = str[numPer + 2];
            PictureBox pic = new PictureBox();
            pic.Size = new Size(71, 96);
            pic.Image = Image.FromFile(Application.StartupPath + @"\images\" + pers[numPer] + ".bmp");
            pic.Location = new Point(300 + 20 * numPer, 350);
            this.Controls.Add(pic);
            pic.BringToFront();
            numPer++;
            sumPer = Sum(pers, numPer);
            if (sumPer > 21)
            {
                //判断完超出21点后将叫牌、开牌操作禁止点击,下注操作可再次点击
                this.bottomPour.Enabled = true;
                this.openCard.Enabled = false;
                this.callCard.Enabled = false;
                MessageBox.Show("超出21点,电脑获胜,你输了!!");
                savings.Text = (int.Parse(savings.Text.Trim()) - int.Parse(inputMoney.Text.Trim())).ToString();
            }
        }


        /// <summary>
        /// 开牌操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void openCard_Click(object sender, EventArgs e)
        {
            this.openCard.Enabled = false;
            this.callCard.Enabled = false;
            sumPer = Sum(pers, numPer);
            if (sumPer > 21)
                MessageBox.Show("超出21点,电脑获胜,你输了!!");
            else
            {
                //将电脑中背面图替换成对应的图片
                for (int i = 0; i < this.Controls.Count; i++)
                {
                    if ((this.Controls[i] as PictureBox).Location.Y == 50)
                    {
                        (this.Controls[i] as PictureBox).Image = Image.FromFile(Application.StartupPath + @"\images\" + computer[1] + ".bmp");
                        break;
                    }
                }
                int count = numPer + 2;
                sumComputer = Sum(computer, numComputer);
                while (sumComputer < sumPer)
                {
                    computer[numComputer] = str[count];
                    PictureBox pic = new PictureBox();
                    pic.Size = new Size(71, 96);
                    pic.Image = Image.FromFile(Application.StartupPath + @"\images\" + computer[numComputer] + ".bmp");
                    pic.Location = new Point(300 + 20 * numComputer, 50);
                    this.Controls.Add(pic);
                    pic.BringToFront();
                    for (int i = 0; i < this.Controls.Count; i++)
                    {
                        if ((this.Controls[i] as PictureBox).Location.Y == 190)
                        {
                            this.Controls.RemoveAt(i);
                            break;
                        }
                    }
                    countComputer++;
                    numComputer++;
                    sumComputer = Sum(computer, numComputer);
                    count++;
                }
                if (sumComputer > 21)
                {
                    MessageBox.Show("电脑超出21点,你:" + sumPer + ",你赢了!!");
                    savings.Text = (int.Parse(savings.Text.Trim()) + int.Parse(inputMoney.Text.Trim())).ToString();
                }
                else
                {
                    MessageBox.Show("你:" + sumPer + "电脑:" + sumComputer + ",你输了!!");
                    savings.Text = (int.Parse(savings.Text.Trim()) - int.Parse(inputMoney.Text.Trim())).ToString();
                }
                //判断完输赢后将叫牌、开牌操作禁止点击,下注操作可再次点击
                this.bottomPour.Enabled = true;
                this.openCard.Enabled = false;
                this.callCard.Enabled = false;
            }
        }


        /// <summary>
        /// 判断玩家或者电脑手中牌的加法
        /// </summary>
        /// <param name="sum"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        private int Sum(int[] sum,int count)
        {
            int s = 0, n = 0;
            for (int i = 0; i < count; i++)
            {
                if (sum[i] == 0)
                    s += sum[i];
                else if ((sum[i] % 13 >= 10 && sum[i] % 13 <= 12) || (sum[i] % 13 == 0 && sum[i] != 0))
                    s += 10;
                else
                {
                    if (sum[i] % 13 == 1)
                    {
                        if (s + 11 > 21)
                            s += 1;
                        else
                        {
                            s += 11;
                            n++;
                        }
                    }
                    else
                        s += sum[i] % 13;
                }
            }
            if (s > 21 && n > 0)
            {
                if (n == 1)
                    s -= 10 * n;
                else
                    s -= 10 * (n - 1);
            }
            return s;
        }      
    }
}

猜你喜欢

转载自blog.csdn.net/fffssso/article/details/80529014