2015年4月9日 星期四

[2015][Homework]Team02 - Hw06

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 WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        CardDeck Poker = new CardDeck();
        public Form1()
        {
            InitializeComponent();
        }

        private void message_show()
        {
            string display = "";
            for (int i = 0; i < 52; i++)
            {
                display += Poker.Cards[i].SuitToUniCode() + Poker.Cards[i].RankToStr() + "  ";
                if (((i + 1) % 13) == 0)
                {
                    display += "\n";
                }
            }
            MessageBox.Show(display);
        }

        private void btnCboxTest_Click(object sender, EventArgs e)
        {
            Poker.CreateDeck();
            Poker.ResetDeck();

            switch(cbox_Select.SelectedIndex)
            {
                case 0:
                    txt_Display.AppendText("選擇了按花色排列" + "\n");
                    Poker.Reset();
                    message_show();
                    break;
                case 1:
                    txt_Display.AppendText("選擇了亂數洗牌" + "\n");
                    Poker.Shuffle();
                    message_show();
                    break;
                case 2:
                    txt_Display.AppendText("選擇了按點數大小排列" + "\n");
                    Poker.ResetDeck();
                    message_show();
                    break;
                default:
                    MessageBox.Show("請選擇!");
                    break;
            }
        }

        private void btn_ChkboxTest_Click(object sender, EventArgs e)
        {
            if (chkbox_Select1.Checked)
            {
                txt_Display.AppendText("選擇了按花色排列" + "\n");
                Poker.ResetDeck();
                message_show();
            }
            if(chkbox_Select2.Checked)
            {
                txt_Display.AppendText("選擇了亂數洗牌" + "\n");
                Poker.Shuffle();
                message_show();
            }
            if(chkbox_Select3.Checked)
            {
                txt_Display.AppendText("選擇了按點數大小排列" + "\n");
                Poker.Reset();
                message_show();
            }

        }

    }
}

class CardDeck


namespace WindowsFormsApplication2
{
    class CardDeck
    {
        //產生52張牌
        public PokerCard[] Cards = new PokerCard[52];
        public void CreateDeck()
        {
            for (int i = 0; i < 52; i++)
                Cards[i] = new PokerCard();
        }
        
        //將撲克排回復初始照點數大小排列
        public void ResetDeck()
        {
            for (int i = 0; i <52; i++)
            {
                Cards[i].Rank = i % 13 + 1;
                Cards[i].Suit = i / 13 + 3;
            }
        }

        //洗牌以作亂數排列
        public void Shuffle()
        {
            PokerCard Change = new PokerCard();
            for (int i = 0; i <52; i++)
            {
                Random rand = new Random();
                Change = Cards[i];
                int another = rand.Next(52);
                Cards[i] = Cards[another];
                Cards[another] = Change;
            }
        }

        //將撲克牌依照花色排列
        public void Reset()
        {
            for (int i = 0; i <52; i++)
            {
                Cards[i].Rank = (i / 4) + 1;
                Cards[i].Suit = (i % 4) + 3;
            }
        }
    }
}

Class PokerCard
namespace WindowsFormsApplication2
{
    class PokerCard
    {
        public int Suit;
        public int Rank;
        public string RankToStr()
        {
            switch (Rank)
            {
                case 1:
                    return "A";
                case 11:
                    return "J";
                case 12:
                    return "Q";
                case 13:
                    return "K";
                default:
                    return Convert.ToString(Rank);
            }
        }
        public string SuitToUniCode()
        {
            string Str_Suit;
            switch (Suit)
            {
                case 3:
                    Str_Suit = "\u2665";
                    break;
                case 4:
                    Str_Suit = "\u2666";
                    break;
                case 5:
                    Str_Suit = "\u2663";
                    break;
                default:
                    Str_Suit = "\u2660";
                    break;
            }
            return Str_Suit;
        }
    }
}

沒有留言:

張貼留言