Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mikhail Shilkov
committed
Feb 1, 2016
1 parent
bc31273
commit ad9697b
Showing
2 changed files
with
114 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
open System.Drawing | ||
|
||
module HandRecognition = | ||
type BW = B | W | ||
type CardPattern = { | ||
Card: string | ||
Pattern: BW array | ||
} | ||
|
||
let patterns = [| | ||
{ Card = "2"; Pattern = [|W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;B;B;B;W;W;W;W;W;W;B;B;B;W;W;W;W;B;B;B;B;W;W;W;W;W;B;B;B;B;W;W;W;B;B;B;B;W;W;W;W;B;B;B;B;B;B;W;W;W;B;B;W;W;W;W;W;W;B;B;W;W;B;B;W;W;W;B;B;W;W;W;W;W;B;B;W;W;W;B;B;W;W;W;B;B;B;W;W;B;B;B;B;W;W;W;B;B;W;W;W;W;B;B;B;B;B;B;B;W;W;W;W;B;B;W;W;W;W;W;B;B;B;B;W;W;W;W;W;W;B;B;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W|] } | ||
{ Card = "3"; Pattern = [|W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;B;B;W;W;W;W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B;B;B;B;B;B;B;W;W;W;W;W;W;B;B;B;B;B;B;B;B;B;B;W;W;W;W;W;W;B;B;B;B;B;W;W;W;B;B;B;W;W;W;W;W;W;B;B;B;B;W;W;W;W;B;B;B;W;W;W;W;W;W;B;B;B;B;B;B;B;B;B;B;B;W;W;W;W;W;W;W;W;W;W;B;B;B;B;B;B;B;B;W;W;W;W;W;W;W;W;W;W;W;W;B;B;B;B;B;B;B;W;W;W;W;W;W;W;W;W;W;W;W;W;B;B;B;B;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W;W|] } | ||
|] | ||
|
||
let getCardPattern getPixel width height = | ||
let isWhite (c : Color) = | ||
if c.B > 127uy && c.G > 127uy && c.R > 127uy then W | ||
else B | ||
|
||
seq { for x in 0 .. width - 1 do | ||
for y in 0 .. height - 1 do | ||
yield isWhite (getPixel x y)} | ||
|
||
let getCardSuit getPixel width height = | ||
let getSuit (c : Color) = | ||
match c with | ||
| _ when c.B < 127uy && c.G < 127uy && c.R > 127uy -> Some "h" | ||
| _ when c.B > 127uy && c.G < 127uy && c.R < 127uy -> Some "d" | ||
| _ when c.B < 127uy && c.G > 127uy && c.R < 127uy -> Some "c" | ||
| _ when c.B < 127uy && c.G < 127uy && c.R < 127uy -> Some "s" | ||
| _ -> None | ||
seq { for x in 0 .. width - 1 do | ||
for y in 0 .. height - 1 do | ||
yield getSuit (getPixel x y)} | ||
|> Seq.choose id | ||
|> Seq.countBy id | ||
|> Seq.maxBy (fun (v, c) -> c) | ||
|> fst | ||
|
||
let getCardValue patterns bws = | ||
let matchCount h p = | ||
Seq.zip h p | ||
|> Seq.map (fun (v1, v2) -> if v1 = v2 then 1 else 0) | ||
|> Seq.sum | ||
|> decimal | ||
let maxPattern = patterns |> Array.maxBy (fun p -> matchCount bws p.Pattern) | ||
maxPattern.Card | ||
|
||
let getCardString getCardPattern getCardSuit = | ||
let value = getCardPattern |> getCardValue patterns | ||
let suit = getCardSuit | ||
value + suit | ||
|
||
let recognizeCard getPixel width height = | ||
let value = getCardPattern getPixel width height |> getCardValue patterns | ||
let suit = getCardSuit getPixel width height | ||
value + suit | ||
|
||
let parsePattern getPixel width height = | ||
getCardPattern getPixel width height | ||
|> Seq.map (fun x -> if x = B then "B" else "W") | ||
|> String.concat ";" |