I hope this is not my legacy

blog thoughts software stuff
RSS
  • About
  • BeatButcherMPC
  • Design of an Audio Sampler
  • Find Me
  • food

Poker Hand Rank (Haskell)

by admin on December 2, 2010 at 12:37 am

I’ve spent the last couple of days trying to get my head around Haskell beyond the cursory level which my university course demanded. The first project I’m attempting is to write a little poker game, probably not with any AI, but requiring a bunch of different techniques which I need to work on. Tonight, happily, I’ve managed to implement the hand ranking algorithm. It’s not *fully* tested yet but it seems to work.

No licence with this code (use as you like) and no warranty obviously!
I would appreciate any pointers on improving the code if you see fit…


import Control.Monad
import Data.List
import List

data Suit = Hearts | Clubs | Spades | Diamonds
  deriving (Read, Show, Eq, Ord, Enum)

data Rank = Two | Three | Four | Five | Six | Seven | Eight
  | Nine | Ten | Jack | Queen | King | Ace
  deriving (Read, Show, Eq, Ord, Enum, Bounded)

data Card = Card {
  rank :: Rank,
  suit :: Suit
  }
  deriving (Eq)

instance Ord Card where
  compare (Card x1 _) (Card x2 _) = compare x1 x2

-- Pretty card display print out
instance Show Card
  where show x = show(rank x) ++ " of " ++ show(suit x)

data HandType = StraightFlush Rank | FourOfAKind Rank Rank |
  FullHouse Rank Rank |  Flush [Rank] |  Straight Rank |
  ThreeOfAKind Rank [Rank] | TwoPair Rank Rank Rank |
  Pair Rank [Rank] | HighCard [Rank]
  deriving (Show)


evaluate :: [Card] -> HandType
evaluate cards = let sorted = sort cards in
    case (flush sorted, straight sorted) of
    (Just f, Just s) -> StraightFlush s
    (Just f, Nothing) -> Flush f
    (Nothing, Just s) -> Straight s
    _ ->
    case nOfAKind (groupBy (\c d -> rank c == rank d) sorted) of
    ((3,c):(2,d):[]) -> FullHouse c d
    ((4,c):(1,d):[]) -> FourOfAKind c d
    ((3,c): xs)    -> ThreeOfAKind c (recombine xs)
    ((2,c):(2,d):(1,e):[]) -> TwoPair c d e
    ((2,c): xs)    -> Pair c (recombine xs)
    xs             -> HighCard (recombine xs)
 where
    recombine xs = snd (unzip xs)
    nOfAKind [] = []
    nOfAKind (x:xs) = (length x, rank (head x)) : (nOfAKind xs)
    flush (c:cards) = case dropWhile (\x -> suit x == suit c) cards of
    [] -> Just (map (\x -> rank x) (c:cards))
    _ -> Nothing
    -- N.B must account for 'Wheel' hand
    straight (x:xs) = if (rank x == Two) && rank (last xs) == Ace
                      then checkNext Two (init xs)
                      else checkNext (rank x) xs
    checkNext r []        = Just r
    checkNext r (x:xs)    = if (r /= maxBound && (succ r) == (rank x))
                            then checkNext (rank x) xs
                            else Nothing

1 Comment

Apple Store and The Beatles

by admin on November 21, 2010 at 7:26 pm

I was in Birmingham at the weekend with R- we were going to see deftones play at the Academy. Beforehand we decided to stop in at the Bullring and have a wander around, and it was here I decided that being a Mac user, I should really break my Apple store virginity and go and see what they are all about. I’ve always avoided them in the past because they are always really really busy and I’ve never really been interested in buying anything. I wasn’t particularly interested in buying anything on Saturday, but nevertheless felt somehow qualified to enter.

