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.Windows.Forms;
namespace WindowsFormsApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
CardDeck deck;
private void btn_combox_Click(object sender, EventArgs e)
{
if (deck == null)
{
deck = new CardDeck();
deck.CreateDeck();
}
txt_Message.Clear();
switch (cbox_Select.SelectedIndex)
{
case 0:
txt_Message.AppendText("Combox 按花色排列 已被選定" + "\n");
deck.ResetDeck();
ShowDeck();
break;
case 1:
txt_Message.AppendText("Combox 亂數排列 已被選定" + "\n");
deck.Shuffle();
ShowDeck();
break;
case 2:
txt_Message.AppendText("Combox 按點數大小 已被選定" + "\n");
deck.OrderByRank();
ShowDeck();
break;
}
}
private void btn_CheckboxTest_Click(object sender, EventArgs e)
{
if (deck == null)
{
deck = new CardDeck();
deck.CreateDeck();
}
txt_Message.Clear();
if (chk_Reset.Checked)
{
txt_Message.AppendText("Checkbox 按花色排列 已被選定" + "\n");
deck.ResetDeck();
ShowDeck();
}
if (chk_Shuffle.Checked)
{
txt_Message.AppendText("Checkbox 亂數排列 已被選定" + "\n");
deck.Shuffle();
ShowDeck();
}
if (chk_OrderByRank.Checked)
{
txt_Message.AppendText("Checkbox 按點數大小 已被選定" + "\n");
deck.OrderByRank();
ShowDeck();
}
}
private void ShowDeck()
{
string Msg_txt = "";
for (int i = 0; i < 52; i++)
{
Msg_txt += " " + deck.Cards[i].RankToStr() + deck.Cards[i].SuitToUnicode();
if ((i + 1) % 13 == 0)
{
Msg_txt += "\n";
}
}
MessageBox.Show(Msg_txt);
}
}
}
CardDeck.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApp
{
public class CardDeck
{
public PokerCard[] Cards = new PokerCard[52];
private Random rand = new Random();
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 + 1;
Cards[i].Suit = i / 13 ;
}
}
public void Shuffle() //洗牌
{
for (int i = 0; i < 52; i++)
{
int card1 = rand.Next(52);
ChangeTwoCards(i, card1);
}
}
public void OrderByRank() //按點數大小排列
{
for (int i = 0,num=0; i < 13; i++)
{
for (int j = 0; j < 4; j++, num++)
{
Cards[num].Rank = i + 1;
Cards[num].Suit = j ;
}
}
}
private void ChangeTwoCards(int card1, int card2)
{
PokerCard temp = new PokerCard();
temp = Cards[card1];
Cards[card1] = Cards[card2];
Cards[card2] = temp;
}
}
}
PokerCard.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApp
{
public class PokerCard
{
public int Suit; //花色
public int Rank; //點數
public string RankToStr() //將點數轉為字串
{
if (Rank == 1)
return "A";
else if (Rank == 11)
return "J";
else if (Rank == 12)
return "Q";
else if (Rank == 13)
return "K";
else
return Convert.ToString(Rank);
}
public string SuitToUnicode() //將花色轉為unicode
{
switch (Suit)
{
case 0: //愛心
return ((char)0x2665).ToString();
case 1: //菱形
return ((char)0x2666).ToString();
case 2: //梅花
return ((char)0x2663).ToString();
case 3: //黑桃
return ((char)0x2660).ToString();
default:
return "";
}
}
}
}
===林高遠===
回覆刪除你不會在書局買到只有空盒子的撲克牌,所以CarDeck被new 出來的時候就要馬上有52張有大小和花色的牌比較合理
===林高遠===
回覆刪除你們寫的這段
if (deck == null)
{
deck = new CardDeck();
deck.CreateDeck();
}
如果沒有deck就去new 一個,這樣不是搓掉錯誤嗎?