按我顯示
using System;
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;
namespace Poker
{
public partial class Poker : Form
{
CardDeck newCards = new CardDeck();
public Poker()
{
InitializeComponent();
newCards.CreateDeck();
}
private void btnComBox_Click(object sender, EventArgs e) //ComboBox
{
switch(cboxSelect.SelectedIndex)
{
case 0:
txtDisplay.AppendText("ComboBox 依花色排列 (原始設定)\n");
newCards.ResetDeck();
ShowDeckMessage();
break;
case 1:
txtDisplay.AppendText("ComboBox 亂數洗牌\n");
newCards.ShuffleDeck();
ShowDeckMessage();
break;
case 2:
txtDisplay.AppendText("ComboBox 依點數大小排列\n");
newCards.ArrangeDeck();
ShowDeckMessage();
break;
default:
txtDisplay.AppendText("選擇一個選項\n");
break;
}
}
private void btnCheckBox_Click(object sender, EventArgs e) //CheckBox
{
if (chkBoxReset.Checked)
{
txtDisplay.AppendText("CheckBox 依花色排列 (原始設定)\n");
newCards.ResetDeck();
ShowDeckMessage();
}
if (chkBoxShuffle.Checked)
{
txtDisplay.AppendText("CheckBox 亂數洗牌\n");
newCards.ShuffleDeck();
ShowDeckMessage();
}
if (chkBoxArrange.Checked)
{
txtDisplay.AppendText("CheckBox 依點數大小排列\n");
newCards.ArrangeDeck();
ShowDeckMessage();
}
}
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);
}
}
}
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Poker
{
class CardDeck
{
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 ResetDeck() //重置52張牌
{
for (int i = 0; i < 52; i++)
{
cards[i].rank = (i % 13) + 1;
cards[i].suit = (i / 13) ;
}
}
public void ShuffleDeck() //亂數洗牌
{
PokerCard tempCard = new PokerCard();
for (int i = 0; i < 52; i++) //交換
{
tempCard.rank = cards[i].rank;
cards[i].rank = cards[rand.Next(52)].rank;
cards[rand.Next(52)].rank = tempCard.rank;
tempCard.suit = cards[i].suit;
cards[i].suit = cards[rand.Next(52)].suit;
cards[rand.Next(52)].suit = tempCard.suit;
}
}
public void ArrangeDeck() //依照點數與花色回復52張牌
{
for (int i = 0; i < 52; i++)
{
cards[i].rank = (i / 4) + 1;
cards[i].suit = (i % 4);
}
}
}
}
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Poker
{
class PokerCard
{
public int rank; //設定點數
public int suit; //設定花色
public string PrintRank() //數字輸出
{ //將1.11.12.13轉化為A.J.Q.K輸出
switch(rank)
{
case 1:
return "A";
case 11:
return "J";
case 12:
return "Q";
case 13:
return "K";
default:
return rank.ToString();
}
}
public string PrintSuit() //花色輸出
{
string suitSign = null;
switch (suit)
{
case 0: //梅花
suitSign = "\u2663";
break;
case 1: //方塊
suitSign = "\u2666";
break;
case 2: //愛心
suitSign = "\u2665";
break;
case 3: //黑桃
suitSign = "\u2660";
break;
default:
break;
}
return suitSign;
}
}
}
沒有留言:
張貼留言