My experience of the store was slightly strange. About half of the store is taken up by the various electronic products they sell and an absolute sea of people doing one of two things- looking at the Apple homepage, or checking their facebook page. The second half of the store is split into one section for people getting help using their own products and getting things fixed, and another section for accessories and software. The latter section of the store was empty. There was one guy doing some sort of console hacking on a MacBook Pro near the entrance, who was promptly removed from the store and that resonated with a story I saw online about a guy who went into an Apple retail store and applied a jailbreak patch to all of the iPhone 4′s on display.

It is a really interesting place though- loads of people with no intention of buying anything are totally welcome to browse and play with the products. Why? Because they are the ones creating this overwhelming feel of excitement and desire for the products, which is infectious for the few people in the store with enough cash to buy things. I guess the image they create is really distinct from the elitist image which they could go for. Apple aren’t saying that only the special few are welcome and trying to keep the store clear for them (like, for example, a prestige car showroom or fine jewellers)- they are saying here are products which everyone wants but are still compromise free (which is, of course, incorrect).

I’ve never really liked the hype around Apple product releases, something which I was reminded of this week with the Beatles release announcement. To be honest I couldn’t have been more underwhelmed by the news, but I did breathe a sigh of relief that they announcement wasn’t that there would be an imminent replacement to the Macbook Pro 13″ I just bought! I think that the marketing department for the Beatles should be commended for their terrific negotiating skills, and for getting the X Factor to do a themed night to coincide with the release. However I think Apple made a mistake with the deal.

Apple generate tremendous amounts of publicity for their products by calling elaborate press conferences (sorry, ‘key notes’) where Steve Jobs stands up and announces the next life changing gadget that everybody must own. This generally generates a massive amount of discussion from the tech blogs, and along with a glossy advertising campaign and an army of fan boys telling all their friends, a new product is born. I don’t think strategy is incredibly clever, but I do think that it is immaculately executed. The products are just right (which is the really hard bit), they are well engineered and aimed at a specific market, and that market happens to respond well to the aforementioned marketing. Apple provide the total package from product to purchasing experience to support- the packaging is amazingly tactile, the aforementioned stores allow free reign on the products, and the aftercare is great.

Where I think that Apple have gone wrong is to sell out this marketing channel they have created to a third party, The Beatles. The music is not a product of Apple, they simply make a profit from selling it. Perhaps Steve Jobs really likes The Beatles’ music and wants to promote it but, well, I don’t care. I don’t want a biased music store which favours one band over another, even, no, especially a band which pays more. They make a good profit selling a lot of different products, but I don’t expect an email telling me to expect a life changing experience every time they get a new piece of stock. Please do not repeat.

5 Comments

MAC Address Converter

by admin on November 17, 2010 at 9:17 pm

I had to calculate a bunch of MAC address long long values from strings today and I wished there was an online tool to do it. Now there is.


2 Comments

On Web Apps

by admin on November 16, 2010 at 12:19 am

I’m currently ploughing my way through the Ruby on Rails documentation, trying to get used to thinking of applications in the server-client model rather than the far more familiar (to me) desktop model.

I really like the idea of web apps as a portable platform to do nearly anything you would want. I love the idea of being able to do work from any computer. I love the idea that if you are paying, then you are paying for service and/or data, not for the lame idea of owning a piece of software (because you never really own software). And I love the idea of being able to share work with other people with a single click- no need for installed software. Prezi is a an excellent example (you should check out their cheesy demonstration video if you haven’t seen them before).

I don’t have a problem with frameworks like Silverlight or Flash (or Java…)- I think that they can solve problems which would otherwise be very difficult to solve. However, I think that they should be a last resort, and in being a last resort they kind of become redundant, since the ubiquity of installations will drop and thus the idea of not relying on installed software fails if the user needs to download a framework.

Furthermore there are further problems with these frameworks since they are not open standards, it makes it hard for interoperability beyond the manufacturers intention. How long did we have to wait for a 64 bit Linux build of Flash? From my experiments, Moonlight (Silverlight Linux port by the Mono project) still has a way to go, and will always lag behind the latest version of Silverlight. There are rumours going around that Silverlight’s future may be in jeopardy, and we all know what Apple have in mind for Flash, so perhaps it is a bad idea to be investing our time as developers learning to use these frameworks when their control is out of our hands.

