|
There are a number of ways to do this, but the way I'd probably start is by creating a Card and Suit:
public enum Suit
{
Spades = 0,
Diamonds = 1,
Clubs = 2,
Hearts = 3,
}
public class Card
{
private int _Value;
public Suit Suit { get { return (Suit)(_Value / 13); } }
public int Value { get { return _Value % 13; } }
public Card(int value)
{
if (value < 0 || value >= 52)
{
throw new ArgumentException("Invalid Card : " + value);
}
_Value = value;
}
}
Then, it's a simple matter of creating the deck and shuffling it:
private List<Card> deck = new List<Card>();
private Random rand = new Random();
private Card GetNextCard(List<Card> deck)
{
if (deck.Count <= 0)
{
Enumerable.Range(0, 52).Select(c => new Card(c)).ToList();
deck = Shuffle(deck);
}
Card card = deck[0];
deck.RemoveAt(0);
return card;
}
private List<Card> Shuffle(List<Card> deck)
{
List<Card> shuffled = new List<Card>();
int cards = deck.Count;
for (int i = 0; i < cards; i++)
{
int index = rand.Next(deck.Count);
shuffled.Add(deck[index]);
deck.RemoveAt(index);
}
return shuffled;
}
Then just "peel off" the cards you want:
Card c1 = GetNextCard(deck);
Card c2 = GetNextCard(deck);
Card c3 = GetNextCard(deck);
Card c4 = GetNextCard(deck);
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
This is what I am trying to do with this:
Write a C# program to simulate the drawing from a deck of 52 poker cards valued from 1 to 13 with 4 suits. One round of drawing consists of randomly pull any 4 cards from the 52-card deck. Therefore, the same card cannot appeared more than once. The program will perform 10,000 rounds of drawing and tallying the frequencies of the following possible results for the 4 cards pulled: (a)the 4 cards are having the same suit, (b)the 4 cards are from the 4 different suits, (c)the 4 cards are having the same value, (d)3 cards have the same value, (e)exactly 2 different pairs of 2 same-value cards, and (f)only 2 cards have the same value. Notice that (b) may occurred together with (c) - (f).
This is what I have so far:
using System;
namespace Draw4
{
class Program
{
static int[] ranData = new int[4];
static Random ran = new Random();
static int[] KS = new int[4];
static int GenerateRandomNum()
{
int cleanRanNum;
cleanRanNum = ran.Next(1, 53);
return cleanRanNum;
}
static void Main(string[] args)
{
int idx, ranNum;
int[] cdv = new int[4];
int[] cds = new int[4];
for (idx = 0; idx < 4; idx++) KS[idx] = 0;
{
Console.Write("Random numbers: \t");
for (idx = 0; idx < cdv.Length; idx++)
{
ranNum = GenerateRandomNum();
Console.Write(ranNum + ",\t");
cdv[idx] = ranNum % 13;
if (cdv[idx] == 0)
{
cds[idx] = ranNum / 13;
cdv[idx] = 13;
}
else
cds[idx] = ranNum / 13 + 1;
}
Array.Sort(cdv);
Console.Write("\nThe suits:\t\t");
PrintingSuits(cds);
Console.Write("\nThe values:\t\t");
foreach (int val in cdv) Console.Write(val + ",\t");
CountSuits(cds);
}
Console.Read();
}
static void CountSuits(int[] cds)
{
Array.Sort(cds);
int howManySuits = 0;
for (int j = 0; j < 3; j++)
if (cds[j] != cds[j + 1]) howManySuits++;
Console.WriteLine("\nThis hand has {0} suits", howManySuits + 1);
return;
}
static void PrintingSuits(int[] suit)
{
string s = " ";
foreach (int su in suit)
{
switch (su)
{
case 1: s = "H"; break;
case 2: s = "S"; break;
case 3: s = "D"; break;
case 4: s = "C"; break;
}
Console.Write(s + ",\t");
}
}
}
}
|
|
|
|
|
Ok, your method won't work to prevent numbers from repeating - which is why I created a list of Cards (but you could use int values instead). If you uncomment the line adding the card back to the bottom of the deck, you can simply have a loop which grabs the top four cards, prints them, and then calls the Shuffle method. It can then loop 10,000 times without problems.
The advantage of using a Card class is that it moves the complexity out of the "main" code: you can tell the Card to print itself, by adding a ToString override that returns "4 of Hearts" or "Ace of Spades" and just call Console.WriteLine:
for (idx = 0; idx < cdv.Length; idx++)
{
cdv[idx] = GetNext(deck);
Console.WriteLine(cdv[idx]);
}
So you main method consists of a little init code, then a loop, which calls shuffle, and then does the loop above.
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
|
Okay so I have gotten the random numbers to stop. What about the conditions A-F? I know what I want to say, problem is how to say it and where to say it if that makes sense. Sorry if these noob questions are tedious. Just trying to learn.
|
|
|
|
|
(from my Windows Phone)
make some variables for the statistics you need, unit to 0
write a for loop for the 10,000 iterations
inside, shuffle the deck and draw 4 cards (1st 4 will do)
for each of the statistics check for the condition and increment the appropriate counter variable if true
after all 10,000 iterations, compute the frequencies and report them.
|
|
|
|
|
You could build it even simpler then a list.
bool [] deck = new bool [52];
the index is the card you draw and true is drawn, false is not drawn. .
A little more complex is to create a class Card which holds a boolean "drawn".
I personally only use Lists (and other collections) on dynamic arrays (those that can grow/shrink) and arrays on static arrays (like this case, because there are always 52 cards in a card game. This is just personal and there is nothing wrong in using a list.
|
|
|
|
|
Hey Guys,
Am a beginner to C# Programming, i have a requirement to create a smal project.
The project is to track the no of task done by individual. Few descriptions below.
1. Once a person is assigned with a Task, he need to enter the same in the front end tool.
2. This data will be getting captured in the backend (e.g: sql database) like Task#, Task Name, date, person name.
3. On completing the task, the person will enter the details in the tool.
4. So in the database we have the details for
a) No.of task done by induviual (e.g; in a month)
b) time taken to complete the task etc.
Planning to create front end of the tool in C#
Backend in SQL.
This frontend tool will be installed to all the users (e.g: 50 machines)
and the SQL server will be on Manager's machine, so that he will pull the report daily to see the status of the task assigned.
Planning to create the tool in Client-Server model.
is the above tool be done with visual C# + SQL ?
My Queries:
1. When i use windows application in C#, on linking the SQL database am hardcoding the SQL server details (e.g: i harcode Machine1\SQLEXPRESS ) but if at all am changing the server in future to Machine2\SQLEXPRESS do i need to recompile my Code again ?
|
|
|
|
|
Mohan Subramani wrote: 1. When i use windows application in C#, on linking the SQL database am hardcoding the SQL server details (e.g: i harcode Machine1\SQLEXPRESS ) but if at all am changing the server in future to Machine2\SQLEXPRESS do i need to recompile my Code again ? Yes you do. If you have hard coded the value you would have to change the value in code and recompile it. A better bet is to use configuration to store this value - in your case, you would want to store this in the ConnectionStrings section in app.config. This way, you only have to change the value in the config file and you will be able to run it against the new SQL database without recompilation.
|
|
|
|
|
Pete O'Hanlon,
Thanks for your quick reply,
Reply: "you would want to store this in the ConnectionStrings section in app.config"
So when do i need to do this, when am establishing a connection between SQL and my windows application ?
when i make the changes to app.config file with new server details, then i need to make sure that all users have the newly updated configuration file in on their machine.
1. So in this case, is their any option for creating a patch.exe to be installed on the machine which will replace the old config file on users machine to newly updated config file. ?
2. And regarding the front end tool using windows application, my requirement is to have a 2 flavours of tool.
a) one installed to all the users (e.g: 50 machines) this will have only limited options to only enter data of their tasks.
b) another one is the Admin tool , with extra options and dialog boxes and buttons to pull daily or monthly data in form of excel from SQL server.
How this can be done ? please assist.
-->
|
|
|
|
|
Yes you will need to recompile, as the connection string is part of the program. You should take a look at the ConfigurationManager Class[^] for ways of managing this information.
Veni, vidi, abiit domum
|
|
|
|
|
Richard MacCutchan,
Thanks for your quick reply,
Reply: "you would want to store this in the ConnectionStrings section in app.config"
So when do i need to do this, when am establishing a connection between SQL and my windows application ?
when i make the changes to app.config file with new server details, then i need to make sure that all users have the newly updated configuration file in on their machine.
1. So in this case, is their any option for creating a patch.exe to be installed on the machine which will replace the old config file on users machine to newly updated config file. ?
2. And regarding the front end tool using windows application, my requirement is to have a 2 flavours of tool.
a) one installed to all the users (e.g: 50 machines) this will have only limited options to only enter data of their tasks.
b) another one is the Admin tool , with extra options and dialog boxes and buttons to pull daily or monthly data in form of excel from SQL server.
How this can be done ? please assist.
|
|
|
|
|
1. For the connection strings, take a look at Don't hard code your DataProviders[^], which explains how it should be done. I'm not sure how you deliver updates for an installed application, try searching for articles on the subject.
2. I don't see a problem, just create a separate tool for administrator use, or add some sort of password login to it.
Veni, vidi, abiit domum
|
|
|
|
|
Great !!!
Thank you, will proceed with start creating my project. will get back to you when am struck with something
Cheers.
|
|
|
|
|
Mohan Subramani wrote: will get back to you when am struck with something I don't know if we'll be able to provide first aid if you're struck with a heavy object, but feel free to get back to us if you're stuck with something. We may be able to provide you a solution. Or at least a solvent.
/ravi
|
|
|
|
|
Ravi,
i have my SQL Server Database on Machine1
I have installed my visual Studio on Machine2. I created windows forms on Machine1 and try to connect with Database on Machine1
View-->Server Explorer-->Add Connection-->Choose Microsoft SQL Server
In Add Connection window:
when i give Server name as
Scenario1:
Machine1\SQLEXPRESS
i could not find any database in the dropdown list. i get an error (Provider: SQL Network Interfaces, error:26 - Error Locating Server/Instance Specified)
But the Database is present on the Machine1.
Scenario2:
(connecting to local database on same machine)
Machine2\SQLEXPRESS
in dropdown list i get the datbase listed.
Issue: am not able to connect to database on other machines.
|
|
|
|
|
Are your insances on SQL Express or SQL Server? You need to connect to the appropriate server.
/ravi
|
|
|
|
|
Yes you will need to recompile.
A better solution would be to add keys to your web config and read them from there.
Another would be to create a .lic file and read the connection strings from there so that you don't have to recompile the application at all. You can use RSA encryption to protect UID and Passwords.
Thanks
JD
http://www.seitmc.com/seitmcWP
|
|
|
|
|
How can I get proper neural network?
How can I train that neural network?
How can I embed the neural network into software?
|
|
|
|
|
Artificial Neural Network[^]
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
|
|
|
|
|
|
1
1 0 1
1 0 0 0 1
1 0 0 0 0 0 1
1 0 0 0 1
1 0 1
1
i need to print the diamond shape with only border is 1 remaining all elements are zero's. so please help me in this; starting 1 is at the top of zero.....
-- modified 2-Feb-14 8:25am.
|
|
|
|
|
Read the guidelines on how to ask a question then start again,
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
All you need is a few loops and counters. Try think about it as you write it down on paper:
- How many initial spaces to print
- After the
1 , how many zeros to print - How many more lines to process
- etc ...
Veni, vidi, abiit domum
|
|
|
|
|
how do i get opening balance from previous day closing balance.
here is the example
date 14/2/2013 closingbalance 2000
date 15/2/2013 opening balance 2000
credit 2000
debit 500
date 15/2/2013 closingbalance 3500
date 16/2/2013 opening balance 3500 and so on
here previous closing balance become opening balance.
problem is that i have to show data between date and no idea how to get opening balance of first record.
i am using sqlser 2005 and crystal report 10.
|
|
|
|