2014年3月24日 星期一

[Code Review] Team 06 - Hw04



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

namespace quiz04
{
    class CardDeck
    {
        public PokerCard[] Cards = new PokerCard[52];
        Random Rand = new Random();
        public void CreateDeck()   //產生出52個PokerCard的物件
        {
            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 Temp;
            for (int i = 0; i < 52; i++)
            {
                int TempRand = Rand.Next(1, 52);
                Temp = Cards[i];
                Cards[i] = Cards[TempRand];
                Cards[TempRand] = Temp;
            }
        }

        public void ArrangeBySuit()
        {
            for (int i = 0; i < 4; i++)     //4副牌
            {
                //ex: i=0; 表示從第1張牌排到第13張,也就是第一副牌
                SortBySuit((i * 13), (i + 1) * 13);
            }
        }

        public void ArrangeByRank()
        {
            for (int i = 0; i < 4; i++)     //4副牌
            {
                //ex: i=0; 表示從第1張牌排到第13張,也就是第一副牌
                SortByRank((i * 13), (i + 1) * 13);
            }
        }

        private void SortBySuit(int FirstTerm, int LastTerm)   //BubbleSort依花色整理成堆
        {
            for (int i = FirstTerm; i < LastTerm; i++)
            {
                //           起始點         起始點 + 差距 - i
                for (int j = FirstTerm; j < FirstTerm + (LastTerm - 1) - i; j++)
                {
                    if (Cards[j].Suit > Cards[j + 1].Suit)
                        Swap(ref Cards[j], ref Cards[j + 1]);
                }
            }
        }

        private void SortByRank(int FirstTerm, int LastTerm)   //依點數整理成堆
        {
            for (int i = FirstTerm; i < LastTerm; i++)
            {
                for (int j = FirstTerm; j < FirstTerm + (LastTerm - 1) - i; j++)
                {
                    if (Cards[j].Rank > Cards[j + 1].Rank)
                        Swap(ref Cards[j], ref Cards[j + 1]);
                }
            }
        }

        private void Swap(ref PokerCard Input_1, ref PokerCard Input_2)
        {
            PokerCard Temp;
            Temp = Input_1;
            Input_1 = Input_2;
            Input_2 = Temp;
        }

        /*
        public void Shuffle()      //洗牌
        {
            for (int i = 0; i < 52; i++)
            {
                Swap(Cards[i], Cards[Rand.Next(1,52)]);
            }
        }*/

        /*
        private void Swap(PokerCard ObjectA, PokerCard ObjectB)
        {
            int Temp;
            Temp = ObjectA.Suit;
            ObjectA.Suit = ObjectB.Suit;
            ObjectB.Suit = Temp;

            Temp = ObjectA.Rank;
            ObjectA.Rank = ObjectB.Rank;
            ObjectB.Rank = Temp;
        }*/

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

namespace quiz04
{
    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 Rank.ToString();
            }
        }
        public char SuitToAscii()
        {
            return (char)Suit;
        }

        public string SuitToUnicode()
        {
            switch (Suit)
            {
                /*
                case 3:
                    return "\u2665";    //heart 2665
                case 4:
                    return "\u2666";    //diamond 2666
                case 5:
                    return "\u2663";    //club 2663
                case 6:
                    return "\u2660";    //spade 2660
                */
                case 3:
                    return "\u2660";    //heart 2665
                case 4:
                    return "\u2665";    //diamond 2666
                case 5:
                    return "\u2666";    //club 2663
                case 6:
                    return "\u2663";    //spade 2660
                default:
                    return "fail";
            }
        }
    }
}
Form1.cs
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;
using quiz04;

namespace st_hw04
{
    public partial class Form1 : Form
    {

        CardDeck Poker = null;

        public Form1()
        {
            InitializeComponent();
            InitializeDeck();
        }

        private void btn_ComboBox_Click(object sender, EventArgs e)
        {
            switch (cbox_Select.SelectedIndex)
            {
                
                case 0:     //Reset
                    txtbox_Result.AppendText("ComboBox: 順序還原 已選擇\n");
                    Poker.ResetDeck();
                    ShowDeck();
                    break;
                case 1:     //Shuffle
                    txtbox_Result.AppendText("ComboBox: 亂數洗牌 已選擇\n");
                    Poker.Shuffle();
                    ShowDeck();
                    break;
                case 2:     //ArrangeBySuit
                    txtbox_Result.AppendText("ComboBox: 按花色排列 已選擇\n");
                    //Poker.ResetDeck();
                    Poker.ArrangeBySuit();
                    ShowDeck();
                    break;
                case 3:     //ArrangeByRank
                    txtbox_Result.AppendText("ComboBox: 按點數大小排列 已選擇\n");
                    break;
                default:
                    break;
            }
        }        

        private void btn_CheckBox_Click(object sender, EventArgs e)
        {
            if (chkbox_Reset.Checked)
            {
                txtbox_Result.AppendText("CheckBox: 順序還原 已選擇\n");
                Poker.ResetDeck();
                ShowDeck();
            }

            if (chkbox_Shuffle.Checked)
            {
                txtbox_Result.AppendText("CheckBox: 亂數洗牌 已選擇\n");
                Poker.Shuffle();
                ShowDeck();
            }

            if (chkbox_ArrangeBySuit.Checked)
            {
                txtbox_Result.AppendText("CheckBox: 按花色排列 已選擇\n");
                //Poker.ResetDeck();
                Poker.ArrangeBySuit();
                ShowDeck();
            }
            
            if (chkbox_ArrangeByRank.Checked)
            {
                txtbox_Result.AppendText("CheckBox: 按點數大小排列 已選擇\n");
                Poker.ArrangeByRank();
                ShowDeck();
            }
        }

        private void InitializeDeck()
        {
            Poker = new CardDeck();
            Poker.CreateDeck();
            Poker.ResetDeck();
        }

        private void ShowDeck()
        {
            string Str_Deck = null;
            for (int i = 0; i < 52; i++)
            {
                Str_Deck += Poker.Cards[i].RankToStr() + Poker.Cards[i].SuitToUnicode();
                Str_Deck += "\t";
                if ((i + 1) % 13 == 0)
                    Str_Deck += '\n';
            }
            MessageBox.Show(Str_Deck);
        }

    }
}

沒有留言:

張貼留言