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 /
avatar image
0
Question by Yopie · Jul 21, 2013 at 06:31 AM · shuffleshort

ANSWERED [C#] Coding Noob here- could somebody help with this short shuffling program?

Hello everyone, I am trying to make an EXTREMELY simple shuffler. Basically, the end result should be "H, C, D, and S" printed in the console in a random order. The problem is I am just starting out, and I can't understand most of the errors I look up. Could someone help?

 using System.Linq;
 using System;
 using System.Collections.Generic;
     
 class test
 {
     static void Main()
     {
     string[] arr = new string[]
     {
         "H",
         "C",
         "D",
         "S"
     };
 Random rnd = new Random();
 
 int r = rnd.Next(3);
 
 int str = Array.IndexOf(arr, r);
 
 Console.Write (str);}}  

Thanks for the help! I'll see if I can figure out how to mark answered. For anybody who sees this, here's the code I have now. It works, but it's not finished or perfect. For one thing, the shuffler isn't totally random. When you run it, in the console it prints the contents of two player hands and the rest of the deck, and you can press 0-9 and JQK to print on the game screen. If anyone is in the same position i was in, maybe this will help.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class a : MonoBehaviour
 {
     void Start ()
     {
         //create unshuffled deck
         List<int> deck = new List<int>();
         for(int i = 1; i < 53; i++)
         {
             deck.Add(i);
         }
         
         //shuffle deck
         for(int i = 0 ; i < 52 ; i++)
         {
             int rnd = Random.Range(0,51-i); 
             valueSwap(deck,i,rnd);
         }
         dealTwoHands (deck, 10);
     }    
         /* dealing: */
         
     void dealTwoHands(List<int> deck, int sizeHand)
     {
         List<int> player1Hand = new List<int>();
     
         for (int i = 0; i < sizeHand; i++)
         {
             player1Hand.Add(deck[i]);
         }
         
         List<int> player2Hand = new List<int>();
         
         for(int i = sizeHand; i < sizeHand + sizeHand; i++)
         {
             player2Hand.Add (deck[i-sizeHand]);
         }
         
         List<int> drawDeck = new List<int>();
         
         for(int i = sizeHand + sizeHand; i<52; i++)
         {
             drawDeck.Add (deck[i-sizeHand-sizeHand]);
         }
         //organize hands by value
         orderCards(player1Hand);
         orderCards(player2Hand);
         orderCards(drawDeck);
         
         //print deck
         Debug.Log ("Player 1 Hand:");
         printDeck (player1Hand);
         Debug.Log ("Player 2 Hand");
         printDeck (player2Hand);
         Debug.Log ("Draw Deck");
         printDeck (drawDeck);
     }
     string myString = "Please select a card.";
     
     void OnGUI () 
         {
             GUI.Label(new Rect (10, 10, 200, 20), myString );
         }        
         void Update () 
         {
                     //I know this bit is messy, it's what I'm working on now.
             if(Input.inputString == "j")
             myString = "You selected Jack";
             if(Input.inputString == "q")
             myString = "You selected Queen";
             if(Input.inputString == "k")
             myString = "You selected King";
             if(Input.inputString == "1")
             myString = "You selected Ace";
             if(Input.inputString == "2")
             myString = "You selected 2";
             if(Input.inputString == "3")
             myString = "You selected 3";
             if(Input.inputString == "4")
             myString = "You selected 4";
             if(Input.inputString == "5")
             myString = "You selected 5";
             if(Input.inputString == "6")
             myString = "You selected 6";
             if(Input.inputString == "7")
             myString = "You selected 7";
             if(Input.inputString == "8")
             myString = "You selected 8";    
             if(Input.inputString == "9")
             myString = "You selected 9";
             if(Input.inputString == "0")
             myString = "You selected 10";
         }
     
     //organize cards by value function
     List<int> orderCards (List<int> orderList)
     {
         for (int i = 0; i < orderList.Count -1; i++)
         {
             for (int j = i+1; j < orderList.Count; j++)
             {
                 if (getRank(orderList[i]) > getRank(orderList[j]))
                 {
                     valueSwap(orderList,i,j);
                 }        
             }
         }
         return orderList;
     }
     
     //gets a card's number 0 - 13
     int getRank(int card)
     {
         while (card>13) 
         {
             card = card - 13;
         }
 
         return card;
     }
     //prints the deck    
     void printDeck(List<int> deck)
     {
         for(int i = 0; i < deck.Count; i++)
         {
             Debug.Log (deck[i] +"");
         }
     }     
     //gives a card's suit. Only works with the cards index
     string cardSuit (int card)
     {
         if  (card < 13)
         {
             return ("Spade");
         }
         else if  (card < 26)
         {
             return ("Heart");
         }
         else if  (card < 39)
         {
             return ("Diamond");
         }
         else 
         {
             return ("Club");
         }
     }
             //switch two cards' value function
     List<int> valueSwap(List<int> myList, int swap1, int swap2)
     {
 
         int cardAtIndex1 = myList[swap1];
         int cardAtIndex2 = myList[swap2];
         
         myList.RemoveAt (swap1);
         myList.Insert(swap1, cardAtIndex2);
         
         myList.RemoveAt (swap2);
         myList.Insert(swap2, cardAtIndex1);
         
         return myList;
     }
     
 }
Comment
Add comment · Show 1
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 xikky · Jul 21, 2013 at 07:14 AM 0
Share

what errors are occuring?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by bubzy · Jul 21, 2013 at 07:46 AM

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class shuffle : MonoBehaviour {
 
     // Use this for initialization
     char[] arr;
     bool doneShuffling = false;
     List<char> letters = new List<char>();
     List<char> tempLetters = new List<char>();
     
     void Start () {
         
         letters.Add('C'); // you can add as many letters as you like here.
          letters.Add('H');
         letters.Add('D');
         letters.Add('S');
     }
     
     // Update is called once per frame
     void Update () {
     if (Input.GetKeyDown(KeyCode.S))
         {
             populateLetters(); //fill the tempLetters list with values from letters list
             shuffleEm();
         }
     }
     
     void populateLetters()
     {
         for(int i = 0; i < letters.Count; i++)
         {
             tempLetters.Add(letters[i]);
         }
     }
     void shuffleEm()
     {
         int range = tempLetters.Count;
         Debug.Log(tempLetters.Count + "");
      for(int i = 0; i < range; i++)
         {
             int index = Random.Range(0,tempLetters.Count);
             Debug.Log(tempLetters[index]);
             tempLetters.Remove(tempLetters[index]);
           }    
     }
 }
 
Comment
Add comment · Show 1 · 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 Yopie · Jul 28, 2013 at 02:13 PM 0
Share

Thank you for the help! I'll try to mark this as the answer.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

Multiple Cars not working 1 Answer

Perform Action on frame/time of animation 1 Answer

how to trigger another script in unityscript 1 Answer

I made a better shader how do i fix[add _Shadow Strength]help???>Sorry that im asking for to much 1 Answer

NEED HELP CODING, CANT FIND MISTAKE 2 Answers


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