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 Homework5
{
public partial class Form1 : Form
{
private CardDeck deck = null;
private static string[] type = {"","","Hight Card", "One Pair", "Two Pair", "Three Of a Kind",
"Straight", "Flush", "Full House", "Four Of a Kind", "Straight Flush" };
private int[] freq;
double N = 50000000;
public Form1()
{
InitializeComponent();
deck = new CardDeck();
freq = new int[11];
}
private void button1_Click(object sender, EventArgs e)
{
StudCards hand1 = new StudCards();
StudCards hand2 = new StudCards();
if (rbtn1.Checked == true)
{
deck.Shuffle();
hand1.Reset();
hand2.Reset();
for (int i = 0; i < 5; i++)
{
hand1.Add(deck.Deal());
hand2.Add(deck.Deal());
}
WhoWin(hand1, hand2);
}
if (rbtn2.Checked == true)
{
TxtB.Text = "";
for (int i = 0; i < 11; i++)
freq[i] = 0;
for (int i = 1; i <= (N*0.1)+1; i++)
{
if (i %(N/1000) == 0)
{
PrgBar.Value++;
}
deck.Shuffle();
for (int j = 0; j < 10; j++)
{
hand1.Reset();
for (int k = 0; k < 5; k++)
hand1.Add(deck.Deal());
freq[(int)(hand1.Rank)]++;
}
}
ShowStatistics();
}
}
private void ShowCardsForStudPoker(StudCards hand)
{
MessageBox.Show(hand.ToString());
}
private void WhoWin(StudCards hand1, StudCards hand2)
{
string str = "";
str += "玩家1:" + "\t" + hand1.ToString() + Environment.NewLine;
str += "玩家2:" + "\t" + hand2.ToString() + Environment.NewLine;
if (hand1.Rank > hand2.Rank)
str += "玩家1獲勝!";
else if(hand1.Rank==hand2.Rank)
str+="無法分出勝負!";
else
str += "玩家2獲勝!";
MessageBox.Show(str);
}
private void ShowStatistics()
{
string str = "";
for (int i = 2; i < 11;i++)
{
str += (type[i] + ": " + (freq[i]/N) + Environment.NewLine);
}
TxtB.AppendText(str);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework5
{
class StudCards
{
private static string[] type ={ "","","Hight Card", "One Pair", "Two Pair", "Three Of a Kind",
"Straight", "Flush", "Full House", "Four Of a Kind", "Straight Flush" };
public enum CardType {UnKnown=1, HighCard, OnePair, TwoPair, ThreeOfaKind, Straight,
Flush, FullHouse, FourOfaKind, StraightFlush}
private PokerCard[] cards = null;
private int amount;
private bool isSorted;
private CardType rank;
//建構子
public StudCards()
{
cards = new PokerCard[5];
amount = 0;
isSorted = false;
rank = CardType.UnKnown;
}
public CardType Rank
{
get
{
if (rank == CardType.UnKnown)
EvaluateType();
return rank;
}
}
public void Reset()
{
for (int i = 0; i < 5; i++)
{
cards[i] = null;
}
amount = 0;
isSorted = false;
rank = CardType.UnKnown;
}
public bool Add(PokerCard card)
{
if (null == card || 5 == amount)
return false;
cards[amount++] = card;
return true;
}
public void Sort()
{
if (isSorted)
return;
PokerCard temp;
for (int pass = 0; pass < 4; pass++)
{
for (int i = 0; i < 4; i++)
{
if (cards[i].Rank > cards[i + 1].Rank)
{
temp = cards[i];
cards[i] = cards[i + 1];
cards[i + 1] = temp;
}
}
}
isSorted = true;
}
//***********************************************************************************
public bool IsFlush()
{
bool flag = true;
int i = 0;
while (i < 4)
{
if (cards[i].Suit != cards[i + 1].Suit)
{
flag = false;
break;
}
i++;
}
return flag;
}
public bool IsStraight()
{
Sort();
bool flag = true;
int i = 0;
while (i < 4)
{
if (cards[i + 1].Rank != cards[i].Rank + 1)
{
flag = false;
break;
}
i++;
}
//case for A 10 J Q K
if ((1 == cards[0].Rank) && (10 == cards[1].Rank) && (11 == cards[2].Rank)
&& (12 == cards[3].Rank) && (13 == cards[4].Rank))
flag = true;
return flag;
}
public override string ToString()
{
if (rank == CardType.UnKnown)
EvaluateType();
string str = "";
for (int i = 0; i < 5; i++)
{
str += cards[i].ToString() + " ";
}
str += "\n";
str += type[(int)rank];
return str;
}
private int Plateau()
{
Sort();
int max = 1;
int count = 1;
for (int i = 0; i < 4; i++)
{
if (cards[i].Rank == cards[i + 1].Rank)
{
count++;
if (i < 3)
continue;
}
if (count > max)
max = count;
count = 1;
}
return max;
}
//***********************************************************************************
public void EvaluateType()
{
if (IsFlush())
{
if (IsStraight())
{
rank = CardType.StraightFlush;
return;
}
else
{
rank = CardType.Flush;
return;
}
}
if (IsStraight())
{
rank = CardType.Straight;
return;
}
int longPlateau = Plateau();
if (4 == longPlateau)
{
rank = CardType.FourOfaKind;
return;
}
if (3 == longPlateau)
{
if ((cards[0].Rank != cards[1].Rank) || (cards[3].Rank != cards[4].Rank))
rank = CardType.ThreeOfaKind;
else
rank = CardType.FullHouse;
return;
}
if (2 == longPlateau)
{
if (cards[0].Rank == cards[1].Rank)
{
if ((cards[2].Rank == cards[3].Rank) || (cards[3].Rank == cards[4].Rank))
{
rank = CardType.TwoPair;
return;
}
}
else
{
if ((cards[1].Rank == cards[2].Rank) && (cards[3].Rank == cards[4].Rank))
{
rank = CardType.TwoPair;
return;
}
}
rank = CardType.OnePair;
return;
}
rank = CardType.HighCard;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Homework5
{
class CardDeck
{
private PokerCard[] Cards = null;
private int topindex = 0;
//===============================================
public CardDeck()
{
Cards = new PokerCard[52];
for (int i = 0; i < 52; i++)
{
Cards[i] = new PokerCard(i % 13 + 1, i / 13);
}
topindex = 0;
}
//===============================================
public void ResetDeck()
{
for (int i = 0; i < 52; i++)
{
Cards[i].Suit = i / 13;
Cards[i].Rank = i % 13 + 1;
}
topindex = 0;
}
//===============================================
public void Shuffle()
{
Random rand = new Random();
PokerCard temp;
int RanNum;
for (int i = 0; i < 52; i++)
{
RanNum = rand.Next(52);
temp = Cards[i];
Cards[i] = Cards[RanNum];
Cards[RanNum] = temp;
}
topindex = 0;
}
//===============================================
public PokerCard Deal()
{
if (topindex >= 52)
return null;
return Cards[topindex++];
}
}
}
1. Form1程式看起來很流暢。
回覆刪除2. Sort的i < 4 可以改成 i < 4 – pass;,會更有效率一些。
3. Plateau的概念配合Sort是可行的判斷方法,不過就機率的觀點來看,先做Plateau(),如果longPlateau==1再判斷IsFlush()跟IsStraight()應該會更為經濟。
好的 謝謝你的建議!
刪除