Form1.cs
namespace Hw4
{
public partial class Form1 : Form
{
CardDeck Card = new CardDeck();
public Form1()
{
InitializeComponent();
Card.CreateDeck();
}
private void btnchk_Click(object sender, EventArgs e)
{
if (chknormal.Checked)
{
txt_Print.AppendText("CheckBox按花色排列 已選定" + "\n");
Card.ResetDeck();
print_message();
}
if (chkrand.Checked)
{
txt_Print.AppendText("CheckBox亂數洗牌 已選定" + "\n");
Card.ResetDeck();
Card.Shuffle();
print_message();
}
if (chkarrange.Checked)
{
txt_Print.AppendText("CheckBox按點數大小排列 已選定" + "\n");
Card.Arrange();
print_message();
}
}
private void btncombo_Click(object sender, EventArgs e)
{
switch (cbox_3.SelectedIndex)
{
case 0:
txt_Print.AppendText("ComboBox按花色排列 已選定"+"\n");
Card.ResetDeck();
print_message();
break;
case 1:
txt_Print.AppendText("ComboBox亂數洗牌 已選定" + "\n");
Card.ResetDeck();
Card.Shuffle();
print_message();
break;
case 2:
txt_Print.AppendText("ComboBox按點數大小排列 已選定" + "\n");
Card.Arrange();
print_message();
break;
}
}
private void print_message()
{
string message = "";
for (int i=0; i <= 51; i++)
{
message += Card.Cards[i].SuitToUni() + Card.Cards[i].RankToStr() + " ";
if (((i + 1) % 13) == 0)
{
message += "\n";
}
}
MessageBox.Show(message);
}
}
}
CardDeck.cs
namespace Hw4
{
class CardDeck
{
public PokerCard[] Cards = new PokerCard[52];
Random rand = new Random();
public void CreateDeck()
{
for (int i = 0; i <= 51; i++)
{
Cards[i] = new PokerCard();
}
}//產生出52個PokerCard的物件
public void ResetDeck()
{
for (int i = 0; i <= 51; i++)
{
Cards[i].Rank = (i % 13) + 1;
Cards[i].Suit = (i / 13) + 3;
}
} //牌堆回復原始狀態
public void Shuffle()
{
PokerCard Change = new PokerCard();
for (int i = 0; i <= 51; i++)
{
Change = Cards[i];
int position = rand.Next(52);
Cards[i] = Cards[position];
Cards[position] = Change;
}
}//洗牌
public void Arrange()
{
for (int i = 0; i <= 51; i++)
{
Cards[i].Suit = (i%4) + 3;
Cards[i].Rank = (i / 4) + 1;
}
}
}
}
PokerCard.cs
namespace Hw4
{
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 SuitToUni()
{
string Str_Suit;
switch (Suit)
{
case 3:
Str_Suit = "\u2665";
break;
case 4:
Str_Suit = "\u2666";
break;
case 5:
Str_Suit = "\u2663";
break;
default:
Str_Suit = "\u2660";
break;
}
return Str_Suit;
}
}
}
一副牌有52張,所以我認為
回覆刪除for (int i = 0; i <= 51; i++)
這樣寫容易有程式的+1-1問題
可能這樣寫比較好
for (int i = 0; i < 52; i++)
SuitToUni()裡的
回覆刪除default:
Str_Suit = "\u2660";
break;
應該在新增一個case回傳"\u2660"
若是有錯誤,不該直接都回傳"\u2660"
===林高遠===
回覆刪除你不會在書局買到只有空盒子的撲克牌,所以CarDeck被new 出來的時候就要馬上有52張有大小和花色的牌比較合理
Form1.cs中
回覆刪除行23Card.ResetDeck();非必要?
PokerCard.cs中
public string RankToStr() 另以RankStr 回傳是否較佳?
如果有誤,則較易修改
By第七組 吳珠鈺