Click here to Skip to main content
15,882,063 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello! I'm making my own deck of cards. I was trying to make it "draw" two random keys and match 'em togheter and do a if- to check them.
What is the command to make it draw a random key from a dictionary?
I'm supposed to use a kortlek.Keys command, but i dont seem to get it to work
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, int> kortlek = new Dictionary<string, int>();
            for (int i = 2; i <= 10; i++)
            {
                kortlek.Add("klöver" + i, i);
                kortlek.Add("spader" + i, i);
                kortlek.Add("ruter" + i, i);
                kortlek.Add("hjärter" + i, +i);
            }
            {
                kortlek.Add("spader dam", 12);
                kortlek.Add("spader knekt", 11);
                kortlek.Add("spader ess", 1);
                kortlek.Add("spader kung", 13);
                kortlek.Add("hjärter ess", 1);
                kortlek.Add("hjärter dam", 12);
                kortlek.Add("hjärter knekt", 11);
                kortlek.Add("hjäter kung", 13);
                kortlek.Add("ruter ess", 1);
                kortlek.Add("ruter dam", 12);
                kortlek.Add("ruter knekt", 11);
                kortlek.Add("ruter kung", 13);
                kortlek.Add("klöver kung", 13);
                kortlek.Add("klöver ess", 1);
                kortlek.Add("klöver dam", 12);
                kortlek.Add("klöver knekt", 11);
            }
            Random dealer = new Random();

            int cardone = dealer.Next(0, 53);
            int cardtwo = dealer.Next(0, 53);
            //HERE I'M SUPPOSED TO USE THE VALUES IN CARDONE/CARDTWO TO GRAB A CARD FROM THE DECK AND THEM MATCH 'EM.
            if (cardone == cardtwo)
            {
                Console.WriteLine("It's a pair!");
            }
            else
            {
                Console.WriteLine("No pair :(");
            }
            
        }
    }
}
Posted
Comments
Member 7896310 15-Feb-12 18:21pm    
anyone, please?

Hello

1. generate 2 random varous numbers>=0 and < keys.Count.

How to generate many random various numbers?[^]

C#
int first, second;
//Follow Link Above...


2.

C#
string key1 = ((kortlek.Keys).ToArray())[first];
string key2 = ((kortlek.Keys).ToArray())[second];
 
Share this answer
 
Comments
Member 7896310 15-Feb-12 19:15pm    
How do I make it randomize 1000 times then check how many times there was a PAIR?
Shahin Khorshidnia 15-Feb-12 20:04pm    
It's another question, but:

public int[] RandomNumbers(int min, int max)
{
return RandomNumbers(min, max, 2);
}

private Random random;
public int[] RandomNumbers(int min, int max, int derangement)
{
if (min > max)
{
throw new Exception("The first parameter must be less (or equal) than the second.");
}
int count = max - min; ;
int[] tempList = new int[count + 1];
int counter = 0;
for (int i = min; i <= max; i++)
{
tempList[counter] = i;
counter++;
}

for (int i = 0; i < derangement; i++)
for (int j = 0; j < count; j++)
{
int k = random.Next(0, count + 1);
int l = random.Next(0, count + 1);
if (k != l)
{
//Swap TempList[k] with TempList[l]
tempList[k] += tempList[l];
tempList[l] = tempList[k] - tempList[l];
tempList[k] = tempList[k] - tempList[l];
}
}
return tempList;
}

private void Check(Dictionary<string, int=""> kortlek)
{
int count = 0;

random = new Random();

for (int i = 0; i < 1000; i++)
{
int[] numbers = RandomNumbers(0, kortlek.Keys.Count() - 1);

string key1 = ((kortlek.Keys).ToArray())[numbers[0]];
string key2 = ((kortlek.Keys).ToArray())[numbers[1]];

if (kortlek[key1] == kortlek[key2])
count++;

}

MessageBox.Show(count.ToString());
}
If you do not insist on Dictionary, you may want to replace it with List<keyvaluepair><string,int>>



ex:

List<keyvaluepair><string,int>> kortlek = new List<keyvaluepair><string,int>>(); 
...
kortlek.Add(new KeyValuePair<string,int>("spader dam", 12));



// you can reach it like this 
//kortlek[cardone].Value
</keyvaluepair></keyvaluepair>


Hope it can help
 
Share this answer
 
v2
Comments
Member 7896310 15-Feb-12 18:42pm    
Unfortunately the tutorial more or less insists on using the dictionarys keys.. How do I use the kortlek.keys? is it a method?
aidin Tajadod 15-Feb-12 19:06pm    
No, It is not a method.
you can use it like this: foreach (string s in kortlek.Keys) { // access to the value like this kortlek[s] }

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900