DateTime.Now update rate???
Quote:
Originally Posted by Cyanide
I found out that one has to use at least Thread.Sleep(10); in order for the timer to update properly
All gurus out there, do you have an explanation for the above? I have been tracking the DateTime.Now function and found out that it updates only every 10 msec. Is this normal???
Code:
for (int i = 0; i < 20; i++)
{
// Wait to allow the timer to advance.
System.Threading.Thread.Sleep(2);
textBox1.Text += DateTime.Now.Millisecond + "\r\n";
}
gives the following output
Code:
879
889
889
889
889
889
899
899
899
899
899
909
909
909
909
919
919
919
929
929
Re: DateTime.Now update rate???
Hi Cyanide!
If it is normal depends on your operating system.
Have a look at the documentation of the DateTime.Now property; DateTime.Now Property
This states that the resolution is 10ms on Windows NT 3.5 and later, and 55 ms on Win98.
However the DateTime.Ticks property has an accuracy of 100 nano seconds; DateTime.Ticks Property
Hope this helps!
Regards,
Laitinen
Re: DateTime.Now update rate???
here's what you need to do.
I wrote a card game a while ago, and the best way I found to get a random shuffle, was to give each card object a GUID, and sort the deck on that guid. each time you shuffled the deck, you re-assign each card w/ a new guid before calling sort.
It had way better results than using random.
here's a sample of what I used (just a quick and dirty sample):
Code:
class Program {
[STAThread]
static void Main(string[] args) {
Deck deck = new Deck();
deck.Shuffle();
deck.Shuffle();
}
}
public enum Suit { None, Club, Spade, Heart, Diamond }
public enum FaceValue { None, Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Eleven, Jack, Queen, King }
public class Card : IComparable<Card> {
Guid cardId = Guid.Empty;
Suit suit = Suit.None;
FaceValue value = FaceValue.None;
public Guid CardId {
get { return cardId; }
}
public Suit Suit {
get { return suit; }
}
public FaceValue Value {
get { return this.value; }
}
public void NewId() { cardId = Guid.NewGuid(); }
public int CompareTo(Card other) {
return cardId.CompareTo(other.cardId);
}
public override string ToString() {
return string.Format("{0} of {1}", value, suit);
}
public Card(Suit suit, FaceValue value) {
this.suit = suit;
this.value = value;
NewId();
}
}
public class Deck {
List<Card> cards = new List<Card>();
public List<Card> Cards {
get { return cards; }
}
public void Shuffle() {
foreach(Card card in cards) { card.NewId(); }
cards.Sort();
}
public Deck() {
for(int suit = 1; suit <= 4; suit++) {
for(int value = 1; value <= 14; value++) {
cards.Add(new Card((Suit)suit, (FaceValue)value));
}
}
Shuffle();
}
}
Re: DateTime.Now update rate???
Thanks MadHatter. That really helps me think I'm on the right track I thought I was doing it wrong. But my program looks exactally like yours' In fact I had to scroll up to make sure I didn't post my actual code. I made a special card class to represent each card, and enums for suit and face value. Almost all of your code looks exactally like mine. :)
Now I know I've been on the right track, and I can modify my shuffler.
What game did you make? Did you have any tricks to validating the players hand? That's one part I'm still working on. There are so many possible combinations that could be correct for them to win, its hard for me to figure out the best way to do it.
Re: DateTime.Now update rate???
I built some foundation classes that were basically the card / deck domain, and then had a rules framework that governed the rules of the specific game implementation. and another that did the rendering (it was just a windows forms app). I'm not much of a card player. I had black jack, and was working on a vanilla version of hearts when my hdd's crashed and I lost it all.
the main thing that gave me a fit (that I remember) was the shuffling of the deck and went the same route you did for a while before thinking of the GUID solution. I forget exactly how I had designed the rules part of the framework (I may think of it later) but it was fairly easy to specify game logic. If I remember it I'll let you know. It was one of those things I came up with one bored night that I never really touched again, then lost.
I think my rules framework basically revolved around game setup, game play, and who wins. the game setup dealt with number of players, which affected the number of decks used (and some other things), the game play dealt with whether a particular card could be dealt to or combined to a specific hand or given to a particular player, whether sets of cards were at all permissible for play, and the winning rules took a list of hands (I had another object that was Hand that was a wrapper around a list of cards) and returned those hands in winning order. each rule set was actually a list of a particular interface (ISetupRule, IGamePlayRule, IWinSorter or something like that) and you could create hierarchies of rules so you could theoretically share common rules amongst various games.
Re: DateTime.Now update rate???
I really have no idea what all you are talking about. I'm still learning programming, and this is my first program I've ever built. I hope you can find an example for me to use so I can understand better what you are trying to do.
I can no longer post my code because the program is starting to become somewhat large, and the files are also too large to attach to the thread. I think its going to be very fast and concise.
Everything is using complete OOP, and Enums.
There is a lack of comments because I'm still working out the kinks, before I go and write all the comments.
Maybe I can ask one favor. Since this is my first solution. I want to make sure I'm on the right track before I completely screw up. I will post the solution files on my website, and maybe you can look at them quick, and see if I'm on the right track. If you have time.
My main concerns are how the dealer is chosen, I'm not sure if its a reasonable solution. Most of the code was in place before, but some I modified because I liked your solution a little better. It made more since.
I intend to create card engine, and then another project with the rules for Gin/Oklahoma Gin, and Wild Gin.
Then a third project for the UI. This way if I make more card games in the figure I can reuse the card engine, and only have to replace Gin Engine which has the rules for Gin with another engine that has the rules for another card game, and recompile. It should be extremely simple to create additional games.
Download (the additional exe file is just me playing around with the animation)
Thanks. Please let me know what you think. :D