相关疑难解决方法(0)

F#造型扑克牌

我想在F#中代表标准扑克牌.我的目标是实现一个微软纸牌(Windows附带的一个)的克隆,一个卡片套装,面部和颜色很重要的游戏.这个练习主要是为了学习一些F#.

我考虑过使用歧视的工会:

type Suit =
    | Diamonds
    | Hearts
    | Clubs
    | Spades

type Color =
    | Red
    | Black

type Face =
    Two | Three | Four | Five | Six | Seven |
    Eight | Nine | Ten | Jack | Queen | King | Ace
Run Code Online (Sandbox Code Playgroud)

卡的记录类型:

type Card = {
    suit: Suit;
    face: Face;
    color: Color;
}
Run Code Online (Sandbox Code Playgroud)

但是,卡片的颜色可以从它的套装中推断出来 - 所有钻石和红心都是红色的,所有的俱乐部和黑桃都是黑色的.不能仅从Color确定套装.也许这样的事情是合适的:

type Suit =
    | Diamonds of Color //should always be red
    | Hearts …
Run Code Online (Sandbox Code Playgroud)

f# playing-cards

6
推荐指数
1
解决办法
781
查看次数

F#类型和循环

我正在研究一个创建一副牌的F#教程.列出了类型,但我无法理解如何遍历类型以创建完整套牌的地图.我期待做类似的事情

Foreach rank in ranks
   Foreach suit in suits
       somehow combine the two
   next suit
next rank
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点?以下是创建的类型.

我想如果我把它们从类型更改为列表它们可以合并,对吧?那么,类型有什么意义呢?

type suits=
    |Spade=1
    |Heart=2
    |Club=3
    |Diamond=4

type ranks=
    |ValCard of int
    |Jack 
    |Queen
    |King

type deck= Deck of ranks * suits
Run Code Online (Sandbox Code Playgroud)

f# types loops

5
推荐指数
2
解决办法
290
查看次数

标签 统计

f# ×2

loops ×1

playing-cards ×1

types ×1