撲克牌猜大小遊戲-C#

 

    這是一個很簡單的撲克牌遊戲,適合有一定程度的初學者練習,猜撲克牌的大小或是牌色,而且可以下注,猜對的話贏得籌碼,猜錯輸掉籌碼。

 

遊戲畫面如下:

遊戲一開始畫面,按下發牌。


按下發牌以後輸入你要押注的金額,然後選擇你要押哪一個,大牌、小牌、黑色牌、紅色牌,點選按鈕看結果。


押對的話籌碼就會增加。


押錯的話籌碼就會減少。


如果不幸的籌碼變成0,那你就破產了,要按重新開始,遊戲就會開新的一局,就可以繼續遊戲。


結束遊戲的按鈕按下後會問你要不要結束遊戲,按是就會關閉這個程式,按否則遊戲繼續。

 

 

撲克牌圖案建置的程式如下,建一個名稱為pic的資料夾,把撲克牌的圖案丟到這個資料夾內,下面這個程式是去讀取52張牌的圖片路徑,後面要秀出撲克牌時就會讀取這個路徑。

   private void Form1_Load(object sender, EventArgs e)

 {      

            //撲克牌圖案建置

            string picPath = GetDirectory("pic");     

            for (int i = 0; i <= 52; i++)

                d[i] = picPath + @"\g" + i.ToString() + ".jpg";

 }

//取得撲克牌檔案路徑

private string GetDirectory(string name)

