Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Aug 02, 2017 at 02:30 PM by ShadyProductions for the following reason:

De vraag wordt beantwoord, juiste antwoord werd aanvaard

avatar image
0
Question by ShadyProductions · Aug 02, 2017 at 02:31 PM · arraylistprintforloop

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


Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image HolBol · Aug 02, 2014 at 12:14 AM 0
Share

Well the first thing I notice is that "Spades" isn't showing. Have you forgotten to define "Spades somewhere"? That might help.

avatar image ShadyProductions · Aug 02, 2014 at 12:20 AM 0
Share

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

2 Replies

  • Sort: 
avatar image
1
Best Answer

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 ;)

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ShadyProductions · Aug 02, 2014 at 03:35 PM 0
Share

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.

avatar image gjf · Aug 02, 2014 at 04:11 PM 0
Share

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]);
         }
     }
 }

avatar image ShadyProductions · Aug 02, 2014 at 05:39 PM 1
Share

Thanks for the advice

avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ShadyProductions · Aug 02, 2014 at 02:41 PM 0
Share

? 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?

avatar image flaviusxvii · Aug 02, 2014 at 06:10 PM 0
Share

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

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges