2015年4月8日 星期三

[2015][Homework]Team04-Hw06

Program為進入程式點
Poker為類別,視窗設計
PokerCard為類別,轉換點數和花色
CardDeck為類別,產生Poker cards,依花色排列、亂數洗牌、依點數大小排列

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

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

Poker
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace homework6
{
    public partial class Poker : Form
    {
        CardDeck newCards = new CardDeck();

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

        private void comboBoxEnter_Click(object sender, EventArgs e)      //combobox
        {
            switch (comboBox.SelectedIndex)
            {
                case 0:
                    textBox.AppendText("ComboBox 依花色排列(原始設定)\n");      //arrange Poker cards by suit
                    newCards.ResetDeck();
                    ShowMessage();
                    break;
                case 1:
                    textBox.AppendText("ComboBox 亂數洗牌\n");      //shuffle randomly
                    newCards.Shuffle();
                    ShowMessage();
                    break;
                case 2:
                    textBox.AppendText("ComboBox 依點數大小排列\n");      //arrange Poker cards by rank
                    newCards.ArrangeDeck();
                    ShowMessage();
                    break;
                default:
                    textBox.AppendText("Please chose one!\n");
                    break;
            }
        }

        private void checkBoxEnter_Click(object sender, EventArgs e)      //checkbox
        {
            if (checkBoxReset.Checked)
            {
                textBox.AppendText("CheckBox 依花色排列(原始設定)\n");      //arrange Poker cards by suit
                newCards.ResetDeck();
                ShowMessage();
            }
            if (checkBoxShuffle.Checked)
            {
                textBox.AppendText("CheckBox 亂數洗牌\n");      //shuffle randomly
                newCards.Shuffle();
                ShowMessage();
            }
            if (checkBoxArrange.Checked)
            {
                textBox.AppendText("CheckBox 依點數大小排列\n");      //arrange Poker cards by rank
                newCards.ArrangeDeck();
                ShowMessage();
            }
        }

        public void ShowMessage()      //message box
        {
            string message = "";
            for (int i = 0; i < 52; i++)
            {
                message += (newCards.cards[i].SuitToStr() + newCards.cards[i].RankToStr() + " ");
                if ((i + 1) % 13 == 0)
                {
                    message += "\n";
                }
            }
            MessageBox.Show(message);
        }
    }
}

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

namespace homework6
{
    class PokerCard
    {
        public int rank;
        public int suit;

        public string RankToStr()      //tranfer rank to string
        {
            switch (rank)
            {
                case 1:
                    return "A";
                case 11:
                    return "J";
                case 12:
                    return "Q";
                case 13:
                    return "K";
                default:
                    return rank.ToString();
            }
        }

        public string SuitToStr()      //transfer suit to sign
        {
            string suitSign = null;
            switch (suit)
            {
                case 0:
                    suitSign = "\u2660";
                    break;
                case 1:
                    suitSign = "\u2663";
                    break;
                case 2:
                    suitSign = "\u2665";
                    break;
                case 3:
                    suitSign = "\u2666";
                    break;
            }
            return suitSign;
        }
    }
}

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

namespace homework6
{
    class CardDeck
    {
        public PokerCard[] cards = new PokerCard[52];
        Random rand = new Random();

        public void CreateCard()      //create Poker cards array
        {
            for (int i = 0; i < 52; i++)
            {
                cards[i] = new PokerCard();
            }
        }

        public void ResetDeck()      //arrange Poker cards by suit
        {
            for (int i = 0; i < 52; i++)
            {
                cards[i].rank = (i % 13) + 1;
                cards[i].suit = i / 13;
            }
        }

        public void Shuffle()      //shuffle randomly
        {
            PokerCard temp = new PokerCard();
            ResetDeck();
            for (int i = 0; i < 52; i++)
            {
                temp.rank = cards[i].rank;
                cards[i].rank = cards[rand.Next(52)].rank;
                cards[rand.Next(52)].rank = temp.rank;
                temp.suit = cards[i].suit;
                cards[i].suit = cards[rand.Next(52)].suit;
                cards[rand.Next(52)].suit = temp.suit;
            }
        }

        public void ArrangeDeck()      //arrange Poker cards by rank
        {
            for (int i = 0; i < 52; i++)
            {
                cards[i].rank = (i / 4) + 1;
                cards[i].suit = i % 4;
            }
        }
    }
}

沒有留言:

張貼留言