我先說自己覺得寫很爛的地方:
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);
}
}
}
小地方
回覆刪除Program.cs 中
行27 註解 轉為 「unicode」
By 吳珠鈺
在btn_CheckBox_Test_Click中
回覆刪除是用comboBox1.SelectedIndex做判斷而不是CheckBox
by楊智翔
智翔退後 !
刪除PokerCard的public String SuitToUnicode() 裡面
回覆刪除為甚麼前面有return了 後面還要break呢?
第十組
break 雖然是無效程式碼,但這是為了維持程式碼結構一致,下次Copy來修改的時候比較方便,才不會忘了加上 break 結果後面整串都做
刪除