搞了一整天,弄了个图形界面的发牌源码,里面包含了54张牌,才只有初步的东西。
搞了一整天,弄了个图形界面的发牌源码,里面包含了54张牌,才只有初步的东西。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace Poker
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Primary.Width = imageList1.Images[0].Width;
Primary.Height = imageList1.Images[0].Height;
}
private void 开始ToolStripMenuItem_Click(object sender, EventArgs e)
{
CardY = this.Height - imageList1.Images[0].Height - 20;
int[] pv ={ 14, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
int[] pt ={ 0, 1, 2, 3 };
ArrayList AllCard = new ArrayList();
for (int f = 0; f < 2; f++)
{
for (int i = 0; i < pt.Length; i++)
{
for (int j = 0; j < pv.Length; j++)
{
AllCard.Add(new Card(pt[i], pv[j], i * pv.Length + j));
}
}
AllCard.Add(new Card(7, 0, 52));
AllCard.Add(new Card(7, 1, 53));
}
this.SelectPrimary(AllCard);
AllCard = SetPrimary(AllCard, PrimaryFace, PrimarySuit,false);
for (int i = 0; i < Gamer.Length; i++) Gamer[i] = new ArrayList();
int alcount = AllCard.Count - 8;
for (int i = 0; i < Gamer.Length; i++)
{
for (int j = 0; j < alcount / 4; j++)
{
int m = rand.Next(AllCard.Count - 1);
Gamer[i].Add(AllCard[m]);
AllCard.RemoveAt(m);
}
}
IComparer cardcomparer = new CardComparer();
Gamer[0].Sort(0, Gamer[0].Count, cardcomparer);
DrawCard(Gamer[0]);
PrimaryCard.Face = PrimaryFace;
PrimaryCard.Suit = PrimarySuit;
SelectPri sp = new SelectPri(PrimaryCard);
sp.ShowDialog();
PrimaryFace = PrimaryCard.Face;
for (int i = 0; i < AllCard.Count; i++) Gamer[0].Add(AllCard[i]);
for (int i = 0; i < 4; i++)
{
Gamer[i] = SetPrimary(Gamer[i], PrimaryFace, PrimarySuit, true);
Gamer[i].Sort(0, Gamer[i].Count, cardcomparer);
}
DrawCard(Gamer[0]);
}
private void SelectPrimary(ArrayList allcard) //随机一张牌
{
PrimaryCard = (Card)allcard[rand.Next(allcard.Count - 1)];
PrimaryFace = PrimaryCard.Face;
PrimarySuit = PrimaryCard.Suit;
Primary.Image = imageList1.Images[PrimaryCard.Index];
}
private void DrawCard(ArrayList GameCard)
{
this.panel1.Controls.Clear();
PictureBox[] GamerButton = new PictureBox[GameCard.Count];
for (int i = GamerButton.Length-1; i >=0; i--)
{
Card MyCard = new Card(0, 0, 0);
MyCard = (Card)GameCard[i];
this.panel1.Controls.Add(GamerButton[i]);
GamerButton[i] = new PictureBox();
GamerButton[i].BackgroundImage = imageList1.Images[MyCard.Index];
GamerButton[i].Location = new Point(0 + CardXSikp * i, 50 );
GamerButton[i].Size = new Size(GamerButton[i].BackgroundImage.Width, GamerButton[i].BackgroundImage.Height);
GamerButton[i].Tag = MyCard;
GamerButton[i].TabIndex = i;
GamerButton[i].TabStop = false;
GamerButton[i].Click += new System.EventHandler(this.Card_Click);
GamerButton[i].Parent = this.panel1;
}
}
private void Card_Click(object sender, System.EventArgs e)
{
PictureBox b = (PictureBox)sender;
int cardindex = this.panel1.Controls.Count - b.TabIndex - 1;
int cardy = this.panel1.Controls[cardindex].Location.Y;
cardy = cardy < 50 ? 50 : cardy - 50;
this.panel1.Controls[cardindex].Location = new Point(this.panel1.Controls[cardindex].Location.X, cardy);
}
private ArrayList SetPrimary(ArrayList allcard, int primaryface, int primarysuit,bool selectface) //设置主牌
{
if (primaryface == 7) return allcard;
for (int i = 0; i < allcard.Count; i++)
{
Card GamerCard = new Card(0, 0, 0);
GamerCard = (Card)allcard[i];
if (selectface&&GamerCard.Face == PrimaryFace && GamerCard.Suit != PrimarySuit) GamerCard.Face = 4;
if (GamerCard.Face != PrimaryFace && GamerCard.Suit == PrimarySuit) GamerCard.Face = 5;
if (GamerCard.Face == PrimaryFace && GamerCard.Suit == PrimarySuit) GamerCard.Face = 6;
allcard[i] = GamerCard;
}
return allcard;
}
}
}
public class Card
{
int suit;
int face;
int index;
public int Suit
{
get
{
return suit;
}
set
{
suit = value;
}
}
public int Face
{
get
{
return face;
}
set
{
face = value;
}
}
public int Index
{
get
{
return index;
}
set
{
index = value;
}
}
public Card(int Face, int Suit, int Index)
{
face = Face;
suit = Suit;
index = Index;
}
}
class CardComparer : IComparer
{
public int Compare(object info1, object info2)
{
Card Card1 = info1 as Card;
Card Card2 = info2 as Card;
if (Card1.Face != Card2.Face)
{
if (Card1.Face > Card2.Face) return -1;
if (Card1.Face < Card2.Face) return 1;
}
else
{
if (Card1.Suit > Card2.Suit) return -1;
if (Card1.Suit < Card2.Suit) return 1;
}
return 0;
}
}
楼主是否能将你的代码,标上注释,或详细讲解一下,谢谢了,主要是如何把牌写到imagelist里面的,还有怎么显示在panel里面的,对了我还有一个疑问,怎么在你的程序文件夹里怎么找不到那些扑克牌的图片!我现在也想做个这,你的代码有点看不懂,谢谢楼主给标上注释,谢谢了!!
调用的是系统图片。
请楼主加上注解吧,多谢了