我一直在添加和修改来自我的Baloot理货机的代码。我对代码进行了一些更新,并添加了一些新内容,但对于这篇文章,我尝试了不同的方法:检测您手中的卡片是否有“项目”。
对于这些项目,Baloot有一些特殊的名称,但它们类似于扑克之手。因为在一轮开始的时候,你手里只有7张牌,所以代码中的一些捷径就是这样做的。
Sira是连续三次。有时候,当你幸运的时候,你手里有两个。我不知道如何处理这件事。Fifty是连续四次Hundred要么是连续五个,要么是一个类型的四个。FourHundred是四个Aces。Baloot的名字命名,它是王牌套装的国王和王后。我对整个功能合成的事情有点不确定,因为我觉得我做得太过分了。如果代码可以变得更简单和/或更易读,我会全神贯注的。更好的名称为这些功能的想法也将是非常感谢的。
不加进一步的限制:
领域模型
// Similar to previous post but edited a bit.
// Ranks 2 - 6 don't exist in Baloot, so (A, 2, 3) sequence isn't a thing.
type Rank = Ace | King | Queen | Jack | Ten | Nine | Eight | Seven
type Suit = Hearts | Clubs | Diamonds | Spades
type Card =
{ Rank : Rank
Suit : Suit }
member this.IsPicture = // These two properties are instead of simple functions to simplify the code and make it more reusable.
match this.Rank with
| Nine | Eight | Seven -> false
| _ -> true
member this.SortValue =
match this.Rank with
| Ace -> 0
| King -> 1
| Queen -> 2
| Jack -> 3
| Ten -> 4
| Nine -> 5
| Eight -> 6
| Seven -> 7
type Mode = Sun | Trump of Suit
type Hand =
{ Mode : Mode
Cards : Card list }辅助函数
// only works for lists of distinct integers.
// This function is just to make the logic of the next one clearer.
let hasIntSeqOf n =
List.sort
>> List.mapi (-) // subtract number from index so consecutive numbers have the same value.
>> List.groupBy id // group by value
>> List.filter(snd // If any group has (n <= count) members, a sequence exists
>> List.length
>> (<=) n)
>> List.map Some
let cardsHaveSeqOf n = // Sig: int -> Card list -> Card list option list
List.sortBy (fun (c : Card) -> c.SortValue)
>> List.mapi (fun i c -> (i - c.SortValue, c)) // tuple with the card and the invariant if there are consecutive cards
>> List.groupBy fst //group by the invariant
>> List.map snd // then strip it out
>> List.filter (List.length >> (<=) n)
>> List.map ((List.map snd) >> Some) // clean up
let hasSeqOf n hand = // Sig: int -> Hand -> Card list option list. I am not sure this is a good idea.
hand.Cards
|> List.groupBy (fun c -> c.Suit)
|> List.map (snd >> cardsHaveSeqOf n)
|> List.concat // This is oddish.
|> List.filter ((<>) None)
let cardsHaveFourOfAKind =
List.groupBy (fun c -> c.Rank)
>> List.map snd
>> List.tryFind (List.length >> (=) 4)实际功能
// All these functions return None if nothing is found and Some(Card list)
// if a project is found, including the cards that make up the project.
// Somehow I imagine these functions would be on a function list of some
// sort that I can use to quickly analyze hands and extract that info. I am
// not there yet.
let hasOneSira =
hasSeqOf 3 >> function
| Some(cl) :: [] -> Some(cl)
| _ -> None
let hasTwoSira = // I would rather not have this function but did not know how to deal with this specific circumstance.
hasSeqOf 3 >> function
| Some(cl1) :: Some(cl2) :: _ -> Some(cl1 @ cl2)
| _ -> None
let hasFifty =
hasSeqOf 4 >> function // there can be only one (four in a row)
| Some(cl) :: _ -> Some(cl)
| _ -> None
// Two separate functions for Hundred because sometimes they exist in the
// same hand and share a card , but the card can be only in one project.
let hasHundredOne =
hasSeqOf 5 >> function // there can be only one (five in a row)
| Some(cl) :: _ -> Some(cl)
| _ -> None
let hasHundredTwo hand =
hand.Cards
|> cardsHaveFourOfAKind
|> function
| Some(c :: tl) when c.IsPicture && c.Rank <> Ace -> Some(c :: tl)
| _ -> None
let hasFourHundred hand =
hand.Cards
|> cardsHaveFourOfAKind
|> function
| Some(c :: tl) when c.Rank = Ace -> Some(c :: tl)
| _ -> None
// This function is the only reason for the Hand type, instead of simply a Card list.
let hasBaloot hand =
match hand.Mode with
| Sun -> None
| Trump(t) ->
let king = List.tryFind (fun c -> c.Rank = King && c.Suit = t) hand.Cards
let queen = List.tryFind (fun c -> c.Rank = Queen && c.Suit = t) hand.Cards
match king, queen with
| Some(k), Some(q) -> Some([ k; q ])
| _ -> None对下列事项的任何评论:
都很感激。
编辑:如果两名Siras来自同一套西装,我修复了原代码中的一个bug。这导致了一些奇怪的密码。
发布于 2016-07-28 10:30:58
这看起来挺有趣的。我很惊讶你没有收到更多的评论/答复。我需要更多的时间来检查代码。下面是一些观察,将编辑的答案,一旦我消化了所有的。
我认为您可以在使用函数组合的地方使用|>。虽然在某些地方确实有道理,比如List.map (snd >> cardsHaveSeqOf n)。我倾向于使用>>创建更具可读性的新函数。
在hasSeqOf而不是|> List.filter ((<>) None)中,您可以只说|> List.choose id。
我发现与function匹配的模式是很难读懂的。也许你可以在同一条线上,缩进或坚持匹配。
实际上,我喜欢在前Q中只在一行中定义type Mode = Sun | Trump of Suit。
我认为C#用户倾向于使用this.,但是可能是任何东西,如果您不想跳过linter,可以使用__. (双下划线)。
在hasIntOfSeq函数上添加160729:。我用|>重做了一遍。我的理解是正确的,这就是你想要提取的东西吗?最初,我考虑使用pairwise来获取连续的数字,但发现List.mapi的想法很酷。
let xs = [10;9;12;11;21;22;23;30;40;50]
let mapmap2 x = (List.map >> List.map) snd x
let map2 x = List.map snd x
let isIntSeqOf xs =
xs |> List.sort
|> List.mapi (fun i x -> (i - x,x))
|> List.groupBy fst |> map2 |> mapmap2
|> List.filter (fun x -> x.Length >=3)
isIntSeqOf xs
// val it : int list list = [[9; 10; 11; 12]; [21; 22; 23]]再看一看,我更加确信,除了一些助手函数之外,您最好还是使用|>。通过这种方式,您可以通过管道生成更小的函数来构建更大的函数,并在执行过程中测试结果。我重做了您的cardsHaveSeqOf函数,并将其与hasSeqOf合并。我不是说它很漂亮..。我也在简化一些,因为可能更好的重新组合一些。这将返回一个卡片列表列表,因此它可以返回两个Siras,每个子列表中一个。
我们最好在某个地方使用>>!
let mapmap2 x = (List.map >> List.map) snd x
let map2 x = List.map snd x
let cardsHaveSeqOf n (hand:Hand) =
hand.Cards |> List.groupBy (fun c -> c.Suit)
|> map2
|> List.map (List.sortBy (fun c -> c.SortValue))
|> List.map (List.mapi (fun i c -> (c.SortValue - i, c)))
|> List.map (List.groupBy fst) |> mapmap2
|> List.collect mapmap2
|> List.filter (fun x -> x.Length = n)现在,对于模式匹配(再次简化一下),我认为function和>>有点过火了。您只需简单地执行match:
let hasTwoSira (cl:Card list list ) =
match cl with
| [cl1;cl2] when cl1.Length =3 && cl1.Length =3 -> cl1 @ cl2
| _ -> []
let hasOneSira (cl:Card list list) =
match cl with
| [cl1] when cl1.Length = 3 -> cl1
| _ -> []如果您沿着这条路线走下去,它的优点是可以快速列出功能的手动检查列表:
[hasTwoSira;hasOneSira] |> List.map (fun x -> x sira2)
其中sira2是一只手。
还有一种使用活动模式进行模式匹配的方法。活动模式可以隐藏模式匹配的复杂实现。例如:
let (|OneSira|TwoSira|Fifty|Other|) ((n:int), (hand:Hand)) =
let result = cardsHaveSeqOf n hand
match result with
| [cl1] when cl1.Length = 3 -> OneSira
| [cl1;cl2] when cl1.Length = 3 && cl2.Length =3 -> TwoSira
| [cl1] when cl1.Length = 4 -> Fifty
| _ -> Other瞧,我们可以用手施点魔法:
let checkHand (n:int) (hand:Hand) =
match (n,hand) with
| OneSira x -> "One"
| TwoSira x -> "Two"
| Fifty x -> "Fifty"
| _ -> "Other"以下是一些测试数据:
let sun = Sun
let card1 = {Rank=Ace;Suit=Hearts}
let card2 = {Rank=King;Suit=Hearts}
let card3 = {Rank=Queen;Suit=Hearts}
let card4 = {Rank=Jack;Suit=Diamonds}
let card5 = {Rank=Ten;Suit=Diamonds}
let card6 = {Rank=Nine;Suit=Diamonds}
let card7 = {Rank=Seven;Suit=Clubs}
let card8 = {Rank=Nine;Suit=Spades}
let hand = {Mode=sun; Cards=[card3;card1;card8;card7;card6;card2;card5;card4]}
let card11 = {Rank=Ace;Suit=Hearts}
let card12 = {Rank=King;Suit=Hearts}
let card13 = {Rank=Queen;Suit=Hearts}
let card14 = {Rank=Jack;Suit=Hearts}
let card15 = {Rank=Ten;Suit=Diamonds}
let card16 = {Rank=Nine;Suit=Diamonds}
let card17 = {Rank=Seven;Suit=Clubs}
let card18 = {Rank=Nine;Suit=Spades}
let hand2 = {Mode=sun; Cards=[card13;card11;card18;card17;card16;card12;card15;card14]}
let sira2 = cardsHaveSeqOf 3 hand
let fifty = cardsHaveSeqOf 4 hand2 我们试试看:
[hasTwoSira;hasOneSira] |> List.map (fun x -> x sira2)
[hasTwoSira;hasOneSira] |> List.map (fun x -> x fifty)我猜你可以把一个函数列表应用到一个手的列表中。并使用checkHand函数:
checkHand 3 hand //val it : string = "Two"
checkHand 2 hand //val it : string = "Other"
checkHand 4 hand2 //val it : string = "Fifty"现在,这显然是错误和不完整,但我希望不要太多!
https://codereview.stackexchange.com/questions/136036
复制相似问题