c#自动发牌程序代码(数组枚举)
3个回答
展开全部
private void Shuffle()
{
int[] All = new int[52];
int X, temp, Len = 52;
for (int i = 0; i < Len; i++) //初始牌的标号
All[i] = i;
Random Shuffler = new Random();
for (int i = Len - 1; i >= 0; i--) //洗牌
{
X = Shuffler.Next(Len);
temp = All[i];
All[i] = All[X];
All[X] = temp;
}
List<Tuple<int,int> >[] players=new List<Tuple<int,int> >[4]; //4个用户
for (int i = 0; i < 4; i++) players[i]=new List<Tuple<int,int> >();
int idx=0;
for (int i = 0; i < Len; i++)
{
int color = All[i] / 13; //花色 0 ,1 ,2 ,3
int number = All[i] % 13 + 1; //数值1~13 对应 1~K
//在这里处理将牌发到用户的操作。
//添加相应代码,例如:
plays[idx++].Add(new Tuple(color,number));
if(idx==4) idx=0;
}
}
{
int[] All = new int[52];
int X, temp, Len = 52;
for (int i = 0; i < Len; i++) //初始牌的标号
All[i] = i;
Random Shuffler = new Random();
for (int i = Len - 1; i >= 0; i--) //洗牌
{
X = Shuffler.Next(Len);
temp = All[i];
All[i] = All[X];
All[X] = temp;
}
List<Tuple<int,int> >[] players=new List<Tuple<int,int> >[4]; //4个用户
for (int i = 0; i < 4; i++) players[i]=new List<Tuple<int,int> >();
int idx=0;
for (int i = 0; i < Len; i++)
{
int color = All[i] / 13; //花色 0 ,1 ,2 ,3
int number = All[i] % 13 + 1; //数值1~13 对应 1~K
//在这里处理将牌发到用户的操作。
//添加相应代码,例如:
plays[idx++].Add(new Tuple(color,number));
if(idx==4) idx=0;
}
}
追问
c#数组实验(自动发牌)描述:利用数组实现发牌过程,52张牌。一共四家,每家随机发13张牌。每家的牌用一维数组表示,发牌时,用二维数组记录未发的牌,第一维代表扑克的花色,第二维表示牌面大小 ,花色:enum Suit { Clubs, Diamonds, Hearts, Spades } ,牌面:enum Value { Two, Three, ..... King, Ace },每张牌可以独立作为一个类来实现,该类中包含两个成员,即花色和牌面(谢谢你的解答,能继续帮我吗?)
追答
enum Suit { Clubs, Diamonds, Hearts, Spades }
enum Value { Two=1, Three=2, ..... King=12, Ace = 0 }
class Card { Suit CardSuit { get; set; } Value CardValue { get; set; } }
List[] players=new List[4]; //4个用户
private void Shuffle()
{
int[] All = new int[52];
int X, temp, Len = 52;
for (int i = 0; i = 0; i--) //洗牌
{
X = Shuffler.Next(Len);
temp = All[i];
All[i] = All[X];
All[X] = temp;
}
for (int i = 0; i ();
int idx=0;
for (int i = 0; i < Len; i++)
{
int color = All[i] / 13; //花色 0 ,1 ,2 ,3
int number = All[i] % 13 ; //数值0~12 对应 1~K
//在这里处理将牌发到用户的操作。
//添加相应代码,例如:
Card card= new Card { CardSuit=(Suit)color , CardValue=(Value)number };
plays[idx++].Add(card);
if(idx==4) idx=0;
}
}
展开全部
class pai
{
private string suit;
private string values;
public string Suit
{
get { return suit; }
set { suit = value; }
}
public string Value
{
get { return values; }
set { values = value; }
}
}
class Program
{
static void Main(string[] args)
{
//string[] sijia=new string[4]{"south","north","west","east"};
//string[] meijia = new string[13];
string[] Values=new string[13]{"Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King","Ace"};
string[] Suits=new string [4]{"Clubs","Diamonds","Hearts","Spades"};
pai []all=new pai[52];
for (int i=0; i < 4; i++)
for(int j=0;j<13;j++)
{
pai pai2=new pai();
pai2.Suit=Suits[i];
pai2.Value=Values[j];
all[13 * i + j] = pai2;
}
int[] a = new int[52];
for (int n = 0; n < 52; n++)
{
one:
Random r = new Random();
int m = r.Next(51);
if (a[m] == 0)
{
a[m] = n;
}
else
{ goto one;}
}
for (int i = 0; i < 4; i++)
{
Console.WriteLine();
for (int j = 0; j < 13; j++)
{
int p = a[13 * i + j];
Console.Write(all[p].Suit);
Console.Write(all[p].Value);
Console.WriteLine();
int num = 51 - (13 * i + j);
string numb = Convert.ToString(num);
Console.WriteLine("还剩{0}张", numb);
Console.Write(" ");
Console.WriteLine();
}
}
Console.ReadLine();
{
private string suit;
private string values;
public string Suit
{
get { return suit; }
set { suit = value; }
}
public string Value
{
get { return values; }
set { values = value; }
}
}
class Program
{
static void Main(string[] args)
{
//string[] sijia=new string[4]{"south","north","west","east"};
//string[] meijia = new string[13];
string[] Values=new string[13]{"Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King","Ace"};
string[] Suits=new string [4]{"Clubs","Diamonds","Hearts","Spades"};
pai []all=new pai[52];
for (int i=0; i < 4; i++)
for(int j=0;j<13;j++)
{
pai pai2=new pai();
pai2.Suit=Suits[i];
pai2.Value=Values[j];
all[13 * i + j] = pai2;
}
int[] a = new int[52];
for (int n = 0; n < 52; n++)
{
one:
Random r = new Random();
int m = r.Next(51);
if (a[m] == 0)
{
a[m] = n;
}
else
{ goto one;}
}
for (int i = 0; i < 4; i++)
{
Console.WriteLine();
for (int j = 0; j < 13; j++)
{
int p = a[13 * i + j];
Console.Write(all[p].Suit);
Console.Write(all[p].Value);
Console.WriteLine();
int num = 51 - (13 * i + j);
string numb = Convert.ToString(num);
Console.WriteLine("还剩{0}张", numb);
Console.Write(" ");
Console.WriteLine();
}
}
Console.ReadLine();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//用C#模拟实现扑克牌发牌、排序程序。
//(1)52张扑克牌,四种花色(红桃、黑桃、方块和梅花),随机发牌给四个人。
//(2)最后将四个人的扑克牌包括花色打印在控制台上。
//其中:
// 花色和点数用枚举类型实现
// 每张扑克牌用结构实现
//提示:可以用ArrayList初始化52张扑克牌,然后从这个链表中随机取牌发给四个玩家,直到链表为空为止。
namespace Test5
{
enum Color {HongTao=-1,HeiTao=-2,MeiHua=-3,FangPian=-4}//花色
enum Point {A,two,three,four,five,six,seven,eight,nine,ten,J,Q,K}//点数
struct Poker
{//扑克
private string p1,p2;
public Poker(string p1, string p2)
{
// TODO: Complete member initialization
this.p1 = p1;
this.p2 = p2;
}
public void Printp()
{
Console.Write("({0},{1}) ", this.p1,this.p2);
}
}
class Program
{
static void Main(string[] args)
{
Poker po = new Poker();
ArrayList myPoker=new ArrayList();//实例化一个ArrayList存放所有的扑克牌
ArrayList Person1 = new ArrayList();
ArrayList Person2 = new ArrayList();
ArrayList Person3 = new ArrayList();
ArrayList Person4 = new ArrayList();
Random r = new Random();
for (int i = -4; i <= -1; i++)
{//外循环初始化扑克的花色
for (int j = 0; j <= 12; j++)
{//外层循环初始化扑克的点数
myPoker.Add(new Poker(
Enum.GetName(typeof(Color),i),
Enum.GetName(typeof(Point),j)
));
}
}//for循环结束,52张扑克牌已经生成
Console.WriteLine("打印所有的扑克牌:");
for (int i = 0; i < 52; i++)
{//打印52张生成的扑克牌
Poker poAll = (Poker)myPoker[i];
poAll.Printp();
}
//开始发牌,一个人一个人的发,每发一张牌得从myPoker中RemoveAt掉扑克牌,Count数减小;第一个人Add到一张牌。
for (int i = 0; i < 13; i++)
{
int te = r.Next(0,myPoker.Count);
Person1.Add(myPoker[te]);
myPoker.RemoveAt(te);
}
for (int i = 0; i < 13; i++)
{
int te = r.Next(0, myPoker.Count);
Person2.Add(myPoker[te]);
myPoker.RemoveAt(te);
}
for (int i = 0; i < 13; i++)
{
int te = r.Next(0, myPoker.Count);
Person3.Add(myPoker[te]);
myPoker.RemoveAt(te);
}
for (int i = 0; i < 13; i++)
{
int te = r.Next(0, myPoker.Count);
Person4.Add(myPoker[te]);
myPoker.RemoveAt(te);
}
Console.WriteLine();
Console.WriteLine("打印第一个人的扑克牌:");
for (int i = 0; i < 13;i++ )
{
Poker po1 = (Poker)Person1[i];
po1.Printp();
}
Console.WriteLine();
Console.WriteLine("打印第二个人的扑克牌:");
for (int i = 0; i < 13; i++)
{
Poker po2 = (Poker)Person2[i];
po2.Printp();
}
Console.WriteLine();
Console.WriteLine("打印第三个人的扑克牌:");
for (int i = 0; i < 13; i++)
{
Poker po3 = (Poker)Person3[i];
po3.Printp();
}
Console.WriteLine();
Console.WriteLine("打印第四个人的扑克牌:");
for (int i = 0; i < 13; i++)
{
Poker po4 = (Poker)Person4[i];
po4.Printp();
}
Console.WriteLine();
}
}
}
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//用C#模拟实现扑克牌发牌、排序程序。
//(1)52张扑克牌,四种花色(红桃、黑桃、方块和梅花),随机发牌给四个人。
//(2)最后将四个人的扑克牌包括花色打印在控制台上。
//其中:
// 花色和点数用枚举类型实现
// 每张扑克牌用结构实现
//提示:可以用ArrayList初始化52张扑克牌,然后从这个链表中随机取牌发给四个玩家,直到链表为空为止。
namespace Test5
{
enum Color {HongTao=-1,HeiTao=-2,MeiHua=-3,FangPian=-4}//花色
enum Point {A,two,three,four,five,six,seven,eight,nine,ten,J,Q,K}//点数
struct Poker
{//扑克
private string p1,p2;
public Poker(string p1, string p2)
{
// TODO: Complete member initialization
this.p1 = p1;
this.p2 = p2;
}
public void Printp()
{
Console.Write("({0},{1}) ", this.p1,this.p2);
}
}
class Program
{
static void Main(string[] args)
{
Poker po = new Poker();
ArrayList myPoker=new ArrayList();//实例化一个ArrayList存放所有的扑克牌
ArrayList Person1 = new ArrayList();
ArrayList Person2 = new ArrayList();
ArrayList Person3 = new ArrayList();
ArrayList Person4 = new ArrayList();
Random r = new Random();
for (int i = -4; i <= -1; i++)
{//外循环初始化扑克的花色
for (int j = 0; j <= 12; j++)
{//外层循环初始化扑克的点数
myPoker.Add(new Poker(
Enum.GetName(typeof(Color),i),
Enum.GetName(typeof(Point),j)
));
}
}//for循环结束,52张扑克牌已经生成
Console.WriteLine("打印所有的扑克牌:");
for (int i = 0; i < 52; i++)
{//打印52张生成的扑克牌
Poker poAll = (Poker)myPoker[i];
poAll.Printp();
}
//开始发牌,一个人一个人的发,每发一张牌得从myPoker中RemoveAt掉扑克牌,Count数减小;第一个人Add到一张牌。
for (int i = 0; i < 13; i++)
{
int te = r.Next(0,myPoker.Count);
Person1.Add(myPoker[te]);
myPoker.RemoveAt(te);
}
for (int i = 0; i < 13; i++)
{
int te = r.Next(0, myPoker.Count);
Person2.Add(myPoker[te]);
myPoker.RemoveAt(te);
}
for (int i = 0; i < 13; i++)
{
int te = r.Next(0, myPoker.Count);
Person3.Add(myPoker[te]);
myPoker.RemoveAt(te);
}
for (int i = 0; i < 13; i++)
{
int te = r.Next(0, myPoker.Count);
Person4.Add(myPoker[te]);
myPoker.RemoveAt(te);
}
Console.WriteLine();
Console.WriteLine("打印第一个人的扑克牌:");
for (int i = 0; i < 13;i++ )
{
Poker po1 = (Poker)Person1[i];
po1.Printp();
}
Console.WriteLine();
Console.WriteLine("打印第二个人的扑克牌:");
for (int i = 0; i < 13; i++)
{
Poker po2 = (Poker)Person2[i];
po2.Printp();
}
Console.WriteLine();
Console.WriteLine("打印第三个人的扑克牌:");
for (int i = 0; i < 13; i++)
{
Poker po3 = (Poker)Person3[i];
po3.Printp();
}
Console.WriteLine();
Console.WriteLine("打印第四个人的扑克牌:");
for (int i = 0; i < 13; i++)
{
Poker po4 = (Poker)Person4[i];
po4.Printp();
}
Console.WriteLine();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询