2015年4月6日 星期一

[2015][Homework]Team06 - Hw06

程式分為四個部分
Program 為程式進入點
PokerForm 為視窗主程式
CardDeck 為類別 建立一整副牌.進行重置.洗牌.依點數和花色大小排列
PokerCard 為類別 設定每張牌的點數.花色 以及轉換輸出

Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Poker
{
    static class Program
    {
        /// 
        /// 應用程式的主要進入點。
        /// 
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Poker());
        }
    }
}

PokerForm
using System;
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 Poker
{
    public partial class Poker : Form
    {
        CardDeck newCards = new CardDeck();

        public Poker()
        {
            InitializeComponent();
            newCards.CreateDeck();
        }

        private void btnComBox_Click(object sender, EventArgs e)  //ComboBox
        {
            switch(cboxSelect.SelectedIndex)
            {
                case 0:
                    txtDisplay.AppendText("ComboBox 依花色排列 (原始設定)\n");
                    newCards.ResetDeck();
                    ShowDeckMessage();
                    break;
                case 1:
                    txtDisplay.AppendText("ComboBox 亂數洗牌\n");
                    newCards.ShuffleDeck();
                    ShowDeckMessage();
                    break;
                case 2:
                    txtDisplay.AppendText("ComboBox 依點數大小排列\n");
                    newCards.ArrangeDeck();
                    ShowDeckMessage();
                    break;
                default:
                    txtDisplay.AppendText("請選擇一個選項!\n");
                    break;
            }
        }

        private void btnCheckBox_Click(object sender, EventArgs e)  //CheckBox
        {
            if (chkBoxReset.Checked)
            {
                txtDisplay.AppendText("CheckBox 依花色排列 (原始設定)\n");
                newCards.ResetDeck();
                ShowDeckMessage();
            }
            if (chkBoxShuffle.Checked)
            {
                txtDisplay.AppendText("CheckBox 亂數洗牌\n");
                newCards.ShuffleDeck();
                ShowDeckMessage();
            }
            if (chkBoxArrange.Checked)
            {
                txtDisplay.AppendText("CheckBox 依點數大小排列\n");
                newCards.ArrangeDeck();
                ShowDeckMessage();
            }
        }

        public void ShowDeckMessage()  //訊息顯示(整副牌)
        {
            string message = "";
            for (int i = 0; i < 52; i++)
            {
                message += (newCards.cards[i].PrintSuit() + newCards.cards[i].PrintRank() + " ");
                if ((i + 1) % 13 == 0)
                {
                    message += "\n";
                }
            }
            MessageBox.Show(message);
        }
    }
}

CardDeck
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Poker
{
    class CardDeck
    {
        public PokerCard[] cards = new PokerCard[52];  //建立一副牌
        Random rand = new Random();                    //建立亂數

        public void CreateDeck()                       //產生52張牌的屬性
        {
            for (int i = 0; i < 52; i++)
            {
                cards[i] = new PokerCard();
            }
        }

        public void ResetDeck()                        //重置52張牌
        {
            for (int i = 0; i < 52; i++)
            {
                cards[i].rank = (i % 13) + 1;
                cards[i].suit = (i / 13) ;
            }
        }

        public void ShuffleDeck()                      //亂數洗牌
        {
            PokerCard tempCard = new PokerCard();
            for (int i = 0; i < 52; i++)               //隨機挑一張牌讓它跟第i張牌交換
            {
                tempCard.rank = cards[i].rank;
                cards[i].rank = cards[rand.Next(52)].rank;
                cards[rand.Next(52)].rank = tempCard.rank;
                tempCard.suit = cards[i].suit;
                cards[i].suit = cards[rand.Next(52)].suit;
                cards[rand.Next(52)].suit = tempCard.suit;
            }
        }

        public void ArrangeDeck()                      //依照點數.花色回復52張牌
        {
            for (int i = 0; i < 52; i++)
            {
                cards[i].rank = (i / 4) + 1;
                cards[i].suit = (i % 4);
            }
        }

    }
}


PokerCard
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Poker
{
    class PokerCard
    {
        public int rank;          //設定點數
        public int suit;          //設定花色

        public string PrintRank() //數字輸出
        {                         //將1.11.12.13轉化為A.J.Q.K輸出
            switch(rank)
            {
                case 1:
                    return "A";
                case 11:
                    return "J";
                case 12:
                    return "Q";
                case 13:
                    return "K";
                default:
                    return rank.ToString();
            }
        }

        public string PrintSuit() //花色輸出
        {                         //將梅花.方塊.愛心.黑桃以unicode輸出符號
            string suitSign = null;
            switch (suit)
            {
                case 0:                  //梅花
                    suitSign = "\u2663";
                    break;
                case 1:                  //方塊
                    suitSign = "\u2666";
                    break;
                case 2:                  //愛心
                    suitSign = "\u2665";
                    break;
                case 3:                  //黑桃
                    suitSign = "\u2660";
                    break;
                default:
                    break;
            }
            return suitSign;
        }
    }
}

沒有留言:

張貼留言