On the other hand, from what I have seen, developing web apps is incredibly messy. HTML, CSS and Javascript are the basic tools, and HTTP is the basic method of communication. It seems like anyone writing web applications front ends with just these tools must be crazy. What with browser incompatibilities, AJAX, and the horrific DOM specification, it seems very difficult to achieve anything of any significance using only these tools.

With the rise of MVC frameworks such as Rails, ASP.Net MVC and Cake there is a wealth of support for developing complex server side solutions with data layers bound to presentation layer content, and toolkits such as GWT and jQuery provide functionality to keep the evil aspects of DOM and AJAX at arms length. All this is great and the sheer amount of different frameworks and libraries available is incredible. This can only be good for web development as a whole.

I don’t believe in a silver bullet for web or any other type of development. Design is a hard problem which cannot be solved without being rigorously specified, in code or other formal method. However I do think it is possible to position ourselves as developers in a place where we can see the way in which all of the components in our system are working together, and go down the levels and see how each of them is working. Getting the right level of abstraction is the hard part of development, and that goes for development of frameworks as well.

Given the amount of work required to build really cool stuff using raw HTML + Javascript. I guess the problem for the developer is which framework to choose. No-one wants to spend time learning something only to find it deprecated after a single project. However, it seems that with web development, whether you like it or not, things will move on and you will find yourself constantly challenged to learn new technologies and libraries. You can choose to focus on learning one thing properly like the old school UNIX hackers prided themselves on, or you can keep Google and StackOverflow open at all times, set up unit tests and get to work. This can either scare you, or it can excite you.

In fact I think it can do a little bit of both. It can be hard to know where to start with all of it, but the great thing about the web is that you only need a text editor and a browser to get started.

2 Comments

Sidi B2 Gore-Tex Boot Review

by admin on November 6, 2010 at 10:33 am

When I was looking to buy a pair of motorcycle boots, I spent a long time making a decision. I ended up with a pair of Sidi B2 boots, and I thought I would share my thoughts on them for other people who are looking at the same market. What I wanted was a pair of boots which were 100% waterproof, without looking like wellingtons. I wasn’t after a race boot, or anything particularly space age, just something I wouldn’t be embarrassed to wear when not on the bike that would also keep me dry. There isn’t a huge amount of choice in this area of the market, but there are a few good options, including the Spada ST1, the Alpine Stars S-MX and the TCX SS. I was reluctant to spend a lot of money, but in the end I decided it would be better to have a boot which I liked and wanted to wear year round than something I only look to in the rain.

Sidi B2 boots

So after a couple of months of riding with them I can summarize: They are totally dry. They are warm, but not too warm. They do look good. The impact protection feels *really* sturdy, though I haven’t tested it ;)
But…
They are quite hard to walk in (solid soles), and they do still look pretty ridiculous if you are not on a bike. Plus they are fairly expensive (£210 RRP, although I paid £142 delivered).

Overall I’m fairly happy with the boots and would buy them again and not hesitate to recommend them to someone if they like the styling (which obviously may not appeal to all). Sidi also includes a bunch of stickers in the box, which is always a winner with me.

http://www.fc-moto.de/Sidi-B2-Gore-Tex/en

1 Comment
  • Page 4 of 14
  • « First
  • «
  • 2
  • 3
  • 4
  • 5
  • 6
  • »
  • Last »

Links

  • Chris Evans Dev
  • Darren Steadman
  • Derecho Falls
  • Eaton Media
  • MiniRant
  • More News From Nowhere
  • Paul S Usowicz
  • PPC Agency
  • Wilcox Dev

©2009-2012 I hope this is not my legacy | Powered by WordPress with ComicPress | Subscribe: RSS | Back to Top ↑