您的位置:首页 > 编程语言 > C#

c#实验报告模拟发牌程序

2011-10-25 20:34 274 查看
using System;
using System.Collections.Generic;
using System.Text;
namespace card
{
class Card
{
public enum _suit
{红桃,黑桃,草花,方块}
private _suit suit = new _suit();
public _suit Suit
{
get { return suit; }
set { suit = value; }
}

private int rank = new int();
public int Rank
{
get { return rank; }
set
{
if (value <= 13 && value >= 0)
rank = value;
else
rank = 1;
}
}
}
}
---------------------------------------------空虚的分割线-----------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections ;
namespace card
{
class Deck
{
private ArrayList m_cards = new ArrayList();
public Deck()
{
for (int i = 0; i <= 3; i++)
{
for (int j = 1; j <= 13; j++)
{
Card temp=new Card ();
temp.Suit=(Card._suit)i;
temp.Rank=j;
m_cards.Add(temp);
}
}
}
public int Count
{
get { return m_cards.Count; }
}
public void Shuffle()          //洗牌
{
Random r = new Random();
ArrayList temp_m_cards = new ArrayList();
for (int j = 1; j <= 52; j++)
{
int i = r.Next(0, m_cards.Count);
temp_m_cards.Add(m_cards[i]);
m_cards.RemoveAt(i);
}
for (int j = 0; j < 52; j++)
{
Card temp = (Card)temp_m_cards[j];
m_cards.Add(temp);
}
}
public void Deal(Hand[] hands)
{
int k=0 ;
for (int i = 0; k<52 ; i++)
{
hands[i].Add((Card)m_cards[k]);
k++;
if (i == (hands.Length-1))
{
i = -1;
}
}
}
public void Show()
{
for (int i = 0; i < m_cards.Count; i++)
{
Console.Write(((Card)m_cards[i]).Suit);
Console.WriteLine(((Card)m_cards[i]).Rank);
}
}
}
}
---------------------------话说陈绮贞演唱会真激情----------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace card
{
class Hand
{
private string name;
private ArrayList m_cards = new ArrayList();
public Hand(string name)
{
this.name = name;
}
public Hand(string name, Card[] cards)
{
this.name = name;
m_cards.AddRange(cards);
}
public int Count
{
get { return m_cards.Count; }
}
public void Add(Card acard)
{
m_cards.Add(acard);
}
public void Show()
{
Console.WriteLine(name);
for (int i = 0; i < m_cards.Count; i++)
{
Console.Write(((Card)m_cards[i]).Suit);
Console.WriteLine(((Card)m_cards[i]).Rank);
}
}
}
}
------------------------------哈哈~~~陈绮贞 我爱你~~~------------
using System;
using System.Collections.Generic;
using System.Text;
namespace card
{
class Program
{
static void Main(string[] args)
{
Deck aDeck = new Deck();
Console.WriteLine("before shuffle");
aDeck.Show();
aDeck.Shuffle();
Console.WriteLine("after shuffle");
aDeck.Show();
Hand hand1 = new Hand("Zhao");
Hand hand2 = new Hand("Qian");
Hand hand3 = new Hand("Sun");
Hand hand4 = new Hand("Li");
aDeck.Deal(new Hand[] { hand1, hand2, hand3, hand4 });
hand1.Show();
hand2.Show();
hand3.Show();
hand4.Show();
}
}
}




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: