public class Poker
{
public string Suit { get; set; }
public string Value { get; set; }
public Poker()
{
int m;
Random r1 = new Random();
m = r1.Next(1, 5);
int n;
Random r2 = new Random();
n = r2.Next(1, 14);
if (m == 1)
Suit = "黑桃";
else if (m == 2)
Suit = "红桃";
else if (m == 3)
Suit = "方块";
else
Suit = "梅花";
if (n == 1)
Value = "A";
if (n == 2)
Value = "2";
if (n == 3)
Value = "3";
if (n == 4)
Value = "4";
if (n == 5)
Value = "5";
if (n == 6)
Value = "6";
if (n == 7)
Value = "7";
if (n == 8)
Value = "8";
if (n == 9)
Value = "9";
if (n == 10)
Value = "10";
if (n == 11)
Value = "J";
if (n == 12)
Value = "Q";
else
Value = "K";
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.StartPosition = FormStartPosition.CenterScreen;
}
public int GetValue(Poker ob)
{
if (ob.Value.Equals("A"))
return 1;
else if (ob.Value.Equals("2"))
return 2;
else if (ob.Value.Equals("3"))
return 3;
else if (ob.Value.Equals("4"))
return 4;
else if (ob.Value.Equals("5"))
return 5;
else if (ob.Value.Equals("6"))
return 6;
else if (ob.Value.Equals("7"))
return 7;
else if (ob.Value.Equals("8"))
return 8;
else if (ob.Value.Equals("9"))
return 9;
else if (ob.Value.Equals("10"))
return 10;
else if (ob.Value.Equals("J"))
return 11;
else if (ob.Value.Equals("Q"))
return 12;
else
return 13;
}//方法:获取牌的大小
public static Poker[] Shuffle()
{
Poker[] pokers = new Poker[52];
for (int i = 0; i < 52; i++)
{
pokers[i] = new Poker();
for (int u = 0; u < i; u++)
{
while (pokers[u].Suit.Equals(pokers[i].Suit) == true && pokers[u].Value.Equals(pokers[i].Value) == true)
{
pokers[i] = null;
pokers[i] = new Poker();
u = 0;
continue;
}
}
}
return pokers;
}//方法:洗牌,生成一副牌
Poker[] pokers = Shuffle();
int click1_time = 1;
Poker[] gamerpoker = new Poker[26];
Poker[] AIpoker = new Poker[26];
private void button1_Click(object sender, EventArgs e)
{
if (click1_time == 26)
MessageBox.Show("没有牌了!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
if (click1_time < 26)
{
listBox1.Items.Add("???");
AIpoker[click1_time] = pokers[2 * click1_time - 2];
gamerpoker[click1_time] = pokers[2 * click1_time-1];
listBox2.Items.Add(gamerpoker[click1_time-1].Suit + gamerpoker[click1_time-1].Value);
click1_time++;
}
}//按顺序摸牌
int click2_time = 0;
int AIpoint = 0;
int gamerpoint = 0;
private void button2_Click(object sender, EventArgs e)
{
listBox1.SelectedIndex = click2_time;
listBox2.SelectedIndex = click2_time;
listBox1.Items[click2_time] = AIpoker[click2_time].Suit + AIpoker[click2_time].Value;
if (GetValue(AIpoker[click2_time]) > GetValue(gamerpoker[click2_time]))
{
AIpoint += 1;
label5.Text = "电脑+1";
label3.Text += "{0},AIpoint";
}
if (GetValue(AIpoker[click2_time]) < GetValue(gamerpoker[click2_time]))
{
gamerpoint += 1;
label5.Text = "玩家+1";
label4.Text += "{0},gamerpoint";
}
click2_time++;
}//比较大小并计分
}