在c#中将牌洗牌到4个玩家

本文关键字:4个 玩家 | 更新日期: 2023-09-27 18:06:58

 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.....)有人能指点我一下吗?

谢谢

在c#中将牌洗牌到4个玩家

您需要一种从牌堆中移除顶部牌的方法。然后你可以绕着桌子转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的所有内容,包括洗牌。