{

       System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(Application.StartupPath);

            string path = (dir.Parent).Parent.FullName;

            return path + string.Format(@"\{0}\", name);

}

 

 

在按下發牌按鈕所做的動作如下:

//按下發牌

private void btnDeal_Click(object sender, EventArgs e)

{

                      int i, rnum, temp;

            EnableButton(true);

            pictureBox1.Image = Image.FromFile(@d[52]);//蓋牌

           

            //隨機出一張牌

                Random ran = new Random();

                for (i = 0; i < 52; i++)

                {

                    a[i] = i;

                }

                for (i = 0; i < 52; i++)

                {

                    rnum = ran.Next(51);

                    temp = a[i];

                    a[i] = a[rnum];

                    a[rnum] = temp;

                }                              

           

 

            if(gameover)

            {

                lbtoptext.Text = topText;

                btnDeal.Text = "發牌";

                tbmoney.Text = "10";

                lbmoney.Text = "100";

                gameover = false;

            }

}

按下發牌鈕以後會蓋牌,並且發牌,而且由於發牌按鈕和重新開始是同一個按鈕,所以在這裡程式會判斷是不是gameover的狀態,如果是的話那這裡就是做按下重新開始,遊戲產生新的一局的動作,玩家籌碼變100,押注金額10,並且這個按鈕的文字改成發牌。

 

 

以下是開牌的動作。

public void opencard(guessCard guess)

{

            bool bingo = false;

            int playerguess = (int)guess;

            int playermoney;       

 

            if (betMoney > 0 && betMoney <= playermoney)  //押注金額>0並且<=剩下籌碼

            {

                EnableButton(false); //隱藏按鈕

 

                pictureBox1.Image = Image.FromFile(@d[a[0]]);

                int cardRate = -1;

                red = true;

                infolabel.Text = "";

 

                flower = (byte)a[0] % 4;       //花色

                point = a[0] / 4;              //點數

 

                //四種花色依序是-ClubDiamondHeartSpade

                //判斷紅色還是黑色               

                if (flower == 3 || flower == 0) //黑色牌

                {

                    red = false;

                    infolabel.Text = "黑色,";

                    if (playerguess == 1)

                        bingo = true;

                }

                else

                {

                    infolabel.Text = "紅色,";

                    if (playerguess == 0)

                        bingo = true;

                }

 

                //判斷大小牌

                if (point == 7)

                {

                    bigsmall = 0;

                    infolabel.Text += "大小通吃";

                    if (playerguess > 1)

                        bingo = true;

                }

                else if (point < 7 && point != 0)

                {

                    bigsmall = 1;

                    infolabel.Text += "";

                    if (playerguess == 3)

                        bingo = true;

                }

                else

                {

                    bigsmall = 2;

                    infolabel.Text += "";

                    if (playerguess == 2)

                        bingo = true;

                }

 

                lbwinlose.Text = bingo ? "你贏了" : "你輸了";

                if (bingo)

                {

                    cardRate = 1;

                }

                playermoney += betMoney * cardRate;

                lbmoney.Text = playermoney.ToString();

 

                //籌碼輸光

                if (playermoney <= 0)

                {

                    btnDeal.Text = "重新開始";

                    lbtoptext.Text = "你破產了,請按重新開始";

                    gameover = true;

                }

            }

 

            else

            {

                MessageBox.Show("請輸入大於0並且小於等於剩餘籌碼的金額。");

            }

}

開牌是決定要押注哪一個以後會做的動作,這時候就會判斷這個牌是大牌還是小牌,是紅色牌還是黑色牌,玩家是否猜對,猜對的話玩家就贏得籌碼,猜錯就輸掉籌碼,玩家籌碼如果是0就變成遊戲要重新開始。

這裡說明一下撲克牌的順序是同樣的數字,照著梅花、方塊、愛心、黑桃這個順序下來的,如果不是這個順序的話,當然在花色和撲克牌點數的判斷程式也就不一樣,如果不習慣這樣牌也可以同樣花色然後照1~13的順序排,但是本範例程式的圖片名稱的數字是照著同樣點數然後梅花、方塊、愛心、黑桃這個順序下來的,如果用同樣花色然後照1~13的順序排那要記得檔名都要改。

 

如果照著我給的原始順序,那花色和點數的判斷程式就是下面這樣:

flower = (byte)a[0] % 4;       //花色

point = a[0] / 4;              //點數

 

開牌前要先猜牌是屬於哪一種,是大牌、小牌、紅色牌、黑色牌,這四種選一種,然後就按下按鈕,就會開牌,按下這四種按鈕的程式如下:

private void btnbig_Click(object sender, EventArgs e)

        {   

            opencard(guessCard.big);

        }

 

        private void btnsmall_Click(object sender, EventArgs e)

        {          

            opencard(guessCard.samll);

        }

 

        private void btnblack_Click(object sender, EventArgs e)

        {

            opencard(guessCard.black);

        }      

 

        private void btnred_Click(object sender, EventArgs e)

        {        

            opencard(guessCard.red);

        }

這四個按鈕按下其中一個以後就會去做開牌的動作了。

 

 

上面的程式有一些部分較不重要所以沒有講解,並不完整,完整程式碼如下

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 Guest_Big_Small

{

    public partial class Form1 : Form

    {

        string[] d = new string[54];  //紙牌路徑

        static int[] a = new int[52];        //紙牌編碼

 

        int point, flower, bigsmall;

        //判斷大還還是小牌

        //bigsmall: 0-大小通吃, 1:, 2:

 

        //猜顏色和大小

        public enum guessCard { red,black,big,samll };

 

        bool red;      //是否為紅色牌

        bool gameover; //判斷是否還有籌碼,沒籌碼則遊戲結束     

 

        string topText = "A為大牌,2~7小牌,9以上大牌,8大小通吃";

 

        public Form1()

        {

            InitializeComponent();

        }

       

        private void Form1_Load(object sender, EventArgs e)

        {

            gameover = false;

            EnableButton(false);

            infolabel.Text = string.Empty;

 

            //撲克牌圖案建置

            string picPath = GetDirectory("pic");     

            for (int i = 0; i <= 52; i++)

                d[i] = picPath + @"\g" + i.ToString() + ".jpg";

        }

 

        //取得撲克牌檔案路徑

        private string GetDirectory(string name)

        {

            System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(Application.StartupPath);

            string path = (dir.Parent).Parent.FullName;

            return path + string.Format(@"\{0}\", name);

        }

 

        //按下發牌

        private void btnDeal_Click(object sender, EventArgs e)

        {

            int i, rnum, temp;

 

            EnableButton(true);

            pictureBox1.Image = Image.FromFile(@d[52]);//蓋牌

           

            //隨機出一張牌

                Random ran = new Random();

                for (i = 0; i < 52; i++)

                {

                    a[i] = i;

                }

                for (i = 0; i < 52; i++)

                {

                    rnum = ran.Next(51);

                    temp = a[i];

                    a[i] = a[rnum];

                    a[rnum] = temp;

                }                              

           

 

            if(gameover)

            {

                lbtoptext.Text = topText;

                btnDeal.Text = "發牌";

                tbmoney.Text = "10";

                lbmoney.Text = "100";

                gameover = false;

            }

        }

 

        enum state { red=1, black=2, big=4, small=8, both=16 }

 

        //下注完成開牌

        public void opencard(guessCard guess)

        {

            bool bingo = false;

            int playerguess = (int)guess;

            int playermoney;

            if (!Int32.TryParse(lbmoney.Text, out playermoney))

            {

                playermoney = 0;

            }

            int betMoney;

            if (!Int32.TryParse(tbmoney.Text, out betMoney))

            {

                betMoney = 0;

                MessageBox.Show("下注金額請輸入整數數字");

                return;

            }

 

            if (betMoney > 0 && betMoney <= playermoney)  //押注金額>0並且<=剩下籌碼

            {

                EnableButton(false); //隱藏按鈕

 

                pictureBox1.Image = Image.FromFile(@d[a[0]]);

                int cardRate = -1;

                red = true;

                infolabel.Text = "";

 

                flower = (byte)a[0] % 4;       //花色

                point = a[0] / 4;              //點數

 

                //四種花色依序是-ClubDiamondHeartSpade

                //判斷紅色還是黑色               

                if (flower == 3 || flower == 0) //黑色牌

                {

                    red = false;

                    infolabel.Text = "黑色,";

                    if (playerguess == 1)

                        bingo = true;

                }

                else

                {

                    infolabel.Text = "紅色,";

                    if (playerguess == 0)

                        bingo = true;

                }

 

                //判斷大小牌

                if (point == 7)

                {

                    bigsmall = 0;

                    infolabel.Text += "大小通吃";

                    if (playerguess > 1)

                        bingo = true;

                }

                else if (point < 7 && point != 0)

                {

                    bigsmall = 1;

                    infolabel.Text += "";

                    if (playerguess == 3)

                        bingo = true;

                }

                else

                {

                    bigsmall = 2;

                    infolabel.Text += "";

                    if (playerguess == 2)

                        bingo = true;

                }

 

                lbwinlose.Text = bingo ? "你贏了" : "你輸了";

                if (bingo)

                {

                    cardRate = 1;

                }

                playermoney += betMoney * cardRate;

                lbmoney.Text = playermoney.ToString();

 

                //籌碼輸光

                if (playermoney <= 0)

                {

                    btnDeal.Text = "重新開始";

                    lbtoptext.Text = "你破產了,請按重新開始";

                    gameover = true;

                }

            }

 

            else

            {

                MessageBox.Show("請輸入大於0並且小於等於剩餘籌碼的金額。");

            }

        }

 

        private void btnbig_Click(object sender, EventArgs e)

        {   

            opencard(guessCard.big);

        }

 

        private void btnsmall_Click(object sender, EventArgs e)

        {          

            opencard(guessCard.samll);

        }

 

        private void btnblack_Click(object sender, EventArgs e)

        {

            opencard(guessCard.black);

        }      

 

        private void btnred_Click(object sender, EventArgs e)

        {        

            opencard(guessCard.red);

        }

 

        private void EnableButton(bool isEnable)

        {

            btnbig.Enabled = isEnable;

            btnsmall.Enabled = isEnable;

            btnred.Enabled = isEnable;

            btnblack.Enabled = isEnable;

            btnDeal.Enabled = !isEnable;

        }

 

        //按下結束遊戲

        private void btnend_Click(object sender, EventArgs e)

        {

            DialogResult result;

            result = MessageBox.Show("你確定要結束遊戲嗎?","結束遊戲", MessageBoxButtons.YesNo);

            if (result == DialogResult.Yes)

            {

                Close();

            } 

        }

    }

}

 

 

檔案連結

撲克牌猜大小遊戲

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 波羅、希夏普2020 的頭像
    波羅、希夏普2020

    希夏普2020的程式天地

    波羅、希夏普2020 發表在 痞客邦 留言(0) 人氣()