2014年3月24日 星期一

[Code Review] Team 08 - Hw04

PokerCard 視窗版 PokerCard.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApplication1
{
    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()
        {
            switch (Suit)
            {
                case 0:
                    return "\u2663";
                case 1:
                    return "\u2666";
                case 2:
                    return "\u2665";
                default:
                    return "\u2660";
            }
        }//將花色轉為 UniCode 字元碼
    }
}

CarDeck.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApplication1
{
    class CarDeck
    {
        public PokerCard[] Cards = new PokerCard[52];

        public void CreateDeck()
        {
            for (int i = 0; i < 52; i++)
                Cards[i] = new PokerCard();
        }//產生出52個PokerCard的物件
        public void ResetDeck()
        {
            for (int i = 0; i < 52; i++)
            {
                Cards[i].Rank = i % 13 + 1;
                Cards[i].Suit = i / 13;
            }
        }//牌堆回復原始狀態
        public void Shuffle()
        {
            Random r = new Random();
            PokerCard temp;
            int j;
            for (int i = 0; i < 52; i++)
            {
                j = r.Next(52);
                temp = Cards[i];
                Cards[i] = Cards[j];
                Cards[j] = temp;
            }
        }//洗牌
        public void ResetByNum()
        {
            for (int i = 0; i < 52; i++)
            {
                Cards[i].Rank = (i + 4) / 4; ;
                Cards[i].Suit = (i + 4) % 4;
            }

        }
    }
}

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.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
       

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CarDeck deck = new CarDeck();
            PokerCard Cards = new PokerCard();
            deck.CreateDeck();
            switch (cbox_select.SelectedIndex)
            { 
                case 0:
                   btxt_out.AppendText("ComboBox按照花色排列 已被選定\n");
                   deck.ResetDeck();
                   MessageBox.Show(PrintDeck(deck));
                   break;
                case 1:
                   btxt_out.AppendText("ComboBox洗牌 已被選定\n");
                   deck.ResetDeck();
                   deck.Shuffle();
                   MessageBox.Show(PrintDeck(deck));
                   break;
                case 2:
                   btxt_out.AppendText("CheckBox按照數字排列 已被選定\n");
                   deck.ResetDeck();
                   deck.ResetByNum();
                   MessageBox.Show(PrintDeck(deck));
                   break;


            }
        }

        private void btn_correct2_Click(object sender, EventArgs e)
        {
            CarDeck deck = new CarDeck();
            PokerCard Cards = new PokerCard();
            deck.CreateDeck();       
            if (checkBox1.Checked)
            {
                deck.ResetDeck();
                btxt_out.AppendText("CheckBox按照花色排列 已被選定\n");
                MessageBox.Show(PrintDeck(deck));
            }
            if (checkBox2.Checked)
            {
                deck.ResetDeck();
                deck.Shuffle();
                btxt_out.AppendText("CheckBox洗牌 已被選定\n");
                MessageBox.Show(PrintDeck(deck));
            }
            if (checkBox3.Checked)
            {
                deck.ResetDeck();
                deck.ResetByNum();
                btxt_out.AppendText("CheckBox按照點數排列 已被選定\n");
                MessageBox.Show(PrintDeck(deck));
            }
        }

        private void cbox_select_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        private string PrintDeck(CarDeck D)
        {
            string StrDisplay = "";
            for (int i = 0; i < 52; i++)
            {
                StrDisplay += PrintCards(D.Cards[i]) + "   ";
                if ((i + 1) % 13 == 0)
                {
                    StrDisplay += "\n";
                }
            }
            return StrDisplay;
        }
        private string PrintCards(PokerCard C)
        {
            string CardSum = C.RankToStr() + C.SuitToUniCode();
            return CardSum;
        }
    }
}

8 則留言:

  1. MessageBox.Show() 可以寫在 PrintDeck 裡面,會更好喔。 :)

    回覆刪除
  2. switch (Rank)
    {
    case 1:
    return "A";
    case 11:
    return "J";
    case 12:
    return "Q";
    case 13:
    return "K";
    default:
    return Convert.ToString(Rank);
    }

    其他直接 default 比原版更簡潔
    第七組

    回覆刪除
  3. Form1.cs:
    31 36 42行的deck.ResetDeck();移到switch 前面。因為是同一種東西

    回覆刪除
  4. ===林高遠===
    你不會在書局買到只有空盒子的撲克牌,所以CarDeck被new 出來的時候就要馬上有52張牌比較合理

    回覆刪除