求C# 发桥牌程序代码
- 提问者网友:棒棒糖
- 2021-04-27 03:25
- 五星知识达人网友:平生事
- 2021-04-27 05:04
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ch1p392_2
{
class Program
{
static void Main(string[] args)
{
char[] a = { 'C', 'D', 'H', 'S' }; //代表花色,C 代表梅花,D 代表方块,H 代表红心,S 代表黑桃
char[] b = { 'A', '2', '3', '4', '5', '6', '7', '8', '9', '1', 'J', 'Q', 'K' };//代表一条花色13张牌
int[] c = { 1, 2, 3, 4 }; //代表四个人
int[] d = new int[52]; //代表52张牌
int i , j;//循环变量
int count = 0;//累加发牌次数用来判断换行
Random r = new Random(unchecked((int)DateTime.Now.Ticks));//取随机数
for (i = 0; i < 52; i++)
{
while (c[j = r.Next(1, 4)] % 4 <= 0)// 判断是否小于等于零是没用的因为是取不到0这个数的,
//其实就是为了表达式合法化才这样写的
c[j]++;
d[i] = j;
}
for (j = 0; j < 4; j++)
{
for (i = 0; i < 52; i++)
{
if (d[i] != j)
continue;
Console.Write(a[i / 13]);
Console.Write(b[i % 13]);
if (i % 13 == 9)
Console.Write('0');
count++;
if (count >= 13)
{
Console.WriteLine(); //只要发牌到达13张就打印一次换行
count = 0; //在吧count的值初始化为零
}
Console.Write(" ");//控制打印出来的字符和字符之间的位置
}
}
}
}
}
- 1楼网友:上分大魔王
- 2021-04-27 06:22
public class PokeGame { public enum Suits //花色 { Spade, Hearts, Clubs, Diamonds } public enum PointText //点数 { A=1, J=11, Q=12, K=13 } public struct Poke //扑克牌 { Suits suit; int point; public Poke(Suits s, int p) { suit = s; point = p; } public override string ToString() { return suit.ToString() + " " + ((PointText)point).ToString(); } }
private readonly int MaxCount = 13; private Poke[] _test;
public Poke[] Test { get { return _test; } }
public PokeGame() { _test=new Poke[MaxCount * sizeof(Suits)];// 尽量不写死 SortedList<Guid, Poke> t = new SortedList<Guid, Poke>(); for (int i = 0; i < sizeof(Suits); i++) { for (int j = 0; j < MaxCount; j++) { t.Add(Guid.NewGuid(), new Poke((Suits)i, j + 1)); } } t.Values.CopyTo(_test, 0); }
static void Main(string[] args) { PokeGame pg = new PokeGame(); PokeGame.Poke[] ps = pg.Test; //foreach (PokeGame.Poke p in ps) //{ // Console.WriteLine(p.ToString()); //} for (int i = 1; i < 5; i++) { Console.WriteLine("第"+i +":"); for( int j=(i-1)*13; j<i*13; j++) Console.WriteLine(ps[j]); Console.WriteLine("=========================="); } }