首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我的实际联合大小写值和期望值之间有什么区别?

我的实际联合大小写值和期望值之间有什么区别?
EN

Stack Overflow用户
提问于 2016-07-28 17:59:20
回答 1查看 53关注 0票数 0

我的实际联合大小写值和期望值之间有什么区别?

我有以下测试:

代码语言:javascript
复制
[<Test>]
let ``black checker jumps to king``() =
    let redChecker = { RedChecker.Position= { X=1 ; Y=6 } }
    let target = (redChecker, [redChecker])

    { BlackChecker.Position= { X=0 ; Y=5 } } |> jumpRed target
                                             |> fst
                                             |> should equal (BlackKing { BlackKing.Position= { X=2 ; Y=7 } })

,这是在交互式窗口中执行测试操作时的实际结果:

代码语言:javascript
复制
val redChecker : RedChecker = {Position = {X = 1;
                                           Y = 6;};}
val target : RedChecker * RedChecker list =
  ({Position = {X = 1;
                Y = 6;};}, [{Position = {X = 1;
                                         Y = 6;};}])
val it : Checker = BlackKing {Position = {X = 2;
                                          Y = 7;};}

我看到返回BlackKing值时xy为(2,7)。因此,我在测试中用这个特定的xy值来验证这个联合用例值。

我只是不明白为什么我的考试不及格。

有什么建议吗?

附录:

代码语言:javascript
复制
open NUnit.Framework
open FsUnit

(* Types *)
type North = NorthEast | NorthWest
type South = SouthEast | SouthWest

type Direction = 
    | NorthEast 
    | NorthWest
    | SouthEast 
    | SouthWest

type Position =     { X:int; Y:int }

type BlackChecker = { Position:Position }
type RedChecker =   { Position:Position }
type BlackKing =    { Position:Position }
type RedKing =      { Position:Position }

type Checker =
    | BlackChecker of BlackChecker
    | RedChecker   of RedChecker
    | BlackKing    of BlackKing
    | RedKing      of RedKing

type King = 
    | BlackKing of BlackKing
    | RedKing of RedKing 

(* Functions *)
let rec remove item list = list |> List.filter (fun x -> x <> item)

let setRowPosition y1 y2 y3 index =
    match index with 
    | x when x < 4 -> { X=x; Y=y1 }
    | x when x < 8 -> { X=x-4; Y=y2 }
    | _            -> { X=index-8; Y=y3 }

let initializeBlack () =
    let setPosition index =
        index |> setRowPosition 7 6 5

    let blackCheckers = List.init 12 setPosition |> List.map (fun pos -> { BlackChecker.Position= { X=pos.X; Y=pos.Y } })
    blackCheckers

let initializeRed () =
    let setPosition index =
        index |> setRowPosition 0 1 2

    let redCheckers =   List.init 12 setPosition |> List.map (fun pos -> { RedChecker.Position= { X=pos.X; Y=pos.Y } })
    redCheckers

let set (x, y) positions (position:Position) =
    match not (positions |> List.exists (fun pos -> pos = { X=x; Y=y })) with
    | true -> { X=x; Y=y }
    | false -> position

let moveBlack direction positions (checker:BlackChecker) =
    let position = checker.Position

    match direction with
    | North.NorthEast -> { BlackChecker.Position= (positions, position) ||> set ((position.X + 1), (position.Y + 1 )) } 
    | North.NorthWest -> { BlackChecker.Position= (positions, position) ||> set ((position.X - 1), (position.Y + 1 )) }

let moveRed direction positions (checker:RedChecker) =
    let position = checker.Position

    match direction with
    | South.SouthEast -> { RedChecker.Position= (positions, position) ||> set ((position.X + 1), (position.Y - 1 )) }
    | South.SouthWest -> { RedChecker.Position= (positions, position) ||> set ((position.X - 1), (position.Y - 1 )) }

let moveKing direction positions (king:King) =

    let position = match king with
                   | BlackKing bk -> bk.Position
                   | RedKing   rk -> rk.Position

    let result = match direction with
                 | NorthEast -> (positions, position) ||> set ((position.X + 1), (position.Y + 1 ))
                 | NorthWest -> (positions, position) ||> set ((position.X - 1), (position.Y + 1 ))
                 | SouthEast -> (positions, position) ||> set ((position.X + 1), (position.Y - 1 ))
                 | SouthWest -> (positions, position) ||> set ((position.X - 1), (position.Y - 1 ))

    match king with
    | BlackKing _ -> BlackKing { BlackKing.Position= result }
    | RedKing   _ -> RedKing   { RedKing.Position=   result }

let jump target yDirection source =
    let updateX value = { X=target.X + value
                          Y=target.Y + yDirection }
    match source with
    | position when position.Y + yDirection = target.Y &&
                    position.X + 1 = target.X -> updateX 1

    | position when position.Y + yDirection = target.Y &&
                    position.X - 1 = target.X -> updateX -1
    | _ -> source

let jumpRed ((redChecker:RedChecker), (redCheckers:RedChecker list)) (blackChecker:BlackChecker) =

    let yIncrementValue = 1
    let maxY = 7
    let position = blackChecker.Position |> jump redChecker.Position yIncrementValue

    match position with
    | pos when pos = blackChecker.Position -> BlackChecker { blackChecker with Position= position }, redCheckers
    | pos when pos.Y = maxY                -> Checker.BlackKing { BlackKing.Position=position }, redCheckers |> remove redChecker
    | _ -> BlackChecker { blackChecker with Position= position }, redCheckers |> remove redChecker

let jumpBlack ((blackChecker:BlackChecker),(blackCheckers:BlackChecker list))  (redChecker:RedChecker) =

    let yIncrementValue = -1
    let minY = 0
    let position = redChecker.Position |> jump blackChecker.Position yIncrementValue

    match position with
    | pos when pos = redChecker.Position -> RedChecker { redChecker with Position= position }, blackCheckers
    | pos when pos.Y = minY              -> Checker.RedKing { RedKing.Position=position }, blackCheckers |> remove blackChecker
    | _ -> RedChecker { redChecker with Position= position }, blackCheckers |> remove blackChecker
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-28 18:07:13

好吧,这个声明

代码语言:javascript
复制
{ BlackChecker.Position= { X=0 ; Y=5 } } |> jumpRed target
                                         |> fst

给予:

代码语言:javascript
复制
val it : Checker = BlackKing {Position = {X = 2;
                                      Y = 7;};}

它是Checker型的。另一方面:

代码语言:javascript
复制
(BlackKing { BlackKing.Position= { X=2 ; Y=7 } })

给予:

代码语言:javascript
复制
val it : King = BlackKing {Position = {X = 2;
                                   Y = 7;};}

它是King型的。他们不是同一类型的..。

如果要比较它们,则需要通过模式匹配在要比较的事物中展开BlackKing类型。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38643625

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档