2014年3月24日 星期一

[Code Review] Team 04 - Hw04

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;
using quiz4_40273040H;

namespace HW4
{
    public partial class Form1 : Form        
    {
        private CardDeck deck = null;
        public Form1()
        {
            InitializeComponent();
            InitDeck();
            
        }
        private void InitDeck()
        {
            deck=new CardDeck();
            deck.CreateDeck();
            deck.ResetDeck();
        }
        private void Print(CardDeck Deck1)
        {
            
            string x1="";
            for (int i = 0; i < 52; i++)
            {
                x1 = string.Concat(x1, Deck1.Cards[i].RankToStr(), Deck1.Cards[i].SuitToAscii()," ");
                
                if ((i + 1) % 13 == 0)
                    x1 = string.Concat(x1, "\n");
               
            }
            MessageBox.Show(x1);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (null == deck)
            {
                deck = new CardDeck();
                deck.CreateDeck();
                deck.ResetDeck();
            }
            tex_Message.Text = "";
            switch (cbox_Select.SelectedIndex)
            {
                case 0:
                    tex_Message.AppendText("Combokbox 按花色排列 已被選定"+'\n');
                    deck.ResetDeck();
                    Print(deck);
                    break;
                case 1:
                    tex_Message.AppendText("Combobox 亂數排列 已被選定"+'\n');
                    deck.Shuffle();
                    Print(deck);
                    break;
                case 2:
                    tex_Message.AppendText("Combobox 按點數大小" + '\n');
                    deck.OrderByRank();
                    Print(deck);
                    break;
                    
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                tex_Message.AppendText("Checkbox 按花色排列 已被選定" + '\n');
                deck.ResetDeck();
                Print(deck);
            }
            if (checkBox2.Checked)
            {
                tex_Message.AppendText("Checkbox 亂數排列 已被選定" + '\n');
                deck.Shuffle();
                Print(deck);
            }
            if (checkBox3.Checked)
            {
                tex_Message.AppendText("Checkbox 按點數大小 已被選定" + '\n');
                deck.OrderByRank();
                Print(deck);
            }

        }



        
    }
}


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

namespace quiz4_40273040H
{
    class CardDeck
    {
        public PokerCard[] Cards = new PokerCard[52];

        public void CreateDeck()
        {
            for (int i = 0; i < Cards.GetLength(0); i++)
            {
                Cards[i]=new PokerCard();
            }
        }
        public void ResetDeck()
        {
            for (int i = 0; i < 52; i++)
            {
                int j = i;
                Cards[i].Suit = i/13+3;
                Cards[i].Rank=j%13+1;
            }
        }
        public void Shuffle()
        {
            Random rand = new Random();
            PokerCard temp=new PokerCard();
            int j;
            for (int i = 0; i < 52; i++)
            {
                j=rand.Next(52);
                temp = Cards[i];
                Cards[i] = Cards[j];
                Cards[j] = temp;
            }
        }
        public void OrderByRank()
        {
           
         int i,pass;
         PokerCard temp=new PokerCard();
         for(pass=0;pass<51;pass++)
         {
          for(i=0;i<51-pass;i++)
          {
           if(Cards[i].Rank>=Cards[i+1].Rank)
           {
            temp=Cards[i];
            Cards[i]=Cards[i+1];
            Cards[i+1]=temp;
           }
          }
         }
        }
    }

}

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

namespace quiz4_40273040H
{
    class PokerCard
    {
        public int Suit;  //花色
        public int Rank;  //點數
        public string RankToStr()
        {
            switch (Rank)
            {
                case 1:
                    return ("  A");
                    break;
                case 2:                    
                case 3:
                case 4:                    
                case 5:                    
                case 6:                    
                case 7:                    
                case 8:                   
                case 9:
                    return string.Concat(" ", Rank.ToString());
                case 10:
                    return Rank.ToString();
                case 11:
                    return ("  J");
                    break;
                case 12: 
                    return ("  Q");
                    break;
                case 13:
                    return ("  K");
                    break;
                default:
                    return "";
            }
        }//將點數轉為字串
        public string SuitToAscii()
        {
            string Str_Suit="";
            switch(Suit)
            {
                case 3:
                    Str_Suit="\u2665";
                    break;
                case 4:
                    Str_Suit="\u2666";
                    break;
                case 5:
                    Str_Suit = "\u2663";
                    break;
                case 6:
                    Str_Suit = "\u2660";
                    break;
            }
            return Str_Suit;
        }
    }

}


10 則留言:

  1. private void Print(CardDeck Deck1) 寫得很好, 可以選擇要傳入哪副牌

    回覆刪除
  2. ==我是林高遠==
    switch case 的 default 不應該 return ""; 因為switch 到 default 在這裡是一個錯誤,應該要 return 一個容易發現錯誤的字串

    回覆刪除
  3. 44~47
    private void Form1_Load(object sender, EventArgs e)
    {

    }
    沒用到的可以把他刪掉

    回覆刪除
  4. SuitToAscii() 因為功能改變,所以改為SuitToUni() 比較好

    回覆刪除
  5. 這是我看過最好最漂亮最威猛的程式

    回覆刪除
  6. 我覺得一般在函式裡頭並不會再使用到內建的函式
    像是你在Print函式裡使用到的涵式:string.Concat()
    會有浪費空間的可能

    回覆刪除
  7. 小地方~~~
    if (null == deck)
    {
    deck = new CardDeck();
    deck.CreateDeck();
    deck.ResetDeck();
    }
    可以直接使用
    if (null == deck)
    {
    InitDeck()
    }

    另外
    public void OrderByRank()
    {

    int i,pass;
    PokerCard temp=new PokerCard();
    for(pass=0;pass<51;pass++)
    {
    for(i=0;i<51-pass;i++)
    {
    if(Cards[i].Rank>=Cards[i+1].Rank)
    {
    temp=Cards[i];
    Cards[i]=Cards[i+1];
    Cards[i+1]=temp;
    }
    }
    }
    }

    可以直接用

    for (i = 0; i < 52; i++)
    {
    Cards[i].Suit = i % 4 + 3;
    Cards[i].Rank = i / 4 + 1;
    }

    回覆刪除