- Home /
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;
}
}
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]);
}
}
}
Thank you for the help! I'll try to mark this as the answer.