Program為程式進入點
Poker為視窗主程式
Deck為函式類別
PokerCard為輸出花色及數字之類別
Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
static class Program
{
///
/// 應用程式的主要進入點。
///
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Poker());
}
}
}
Poker
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 WindowsFormsApplication1
{
public partial class Poker : Form
{
Deck newCards = new Deck();
public Poker()
{
InitializeComponent();
newCards.CreateDeck();
}
private void btnCheckBox_Click(object sender, EventArgs e)
{
txtOutput.Clear();
if (chkDeckReset.Checked)
{
txtOutput.AppendText("CheckBox 依花色排列 (原始設定)\n");
newCards.Reset();
ShowDeckMessage();
}
if (chkDeckShuffle.Checked)
{
txtOutput.AppendText("CheckBox 亂數洗牌\n");
newCards.Shuffle();
ShowDeckMessage();
}
if (chkDeckSort.Checked)
{
txtOutput.AppendText("CheckBox 依點數大小排列\n");
newCards.SortByNumber();
ShowDeckMessage();
}
if (!chkDeckSort.Checked && !chkDeckShuffle.Checked && !chkDeckReset.Checked)//如果三者皆沒勾選之情況
{
txtOutput.AppendText("至少需勾選一個選項");
}
}
private void btnComboBox_Click(object sender, EventArgs e)
{
txtOutput.Clear();
switch (cboSelect.SelectedIndex)
{
case 0:
txtOutput.AppendText("ComboBox 依花色排列 (原始設定)");
newCards.Reset();
ShowDeckMessage();
break;
case 1:
txtOutput.AppendText("ComboBox 亂數洗牌");
newCards.Shuffle();
ShowDeckMessage();
break;
case 2:
txtOutput.AppendText("ComboBox 依點數大小排列");
newCards.SortByNumber();
ShowDeckMessage();
break;
default:
txtOutput.AppendText("未選擇選項");
break;
}
}
public void ShowDeckMessage() //顯示牌組訊息
{
string message = "";
for (int i = 0; i < 52; i++)
{
message += (newCards.cards[i].PrintSuit() + newCards.cards[i].PrintRank() + " ");
if ((i + 1) % 13 == 0)
{
message += "\n";
}
}
MessageBox.Show(message);
}
}
}
Deck
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication1
{
class Deck
{
public PokerCard[] cards = new PokerCard[52];
Random rand = new Random();
public void CreateDeck()//產生52張牌
{
for (int i = 0; i < 52; i++)
{
cards[i] = new PokerCard();
}
}
public void Reset()//重置52張牌
{
for (int i = 0; i < 52; i++)
{
cards[i].number = (i % 13) + 1;
cards[i].suit = (i / 13);
}
}
public void Shuffle()//亂數洗牌
{
PokerCard tempCard = new PokerCard();
for (int i = 0; i < 52; i++)//swap
{
tempCard.number = cards[i].number;
cards[i].number = cards[rand.Next(52)].number;
cards[rand.Next(52)].number = tempCard.number;
tempCard.suit = cards[i].suit;
cards[i].suit = cards[rand.Next(52)].suit;
cards[rand.Next(52)].suit = tempCard.suit;
}
}
public void SortByNumber()//依照點數花色回復52張牌
{
for (int i = 0; i < 52; i++)
{
cards[i].number = (i / 4) + 1;
cards[i].suit = (i % 4);
}
}
}
}
PokerCards
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication1
{
class PokerCard
{
public int number;//宣告點數
public int suit;//宣告花色
public string PrintRank() //數字輸出
{
switch(number)
{
case 1:
return "A";
case 11:
return "J";
case 12:
return "Q";
case 13:
return "K";
default:
return number.ToString();//回傳數字時需要將資料轉型
}
}
public string PrintSuit() //花色輸出
{
string sign = null;
switch (suit)
{
case 0:
sign = "\u2663";//梅花
break;
case 1:
sign = "\u2666";//方塊
break;
case 2:
sign = "\u2665";//愛心
break;
case 3:
sign = "\u2660";//黑桃
break;
default:
break;
}
return sign;
}
}
}
沒有留言:
張貼留言