{"id":1473,"date":"2007-04-15T21:31:34","date_gmt":"2007-04-16T00:31:34","guid":{"rendered":"http:\/\/www.hoogervorst.ca\/arthur\/?p=1473"},"modified":"2007-10-06T09:55:24","modified_gmt":"2007-10-06T12:55:24","slug":"cards","status":"publish","type":"post","link":"http:\/\/www.hoogervorst.ca\/arthur\/?p=1473","title":{"rendered":"Cards"},"content":{"rendered":"<p><span class=\"dropcap\">F<\/span>or something completely unrelated, I decided to check into my older Delphi code and noticed the &#8216;remnants&#8217; of the code that actually started as a framework for a (basic) <a href=\"http:\/\/www.pagat.com\/boston\/spades.html\">Spades<\/a><sup>1<\/sup> card game. So hey: the code was actually so readable that most of it was easily turned into a C# version (that is minus the bidding code).\n<\/p>\n<p>The fun part of it all was the ease of sorting in C#. Actually, you still need to write code to get that going, but compared to the things I needed to get a <em>hand<\/em> sorted in Delphi, that can be a different story (it&#8217;s actually not too bad either if you use regular TLists and that):\n<\/p>\n<p>First of all make your base object descend from the IComparable class. This means, you&#8217;ll have to add a CompareTo method to your base object\n<\/p>\n<pre> public class TCard : IComparable<tcard>\r\n    {\r\n        private TFaceValue ffacevalue;\r\n        private bool ffaceup = false;\r\n        private TSuit fsuit;\r\n        \r\n        \/\/ Code removed for brevity reasons...\r\n\r\n        public int CalcValue\r\n        {\r\n            get { return NumericValue(); }\r\n        }\r\n\r\n        public int CompareTo(TCard acard)\r\n        {\r\n            return this.CalcValue.CompareTo(acard.CalcValue);\r\n        }\r\n    }\r\n<\/tcard><\/pre>\n<p>Then create a class based on IComparer:\n<\/p>\n<pre>\r\n    public class THandComparer : IComparer<tcard>\r\n    {\r\n        private TSortOptions fsort = TSortOptions.Ascending;\r\n\r\n        public TSortOptions SortOption\r\n        {\r\n            get { return fsort; }\r\n            set { fsort = value; }\r\n        }\r\n\r\n        public int Compare(TCard x, TCard y)\r\n        {\r\n            if (fsort == TSortOptions.Ascending)\r\n            {\r\n                return x.CompareTo(y);\r\n            }\r\n            else\r\n            {\r\n                return y.CompareTo(x);\r\n            }\r\n        }\r\n    }\r\n<\/tcard><\/pre>\n<p>I decided to give the player an option to sort the cards descending or ascending based on the combination of suit and card value:  Notice that for descending order, you literally just switch the objects (the base CompareTo method returns -1, 0 or 1!)\n<\/p>\n<p>And then in the THand Class:\n<\/p>\n<pre> \r\n        \/\/ [....]\r\n        public void Sort()\r\n        {\r\n            THandComparer fcomparer = new THandComparer();\r\n            fcomparer.SortOption = forder;\r\n\r\n            fhand.Sort(fcomparer);\r\n        }\r\n<\/pre>\n<p>What rest are the drawing procedures, the card faces (I should have a couple of sets somewhere on my source backup disks) and the bidding engine.\n<\/p>\n<p class=\"extra\"><small><sup>1<\/sup> I find it amazing that Pagat once again serves as a reference for card games, just like it used to do back in 2000.<br \/>\n<\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>For something completely unrelated, I decided to check into my older Delphi code and noticed the &#8216;remnants&#8217; of the code that actually started as a framework for a (basic) Spades1 card game. So hey: the code was actually so readable &hellip; <a href=\"http:\/\/www.hoogervorst.ca\/arthur\/?p=1473\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[88],"_links":{"self":[{"href":"http:\/\/www.hoogervorst.ca\/arthur\/index.php?rest_route=\/wp\/v2\/posts\/1473"}],"collection":[{"href":"http:\/\/www.hoogervorst.ca\/arthur\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.hoogervorst.ca\/arthur\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.hoogervorst.ca\/arthur\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.hoogervorst.ca\/arthur\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1473"}],"version-history":[{"count":0,"href":"http:\/\/www.hoogervorst.ca\/arthur\/index.php?rest_route=\/wp\/v2\/posts\/1473\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.hoogervorst.ca\/arthur\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1473"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.hoogervorst.ca\/arthur\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1473"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.hoogervorst.ca\/arthur\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1473"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}