扑克牌游戏C#
作者:夏敏捷 2007-10-30
效果图如下:
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections; //ArrayList类
using System.Runtime.InteropServices;//有限制鼠标移动区域的API函数,ClipCursor(nil)则取消限制
namespace mycards
{
public partial class MainForm : Form
{
private PictureBox []pic;
public MainForm()
{
InitializeComponent();
}
private void Btn_start_Click(object sender, EventArgs e)//开始按钮
{
Btn_SendCard.Enabled = true;
Btn_start.Enabled = false;
//panel1.Invalidate();
string filename;
string path = System.Windows.Forms.Application.StartupPath;// bin路径
for(int i=1;i<=52;i++)
{
filename = path + "//..//..//bmp//" + i.ToString() + ".bmp";
pic[i] = new PictureBox();
pic[i].Left = 80 + 8 *i; //13
pic[i].Top = 180 ;
pic[i].Width = 71; pic[i].Height = 96;
pic[i].Image = Image.FromFile(filename);
pic[i].Tag = i.ToString();
this.Controls.Add(pic[i]);
pic[i].BringToFront();
pic[i].Click += new System.EventHandler(bt_Click);
}
}
private void bt_Click(object sender, System.EventArgs e)
//这里处理单击事件过程
{
PictureBox picBox1 = (PictureBox)sender;
int i = Convert.ToInt16(picBox1.Tag);
picBox1.Top -= 10;
}
private void Form1_Load(object sender, EventArgs e)
{
pic = new PictureBox[53];
}
private void button3_Click(object sender, EventArgs e)//产生20随机数组
{
string ps = "";
ArrayList list = new ArrayList();
int k = 0;
do
{
Random random = new System.Random();
k = random.Next(0, 20);
if (!list.Contains(k))
{
list.Add(k);
}
}
while (list.Count < 20);
int[] p = new int[20];
list.CopyTo(p);
for (int i = 0; i < 20; i++)
{
ps = ps + "|" + p[i].ToString();
}
this.Text = ps;
}
//90度旋转纸牌
private void Rotate_card(PictureBox picBox)
{
int w = picBox.Width;
int h = picBox.Height;
picBox.Image.RotateFlip(RotateFlipType.Rotate90FlipNone);
picBox.Height = w;
picBox.Width = h;
picBox.Refresh();
}
private void Btn_SendCard_Click(object sender, EventArgs e)
//随机给4人发牌
{
Btn_SendCard.Enabled = false;
Btn_start.Enabled = true;
一副牌 一副牌实例 = new 一副牌();
int[] key = new int[13];// 一个人牌的序号在1--52中
for (int i = 1; i <= 4; i++)
{
Console.Write("{0}:", i);
for (int j = 1; j <= 13; j++)
{
card card实例 = 一副牌实例.发牌();
if (card实例 != null)
{
key[j - 1] = card实例.pic_order();
}
}
Array.Sort(key);
switch (i)
{
case 1: //牌手自己
for (int j = 1; j <= 13; j++)
{
int k = key[j - 1];pic[k].Left = 150 + 20 * j;
pic[k].Top = 350;
//pic[k].Image = Image.FromFile(filename);
pic[k].BringToFront();
} break;
case 2: //牌手的下家
for (int j = 1; j <= 13; j++)
{
int k = key[j - 1];
Rotate_card(pic[k]);
pic[k].Top = 50 + 20 * j;pic[k].Left = 50;
//pic[k].Image = Image.FromFile(filename);
pic[k].BringToFront();
} break;
case 3: //牌手的对家
for (int j = 1; j <= 13; j++)
{
int k = key[j - 1];
Rotate_card(pic[k]);Rotate_card(pic[k]);
pic[k].Top = 50;pic[k].Left = 150 + 20 * j;
//pic[k].Image = Image.FromFile(filename);
pic[k].BringToFront();
}break;
case 4: //牌手的上家
for (int j = 1; j <= 13; j++)
{
int k = key[j - 1];
Rotate_card(pic[k]);Rotate_card(pic[k]);
Rotate_card(pic[k]);
pic[k].Top = 310 - 20 * j;pic[k].Left = 500;
//pic[k].Image = Image.FromFile(filename);
pic[k].BringToFront();
} break;
}
}
}
}
}
cards.cs文件:实现随机发牌
namespace mycards
{
class card
{
public byte 花色; // 1..4
public byte 点数; // 1..13
public card(byte 花色, byte 点数)
{
this.点数 = 点数;
this.花色 = 花色;
}
public int pic_order()
{
return (花色 - 1) * 13 + 点数;
}
public override string ToString()
{
string 花色名;
switch (花色)
{
case 1:
花色名 = "梅"; break;
case 2:
花色名 = "方"; break;
case 3:
花色名 = "红"; break;
case 4:
花色名 = "黑"; break;
default:
花色名 = ""; break;
}
string 点数名;
switch (点数)
{
case 1:
点数名 = "A"; break;
case 11:
点数名 = "J"; break;
case 12:
点数名 = "Q"; break;
case 13:
点数名 = "K"; break;
default:
点数名 = 点数.ToString(); break;
}
return string.Format("{0}{1}", 花色名, 点数名);
}
}
class 一副牌
{
public static List<card> 牌列表 = new List<card>();
public static Random 随机数 = new Random();
public 一副牌()
{
for (byte i = 1; i <= 4; i++)
{
for (byte j = 1; j <= 13; j++)
{
牌列表.Add(new card(i, j));
}
}
}
public card 发牌()
{
if (牌列表.Count <= 0) return null;
card 返回值 = null;
int i = (int)(牌列表.Count * 随机数.NextDouble());
返回值 = 牌列表[i];
牌列表.RemoveAt(i);
return 返回值;
}
}
}