自己做的斗地主游戏发牌C#程序
using System;class Print
{
public void show(int a) //显牌
{
string s="";
int i,j;
i=a/100;
j=a%100;
switch(i)
{
case 1: s=Convert.ToString('\x0003'); break; //红桃
case 2: s=Convert.ToString('\x0004'); break; //方块
case 3: s=Convert.ToString('\x0005'); break; //梅花
case 4: s=Convert.ToString('\x0006'); break; //黑桃
case 5: s="SK"; break; //小王
case 6: s="BK"; break; //大王
}
switch(j)
{
case 1: s=s+"A"; break;
case 11: s=s+"J"; break;
case 12: s=s+"Q"; break;
case 13: s=s+"K"; break;
case 14: break;
case 15: break;
default: s=s+j.ToString(); break;
}
Console.Write(s);
}
}
class myclass
{
public static void Main()
{
int dz,i,j,k,l,a,b,c,temp;
int []card=new int[54]; int []dp=new int[3]; int [,]player=new int[3,17];
Random Rnd1=new Random();
Print print=new Print();
dz=Rnd1.Next(0,3); //选地主
for(i=1;i<5;i++) //初始化54张牌
for(j=1;j<14;j++)
card[(i-1)*13+j-1]=i*100+j;
card[52]=514; card[53]=615; //小王,大王
do
{
dp[0]=Rnd1.Next(0,54);
dp[1]=Rnd1.Next(0,54);
dp[2]=Rnd1.Next(0,54);
}while(dp[0]==dp[1]||dp[1]==dp[2]||dp[0]==dp[2]); //选底牌
a=card[dp[0]]; card[dp[0]]=0; //记住底牌,并将牌中底牌的值置0
b=card[dp[1]]; card[dp[1]]=0;
c=card[dp[2]]; card[dp[2]]=0;
for(i=0;i<=10;i++) //洗牌10次
for(j=0;j<54;j++)
{
k=Rnd1.Next(0,54);
temp=card[j];
card[j]=card[k];
card[k]=temp;
}
j=0;
for(i=0;i<3;i++) //分牌,每人17张
for(l=0,k=0;j<54;j++)
{
if(card[j]!=0) //略去底牌
{
if(k==17) break;
k++;
player[i,l]=card[j];
l++;
}
}
for(k=0;k<3;k++) //排列每个人的牌
{
for(i=0;i<16;i++) //排列大小
for(j=i+1;j<17;j++)
if((player[k,i]%100)<(player[k,j]%100))
{
temp=player[k,i];
player[k,i]=player[k,j];
player[k,j]=temp;
}
for(i=0;i<16;i++) //排列花色
for(j=i+1;j<17;j++)
if(((player[k,i]/100)<(player[k,j]/100))&&((player[k,i]%100)==(player[k,j]%100)))
{
temp=player[k,i];
player[k,i]=player[k,j];
player[k,j]=temp;
}
}
Console.WriteLine("---模仿QQ游戏中斗地主中的发牌---");
Console.WriteLine("地主是:player[{0}]",dz);
Console.Write("底牌是:"); print.show(a); Console.Write(","); print.show(b); Console.Write(","); print.show(c); //显示底牌
Console.WriteLine();
for(i=0;i<3;i++) //显示每个人的牌
{
Console.Write("player[{0}]的牌是:",i);
for(j=0;j<16;j++)
{
print.show(player[i,j]);
Console.Write(",");
}
if(i!=dz) //不是地主
print.show(player[i,j]);
else //是地主,发给三张底牌
{
print.show(player[i,j]); Console.Write(",");
print.show(a); Console.Write(",");
print.show(b); Console.Write(",");
print.show(c);
}
Console.WriteLine();
}
Console.ReadKey();
}
}
自己比较得意的C#控制台程序,大家分享,水平有限呵呵。另外大家看看有没有好的算法排列扑克的大小和花色?我的没有搞好啊!