在C#中向4名玩家交易洗牌

时间:2014-06-05 14:45:17

标签: c#

 public partial class Form1 : Form
{
    public string[] odeck = { "C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "C10", "CJ", "CQ", "CK", "H1", "H2", "H3", "H4", "H5", "H6", "H7", "H8", "H9", "H10", "HJ", "HQ", "HK", "S1", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "S10", "SJ", "SQ", "SK", "D1", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "D10", "DJ", "DQ", "DK" };
    public string[] sdeck; // array to store the shuffled deck
    const  int cards_length = 51;
    //string[] cards = new string[odeck.Length];
    Random random = new Random();
    const int hand = 13; //number of cards in each player hand
    const int numPlayers = 4; //number of players
    string[,] players = new string[numPlayers, hand];    // Set up players hands arrays


    public void shuffle()
    {
        for (int i = odeck.Length; i > 0; i--)
        {
            int rand = random.Next(i);
            string temp = odeck[rand];
            odeck[rand] = odeck[i - 1];
            odeck[i - 1] = temp;
        }

    }

    public void deal() //iam having trouble how to write this function
    {
        // deal the deck
        for (int i = 0; i < numPlayers; i++)
        {
            for (int j = 0; j < hand; j++)
            {
                players[i, j] = ;
            }
        }
    }
private void GetNewDeck_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < odeck.Length; i++)
        {
            string deck = odeck[i];

            NewDecktextBox.Text = String.Join("  ", odeck);

        }

    }
  private void Shufflebutton_Click(object sender, EventArgs e)
    {
        Form1 myshuffle = new Form1();
        myshuffle.shuffle();
        sdeck = myshuffle.odeck;
        ShuffletextBox.Text = String.Join("  ", sdeck);
    }
private void Dealbutton_Click(object sender, EventArgs e) // having issue with this
    {
      Player1textBox.Text =
      Player2textBox.Text = 
      Player3textBox3.Text =
      Player4textBox.Text= 

    }

嗨,我创造了一个纸牌游戏,创造了一个新的牌组,洗牌并将洗牌的牌组交给4名玩家。我能够完成创建一个新的牌组(点击GetNewDeckButton)并洗牌(ShuffleButton),但不知道如何编写交易功能..当我点击交易按钮时,应该显示洗牌后的牌每个玩家的文本框(每个玩家应该收到13张牌)(player1textbox,palyer2textbox .....)有人可以指导我完成这个吗?

谢谢

1 个答案:

答案 0 :(得分:0)

您需要一种从卡座上移除顶部卡片的方法。然后你可以围着桌子走13次,依次对每个玩家进行顶级卡片处理(编辑:我看到你已经有了那部分)。

目前,您将甲板表示为阵列。问题在于它是固定长度的,因此没有直接的方法来移除顶部卡。您可以做的是将顶部卡片的索引存储在变量中,并在每次取卡时减少该值。类似的东西:

int topCardIndex=51;

string RemoveTopCard()
{
   string topCard = sdeck[topCardIndex];
   topCardIndex--;
   return topCard;
}

使用方法如下:

public void deal() //iam having trouble how to write this function
{
    // deal the deck
    for (int i = 0; i < numPlayers; i++)
    {
        for (int j = 0; j < hand; j++)
        {
            players[i, j] = RemoveTopCard();
        }
    }
}

我建议你创建一个名为Deck的独立类来封装所有Deck的东西,包括改组。