顯示具有 [2014][Code Review]Team 09 標籤的文章。 顯示所有文章
顯示具有 [2014][Code Review]Team 09 標籤的文章。 顯示所有文章

2014年5月4日 星期日

[Code Review] Team 09 - Hw08

整個專案檔連結 https://drive.google.com/file/d/0B9vj2TjBHPutam1SeGxCZ0pvUE0/edit?usp=sharing 換行

2014年4月13日 星期日

[Code Review] Team 09 - Hw06

在此超有誠意的附上整個專案檔
https://drive.google.com/file/d/0B9vj2TjBHPutWVBablNsVU5NNTJ0MTdmd0lrQjRGQ0FlbU00/edit?usp=sharing


2014年4月7日 星期一

[Code Review] Team 09 - Hw05

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

namespace HW5
{
    class CardDeck
    {
        public PokerCard[] Deck = new PokerCard[52];

        public void CreateDeck()
        {
            for (int i = 0; i < 52; i++)
            {
                Deck[i] = new PokerCard(i / 13, i % 13 + 1);
            }
        }

        public void ResetDeck()
        {
            for (int i = 0; i < 52; i++)
            {
                Deck[i].Suit = i / 13;
                Deck[i].Rank = i % 13 + 1;
            }
        }

        public void Shuffle()
        {
            //亂數產生器
            Random rand = new Random();
            PokerCard temp;
            int RandNum;
           
            for (int i = 0; i < 52; i++)
            {
                RandNum = rand.Next(52);
                temp = Deck[i];
                Deck[i] = Deck[RandNum];
                Deck[RandNum] = temp;
            }
        }


    }
}
===================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HW5
{
    class HandCard
    {
        public PokerCard[] Hand = new PokerCard[5];
        private static string[] Type = { "Zilch", "One Pair", "Two Pairs", "Three of a kind", "Straight", "Flush", "Fullhouse", "Four of a Kind", "Straight Flush" };
        int TopIndex = 0;
        private bool isSort = true;

        public void Draw(CardDeck D)
        {
            for (int i = 0; i < 5; i++)
            {
                if (TopIndex >= 50)
                {
                    D.ResetDeck();
                    D.Shuffle();
                    TopIndex = 0;
                }
                Hand[i] = D.Deck[TopIndex];
                TopIndex++;
            }
        }
       
        public void Draw(CardDeck D,int Limit)
        {
            int Lnum = 0;

            while (Lnum<Limit)
            {
                for (int i = 0; i < 5; i++)
                {
                    if (TopIndex >= 50)
                    {
                        D.ResetDeck();
                        D.Shuffle();
                        TopIndex = 0;
                    }
                    Hand[i] = D.Deck[TopIndex];
                    TopIndex++;
                }

                Lnum++;
            }
            TopIndex += 5;
        }
     
        public void Sort()
        {
            PokerCard temp;
           
            for (int i = 0; i < 5; i++)
            {
                for (int j = 1; j < 5; j++)
                {
                    if (Hand[j - 1].Rank > Hand[j].Rank)
                    {
                        temp = Hand[j - 1];
                        Hand[j - 1] = Hand[j];
                        Hand[j] = temp;
                    }
                }
            }
        }

        public int CardsTypeNum()
        {
            if (!isSort)
                Sort();

            int Num = 0;
           
            //判斷對子數目
            int TypeNum = 0;

            for (int i = 0; i < 5; i++)
            {
                for (int j = i + 1; j < 5; j++)
                {
                    if (Hand[i].Rank == Hand[j].Rank)
                        TypeNum++;
                }              
            }

            switch (TypeNum)
            {
                case 0:
                    Num = 0;
                    break;
                case 1:
                    Num = 1;
                    break;
                case 2:
                    Num = 2;
                    break;
                case 3:
                    Num = 3;
                    break;
                case 4:
                    Num = 6;
                    break;
                case 6:
                    Num = 7;
                    break;
            }

            //判斷同花
            int FlushNum = 0;
           
            for (int i = 1; i < 5; i++)
            {
                if (Hand[0].Suit == Hand[i].Suit)
                    FlushNum++;
            }

            if (FlushNum == 4)
                Num = 5;

            //判斷順子
            int StraightNum = 0;

            for (int i = 1; i < 5; i++)
            {
                if (Hand[i - 1].Rank + 1 == Hand[i].Rank)
                {
                    StraightNum++;

                    if (Hand[i - 1].Rank == 13 && Hand[i].Rank == 1)
                        StraightNum++;              
                }
            }

            if (StraightNum == 4)
                Num = 4;

            //判斷同花順
            if (FlushNum == 4 && StraightNum == 4)
                Num = 8;

            return Num;
        }

        public string TypeNumToStr()
        {
            return Type[CardsTypeNum()];
        }

        public string TypeProbability(CardDeck D,int Order)
        {
            double ZilchNum = 0;
            double OnePairNum = 0;
            double TwoPairsNum = 0;
            double ThreeOfaKindNum = 0;
            double StraightNum = 0;
            double FlushNum = 0;
            double FullHouseNum = 0;
            double FourOfaKindNum = 0;
            double StraightFlushNum = 0;
            int N = 50000000;
           
            for (int i = 0; i <= N; i++)
            {
                Draw(D);
                Sort();
                CardsTypeNum();

                switch (CardsTypeNum())
                {
                    case 0:
                        ZilchNum++;
                        break;
                    case 1:
                        OnePairNum++;
                        break;
                    case 2:
                        TwoPairsNum++;
                        break;
                    case 3:
                        ThreeOfaKindNum++;
                        break;
                    case 4:
                        StraightNum++;
                        break;
                    case 5:
                        FlushNum++;
                        break;
                    case 6:
                        FullHouseNum++;
                        break;
                    case 7:
                        FourOfaKindNum++;
                        break;
                    case 8:
                        StraightFlushNum++;
                        break;
                    default:
                        break;
                }
            }

            switch (Order)
            {
                case 0:
                    return (ZilchNum / N).ToString();
                case 1:
                    return (OnePairNum / N).ToString();
                case 2:
                    return (TwoPairsNum / N).ToString();
                case 3:
                    return (ThreeOfaKindNum / N).ToString();
                case 4:
                    return (StraightNum / N).ToString();
                case 5:
                    return (FlushNum / N).ToString();
                case 6:
                    return (FullHouseNum / N).ToString();
                case 7:
                    return (FourOfaKindNum / N).ToString();
                case 8:
                    return (StraightFlushNum / N).ToString();
                default:
                    return "Error!!";
            }
        }
    }
}

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

namespace HW5
{
    class PokerCard
    {
        //Fields
        private int suit;
        private int rank;
        private static string[] StrUni = { "\u2665", "\u2666", "\u2663", "\u2660" };

        //Constructors
        public PokerCard(int suit, int rank)
        {
            this.suit = suit;
            this.rank = rank;
        }

        //Attributes
        public int Suit
        {
            get { return suit; }
            set
            {
                if (value >= 0 && value < 4)
                    suit = value;
            }
           
        }

        public int Rank
        {
            get { return rank; }
            set
            {
                if (value > 0 && value <= 13)
                    rank = value;
            }
        }

        /// <summary>
        /// method
        /// </summary>
        /// <returns></returns>
        public string SuitToUni()
        {
            return StrUni[Suit];
        }

        public string RankToStr()
        {
            string rankStr="";

            switch (Rank)
            {
                case 1:
                    rankStr = "A";
                    return rankStr;
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                case 7:
                case 8:
                case 9:
                case 10:
                    rankStr = rank.ToString();
                    return rankStr;
                case 11:
                    rankStr = "J";
                    return rankStr;
                case 12:
                    rankStr = "Q";
                    return rankStr;
                case 13:
                    rankStr = "K";
                    return rankStr;
                default:
                    return "Error!!";
            }          
        }
    }
}

===================================================

2014年3月24日 星期一

[Code Review] Team 09 - Hw04

我先說自己覺得寫很爛的地方:
1. SuitToUnicode()沒用陣列,因為這次都在看新聞,寫得很倉促
2. Sort用最笨演算法(好像有內建Sort?)
3. Message輸出還是無法對齊,"\t"寬度會超過
//這是Program.cs******************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HW4___WinForm_Getting_start
{
    class PokerCard
    {
        public int Rank;  //點數
        public int Suit;  //花色

        private static string[] strRank = new string[13] { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };

        public string RankToStr()  //將點數轉為字串
        {
            return strRank[Rank];
        }

        public char SuitToAscii()  //將花色轉為 Ascii 字元碼
        {
            return (char)Suit;
        }

        public String SuitToUnicode()  //將花色轉為 Ascii 字元碼
        {
            switch(Suit)
            {
                case 3: return "\u2660"; break;
                case 4: return "\u2663"; break;
                case 5: return "\u2666"; break;
                case 6: return "\u2665"; break;
            }

            return "ERROR";
        }
    }

    class CardDeck
    {
        public PokerCard[] Cards;

        public CardDeck()
        {
            Cards = new PokerCard[52];
            CreateDeck();
            ResetDeck();
        }

        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;
                Cards[i].Suit = (i / 13) + 3;
            }
        }

        public void Shuffle() //洗牌
        {
            PokerCard TempCard;
            Random rand = new Random();

            for (int i = 0; i < 52; i++)
            {
                int A = rand.Next(52);
                TempCard = Cards[A];
                Cards[A] = Cards[i];
                Cards[i] = TempCard;
            }
        }

        public void SortByRank()
        {
            for (int i = 0; i < Cards.Length - 1; i++)
            {
                for (int j = 0; i + j < Cards.Length; j++)
                {
                    if (Cards[i].Rank > Cards[i + j].Rank)
                        Swap(ref Cards[i], ref Cards[i + j]);
                }
            }
        }

        public void SortBySuit()
        {
            for (int i = 0; i < Cards.Length - 1; i++)
            {
                for (int j = 0; i + j < Cards.Length; j++)
                {
                    if (Cards[i].Suit > Cards[i + j].Suit)
                        Swap(ref Cards[i], ref Cards[i + j]);
                }
            }
        }

        private static void Swap(ref PokerCard Card_A, ref PokerCard Card_B)
        {
            PokerCard TempCard;

            TempCard = Card_A;
            Card_A = Card_B;
            Card_B = TempCard;
        }
    }

    static class Program
    {
        /// 
        /// 應用程式的主要進入點。
        /// 
        [STAThread]

        

        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }


    }
}

