2012-07-29 3 views
-4

안녕하세요 여러분, 6 개의 문자열을 임의의 반복없이 6 개의 버튼으로 6 개의 문자열을 무작위로 배포하고 싶습니다. 그게 내가 어떤 종류의 셔플을하고 그것을 배포하고 싶지만 중복되지 않습니다 각 버튼은 고유 문자열을 보유합니다. 사람이 :) 좋은 것입니다 코드를 게시 할 수 있다면 감사C# 반복없이 문자열을 무작위로 배포

class Card_Deck 
{ 
    public Random r; 
    public string ReceiveCards() 
    { 
     List<string> cards = new List<string>(); 
     cards.Add("♣ King"); 
     cards.Add("♦ King"); 
     cards.Add("♥ King"); 
     cards.Add("♠ King"); 
     cards.Add("♣ Jack"); 
     cards.Add("♦ Jack"); 

     int index = r.Next(cards.Count); 
     var card = cards[index]; 
     cards.RemoveAt(index); 
     return card; 

    } 
} 

}

이 기본 폼

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
     Card_Deck cd = new Card_Deck() { r = new Random(DateTime.Now.Millisecond) }; 

     button1.Text = cd.ReceiveCards(); 
     button2.Text = cd.ReceiveCards(); 
     button3.Text = cd.ReceiveCards(); 
     button4.Text = cd.ReceiveCards(); 
     button5.Text = cd.ReceiveCards(); 
     button6.Text = cd.ReceiveCards(); 
    } 
} 

}

+3

우리는 코드 공장은 아니다? 너 뭐 해봤 니? 어디서 붙어 있니? 우리는 스스로를 도울 수없는 사람들을 도울 수 없습니다. – zellio

+0

죄송하지만 그 시간에 코드를 저장할 수 없지만 이제는 지금 나를 도와주세요. –

답변

0

공용 부분 Form1 클래스 : 폼 { 공개를 Form1() { 의 InitializeComponent(); }

private void button1_Click(object sender, EventArgs e) 
    { 
     label1.Text = Card_Deck.ReceiveCards(); 
    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     label2.Text = Card_Deck.ReceiveCards(); 
    } 

    private void button3_Click(object sender, EventArgs e) 
    { 
     label3.Text = Card_Deck.ReceiveCards(); 
    } 
    private void button4_Click(object sender, EventArgs e) 
    { 
     label4.Text = Card_Deck.ReceiveCards(); 
    } 
    private void button5_Click(object sender, EventArgs e) 
    { 
     label5.Text = Card_Deck.ReceiveCards(); 
    } 
    private void button6_Click(object sender, EventArgs e) 
    { 
     label6.Text = Card_Deck.ReceiveCards(); 
    } 
    class Card_Deck 
    { 
     public static Random r = new Random(); 
     private static List<string> cards = new List<string>() { "♣ King 1", 
      "♦ King 2", 
      "♥ King 3", 
      "♠ King 4", 
      "♣ Jack 5", 
      "♦ Jack 6" }; 

     public static string ReceiveCards() 
     { 
      if (cards.Count > 0) 
      { 
       int index = r.Next(cards.Count); 
       var card = cards[index]; 
       cards.RemoveAt(index); 
       return card; 
      } 
      else 
      { 
       return ""; 
      } 

     } 
    } 

} 
+0

고맙습니다. 매우 도움이되었습니다. :) –

0

에서이 작업을 수행하는 가장 간단한 방법입니다 CardDeck 클래스의 내부에서 셔플 알고리즘을 구현하는 것입니다. 피셔 - 예이츠 같은 것.

문제는 매번 배열을 다시 만들고 나서 radom 요소를 잡는 것입니다. 이로 인해 중복이 발생합니다. 당신은 전체 셔플 배열 (더 좋은 카드)을 돌려 주거나 개인 변수를 사용하여 카드 데크를 상태 저장하기로 선택할 수 있습니다.

public class CardDeck { 

    private List<String> cards; 

    public CardDeck() { 
     cards = { "♣ King", "♦ King", "♥ King", 
        "♠ King", "♣ Jack", "♦ Jack" }.toList<String>(); 
    } 

    public List<String> Shuffle() { 
     // shuffle cards here 

     var rand = new System.Random(); 
     return cards.OrderBy(x => rand.Next()).toList<String>(); 
    } 
} 

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 

     var deck = new CardDeck(); 
     var shuffledDeck = deck.Shuffle(); 

     buttom1.Text = shuffledDeck.pop(); 
     buttom2.Text = shuffledDeck.pop(); 
     // ... 
    } 
} 
+0

카드 게임을 만들어 줘서 고맙습니다. 도움이 필요합니다. 내가 할 게임은 4 명의 플레이어가 온라인 게임이 될 것이므로 C#으로 네트워킹하기 위해 어떤 책을 참조 할 수 있습니까? –