- Home /
 
De vraag wordt beantwoord, juiste antwoord werd aanvaard
Problem with array
I got this weird bug that is keeping me awake right now, and I can't seem to get it fixed, sometimes it displays all of them filled in, but sometimes it leaves gaps like you can see..
Here's the code:
     static string[] cards = new string[52]; //all indexes already assigned
     static string[] AI1cards = new string[13]; // 13 cards for AI 1
     static Random rand = new Random();
     static List<int> uniqueNumber = new List<int>();
 
             for (int i = 0; i < AI1cards.Length; i++)
             {
                 int number = rand.Next(0, 51);
                 while (uniqueNumber.Contains(number))
                 {
                     number = rand.Next(0, 51);
                 }
                 uniqueNumber.Add(number);
                 AI1cards[i] = cards[number];
             }
 //Print on console
             for (int i = 0; i < AI1cards.Length; i++)
             {
                 Console.WriteLine("AI1 Card " +i+ ": " + AI1cards[i]);
             }
 
               Console prints: (always random but leaves gaps sometimes for some reason)
 AI1 Card 0: Heart 5
 AI1 Card 1: 
 AI1 Card 2: Diamonds 4
 AI1 Card 3: Heart J
 AI1 Card 4: 
 AI1 Card 5: Diamonds J
 AI1 Card 6: Heart 8
 AI1 Card 7: Clubs 5
 AI1 Card 8: Diamonds 6
 AI1 Card 9: Clubs 9
 AI1 Card 10: Heart 2
 AI1 Card 11: Clubs 4
 AI1 Card 12: Heart 10
 
              Well the first thing I notice is that "Spades" isn't showing. Have you forgotten to define "Spades somewhere"? That might help.
No as you can see spades does go into it heres another generated:
 AI1 Card 0: Heart $$anonymous$$
 AI1 Card 1: Spades J
 AI1 Card 2: Diamonds 7
 AI1 Card 3: Heart J
 AI1 Card 4: Spades 3
 AI1 Card 5: Heart 5
 AI1 Card 6: Diamonds 3
 AI1 Card 7: Clubs 2
 AI1 Card 8: Diamonds 6
 AI1 Card 9: 
 AI1 Card 10: Heart 3
 AI1 Card 11: Diamonds J
 AI1 Card 12: Heart 4
 
                 Answer by gjf · Aug 02, 2014 at 03:21 PM
 using UnityEngine;
 using System.Collections.Generic;
 
 public class ShadyTest : MonoBehaviour
 {
     public int NumberOfPlayerCards = 13;
 
     private string[] _cards;
     private string[] _playerCards;
 
     private readonly string[] _suits = new[] { "Spades", "Hearts", "Clubs", "Diamonds" };
     private readonly string[] _cardValues = new[] { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };
     
     public void Awake()
     {
         CreateDeck();
         DrawCards();
         DisplayDrawnCards();
     }
 
     private void CreateDeck()
     {
         _cards = new string[(_suits.Length * _cardValues.Length)];
 
         for (var suit = 0; suit < _suits.Length; suit++)
         {
             for (var card = 0; card < _cardValues.Length; card++)
             {
                 _cards[(suit * _cardValues.Length) + card] = _cardValues[card] + " of " + _suits[suit];
             }
         }
     }
 
     private void DrawCards()
     {
         _playerCards = new string[NumberOfPlayerCards];
 
         var uniqueNumber = new List<int>();
 
         for (var i = 0; i < _playerCards.Length; i++)
         {
             int card;
 
             while (true)
             {
                 card = Random.Range(0, _cards.Length);
 
                 if (!uniqueNumber.Contains(card))
                 {
                     uniqueNumber.Add(card);
                     break;
                 }
             }
 
             _playerCards[i] = _cards[card];
         }
     }
 
     private void DisplayDrawnCards()
     {
         for (var i = 0; i < _playerCards.Length; i++)
         {
             Debug.Log("Player card [" + i + "] = " + _playerCards[i]);
         }
     }
 }
 
               obviously Random isn't a great way to shuffle the cards ;)
Thankyou, unfortunately I had to abandon my project since I had made it a whole different way and it was too difficult to re-write it and re-implement everything, but thanks in advance this helped alot.
just in case you want to do something similar in the future, here's a nicer version:
 using UnityEngine;
 using UnityEditor;
 using System.Collections.Generic;
 
 public class ShadyTest : $$anonymous$$onoBehaviour
 {
     public int NumberOfPlayerCards = 13;
 
     private List<string> _completeDeck;
     private List<string> _cards;
     private List<string> _playerCards;
 
     private readonly string[] _suits = new[] {"Spades", "Hearts", "Clubs", "Diamonds"};
 
     private readonly string[] _cardValues = new[]
         {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "$$anonymous$$ing"};
 
     public void Awake()
     {
         CreateDeck();
 
         DrawCards(NumberOfPlayerCards);
 
         DisplayCardSet("Complete deck", _completeDeck);
         DisplayCardSet("Player cards", _playerCards);
         DisplayCardSet("Remaining cards", _cards);
 
         EditorApplication.Execute$$anonymous$$enuItem("Edit/Play");
     }
 
     private void CreateDeck()
     {
         _completeDeck = new List<string>();
 
         foreach (var s in _suits)
         {
             foreach (var v in _cardValues)
             {
                 _completeDeck.Add(v + " of " + s);
             }
         }
 
         NewDeck();
     }
 
     private void NewDeck()
     {
         _cards = new List<string>(_completeDeck); // Clone
     }
 
     private void DrawCards(int numberOfCards)
     {
         _playerCards = new List<string>();
 
         for (var i = 0; i < numberOfCards; i++)
         {
             _playerCards.Add(DrawCard());
         }
     }
 
     private string DrawCard()
     {
         var cardIndex = Random.Range(0, _cards.Count);
         var card = _cards[cardIndex];
         
         _cards.RemoveAt(cardIndex);
         
         return card;
     }
 
     private void DisplayCardSet(string cardSetName, List<string> cards)
     {
         Debug.Log(cardSetName + " (" + cards.Count + ")");
         DisplayCardList(cards);
     }
 
     private void DisplayCardList(List<string> cards)
     {
         for (var i = 0; i < cards.Count; i++)
         {
             Debug.Log("Card [" + i + "] = " + cards[i]);
         }
     }
 }
 
                 Answer by flaviusxvii · Aug 02, 2014 at 02:11 AM
Apart from your bug, you code conceptually makes no sense at all! Why not have a list of cards, and shuffle them, and then "deal" (pop them from the list)? You know, like reality.
? I have an array of cards and they are being randomly picked as in 'shuffled' and then dealed to AI1; what are you trying to say?
Something more like what @glf suggested. I'd use a Card class though. Then a Deck class. Deck would have a shuffle() method, that would randomize the order of a List(). Deal() would pop a card off the top and return it.
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How to save and load a list (containing string data) with playerpref ? 1 Answer
How do I stop new elements in a list/array from inheriting values from the last element? 1 Answer
Removing null object from array in unity 0 Answers
How to Sort Multiple Vector Arrays in order of Values 1 Answer