//這是Form1.cs******************************
namespace HW4___WinForm_Getting_start
{
    public partial class Form1 : Form
    {
        CardDeck Carddeck;

        public Form1()
        {
            InitializeComponent();
            Carddeck = new CardDeck(); //已經自動初始化, 跟新買的牌一樣
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (0 == comboBox1.SelectedIndex/*按花色排列*/)
                txt_Message.AppendText("comboBox 按花色排列 已被選定"+"\n");
            if (1 == comboBox1.SelectedIndex/*按大小排列*/)
                txt_Message.AppendText("comboBox 按大小排列 已被選定" + "\n");
            if (2 == comboBox1.SelectedIndex/*亂數洗牌*/  )
                txt_Message.AppendText("comboBox 亂數洗牌 已被選定" + "\n");
        }

        private void btn_ComboBox_Test_Click(object sender, EventArgs e)
        {
            String messageString = null;

            if (0 == comboBox1.SelectedIndex/*按花色排列*/)Carddeck.SortBySuit();
            if (1 == comboBox1.SelectedIndex/*按大小排列*/)Carddeck.SortByRank();
            if (2 == comboBox1.SelectedIndex/*亂數洗牌*/  )Carddeck.Shuffle();

            for (int i = 0; i < Carddeck.Cards.Length; i++)
            {//產生顯示用字串
                messageString +=
                    (Carddeck.Cards[i].Rank == (10 | 12) ? " " : " ") +
                    Carddeck.Cards[i].RankToStr() +
                    Carddeck.Cards[i].SuitToUnicode() +
                    (0 == (i + 1) % 13 ? "\n" : "");
            }

            MessageBox.Show(messageString);
        }

        

        

        private void chk_SortBy_Suit_CheckedChanged(object sender, EventArgs e)
        {//按花色排列
            Chk_CheckedChanged();
        }

        private void chk_SortBy_Rank_CheckedChanged(object sender, EventArgs e)
        {//按點數排列
            Chk_CheckedChanged();
        }

        private void chk_SortBy_Rand_CheckedChanged(object sender, EventArgs e)
        {
            Chk_CheckedChanged();
        }

        private void Chk_CheckedChanged()
        {
            if (true == chk_SortBy_Suit.Checked/*按花色排列*/)
                txt_Message.AppendText("comboBox 按花色排列 已被選定" + "\n");
            if (true == chk_SortBy_Rank.Checked/*按大小排列*/)
                txt_Message.AppendText("comboBox 按大小排列 已被選定" + "\n");
            if (true == chk_SortBy_Rand.Checked/*亂數洗牌*/  )
                txt_Message.AppendText("comboBox 亂數洗牌 已被選定" + "\n");
        }

        private void btn_CheckBox_Test_Click(object sender, EventArgs e)
        {
            if (0 == comboBox1.SelectedIndex/*按花色排列*/) Carddeck.SortBySuit();
            if (1 == comboBox1.SelectedIndex/*按大小排列*/) Carddeck.SortByRank();
            if (2 == comboBox1.SelectedIndex/*亂數洗牌*/  ) Carddeck.Shuffle();

            String messageString = null;

            for (int i = 0; i < Carddeck.Cards.Length; i++)
            {//產生顯示用字串
                messageString +=
                    (Carddeck.Cards[i].Rank == (10|12) ? " " : " ") +
                    Carddeck.Cards[i].RankToStr() +
                    Carddeck.Cards[i].SuitToUnicode() +
                    ( 0 == (i + 1) % 13 ? "\n" : "");
            }

            MessageBox.Show(messageString);
        }

    }
}